-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtallylight.py
369 lines (329 loc) · 12.1 KB
/
tallylight.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Written by Tyler Gerritsen
#td0g.ca
#Changelog
#1.0 2020-05-
# Initial Commit
#1.1 2020-06-27
# Event logs
# More preference for last good IP
# Multi-Ping error catching (would occasionally have an exception)
#1.2 2020-08-14
# If no communication has occurred in the past 30 seconds, pings to make sure connection still exists
# If no connection, resets WiFi
#1.3 2020-08-26
# Code cleanup, easier to read
# Current scene name request timeout after 2 seconds
# Fixed logging to wrong date file issue
# If GPIO pins are already configured, user is given a chance to stop script
#ToDo
# Fade to black transition will turn on Tally Light if a scene with the trigger char is loaded in preview
#make sure to pip3 install multiping socket thread signal
import sys
import time
import RPi.GPIO as GPIO
import logging
from pythonping import ping
import itertools
from multiping import MultiPing
import socket
import os
import signal
todaysDate = str(time.strftime("%Y-%m-%d"))
#################### Setup ##################
#Comment out all but one of the following logFileNames. The first will put all logs into a single file. The second will create a new logfile for each day.
#logFileName = '/home/pi/tally.log'
logFileName = '/home/pi/tally_'+ todaysDate +'.log'
#Set the GPIO Pins
tallyLightGPIO = 26
statusLightGPIO = 16
#Set the trigger character
triggerChar = '+'
#Set the number of seconds of no ping response before resetting
resetSeconds = 120 #Minimum 40 seconds
beginPingSeconds = 1
#Set the websocket port (Should be 4444) and password (set in OBS Studio)
port = 4444
password = "123456"
#################### Functions ##################
def setLogger(fname):
#https://stackoverflow.com/questions/12158048/changing-loggings-basicconfig-which-is-already-set
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
logging.basicConfig(filename=logFileName,level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%y-%m-%d %H:%M:%S %Z')
print("Logging to: " + logFileName)
#Initialize logging
setLogger(logFileName)
logging.debug('Tally Light BOOTED')
#Not sure what this does...
sys.path.append('../')
from obswebsocket import obsws, events, requests # noqa: E402
#Try load last good IP address
lastKnownOBSStudioIP = ""
try:
logging.debug('Opening obsAddr.log')
ipAddressHistory = open("obsAddr.log","r")
logging.debug('Reading last known OBS Studio IP Address')
lastKnownOBSStudioIP = ipAddressHistory.readline()
logging.debug('Last known OBS Studio IP: ' + lastKnownOBSStudioIP)
ipAddressHistory.close()
except:
logging.debug('Could not load last good IP')
#Keep track of last communication time
lastCommunicationTime = time.time()
#2hz Long Blink
def delayBlinkLED(count):
count *= 2
while count:
GPIO.output(statusLightGPIO, GPIO.LOW)
time.sleep(0.25)
GPIO.output(statusLightGPIO, GPIO.HIGH)
time.sleep(0.25)
count -= 1
#4hz Fast Blink
nextBlink = 0
def fastBlink(count):
global nextBlink
if time.time() > nextBlink:
nextBlink = time.time() + 2
while(count):
GPIO.output(statusLightGPIO, GPIO.HIGH)
time.sleep(0.02)
GPIO.output(statusLightGPIO, GPIO.LOW)
if count > 1:
time.sleep(0.23)
count -= 1
#Shutdown and restart WiFi
def resetWiFi():
logging.debug("Shutting down WiFi")
print("Shutting down WiFi")
cmd = 'ifconfig wlan0 down'
os.system(cmd)
print("Bringing up WiFi in 5")
delayBlinkLED(5)
print("Now!")
logging.debug("Bringing up WiFi")
cmd = 'ifconfig wlan0 up'
os.system(cmd)
delayBlinkLED(5)
#Returns tuple of available IP addresses
def scan_all_ip():
ipRange = []
logging.debug("Pinging all IP addresses")
for i in range(1,253):
ipRange.append("192.168.2."+str(i))
ipRange.append("192.168.1."+str(i))
mp = MultiPing(ipRange)
try:
mp.send()
responses, no_responses = mp.receive(2)
except:
logging.debug("Failure to ping addresses")
print("Failure to ping addresses")
return ""
for addr, rtt in responses.items():
logging.debug ("%s responded in %f seconds" % (addr, rtt))
return responses
#Ping a target IP
def pingHost(ipToPing):
p = []
p.append(ipToPing)
mp = MultiPing(p)
try:
mp.send()
responses, no_responses = mp.receive(2)
except:
logging.debug("Ping " + ipToPing + " Failure - Unable To Ping")
print("Ping " + ipToPing + " Failure - Unable To Ping")
return False
if ipToPing in responses:
logging.debug("Ping " + ipToPing + " Successful")
print("Ping " + ipToPing + " Successful")
return True
logging.debug("Ping " + ipToPing + ": No Response")
print("Ping " + ipToPing + ": No Response")
return False
#Returns IP of OBS Studio (or "" if OBS Studio not found)
def find_open_socket():
global lastCommunicationTime
prefferedIP = ""
responses = scan_all_ip()
if responses != "":
for addr, rtt in responses.items():
if str(addr) == lastKnownOBSStudioIP:
prefferedIP = addr
print("Preffered IP loaded: " + lastKnownOBSStudioIP + " " + str(addr))
for addr, rtt in responses.items():
if connected == False:
if prefferedIP != "":
print("Attempting to connect to " + prefferedIP + "(last known IP of OBS Studio)")
logging.debug ("Attempting to connect " + prefferedIP + "(last known IP of OBS Studio)")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((prefferedIP,port))
sock.close
if result == 0:
logging.debug("OBS Studio Websocket Found!")
print("OBS Studio Websocket Found!")
return prefferedIP
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Attempting to connect to " + addr)
logging.debug ("Attempting to connect to " + addr)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((addr,port))
sock.close
if result == 0:
logging.debug("OBS Studio Websocket Found!")
print("OBS Studio Websocket Found!")
lastCommunicationTime = time.time()
return addr
return ""
def signal_handler(signum, frame):
raise Exception("Timed out!")
#Asks OBS Studio for current scene name, times out after 2 seconds
def requestCurrentSceneName():
global currentSceneName, lastCommunicationTime
message = ""
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(2)
print("Requesting current scene name - ",end="")
try:
message = ws.call(requests.GetCurrentScene())
currentSceneName = getSceneName(message)
print("Response: " + currentSceneName)
logging.debug("Current scene name: " + currentSceneName)
lastCommunicationTime = time.time()
except:
print("No response!")
logging.debug("No response to current scene name request!")
signal.signal(signal.SIGALRM, signal.SIG_IGN)
signal.alarm(0)
#Function called if any Websocket Message rec'd
def on_event(message):
global connected, lastCommunicationTime
logging.debug(u"Got message: {}".format(message))
print(u"Got message: {}".format(message))
if format(message).find("SourceDestroyed event") > -1:
connected = False
else:
lastCommunicationTime = time.time()
#Function called if scene changed
def on_switch(message):
global LEDstate, lastCommunicationTime, currentSceneName
logging.debug(u"Scene Changed To {}".format(message.getSceneName()))
lastCommunicationTime = time.time()
currentSceneName = format(message.getSceneName())
setLEDfromSceneName()
#Parses scene name from websocket message
def getSceneName(message):
sn = str(message)[str(message).find("name"):]
sn = sn[:sn.find(",")]
return sn
#Saves IP address to file for next time
def saveGoodIP(addr):
try:
ipAddressHistory = open("obsAddr.log","w+")
ipAddressHistory.write(addr)
ipAddressHistory.close()
logging.debug("Saved new OBS Studio IP: " + str(addr))
except:
pass
lastKnownOBSStudioIP = str(addr)
#Sets the LED status from the scene name
def setLEDfromSceneName():
global currentSceneName, LEDstate
if currentSceneName.find(triggerChar) > -1:
GPIO.output(tallyLightGPIO, 1)
logging.debug("LED ON")
print("LED ON")
LEDstate = 1
else:
GPIO.output(tallyLightGPIO, 0)
logging.debug("LED OFF")
print("LED OFF")
LEDstate = 0
#################### Main Loop ##################
#Setup GPIO pins
GPIO.setmode(GPIO.BCM)
if GPIO.gpio_function(statusLightGPIO) == 0:
print('WARNING!!!!!!')
print('GPIO pins are already in use!!!')
print('Please press ctl-C immediately to stop script!')
time.sleep(1)
print('You have 8 seconds to comply')
time.sleep(2)
print('Yes, this is Dredd :)')
time.sleep(5)
GPIO.setup(statusLightGPIO, GPIO.OUT)
GPIO.output(statusLightGPIO, GPIO.HIGH)
GPIO.setup(tallyLightGPIO, GPIO.OUT)
connected = False
LEDstate = 0
currentSceneName = ""
try:
while 1:
#Begin attempting to find and connect to OBS Studio
addr = find_open_socket() #Get the address of OBS Studio
if addr != "": #If OBS Studio found
ws = obsws(addr, port, password)
ws.register(on_event)
ws.register(on_switch, events.SwitchScenes)
try:
ws.connect()
requestCurrentSceneName()
setLEDfromSceneName()
connected = True
saveGoodIP(addr)
if todaysDate != str(time.strftime("%Y-%m-%d")):
print("Wrong logfile date!! Changing from " + todaysDate + " to " + str(time.strftime("%Y-%m-%d")))
logging.debug("Wrong logfile date!! Changing from " + todaysDate + " to " + str(time.strftime("%Y-%m-%d")))
if logFileName != '/home/pi/tally.log':
todaysDate = str(time.strftime("%Y-%m-%d"))
logFileName = '/home/pi/tally_'+ todaysDate +'.log'
setLogger(logFileName)
logging.debug("WAS logging to wrong logfile date!! Changed from " + todaysDate + " to " + str(time.strftime("%Y-%m-%d")))
except:
logging.debug("Connection Refused")
print("Connection Refused")
#Connected!
while connected: #blink status LED once for connected, twice for connected and Tally Light ON
if lastCommunicationTime + beginPingSeconds < time.time():
logging.debug("Haven't heard from OBS in " + str(time.time()-lastCommunicationTime) + " seconds, Pinging!")
print("Haven't heard from OBS in " + str(time.time()-lastCommunicationTime) + " seconds, Pinging!")
if pingHost(addr):
requestCurrentSceneName()
setLEDfromSceneName()
else:
time.sleep(2)
if lastCommunicationTime + resetSeconds < time.time():
logging.debug("TIMEOUT!!!")
print("TIMEOUT!!!")
resetWiFi()
connected = False
if lastCommunicationTime + resetSeconds / 2 < time.time():
fastBlink(4)
elif LEDstate == 1:
fastBlink(2)
else:
fastBlink(1)
#Connection failed (or OBS studio not found!)
try:
GPIO.output(tallyLightGPIO, 0)
ws.disconnect()
except:
pass
logging.debug("Could not find OBS Studio - Waiting 2 seconds and re-attempting")
print("Could not find OBS Studio - Waiting 2 seconds and re-attempting")
time.sleep(2)
#Script stopped by ctl-C
except KeyboardInterrupt:
GPIO.output(tallyLightGPIO, 0)
try:
ws.disconnect()
except:
pass
#Cleanup
logging.debug("Shutting Down")
print("Shutting Down")
GPIO.output(tallyLightGPIO, 0)
GPIO.cleanup()