1
1
import json
2
+ import logging
2
3
import paho .mqtt .client as mqtt
3
4
import re
4
5
import socket
12
13
MQTT_KEEPALIVE_INTERVAL = 45
13
14
HOSTNAME = socket .gethostname ()
14
15
16
+ logging .basicConfig (level = logging .INFO )
15
17
16
-
17
- class Client ():
18
+ class Client :
18
19
19
20
def __init__ (self ):
20
21
self .uid = os .environ .get ('UID' ) or '{}' .format (uuid .getnode ())
21
- print 'UID: ' + self .uid
22
+ logging . info ( 'UID: ' + self .uid )
22
23
self .mqtt_client = mqtt .Client (client_id = self .uid )
23
24
self .mqtt_client .on_connect = self .on_connect
24
25
self .mqtt_client .on_message = self .on_message
@@ -27,57 +28,63 @@ def __init__(self):
27
28
self .mqtt_client .on_log = self .on_log
28
29
# Subscribe to myself
29
30
self .mqtt_client .connect (MQTT_HOST , MQTT_PORT , MQTT_KEEPALIVE_INTERVAL )
30
- self .mqtt_client .subscribe ('/ asterisk/' + self .uid + '/#' , 0 )
31
+ self .mqtt_client .subscribe ('asterisk/' + self .uid + '/#' , 0 )
31
32
self .mqtt_client .loop_forever ()
32
33
33
34
34
-
35
35
def on_connect (self , client , userdata , flags , rc ):
36
- print ("rc: " + str (rc ))
36
+ logging . info ("rc: " + str (rc ))
37
37
38
38
39
39
def on_message (self , client , userdata , msg ):
40
- print (msg .info , msg .dup , msg .mid , msg .qos , msg .retain , msg .state , msg .timestamp , msg .topic )
41
- found = re .search ('^/asterisk/{}/(.+)$' .format (self .uid ), msg .topic )
40
+ logging .info ('Topic: {}, Dup: {}, Mid: {}, QoS: {}, Retain: {}, '
41
+ 'State: {}, Info: {}' .format (msg .topic , msg .dup , msg .mid ,
42
+ msg .qos , msg .retain , msg .state , msg .info ))
43
+ found = re .search ('^asterisk/{}/(.+)$' .format (self .uid ), msg .topic )
42
44
if not found :
43
- print ('Error: topic {} not understood.' .format (msg .topic ))
45
+ logging .error ('Error: topic {} not understood.' .format (msg .topic ))
46
+ return
44
47
event_handler = getattr (self , 'on_' + found .group (1 ), self .handler_not_found )
45
48
event_handler (client , userdata , msg )
46
49
50
+
47
51
def on_publish (self , client , obj , mid ):
48
- print ("mid: " + str (mid ))
52
+ logging . info ("mid: " + str (mid ))
49
53
50
54
51
55
def on_subscribe (self , client , obj , mid , granted_qos ):
52
- print ("Subscribed: " + str (mid ) + " " + str (granted_qos ))
56
+ logging . info ("Subscribed: " + str (mid ) + " " + str (granted_qos ))
53
57
54
58
55
59
def on_log (self , client , obj , level , string ):
56
- print (string )
60
+ logging . info (string )
57
61
58
62
59
63
def handler_not_found (self , client , userdata , msg ):
60
- print 'Topic {} handler not found.' .format (msg .topic )
64
+ logging . error ( 'Topic {} handler not found.' .format (msg .topic ) )
61
65
62
66
63
67
def _extract_message (self , payload ):
64
68
try :
65
69
msg = json .loads (payload )
66
70
return msg
67
71
except ValueError as e :
68
- print (e .message , ': ' , payload )
72
+ logging . error (e .message , ': ' , payload )
69
73
70
74
71
75
def on_file (self , client , userdata , msg ):
72
76
data = self ._extract_message (msg .payload )
73
77
file_name = data .get ('FileName' )
74
78
dest_folder = data .get ('DestinationFolder' )
75
79
filename = os .path .join (dest_folder , file_name )
76
- print 'Updating file %s' % filename
80
+ logging . info ( 'Updating file %s' % filename )
77
81
with open (filename , 'w' ) as f :
78
82
f .write (data .get ('Content' ))
79
83
80
84
85
+ def on_sip_reload (self , client , userdata , msg ):
86
+ logging .info ('SIP reload' )
87
+
81
88
82
89
# Initiate MQTT Client
83
90
mqttc = Client ()
0 commit comments