From 018a40e61407b3e12acaef1f3b6184441f8f26ff Mon Sep 17 00:00:00 2001 From: dandaedre Date: Thu, 30 Sep 2021 17:37:50 +0200 Subject: [PATCH] Added a lame recording for the fake server --- README.md | 6 ++ bindings.py | 59 +++++++---- datadump.txt | 291 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 337 insertions(+), 19 deletions(-) create mode 100644 datadump.txt diff --git a/README.md b/README.md index fb88a6d..474034e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,8 @@ # pythonAnalogWay Python binds for analogway controllers + + +* TODO : If connect returns 0 wait another connect +* TODO : Verbosity levels +* TODO : Do something with all the value obtained +* TODO : Do a fakeserver diff --git a/bindings.py b/bindings.py index bdd191c..19c7905 100755 --- a/bindings.py +++ b/bindings.py @@ -22,7 +22,9 @@ _MSG_ENDING=b"\r\n" # BIG DEFINES _NO_FUNCT=lambda x:None -_SYS_EXIT=lambda x:sys.exit() +def _SYS_EXIT(system,*args): + system.fatal=True + sys.exit() if _VERBOSE: def dprint(*args): @@ -147,7 +149,7 @@ class analogController(object): self.port=port self.running=True self.listening=False - + self.fatal=False # Last received match messages self.messages={i:None for i in ["CONNECT","DEVICE","VERSION","STATUS","KPALIVE","REGEX"]} @@ -181,25 +183,26 @@ class analogController(object): # self.feedback.messageReceived("CONNECT",match) def receive_GENERIC(self,match): "We received a connect message, all the logic will be on the feedback side" - # self.messages["CONNECT"]=match - # self._LOCKS["connect"].release() - # self.feedback.messageReceived("CONNECT",match) - typ=_MATCHS[match.group("msg")] - self.messages[typ]=match #why not, not very clever tho - self._LOCKS[typ].release() - self.feedback.messageReceived("typ",match) + try: + typ=_MATCHS[match.group("msg")] + self.messages[typ]=match #why not, not very clever tho + self._LOCKS[typ].release() + self.feedback.messageReceived("typ",match) + except RuntimeError as e: + iprint("Lock [{}] is already released (async terminated earlier)".format(typ)) + iprint(e) + ############################################## # LOCKS - def waitLock(self,lockname,function_success=_NO_FUNCT,args_succes=(),function_error=_NO_FUNCT,args_error=()): + def waitLock(self,lockname,function_success=_NO_FUNCT,args_success=(),function_error=_NO_FUNCT,args_error=()): """Wait for the lock to be released, non blocking""" - thd=threading.Thread(target=self._initLockWait,args=(lockname,function_success,args_succes,function_error,args_error)) + thd=threading.Thread(target=self._initLockWait,args=(lockname,function_success,args_success,function_error,args_error)) thd.start() return thd - def _initLockWait(self,lockname,function_success=_NO_FUNCT,args_succes=(),function_error=_NO_FUNCT,args_error=()): + def _initLockWait(self,lockname,function_success=_NO_FUNCT,args_success=(),function_error=_NO_FUNCT,args_error=()): """Thread waiting for a lock""" - print(function_success,args_succes,function_error,args_error) iprint("Checking for [{}] lock avaivability".format(lockname)) if not self._LOCKS[lockname].locked(): #The state it's supposed to be in, but let's not be too sure self._LOCKS[lockname].acquire(timeout=0.01) @@ -225,25 +228,37 @@ class analogController(object): iprint('Connecting to server, {self.ip}:{self.port}'.format(self=self)) self.sck.connect((self.ip,self.port)) - wait=self.waitLock("CONNECT",function_error=_SYS_EXIT) + wait=self.waitLock("CONNECT",function_error=_SYS_EXIT,args_error=(self)) self.sendData("*\r\n") - iprint("Waiting for",wait) + # iprint("Waiting for",wait) wait.join() - iprint("Finished wait for",wait) + if self.fatal: + _SYS_EXIT(self) + # iprint("Finished wait for",wait) def getDevice(self): """This read only command gives the device type [?] (DEV ) :_DEVICES_VALUES """ + iprint("Acquiring device type") + wait=self.waitLock("DEVICE",function_error=_SYS_EXIT,args_error=(self)) self.sendData("?") + wait.join() + if self.fatal: + _SYS_EXIT(self) def getVersion(self): """his read only command gives the version number of the command set. It is recommended to check that this value matches the one expected by the controller. [VEvar] (VEvar) """ + iprint("Acquiring version") + wait=self.waitLock("VERSION",function_error=_SYS_EXIT,args_error=(self)) self.sendData("VEvar") + wait.join() + if self.fatal: + _SYS_EXIT(self) def getStatus(self,value=3): """Reading a change of values @@ -332,7 +347,7 @@ class analogController(object): "Execute the full connection sequence" self.start_listening() self.connect() - # self.getDevice() + self.getDevice() # self.getVersion() # self.getStatus() @@ -351,9 +366,14 @@ class analogController(object): "Yield only clean messages" self.running=True reply_bytes=b"" + while self.running: try: - reply_bytes+=self.sck.recv(_MSGSIZE) + # f = open("datadump.txt", "ab+") + received=self.sck.recv(_MSGSIZE) + # f.write(received) + # f.close() + reply_bytes+=received if reply_bytes.find(_MSG_ENDING)==-1: # Message is not finished continue else: @@ -381,7 +401,8 @@ class analogController(object): """Process a match with a message, should only be called by the socketLoop funciton""" try: name=_MATCHS[match.group("msg")] - # self.__getattr__("connect_{}".format(name),"GENERIC")(match) + # self.__getattribute__("receive_{}".format(name),default="GENERIC")(match) + self.receive_GENERIC(match) except KeyError as e: dprint("[pAW:ERROR] Can't find matched key:",match) diff --git a/datadump.txt b/datadump.txt new file mode 100644 index 0000000..0ab0097 --- /dev/null +++ b/datadump.txt @@ -0,0 +1,291 @@ + +*1 +DEV259 +PRinp0,1,1,7 +PUscu0,1 +GCtav0,0 +CTsto1 +PRinp0,1,7,6 +PRinp0,1,1,6 +PUscu0,1 +CTsto0 +GCtav0,1 +AUoci1,12 +GCtav0,0 +CTsto1 +PRinp0,1,7,8 +PRinp0,1,1,8 +PUscu0,1 +CTsto0 +GCtav0,1 +AUoci1,14 +GCtav0,0 +CTsto1 +PRinp0,1,7,4 +PRinp0,1,1,4 +PUscu0,1 +CTsto0 +AUoci1,0 +GCtav0,1 +GCtav0,0 +CTsto1 +PRinp0,1,7,3 +PRinp0,1,1,3 +PUscu0,1 +GCtav0,1 +CTsto0 +GCtav0,0 +CTsto1 +PRinp0,1,7,2 +PRinp0,1,1,2 +PUscu0,1 +AUoci1,9 +GCtav0,1 +CTsto0 +GCtav0,0 +CTsto1 +PRinp0,1,7,1 +PRinp0,1,1,1 +PUscu0,1 +GCtav0,1 +AUoci1,6 +AUoci1,1 +CTsto0 +GCtav0,0 +CTsto1 +PRinp0,1,7,5 +PRinp0,1,1,5 +PUscu0,1 +GCtav0,1 +CTsto0 +AUoci1,11 +GCtav0,0 +CTsto1 +PRinp0,1,7,6 +PRinp0,1,1,6 +PUscu0,1 +CTsto0 +GCtav0,1 +AUoci1,12 +GCply2 +GCtav0,0 +CTsto1 +PRinp0,1,7,1 +PRinp0,1,2,1 +PUscu0,1 +GCtav0,1 +CTsto0 +AUoci1,1 +GCtav0,0 +CTsto1 +PRinp0,1,7,2 +PRinp0,1,2,2 +PUscu0,1 +GCtav0,1 +CTsto0 +AUoci1,6 +GCtav0,0 +CTsto1 +PRinp0,1,7,3 +PRinp0,1,2,3 +PUscu0,1 +CTsto0 +GCtav0,1 +GCtav0,0 +CTsto1 +PRinp0,1,7,4 +PRinp0,1,2,4 +PUscu0,1 +CTsto0 +AUoci1,9 +GCtav0,1 +AUoci1,0 +GCtav0,0 +CTsto1 +PRinp0,1,7,8 +PRinp0,1,2,8 +PUscu0,1 +CTsto0 +GCtav0,1 +GCtav0,0 +CTsto1 +PRinp0,1,7,7 +PRinp0,1,2,7 +PUscu0,1 +GCtav0,1 +CTsto0 +AUoci1,14 +AUoci1,13 +GCtav0,0 +CTsto1 +PRinp0,1,7,6 +PRinp0,1,2,6 +PUscu0,1 +GCtav0,1 +CTsto0 +GCtav0,0 +CTsto1 +PRinp0,1,7,5 +PRinp0,1,2,5 +PUscu0,1 +AUoci1,12 +GCtav0,1 +CTsto0 +AUoci1,11 +GCply0 +GCtav0,0 +CTsto1 +PRinp0,1,0,1 +PUscu0,1 +CTsto0 +GCtio0,1 +GCtav0,1 +GCtav0,0 +CTsto1 +PRinp0,1,0,2 +PUscu0,1 +GCtav0,1 +CTsto0 +GCtav0,0 +CTsto1 +PRinp0,1,0,3 +PUscu0,1 +GCtav0,1 +CTsto0 +GCtav0,0 +CTsto1 +PRinp0,1,0,4 +PUscu0,1 +GCtav0,1 +CTsto0 +GCtav0,0 +CTsto1 +PRinp0,1,0,5 +PUscu0,1 +GCtio0,0 +GCtav0,1 +CTsto0 +GCtak0,1 +GCtav0,0 +AUoci0,11 +CTsto1 +PRinp0,2,0,5 +PRinp0,2,1,7 +PRinp0,2,7,7 +PRinp0,0,1,6 +PRinp0,0,2,5 +PRinp0,0,7,5 +GCtak0,0 +GCtav0,1 +CTsto0 +PSmod2 +PSidx0 +PSidx0 +PSidx1 +PSexe1 +CTsto1 +PSfrv1,0 +PSfsh1,0 +PSfsv1,0 +CTsto0 +PSsta3 +PSprg1 +PSprg2 +PSprg3 +PSprg4 +PSprg5 +PSprg6 +PSprg8 +PSprg9 +PSprg10 +PSprg11 +PSprg12 +PSprg13 +PSprg15 +PSprg16 +PSprg17 +PSprg18 +PSprg19 +PSprg20 +PSprg22 +PSprg23 +PSprg25 +PSprg26 +PSprg28 +PSprg29 +PSprg30 +PSprg31 +PSprg33 +PSprg34 +PSprg35 +PSprg36 +PSprg37 +PSprg39 +PSprg40 +PSprg41 +PSprg42 +PSprg44 +PSprg45 +PSprg46 +PSprg47 +PSprg48 +PSsta0 +PSsta2 +PSprg51 +PSprg52 +PSprg53 +PSprg55 +PSprg56 +PSprg57 +PSprg59 +PSprg60 +PSprg61 +PSprg62 +PSprg64 +PSprg65 +PSprg66 +PSprg68 +PSprg69 +PSprg70 +PSprg71 +PSprg73 +PSprg74 +PSprg75 +PSprg77 +PSprg78 +PSprg80 +PSprg81 +PSprg82 +PSprg84 +PSprg85 +PSprg86 +PSprg88 +PSprg89 +PSprg91 +PSprg92 +PSprg94 +PSprg95 +PSprg96 +PSprg98 +PSprg99 +PSsta0 +PSprg100 +CTsto1 +PSfsh1,1920 +PSfsv1,1080 +PSfrv1,1 +PSprg0 +PSexe0 +CTsto0 +PSmod0 +GCtak0,1 +GCtav0,0 +CTsto1 +PRinp0,2,1,6 +PRinp0,2,2,5 +PRinp0,2,7,5 +GCtak0,0 +GCtav0,1 +CTsto0 +CTqfa0,1 +CTqfa0,0