-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAHF_UDPTrig.py
60 lines (47 loc) · 1.74 KB
/
AHF_UDPTrig.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
#! /usr/bin/python
#-*-coding: utf-8 -*-
import socket
UDP_PORT = 5007 # special open port
class AHF_UDPTrig:
"""
Sends/receives UDP signals as to another pi to start/stop recording
AHF_UDPTrig uses the socket module to do the UDP stuff, but it should be part of
the default install
"""
def __init__ (self, UDPlist_p):
"""Makes a new AHF_UDPtrig object using passed in list of ip addresses.
stores UDPlist in the new object
sets hasUDP to false if object creation fails because of network error, else True
"""
try:
self.UDPlist = UDPlist_p
self.sock=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind (('', UDP_PORT))
hasUDP= True
except socket.error:
hasUDP = False
print ('AHF_UDPTrig failed to create a socket.')
def doTrigger (self, message):
"""
Sends a UDP message to the stored list of ip addresses
"""
try:
for address in self.UDPlist:
self.sock.sendto (bytes (message, "utf-8"),(address, UDP_PORT))
except socket.error as e:
print ('AHF_UDPTrig failed to send a message: ' + str (e))
def getTrigger (self):
"""
Waits for a UDP message and returns a string contaiing the message
"""
data, addr=self.sock.recvfrom(1024)
dataStr = data.decode("utf-8")
return (addr[0], dataStr)
#for testing purposes
if __name__ == '__main__':
hasUDP = True
trigger = AHF_UDPTrig (('142.103.107.241','142.103.107.239'))
if hasUDP == True:
message = 'hello_from_AHF_UDPTrig'
trigger.doTrigger (message)
print (trigger.getTrigger ())