コード例 #1
0
ファイル: BaseTest.py プロジェクト: honir/cogent-house
    def store_state(self, msg):
        """ receive and process a message object from the base station
        """

        # get the last source

        if msg.get_special() != Packets.SPECIAL:
            raise Exception("Corrupted packet - special is %02x not %02x" %
                            (msg.get_special(), Packets.SPECIAL))

        try:
            session = Session()
            current_time = datetime.utcnow()
            node_id = msg.getAddr()
            parent_id = msg.get_ctp_parent_id()
            seq = msg.get_seq()

            node = session.query(Node).get(node_id)
            loc_id = None
            if node is None:
                add_node(session, node_id)
            else:
                loc_id = node.locationId

            pack_state = PackState.from_message(msg)

            if duplicate_packet(session=session,
                                receipt_time=current_time,
                                node_id=node_id,
                                localtime=msg.get_timestamp()):
                LOGGER.info(
                    "duplicate packet %d->%d, %d %s" %
                    (node_id, parent_id, msg.get_timestamp(), str(msg)))

                #send acknowledgement to base station to fwd to node
                self.send_ack(seq=seq, dest=node_id)
                return

            # write a node state row
            node_state = NodeState(time=current_time,
                                   nodeId=node_id,
                                   parent=parent_id,
                                   localtime=msg.get_timestamp(),
                                   seq_num=seq)
            session.add(node_state)

            for i, value in pack_state.d.iteritems():
                if (msg.get_amType() == Packets.AM_BNMSG
                        and i not in [Packets.SC_VOLTAGE]):
                    type_id = i + BN_OFFSET  # TODO: ideally should be a flag in datbase or something
                else:
                    type_id = i

                session.add(
                    Reading(time=current_time,
                            nodeId=node_id,
                            typeId=type_id,
                            locationId=loc_id,
                            value=value))

            session.commit()

            #send acknowledgement to base station to fwd to node
            self.send_ack(seq=seq, dest=node_id)

            LOGGER.debug("reading: %s, %s" % (node_state, pack_state))
        except Exception as exc:
            session.rollback()
            LOGGER.exception("during storing: " + str(exc))
        finally:
            session.close()
コード例 #2
0
ファイル: BaseTest.py プロジェクト: rwilkins87/cogent-house
    def store_state(self, msg):
        """ receive and process a message object from the base station
        """
    
        # get the last source 

        if msg.get_special() != Packets.SPECIAL:
            raise Exception("Corrupted packet - special is %02x not %02x" %
                            (msg.get_special(), Packets.SPECIAL))

        try:
            session = Session()
            current_time = datetime.utcnow()
            node_id = msg.getAddr()
            parent_id = msg.get_ctp_parent_id()
            seq = msg.get_seq()

            node = session.query(Node).get(node_id)
            loc_id = None
            if node is None:
                add_node(session, node_id)
            else:
                loc_id = node.locationId


            pack_state = PackState.from_message(msg)
            
            if duplicate_packet(session=session,
                                receipt_time=current_time,
                                node_id=node_id,
                                localtime=msg.get_timestamp()):
                LOGGER.info("duplicate packet %d->%d, %d %s" %
                            (node_id, parent_id, msg.get_timestamp(), str(msg)))

                #send acknowledgement to base station to fwd to node
                self.send_ack(seq=seq,
                              dest=node_id)
                return

            # write a node state row
            node_state = NodeState(time=current_time,
                                   nodeId=node_id,
                                   parent=parent_id,
                localtime=msg.get_timestamp(),
                seq_num=seq)
            session.add(node_state)

            for i, value in pack_state.d.iteritems():
                if (msg.get_amType() == Packets.AM_BNMSG and
                    i not in [Packets.SC_VOLTAGE]):
                    type_id = i + BN_OFFSET   # TODO: ideally should be a flag in datbase or something
                else:
                    type_id = i

                session.add(Reading(time=current_time,
                                    nodeId=node_id,
                                    typeId=type_id,
                                    locationId=loc_id,
                    value=value))

            session.commit()

            #send acknowledgement to base station to fwd to node
            self.send_ack(seq=seq,
                          dest=node_id)
                     
            LOGGER.debug("reading: %s, %s" % (node_state, pack_state))
        except Exception as exc:
            session.rollback()
            LOGGER.exception("during storing: " + str(exc))
        finally:
            session.close()
