-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoplayback.py
executable file
·146 lines (127 loc) · 4.49 KB
/
oplayback.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python
"""
Copyright (c) 2018 Ian Shatwell
The above copyright notice and the LICENSE file shall be included with
all distributions of this software
"""
import sys
import os.path
import time
import ConfigParser
import getopt
import paho.mqtt.client as mqtt
if __name__ == "__main__":
DEBUG = False
VERBOSE = False
STARTTIME = 0
# noinspection PyUnusedLocal
def on_mqtt_connect(client, userdata, flags, rc):
if rc == 0:
global mqttconnected
global VERBOSE
if VERBOSE:
print("Connected to MQTT broker")
mqttconnected = True
else:
print("MQTT connection failed. Error {} = {}".format(rc, mqtt.error_string(rc)))
sys.exit(3)
# noinspection PyUnusedLocal
def on_mqtt_disconnect(client, userdata, rc):
global mqttconnected
global mqttclient
mqttconnected = False
if VERBOSE:
print("Disconnected from MQTT broker. Error {} = {}".format(rc, mqtt.error_string(rc)))
# rc == 0 means disconnect() was called successfully
if rc != 0:
if VERBOSE:
print("Reconnect should be automatic")
def connect_to_mqtt(broker, port):
global mqttconnected
global mqttclient
if VERBOSE:
print "Connecting to MQTT broker at {}:{}".format(broker, port)
mqttconnected = False
mqttclient.on_connect = on_mqtt_connect
mqttclient.on_disconnect = on_mqtt_disconnect
mqttclient.loop_start()
while mqttconnected is not True:
try:
mqttclient.connect(broker, port, 5)
while mqttconnected is not True:
time.sleep(0.1)
except:
print "Exception while connecting to broker"
# Check command line startup options
configfile = ""
filename = ""
try:
opts, args = getopt.getopt(sys.argv[1:], "hdvc:f:", ["help", "debug", "verbose", "config=", "file="])
except getopt.GetoptError:
sys.exit(1)
for opt, arg in opts:
if opt in ("-h", "--help"):
print "Options are -d [--debug], -v [--verbose], -c [--config=]<configfile> -f [--file=]<playbackfile>"
sys.exit(0)
elif opt in ("-d", "--debug"):
DEBUG = True
print "Debug mode enabled"
elif opt in ("-v", "--verbose"):
VERBOSE = True
print "Verbose mode enabled"
elif opt in ("-c", "--config"):
configfile = arg
print "Config file: {}".format(configfile)
elif opt in ("-f", "--file"):
filename = arg
print "Playback file: {}".format(filename)
if filename == "":
print "No playback file specified"
sys.exit(1)
if configfile == "":
if os.path.isfile("~/.organ.conf"):
configfile = "~/.organ.conf"
if configfile == "":
if os.path.isfile("/etc/organ.conf"):
configfile = "/etc/organ.conf"
if configfile == "":
if os.path.isfile(sys.path[0] + "/organ.conf"):
configfile = sys.path[0] + "/organ.conf"
# Read config file
try:
if VERBOSE:
print "Using config file: {}".format(configfile)
config = ConfigParser.SafeConfigParser()
config.read(configfile)
mqttbroker = config.get("Global", "mqttbroker")
mqttport = config.getint("Global", "mqttport")
except ConfigParser.Error as e:
print "Error parsing the configuration file"
print e.message
sys.exit(2)
mqttclient = mqtt.Client("Playback")
connect_to_mqtt(mqttbroker, mqttport)
with open(filename, 'r') as f:
playback = f.readlines()
playback = [p.strip('\n') for p in playback]
STARTTIME = time.time()
try:
for p in playback:
if len(p) > 0 and p[0] != "#":
pieces = p.split(':')
if len(pieces) == 3:
playtime = pieces[0]
topic = pieces[1]
message = pieces[2]
timetowait = float(playtime) + STARTTIME - time.time()
if timetowait > 0:
time.sleep(timetowait)
mqttclient.publish(topic, message)
if DEBUG:
print "PLAY: ", pieces
except KeyboardInterrupt:
cont = False
if VERBOSE:
print "#Cleaning up"
mqttclient.disconnect()
mqttclient.loop_stop()