Skip to content

Instantly share code, notes, and snippets.

@hdo
Created December 7, 2016 10:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hdo/792eebcb98c14285e65069e325a0e520 to your computer and use it in GitHub Desktop.
Save hdo/792eebcb98c14285e65069e325a0e520 to your computer and use it in GitHub Desktop.
Emulate Advantech ADAM-6050 for Synology Surveillance Station
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
import time
class Echo(DatagramProtocol):
def datagramReceived(self, data, (host, port)):
print "received %r from %s:%d" % (data, host, port)
# Login
if data.startswith('$01PW'):
self.transport.write(">01\r", (host, port))
return
# Request IO status
if data.startswith('$01C'):
self.transport.write("!01" + "000000000000" + "000000000000" + "000000000000" + "\r", (host, port))
return
# Request IO input status
if data.startswith('$016'):
seconds = int(time.time()) % 60
print seconds
if seconds > 20 and seconds < 24:
print "trigger!"
self.transport.write("!01"+"00"+"FFFF\r", (host, port))
else:
self.transport.write("!01"+"01"+"FFFF\r", (host, port))
return
# Request set IO output
if data.startswith('#011'):
print "Channel[0x" + data[4:6]+ "] = " + data[6:7]
self.transport.write(">\r", (host, port))
return
print "unknow command!"
#self.transport.write(data, (host, port))
print "starting ..."
reactor.listenUDP(1025, Echo())
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment