def _parseLength(self): # read header and length head = unpack(self._fmtLength, self._head)[0] head = bits.bitClear(head, 15) head = bits.bitClear(head, 14) # head = bits.bitClear(head, 13) # head = bits.bitClear(head, 12) self._length = head self._header = head >> 14
def __parse(self): """ Parses rawData """ buffer = self.__rawData if buffer == None: return # read header and length archive = False header, length = unpack("<BH", buffer[:3]) if self.isHalved(header): archive = bits.bitTest(length, 15) length = bits.bitClear(length, 15) if length > len(buffer) + 5: raise NeedMoreDataException('Not enough data in buffer') crc = unpack("<H", buffer[length + 3:length + 5])[0] crc_data = buffer[:length + 3] if not self.isCorrectCrc(crc_data, crc): raise Exception('Crc Is incorrect!') # now let's read packet data # but before this, check tagsdata length body = buffer[3:length + 3] if len(body) != length: raise Exception('Body length Is incorrect!') # apply new data self.__rawDataTail = buffer[length + 5:] self.__rawData = buffer[:length + 5] self.__archive = archive self.__header = header self.__length = length self.__body = body self.__crc = crc # parse packet body self._parseBody(body)
def _parseBody(self): """ Parses packet's head @return: self """ super(AvlDataCodec7, self)._parseBody() self._body = self._rawData self._params = {} self._sensors = {} time = self.readFrom('>L') priority = bits.bitRangeValue(time, 30, 32) if priority == 2: self._sensors['sos'] = 1 time = bits.bitClear(time, 30) time = bits.bitClear(time, 31) self._params['time'] = datetime(2007, 1, 1) + timedelta(seconds=time) globalMask = self.readFrom('>B') hasGpsElement = bits.bitTest(globalMask, 0) ioElements = [] ioElementsFormats = ['>B', '>H', '>L'] for index in range(len(ioElementsFormats)): if bits.bitTest(globalMask, index + 1): ioElements.append(ioElementsFormats[index]) if hasGpsElement: mask = self.readFrom('>B') # Latitude and longitude if bits.bitTest(mask, 0): self._params['latitude'] = self.readFrom('>f') self._params['longitude'] = self.readFrom('>f') # Altitude if bits.bitTest(mask, 1): self._params['altitude'] = self.readFrom('>h') # Angle if bits.bitTest(mask, 2): self._params['azimuth'] = self.readFrom('>B') * 360 / 256 # Speed if bits.bitTest(mask, 3): self._params['speed'] = self.readFrom('>B') # Satellites if bits.bitTest(mask, 4): self._params['satellitescount'] = self.readFrom('>B') # CellID if bits.bitTest(mask, 5): self._params['gsm_cell_lac'] = self.readFrom('>H') self._params['gsm_cell_id'] = self.readFrom('>H') # Signal quality if bits.bitTest(mask, 6): self._params['gsm_signal_quality'] = self.readFrom('>B') # Operator code if bits.bitTest(mask, 7): self._params['gsm_operator_code'] = self.readFrom('>L') items = [] for fmt in ioElements: cnt = self.readFrom('>B') for i in range(cnt): items.append({ 'id': self.readFrom('>B'), 'value': self.readFrom(fmt) }) self._parseElements(items) self._params['sensors'] = self._sensors self._ioElement = { # Event IO ID – if data is acquired on event – this field # defines which IO property has changed and generated an event. # If data cause is not event – the value is 0. 'eventIoId': 0, # List of IO elements 'items': items } return self