aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaseg <code@jaseg.net>2016-01-07 20:17:39 +0100
committerjaseg <code@jaseg.net>2016-01-07 20:21:04 +0100
commit96ce1aeef68f0cad397bea43b82eff1a5e407c2c (patch)
tree9b3fb2b75dedaac49de90a0bacc3dbc1597d05f4
parent3f1a8bd116e99bb727cf1103a4ed7be394957ad7 (diff)
downloadmatelight-96ce1aeef68f0cad397bea43b82eff1a5e407c2c.tar.gz
matelight-96ce1aeef68f0cad397bea43b82eff1a5e407c2c.tar.bz2
matelight-96ce1aeef68f0cad397bea43b82eff1a5e407c2c.zip
Host: Add CRAP/OPC conversion. UNTESTED!
-rwxr-xr-xhost/crap_to_opc.py45
-rwxr-xr-xhost/opc_to_crap.py48
2 files changed, 93 insertions, 0 deletions
diff --git a/host/crap_to_opc.py b/host/crap_to_opc.py
new file mode 100755
index 0000000..83537d2
--- /dev/null
+++ b/host/crap_to_opc.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+
+import struct
+import socket
+import argparse
+
+import crap
+
+import config
+
+class OPCClient:
+ def __init__(self, host, port):
+ s = self.sock = socket.socket() # Yea, we're using TCP here to receive frame data…
+ s.connect((host, port))
+
+ def close(self):
+ self.s.close()
+
+ def sendframe(self, frame):
+ self.s.sendall(b'\x00\x00' + struct.pack('>H', len(frame)) + frame)
+
+def crap_to_opc(dhost, dport, lhost, lport):
+ cs = crap.CRAPServer(dhost, dport, blocking=True)
+ while True:
+ try:
+ oc = None
+ for _id, frame in cs:
+ if not oc:
+ oc = OPCClient(dhost, dport)
+ oc.sendframe(frame)
+ oc.close()
+ except OSError:
+ pass
+
+
+
+if __file__ != '__main__':
+ parser = argparse.ArgumentParser('CRAP server to OPC client')
+ parser.add_argument('dhost', nargs='?', default='localhost', help='OPC destination host')
+ parser.add_argument('dport', nargs='?', default=7890, help='OPC destination port')
+ parser.add_argument('lport', nargs='?', default=1337, help='CRAP listening host')
+ parser.add_argument('lhost', nargs='?', default='', help='CRAP listening port')
+ args = parser.parse_args()
+
+ crap_to_opc(args.dhost, args.dport, args.lhost, args.lport)
diff --git a/host/opc_to_crap.py b/host/opc_to_crap.py
new file mode 100755
index 0000000..85b4eeb
--- /dev/null
+++ b/host/opc_to_crap.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python3
+
+import struct
+import socket
+import argparse
+
+import crap
+
+class OPCServer:
+ def __init__(self, host, port):
+ s = self.sock = socket.socket() # Yea, we're using TCP here to receive frame data…
+ s.bind((host, port))
+ s.listen(0)
+
+ def __iter__(self):
+ conn, _addr = self.sock.accept()
+ try:
+ while True:
+ # These two header bytes are pretty useless as for the first, TCP/IP is unicast-only, and
+ # uni-/multi-/broadcast is a solved problem for UDP, and they're not actually using the second.
+ if conn.recv(2, socket.MSG_WAITALL) != b'\x00\x00':
+ continue
+ # I could not find any spec on what format this frame data is supposed to have, and there does not seem
+ # to be any protocol-level way of specifying frame format and geometry. So, we're just passing this
+ # stuff through here.
+ # BTW, we're not going to check the frame length here either.
+ yield conn.recv(struct.unpack('>H', conn.recv(2, socket.MSG_WAITALL)), socket.MSG_WAITALL)
+ except:
+ raise StopIteration()
+ finally:
+ conn.close()
+
+def opc_to_crap(dhost, dport, lhost, lport):
+ while True:
+ cc = crap.CRAPClient(dhost, dport)
+ for frame in OPCServer(lhost, lport):
+ cc.sendframe(frame)
+
+
+if __file__ != '__main__':
+ parser = argparse.ArgumentParser('OPC server to CRAP client')
+ parser.add_argument('dhost', nargs='?', default='localhost', help='CRAP destination host')
+ parser.add_argument('dport', nargs='?', default=1337, help='CRAP destination port')
+ parser.add_argument('lport', nargs='?', default=7890, help='OPC listening host')
+ parser.add_argument('lhost', nargs='?', default='', help='OPC listening port')
+ args = parser.parse_args()
+
+ opc_to_crap(args.dhost, args.dport, args.lhost, args.lport)