original_docstring: Process a single byte as incoming data.
docstring: Process a single byte as incoming data.
docstring_tokens: Process a single byte as incoming data .
code: def handle_received_byte(self, data):
          """Process a single byte as incoming data."""
          # Length of the received data so far
          pdu_len = len(self.ReceiveData)
  
          # If we were expecting a message of a particular length and what we have is already greater then that length then dump the message and resynchronise.
          if 0 < self.pmIncomingPduLen <= pdu_len:   # waiting for pmIncomingPduLen bytes but got more and haven't been able to validate a PDU
              log.debug("[data receiver] Building PDU: Dumping Current PDU " + self.toString(self.ReceiveData))
              # Reset the incoming data to 0 length and clear the receive buffer
              self.ReceiveData = bytearray(b'')
              pdu_len = len(self.ReceiveData)
  
          # If the length is 4 bytes and we're receiving a variable length message, the panel tells us the length we're expecting to receive
          if pdu_len == 4 and self.pmVarLenMsg:
              # Determine length of variable size message
              # The message type is in the second byte
              msgType = self.ReceiveData[1]
              self.pmIncomingPduLen = 7 + int(data) # (((int(self.ReceiveData[2]) * 0x100) + int(self.ReceiveData[3])))
              #if self.pmIncomingPduLen >= 0xB0:  # then more than one message
              #    self.pmIncomingPduLen = 0 # set it to 0 for this loop around
              #log.debug("[data receiver] Variable length Message Being Receive  Message {0}     pmIncomingPduLen {1}".format(hex(msgType).upper(), self.pmIncomingPduLen))
  
          # If this is the start of a new message, then check to ensure it is a 0x0D (message header)
          if pdu_len == 0:
              self.msgType_t = None
              if data == 0x0D:  # preamble
                  #log.debug("[data receiver] Start of new PDU detected, expecting response " + response)
                  #log.debug("[data receiver] Start of new PDU detected")
                  # reset the message and add the received header
                  self.ReceiveData = bytearray(b'')
                  self.ReceiveData.append(data)
                  #log.debug("[data receiver] Starting PDU " + self.toString(self.ReceiveData))
          elif pdu_len == 1:
              # The second byte is the message type
              msgType = data
              #log.debug("[data receiver] Received message Type %d", data)
              # Is it a message type that we know about
              if msgType in pmReceiveMsg_t:
                  self.msgType_t = pmReceiveMsg_t[msgType]
                  self.pmIncomingPduLen = (self.msgType_t is not None) and self.msgType_t.length or 0
                  self.pmVarLenMsg = pmReceiveMsg_t[msgType].variablelength
                  #if msgType == 0x3F: # and self.msgType_t != None and self.pmIncomingPduLen == 0:
                  #    self.pmVarLenMsg = True
              else:
                  log.warning("[data receiver] Warning : Construction of incoming packet unknown - Message Type {0}".format(hex(msgType).upper()))
              #log.debug("[data receiver] Building PDU: It's a message %02X; pmIncomingPduLen = %d", data, self.pmIncomingPduLen)
              # Add on the message type to the buffer
              self.ReceiveData.append(data)
          elif (self.pmIncomingPduLen == 0 and data == 0x0A) or (pdu_len + 1 == self.pmIncomingPduLen): # postamble   (standard length message and data terminator) OR (actual length == calculated length)
              # add to the message buffer
              self.ReceiveData.append(data)
              #log.debug("[data receiver] Building PDU: Checking it " + self.toString(self.ReceiveData))
              msgType = self.ReceiveData[1]
              if (data != 0x0A) and (self.ReceiveData[pdu_len] == 0x43):
                  log.info("[data receiver] Building PDU: Special Case 42 ********************************************************************************************")
                  self.pmIncomingPduLen = self.pmIncomingPduLen + 1 # for 0x43
              elif self.validatePDU(self.ReceiveData):
                  # We've got a validated message
                  #log.debug("[data receiver] Building PDU: Got Validated PDU type 0x%02x   full data %s", int(msgType), self.toString(self.ReceiveData))
                  # Reset some variable ready for next time
                  self.pmVarLenMsg = False
                  self.pmLastPDU = self.ReceiveData
                  self.pmLogPdu(self.ReceiveData, "<-PM  ")
                  # Record the transaction time with the panel
                  #pmLastTransactionTime = self.getTimeFunction()  # get time now to track how long it takes for a reply
  
                  # Unknown Message has been received
                  if self.msgType_t is None:
                      log.info("[data receiver] Unhandled message {0}".format(hex(msgType)))
                      self.pmSendAck()
                  else:
                      # Send an ACK if needed
                      if not self.coordinating_powerlink and self.msgType_t.ackneeded:
                          #log.debug("[data receiver] Sending an ack as needed by last panel status message " + hex(msgType).upper())
                          self.pmSendAck()
                      # Handle the message
                      log.debug("[data receiver] Received message " + hex(msgType).upper() + "   data " + self.toString(self.ReceiveData))
                      self.handle_packet(self.ReceiveData)
                      # Check response
                      if len(self.pmExpectedResponse) > 0 and msgType != 2:   # 2 is a simple acknowledge from the panel so ignore those
                          # We've sent something and are waiting for a reponse - this is it
                          #log.debug("[data receiver] msgType {0}  expected one of {1}".format(hex(msgType).upper(), [hex(no).upper() for no in self.pmExpectedResponse]))
                          if (msgType in self.pmExpectedResponse):
                              self.pmExpectedResponse.remove(msgType)
                              log.debug("[data receiver] msgType {0} got it so removed from list, list is now {1}".format(hex(msgType).upper(), [hex(no).upper() for no in self.pmExpectedResponse]))
                              self.pmSendMsgRetries = 0
                          else:
                              log.debug("[data receiver] msgType not in self.pmExpectedResponse   Waiting for next PDU :  expected {0}   got {1}".format([hex(no).upper() for no in self.pmExpectedResponse], hex(msgType).upper()))
                  self.ReceiveData = bytearray(b'')
              else: # CRC check failed. However, it could be an 0x0A in the middle of the data packet and not the terminator of the message
                  if len(self.ReceiveData) > 0xB0:
                      log.info("[data receiver] PDU with CRC error %s", self.toString(self.ReceiveData))
                      self.pmLogPdu(self.ReceiveData, "<-PM   PDU with CRC error")
                      #pmLastTransactionTime = self.getTimeFunction() - timedelta(seconds=1)
                      self.ReceiveData = bytearray(b'')
                      if msgType != 0xF1:        # ignore CRC errors on F1 message
                          self.pmCrcErrorCount = self.pmCrcErrorCount + 1
                      if (self.pmCrcErrorCount > MAX_CRC_ERROR):
                          self.pmCrcErrorCount = 0
                          self.pmHandleCommException("CRC errors")
                  else:
                      a = self.calculate_crc(self.ReceiveData[1:-2])[0]
                      log.debug("[data receiver] Building PDU: Length is now %d bytes (apparently PDU not complete)    %s    checksum calcs %02x", len(self.ReceiveData), self.toString(self.ReceiveData), a)
          elif pdu_len <= 0xC0:
              #log.debug("[data receiver] Current PDU " + self.toString(self.ReceiveData) + "    adding " + str(hex(data).upper()))
              self.ReceiveData.append(data)
          else:
              log.debug("[data receiver] Dumping Current PDU " + self.toString(self.ReceiveData))
              self.ReceiveData = bytearray(b'')
