-
Notifications
You must be signed in to change notification settings - Fork 7
/
mqtt
63 lines (50 loc) · 1.52 KB
/
mqtt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
#
# Usage:
# AGI(/etc/asterisk/agi/mqtt,/etc/asterisk/agi/mqtt.cfg,calls/missed)
# or, to override the extension:
# AGI(/etc/asterisk/agi/mqtt,/etc/asterisk/agi/mqtt.cfg,calls/missed,+43123456789)
#
import sys
from ConfigParser import ConfigParser
import mosquitto
import json
conffile, topic = sys.argv[1:3]
config = ConfigParser()
config.add_section('mqtt')
# set defaults for anonymous auth
config.set('mqtt', 'username', '')
config.set('mqtt', 'password', '')
config.set('mqtt', 'port', '1883')
config.read(conffile)
mqtt_server = config.get('mqtt', 'server')
mqtt_port = config.getint('mqtt', 'port')
mqtt_username = config.get('mqtt', 'username')
mqtt_password = config.get('mqtt', 'password')
agi = []
while 1:
line = sys.stdin.readline()
if not line or line == "\n":
break
agi.append(line)
agi = dict([line.rstrip('\n').replace('agi_', '', 1).split(': ', 1) for line in agi])
if len(sys.argv) > 3:
agi['extension'] = sys.argv[3]
def agi_exit(rc, *args):
if rc != 0:
print "VERBOSE rc=%s %s" % (rc, args)
sys.exit(rc)
def on_connect(mosq, rc, *args):
if rc != 0:
agi_exit(1, "Connection failed: %d" % rc)
def on_publish(mosq, *args):
# done
agi_exit(0)
client = mosquitto.Mosquitto('agi')
client.username_pw_set(mqtt_username, mqtt_password)
client.connect(mqtt_server, port=mqtt_port)
client.on_connect = on_connect
client.on_publish = on_publish
client.publish(topic, payload=json.dumps(agi))
client.loop()
agi_exit(1, "Message publish timed out")