コード例 #3
0
 def test_from_message(self):
     msg = Msg({5: 1.1, 23: 2.0})
     pack_state = PackState.from_message(msg)
     self.assertEqual('PackState({5: 1.1, 23: 2.0})', repr(pack_state))
コード例 #4
0
 def test_size(self):
     pack_state = PackState({6: 3.0, 23: 1.0})
     self.assertEqual(2, pack_state.size())
コード例 #5
0
 def test___repr__(self):
     pack_state = PackState({5: 1.1})
     self.assertEqual("PackState({5: 1.1})", repr(pack_state))
コード例 #6
0
 def test___str__(self):
     pack_state = PackState({5: 1.1})
     self.assertEqual('{5: 1.1}', str(pack_state))
コード例 #7
0
 def test___init__(self):
     pack_state = PackState({6: 3.0})
     self.assertEqual({6: 3.0}, pack_state.d)
コード例 #8
0
 def test___getitem__(self):
     pack_state = PackState({6: 3.0, 23: 1.0})
     self.assertEqual(1.0, pack_state[23])
コード例 #9
0
 def test_size(self):
     pack_state = PackState({6:3.0, 23:1.0})
     self.assertEqual(2, pack_state.size())
コード例 #10
0
 def test_from_message(self):
     msg = Msg({5:1.1, 23:2.0})
     pack_state = PackState.from_message(msg)
     self.assertEqual('PackState({5: 1.1, 23: 2.0})', repr(pack_state))
コード例 #11
0
ファイル: BaseLogger.py プロジェクト: honir/cogent-house
    def store_state(self, msg):
        """ receive and process a message object from the base station
        """
    
        if msg.get_special() != Packets.SPECIAL:
            raise Exception("Corrupted packet - special is %02x not %02x" %
                            (msg.get_special(), Packets.SPECIAL))

        try:
            session = Session()
            current_time = datetime.utcnow()
            node_id = msg.getAddr()
            parent_id = msg.get_ctp_parent_id()
            seq = msg.get_seq()
            rssi_val = msg.get_rssi()

            node = session.query(Node).get(node_id)
            loc_id = None
            if node is None:
                add_node(session, node_id)
            else:
                loc_id = node.locationId


            pack_state = PackState.from_message(msg)
            
            if duplicate_packet(session=session,
                                receipt_time=current_time,
                                node_id=node_id,
                                localtime=msg.get_timestamp()):
                LOGGER.info("duplicate packet %d->%d, %d %s" %
                            (node_id, parent_id, msg.get_timestamp(), str(msg)))

                return False

            # write a node state row
            node_state = NodeState(time=current_time,
                                   nodeId=node_id,
                                   parent=parent_id,
                localtime=msg.get_timestamp(),
                seq_num=seq,
                rssi = rssi_val)
            session.add(node_state)

            for i, value in pack_state.d.iteritems():
                type_id = i
                if math.isinf(value) or math.isnan(value):
                    value = None

                r = Reading(time=current_time,
                            nodeId=node_id,
                            typeId=type_id,
                            locationId=loc_id,
                            value=value)
                try:
                    session.add(r)
                    session.flush()

                except sqlalchemy.exc.IntegrityError, e:
                    self.log.error("Unable to store reading, checking if node type exists")
                    self.log.error(e)
                    session.rollback()
                    s = session.query(SensorType).filter_by(id=i).first()
                    if s is None:
                        s = SensorType(id=type_id,name="UNKNOWN")
                        session.add(s)
                        self.log.info("Adding new sensortype")
                        session.flush()
                        session.add(r)
                        session.flush()                            
                    else:
                        self.log.error("Sensor type exists")


            self.log.debug("reading: %s, %s" % (node_state, pack_state))
            session.commit()

            #send acknowledgement to base station to fwd to node
            self.send_ack(seq=seq,
                          dest=node_id)