code_tokens: def handle_received_byte ( self , data ) : pdu_len = len ( self . ReceiveData ) if 0 < self . pmIncomingPduLen <= pdu_len : log . debug ( "[data receiver] Building PDU: Dumping Current PDU " + self . toString ( self . ReceiveData ) ) self . ReceiveData = bytearray ( b'' ) pdu_len = len ( self . ReceiveData ) if pdu_len == 4 and self . pmVarLenMsg : msgType = self . ReceiveData [ 1 ] self . pmIncomingPduLen = 7 + int ( data ) if pdu_len == 0 : self . msgType_t = None if data == 0x0D : self . ReceiveData = bytearray ( b'' ) self . ReceiveData . append ( data ) elif pdu_len == 1 : msgType = data if msgType in pmReceiveMsg_t : self . msgType_t = pmReceiveMsg_t [ msgType ] self . pmIncomingPduLen = ( self . msgType_t is not None ) and self . msgType_t . length or 0 self . pmVarLenMsg = pmReceiveMsg_t [ msgType ] . variablelength else : log . warning ( "[data receiver] Warning : Construction of incoming packet unknown - Message Type {0}" . format ( hex ( msgType ) . upper ( ) ) ) self . ReceiveData . append ( data ) elif ( self . pmIncomingPduLen == 0 and data == 0x0A ) or ( pdu_len + 1 == self . pmIncomingPduLen ) : self . ReceiveData . append ( data ) msgType = self . ReceiveData [ 1 ] if ( data != 0x0A ) and ( self . ReceiveData [ pdu_len ] == 0x43 ) : log . info ( "[data receiver] Building PDU: Special Case 42 ********************************************************************************************" ) self . pmIncomingPduLen = self . pmIncomingPduLen + 1 elif self . validatePDU ( self . ReceiveData ) : self . pmVarLenMsg = False self . pmLastPDU = self . ReceiveData self . pmLogPdu ( self . ReceiveData , "<-PM  " ) if self . msgType_t is None : log . info ( "[data receiver] Unhandled message {0}" . format ( hex ( msgType ) ) ) self . pmSendAck ( ) else : if not self . coordinating_powerlink and self . msgType_t . ackneeded : self . pmSendAck ( ) log . debug ( "[data receiver] Received message " + hex ( msgType ) . upper ( ) + "   data " + self . toString ( self . ReceiveData ) ) self . handle_packet ( self . ReceiveData ) if len ( self . pmExpectedResponse ) > 0 and msgType != 2 : if ( msgType in self . pmExpectedResponse ) : self . pmExpectedResponse . remove ( msgType ) log . debug ( "[data receiver] msgType {0} got it so removed from list, list is now {1}" . format ( hex ( msgType ) . upper ( ) , [ hex ( no ) . upper ( ) for no in self . pmExpectedResponse ] ) ) self . pmSendMsgRetries = 0 else : log . debug ( "[data receiver] msgType not in self.pmExpectedResponse   Waiting for next PDU :  expected {0}   got {1}" . format ( [ hex ( no ) . upper ( ) for no in self . pmExpectedResponse ] , hex ( msgType ) . upper ( ) ) ) self . ReceiveData = bytearray ( b'' ) else : if len ( self . ReceiveData ) > 0xB0 : log . info ( "[data receiver] PDU with CRC error %s" , self . toString ( self . ReceiveData ) ) self . pmLogPdu ( self . ReceiveData , "<-PM   PDU with CRC error" ) self . ReceiveData = bytearray ( b'' ) if msgType != 0xF1 : self . pmCrcErrorCount = self . pmCrcErrorCount + 1 if ( self . pmCrcErrorCount > MAX_CRC_ERROR ) : self . pmCrcErrorCount = 0 self . pmHandleCommException ( "CRC errors" ) else : a = self . calculate_crc ( self . ReceiveData [ 1 : - 2 ] ) [ 0 ] log . debug ( "[data receiver] Building PDU: Length is now %d bytes (apparently PDU not complete)    %s    checksum calcs %02x" , len ( self . ReceiveData ) , self . toString ( self . ReceiveData ) , a ) elif pdu_len <= 0xC0 : self . ReceiveData . append ( data ) else : log . debug ( "[data receiver] Dumping Current PDU " + self . toString ( self . ReceiveData ) ) self . ReceiveData = bytearray ( b'' )
short_docstring: Process a single byte as incoming data.
short_docstring_tokens: Process a single byte as incoming data .
comment: """Process a single byte as incoming data."""$SEP$# Length of the received data so far$SEP$# If we were expecting a message of a particular length and what we have is already greater then that length then dump the message and resynchronise.$SEP$# waiting for pmIncomingPduLen bytes but got more and haven't been able to validate a PDU$SEP$# Reset the incoming data to 0 length and clear the receive buffer$SEP$# If the length is 4 bytes and we're receiving a variable length message, the panel tells us the length we're expecting to receive$SEP$# Determine length of variable size message$SEP$# The message type is in the second byte$SEP$# (((int(self.ReceiveData[2]) * 0x100) + int(self.ReceiveData[3])))$SEP$#if self.pmIncomingPduLen >= 0xB0:  # then more than one message$SEP$#    self.pmIncomingPduLen = 0 # set it to 0 for this loop around$SEP$#log.debug("[data receiver] Variable length Message Being Receive  Message {0}     pmIncomingPduLen {1}".format(hex(msgType).upper(), self.pmIncomingPduLen))$SEP$# If this is the start of a new message, then check to ensure it is a 0x0D (message header)$SEP$# preamble$SEP$#log.debug("[data receiver] Start of new PDU detected, expecting response " + response)$SEP$#log.debug("[data receiver] Start of new PDU detected")$SEP$# reset the message and add the received header$SEP$#log.debug("[data receiver] Starting PDU " + self.toString(self.ReceiveData))$SEP$# The second byte is the message type$SEP$#log.debug("[data receiver] Received message Type %d", data)$SEP$# Is it a message type that we know about$SEP$#if msgType == 0x3F: # and self.msgType_t != None and self.pmIncomingPduLen == 0:$SEP$#    self.pmVarLenMsg = True$SEP$#log.debug("[data receiver] Building PDU: It's a message %02X; pmIncomingPduLen = %d", data, self.pmIncomingPduLen)$SEP$# Add on the message type to the buffer$SEP$# postamble   (standard length message and data terminator) OR (actual length == calculated length)$SEP$# add to the message buffer$SEP$#log.debug("[data receiver] Building PDU: Checking it " + self.toString(self.ReceiveData))$SEP$# for 0x43$SEP$# We've got a validated message$SEP$#log.debug("[data receiver] Building PDU: Got Validated PDU type 0x%02x   full data %s", int(msgType), self.toString(self.ReceiveData))$SEP$# Reset some variable ready for next time$SEP$# Record the transaction time with the panel$SEP$#pmLastTransactionTime = self.getTimeFunction()  # get time now to track how long it takes for a reply$SEP$# Unknown Message has been received$SEP$# Send an ACK if needed$SEP$#log.debug("[data receiver] Sending an ack as needed by last panel status message " + hex(msgType).upper())$SEP$# Handle the message$SEP$# Check response$SEP$# 2 is a simple acknowledge from the panel so ignore those$SEP$# We've sent something and are waiting for a reponse - this is it$SEP$#log.debug("[data receiver] msgType {0}  expected one of {1}".format(hex(msgType).upper(), [hex(no).upper() for no in self.pmExpectedResponse]))$SEP$# CRC check failed. However, it could be an 0x0A in the middle of the data packet and not the terminator of the message$SEP$#pmLastTransactionTime = self.getTimeFunction() - timedelta(seconds=1)$SEP$# ignore CRC errors on F1 message$SEP$#log.debug("[data receiver] Current PDU " + self.toString(self.ReceiveData) + "    adding " + str(hex(data).upper()))