def send(self, code, retval_type, destination_id): code_sha1 = hashlib.sha1(bytearray(code)).digest() code_hash = [ord(i) for i in code_sha1[-4:]] code_padding = [ 0 ] if len(code) % 2 == 1 else 0 # Make sure the capsule has even length capsule_length = 2 + len(code_hash) + len(code) + len(code_padding) capsule = [capsule_length % 256, (capsule_length >> 8) % 256 ] + code_hash + code + code_padding retval_size = sizeInBits(retval_type) / 8 capsule_fragments = list( chunks(capsule, CodeGeneratorZwave.MAX_PAYLOAD)) number_of_fragments = len(capsule_fragments) # print "Return type " + str(retval_type) # print "Return size " + str(retval_size) # print "Executing capsule", capsule for i in range(number_of_fragments): message = [ 0x88, CodeGeneratorZwave.WKECO_COMM_ECOCOMMAND, CodeGeneratorZwave.seqnr % 256, CodeGeneratorZwave.seqnr / 256, i, number_of_fragments - 1, retval_size ] + capsule_fragments[i] # print "Fragment ", capsule_fragments[i] # print "Message ", message pyzwave.send(destination_id, message) reply = pyzwave.receive(1000) if (reply[1][3] == CodeGeneratorZwave.WKECO_REPLY_TOO_BIG): raise OutOfMemoryError( "Capsule too big to fit in the node's buffer") if (reply[1][3] == CodeGeneratorZwave.WKECO_REPLY_EXECUTED): # print "Received reply after executing capsule:" + str(reply) return bytearray(reply[1][4:])
def sendcmd(dest, cmd, payload=[], retries=3): pyzwave.receive(10) # Clear pending messages while retries >= 0: try: pyzwave.send(dest, [0x88, cmd] + payload) except: print "=============IOError============ retries remaining:" print retries if retries <= 0: raise else: retries -= 1 else: if cmd == APPMSG: ack = pyzwave.receive(5000) # Receive ack of APPMSG, TODO: see if sending succeeded. print "APPMSG ACK:", ack return
def checkedReceive(allowedReplies, waitmsec=1000): reply = pyzwave.receive(waitmsec) if reply == None: print "No reply received. One of", allowedReplies, "expected." return None if not reply[0] in allowedReplies: print "Incorrect reply received. One of", allowedReplies, "expected, but got", reply return None return reply
def receive(self, timeout_msec=100): while 1: logging.info('receiving') try: src, reply = pyzwave.receive(timeout_msec) if src and reply: # with seq number message = new_message(src, reply[0], reply[1:]) messages.put_nowait(message) except: logging.exception('receive exception') logging.info('receive: going to sleep') gevent.sleep(0.01) # sleep for at least 10 msec
def recv(self): with self._global_lock: try: src, reply = pyzwave.receive(ZWAVE_RECV_TIMEOUT) if src and reply: logger.debug("receives message %s from address %X" % (reply, src)) reply = "".join(map(chr, reply)) return (src, reply) except Exception as e: ret = traceback.format_exc() logger.error("receives exception %s\n%s" % (str(e), ret)) return (None, None)
def receive(self, timeout_msec=100): while 1: try: src, reply = pyzwave.receive(timeout_msec) if src and reply: # with seq number deliver = new_deliver(src, reply[0], reply[1:]) messages.put_nowait(deliver) print 'receive: put a message to messages' except: print 'receive exception' getDeferredQueue().removeTimeoutDefer() #logger.debug('receive: going to sleep') gevent.sleep(0.01) # sleep for at least 10 msec
def receive(self, timeout_msec=100): while 1: try: src, reply = pyzwave.receive(timeout_msec) if src and reply: # with seq number deliver = new_deliver(src, reply[0], reply[1:]) messages.put_nowait(deliver) print '[transport] receive: put a message to messages' except: print '[transport] receive exception' getDeferredQueue().removeTimeoutDefer() #logger.debug('receive: going to sleep') gevent.sleep(0.01) # sleep for at least 10 msec
def receive(waitmsec=1000): return pyzwave.receive(waitmsec)