diff --git a/effects/udp-mcast.json b/effects/udp-mcast.json new file mode 100644 index 0000000..eb492ff --- /dev/null +++ b/effects/udp-mcast.json @@ -0,0 +1,9 @@ +{ + "name" : "UDP multicast listener", + "script" : "udp.py", + "args" : + { + "ListenPort" : 2801, + "ListenIP" : "239.255.28.01" + } +} diff --git a/effects/udp.json b/effects/udp.json index 29df097..0fc6e6e 100644 --- a/effects/udp.json +++ b/effects/udp.json @@ -1,8 +1,8 @@ -{ - "name" : "UDP listener", - "script" : "udp.py", - "args" : - { - "udpPort" : 2391 - } -} +{ + "name" : "UDP listener", + "script" : "udp.py", + "args" : + { + "ListenPort" : 2391 + } +} diff --git a/effects/udp.py b/effects/udp.py index f8317a2..6f39b1d 100644 --- a/effects/udp.py +++ b/effects/udp.py @@ -3,17 +3,33 @@ import time import colorsys import socket import errno +import struct # Get the parameters -udpPort = int(hyperion.args.get('udpPort', 2812)) +ListenPort = int(hyperion.args.get('ListenPort', 2801)) +ListenIP = hyperion.args.get('ListenIP', "") +octets = ListenIP.split('.'); -UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) +UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM, socket.IPPROTO_UDP) UDPSock.setblocking(False) -listen_addr = ("",udpPort) -print "udp.py: bind socket port:",udpPort +listen_addr = (ListenIP,ListenPort) UDPSock.bind(listen_addr) +if ListenIP == "": + print "udp.py: Listening on *.*.*.*:"+str(ListenPort) +else: + print "udp.py: Listening on "+ListenIP+":"+str(ListenPort) + +if len(octets) == 4 and int(octets[0]) >= 224 and int(octets[0]) < 240: + print "ListenIP is a multicast address\n" + # Multicast handling + try: + mreq = struct.pack("4sl", socket.inet_aton(ListenIP), socket.INADDR_ANY) + UDPSock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) + except socket.error: + print "ERROR enabling multicast\n" + hyperion.setColor(hyperion.ledCount * bytearray((int(0), int(0), int(0))) ) # Start the write data loop