Ejemplo n.º 1
0
    def __sendDescription(self):
        """
        Sends out an description.

        Returns
        -------
        None.

        """
        res = 2**self.params["resolutionbit"]
        max = ((res - 1) - res / 2) / res
        min = ((res / 2) - res) / res
        Description = {
            0: ["Sin", "Cos", "Sawtooth",
                "Triangle"],  # 0: "PHYSICAL_QUANTITY",
            1: ["A.U.", "A.U.", "A.U.", "A.U."],  # 1: "UNIT",
            3: [res, res, res, res],  # 3: "RESOLUTION",
            4: [np.sin(max), np.cos(max), max,
                abs(max)],  # 4: "MIN_SCALE",
            5: [np.sin(min), np.cos(min), max,
                abs(min)],  #'5: "MAX_SCALE",
        }
        DescriptonType = {
            0: "str",
            1: "str",
            3: "float",
            4: "float",
            5: "float"
        }
        for desckeys in Description.keys():
            proto_description = messages_pb2.DescriptionMessage()
            proto_description.Sensor_name = "Sensor Simulation"
            proto_description.id = self.params["ID"]
            proto_description.Description_Type = desckeys
            if DescriptonType[desckeys] == "str":
                proto_description.str_Data_01 = Description[desckeys][0]
                proto_description.str_Data_02 = Description[desckeys][1]
                proto_description.str_Data_03 = Description[desckeys][2]
                proto_description.str_Data_04 = Description[desckeys][3]
            if DescriptonType[desckeys] == "float":
                proto_description.f_Data_01 = Description[desckeys][0]
                proto_description.f_Data_02 = Description[desckeys][1]
                proto_description.f_Data_03 = Description[desckeys][2]
                proto_description.f_Data_04 = Description[desckeys][3]
            binproto = proto_description.SerializeToString()
            binarymessage = b"DSCP"
            binarymessage = binarymessage + _VarintBytes(
                len(binproto)) + binproto
Ejemplo n.º 2
0
    def __sendDescription(self):
        """
        Sends out an description.

        Returns
        -------
        None.

        """
        # 0: "PHYSICAL_QUANTITY",1: "UNIT",3: "RESOLUTION",4: "MIN_SCALE",5: "MAX_SCALE",
        DescriptonType = {
            0: "str",
            1: "str",
            3: "float",
            4: "float",
            5: "float"
        }
        DescriptonKeys = {
            0: "PHYSICAL_QUANTITY",
            1: "UNIT",
            3: "RESOLUTION",
            4: "MIN_SCALE",
            5: "MAX_SCALE",
        }
        for desckeys in DescriptonKeys:
            proto_description = messages_pb2.DescriptionMessage()
            proto_description.Sensor_name = self.Description.SensorName
            proto_description.id = self.Description.ID
            proto_description.Description_Type = desckeys
            for i in range(16):
                try:
                    descVal = self.Description[i + 1][DescriptonKeys[desckeys]]
                    if DescriptonType[desckeys] == "str":
                        proto_description.__setattr__(self.strFieldNames[i],
                                                      descVal)
                    if DescriptonType[desckeys] == "float":
                        proto_description.__setattr__(self.fFieldNames[i],
                                                      descVal)
                except KeyError:
                    pass
            binproto = proto_description.SerializeToString()
            binarymessage = b"DSCP"
            binarymessage = binarymessage + _VarintBytes(
                len(binproto)) + binproto
            self.socket.sendto(binarymessage,
                               (self.params["TargetIp"], self.params["Port"]))
        return
Ejemplo n.º 3
0
    def run(self):
        """
        Spwans the Datareceiver task.

        Returns
        -------
        None.

        """
        # implement stop routine
        while not self._stop_event.is_set():
            data, addr = self.socket.recvfrom(
                1500)  # buffer size is 1024 bytes
            wasValidData = False
            wasValidDescription = False
            ProtoData = messages_pb2.DataMessage()
            ProtoDescription = messages_pb2.DescriptionMessage()
            SensorID = 0
            BytesProcessed = 4  # we need an offset of 4 sice
            if data[:4] == b"DATA":
                while BytesProcessed < len(data):
                    msg_len, new_pos = _DecodeVarint32(data, BytesProcessed)
                    BytesProcessed = new_pos

                    try:
                        msg_buf = data[new_pos:new_pos + msg_len]
                        ProtoData.ParseFromString(msg_buf)
                        wasValidData = True
                        SensorID = ProtoData.id
                        message = {
                            "ProtMsg": copy.deepcopy(ProtoData),
                            "Type": "Data"
                        }
                        BytesProcessed += msg_len
                    except:
                        pass  # ? no exception for wrong data type !!
                    if not (wasValidData or wasValidDescription):
                        print("INVALID PROTODATA")
                        pass  # invalid data leave parsing routine

                    if SensorID in self.AllSensors:
                        try:
                            self.AllSensors[SensorID].buffer.put_nowait(
                                message)
                        except:
                            tmp = self.packestlosforsensor[SensorID] = (
                                self.packestlosforsensor[SensorID] + 1)
                            if tmp == 1:
                                print("!!!! FATAL PERFORMANCE PROBLEMS !!!!")
                                print("FIRSTTIME packet lost for sensor ID:" +
                                      str(SensorID))
                                print(
                                    "DROP MESSAGES ARE ONLY PRINTETD EVERY 1000 DROPS FROM NOW ON !!!!!!!! "
                                )
                            if tmp % 1000 == 0:
                                print(
                                    "oh no lost an other  thousand packets :(")
                    else:
                        self.AllSensors[SensorID] = Sensor(SensorID)
                        print("FOUND NEW SENSOR WITH ID=hex" + hex(SensorID) +
                              "==>dec:" + str(SensorID))
                        self.packestlosforsensor[
                            SensorID] = 0  # initing lost packet counter
                    self.msgcount = self.msgcount + 1

                    if self.msgcount % self.params[
                            "PacketrateUpdateCount"] == 0:
                        print("received " +
                              str(self.params["PacketrateUpdateCount"]) +
                              " packets")
                        if self.lastTimestamp != 0:
                            timeDIFF = time.monotonic() - self.lastTimestamp
                            self.Datarate = (
                                self.params["PacketrateUpdateCount"] /
                                timeDIFF)
                            print("Update rate is " + str(self.Datarate) +
                                  " Hz")
                            self.lastTimestamp = time.monotonic()
                        else:
                            self.lastTimestamp = time.monotonic()
            elif data[:4] == b"DSCP":
                while BytesProcessed < len(data):
                    msg_len, new_pos = _DecodeVarint32(data, BytesProcessed)
                    BytesProcessed = new_pos
                    try:
                        msg_buf = data[new_pos:new_pos + msg_len]
                        ProtoDescription.ParseFromString(msg_buf)
                        # print(msg_buf)
                        wasValidData = True
                        SensorID = ProtoDescription.id
                        message = {
                            "ProtMsg": ProtoDescription,
                            "Type": "Description"
                        }
                        BytesProcessed += msg_len
                    except:
                        pass  # ? no exception for wrong data type !!
                    if not (wasValidData or wasValidDescription):
                        print("INVALID PROTODATA")
                        pass  # invalid data leave parsing routine

                    if SensorID in self.AllSensors:
                        try:
                            self.AllSensors[SensorID].buffer.put_nowait(
                                message)
                        except:
                            print("packet lost for sensor ID:" + hex(SensorID))
                    else:
                        self.AllSensors[SensorID] = Sensor(SensorID)
                        print("FOUND NEW SENSOR WITH ID=hex" + hex(SensorID) +
                              " dec==>:" + str(SensorID))
                    self.msgcount = self.msgcount + 1

                    if self.msgcount % self.params[
                            "PacketrateUpdateCount"] == 0:
                        print("received " +
                              str(self.params["PacketrateUpdateCount"]) +
                              " packets")
                        if self.lastTimestamp != 0:
                            timeDIFF = time.monotonic() - self.lastTimestamp
                            self.Datarate = (
                                self.params["PacketrateUpdateCount"] /
                                timeDIFF)
                            print("Update rate is " + str(self.Datarate) +
                                  " Hz")
                            self.lastTimestamp = time.monotonic()
                        else:
                            self.lastTimestamp = time.monotonic()
            else:
                print("unrecognized packed preamble" + str(data[:5]))
Ejemplo n.º 4
0
    def run(self):
        """
        

        Returns
        -------
        None.

        """
        # implement stop routine
        while not self._stop_event.is_set():
            data, addr = self.socket.recvfrom(1500)  # buffer size is 1024 bytes
            wasValidData = False
            wasValidDescription = False
            ProtoData = messages_pb2.DataMessage()
            ProtoDescription = messages_pb2.DescriptionMessage()
            SensorID = 0
            BytesProcessed = 4  # we need an offset of 4 sice
            if data[:4] == b"DATA":
                while BytesProcessed < len(data):
                    msg_len, new_pos = _DecodeVarint32(data, BytesProcessed)
                    BytesProcessed = new_pos

                    try:
                        msg_buf = data[new_pos : new_pos + msg_len]
                        ProtoData.ParseFromString(msg_buf)
                        wasValidData = True
                        SensorID = ProtoData.id
                        message = {"ProtMsg": copy.deepcopy(ProtoData), "Type": "Data"}
                        BytesProcessed += msg_len
                    except:
                        pass  # ? no exception for wrong data type !!
                    if not (wasValidData or wasValidDescription):
                        print("INVALID PROTODATA")
                        pass  # invalid data leave parsing routine

                    if SensorID in self.AllSensors:
                        try:
                            self.AllSensors[SensorID].buffer.put_nowait(message)
                        except:
                            print("packet lost for sensor ID:" + str(SensorID))
                    else:
                        self.AllSensors[SensorID] = Sensor(SensorID)
                        print(
                            "FOUND NEW SENSOR WITH ID=hex"
                            + hex(SensorID)
                            + "==>dec:"
                            + str(SensorID)
                        )
                    self.msgcount = self.msgcount + 1

                    if self.msgcount % self.params["PacketrateUpdateCount"] == 0:
                        print(
                            "received "
                            + str(self.params["PacketrateUpdateCount"])
                            + " packets"
                        )
                        if self.lastTimestamp != 0:
                            timeDIFF = datetime.now() - self.lastTimestamp
                            timeDIFF = timeDIFF.seconds + timeDIFF.microseconds * 1e-6
                            self.Datarate = (
                                self.params["PacketrateUpdateCount"] / timeDIFF
                            )
                            print("Update rate is " + str(self.Datarate) + " Hz")
                            self.lastTimestamp = datetime.now()
                        else:
                            self.lastTimestamp = datetime.now()
            elif data[:4] == b"DSCP":
                while BytesProcessed < len(data):
                    msg_len, new_pos = _DecodeVarint32(data, BytesProcessed)
                    BytesProcessed = new_pos
                    try:
                        msg_buf = data[new_pos : new_pos + msg_len]
                        ProtoDescription.ParseFromString(msg_buf)
                        # print(msg_buf)
                        wasValidData = True
                        SensorID = ProtoDescription.id
                        message = {"ProtMsg": ProtoDescription, "Type": "Description"}
                        BytesProcessed += msg_len
                    except:
                        pass  # ? no exception for wrong data type !!
                    if not (wasValidData or wasValidDescription):
                        print("INVALID PROTODATA")
                        pass  # invalid data leave parsing routine

                    if SensorID in self.AllSensors:
                        try:
                            self.AllSensors[SensorID].buffer.put_nowait(message)
                        except:
                            print("packet lost for sensor ID:" + hex(SensorID))
                    else:
                        self.AllSensors[SensorID] = Sensor(SensorID)
                        print(
                            "FOUND NEW SENSOR WITH ID=hex"
                            + hex(SensorID)
                            + " dec==>:"
                            + str(SensorID)
                        )
                    self.msgcount = self.msgcount + 1

                    if self.msgcount % self.params["PacketrateUpdateCount"] == 0:
                        print(
                            "received "
                            + str(self.params["PacketrateUpdateCount"])
                            + " packets"
                        )
                        if self.lastTimestamp != 0:
                            timeDIFF = datetime.now() - self.lastTimestamp
                            timeDIFF = timeDIFF.seconds + timeDIFF.microseconds * 1e-6
                            self.Datarate = (
                                self.params["PacketrateUpdateCount"] / timeDIFF
                            )
                            print("Update rate is " + str(self.Datarate) + " Hz")
                            self.lastTimestamp = datetime.now()
                        else:
                            self.lastTimestamp = datetime.now()
            else:
                print("unrecognized packed preamble" + str(data[:5]))
Ejemplo n.º 5
0
    def agent_loop(self):
        data, addr = self.socket.recvfrom(1500)  # buffer size is 1024 bytes
        wasValidData = False
        wasValidDescription = False

        ProtoData = messages_pb2.DataMessage()
        ProtoDescription = messages_pb2.DescriptionMessage()
        SensorID = 0
        BytesProcessed = 4  # we need an offset of 4 sice
        if data[:4] == b"DATA":
            while BytesProcessed < len(data):
                msg_len, new_pos = _DecodeVarint32(data, BytesProcessed)
                BytesProcessed = new_pos

                try:
                    msg_buf = data[new_pos:new_pos + msg_len]
                    ProtoData.ParseFromString(msg_buf)
                    wasValidData = True
                    SensorID = ProtoData.id
                    message = {"ProtMsg": msg_buf, "Type": "Data"}
                    BytesProcessed += msg_len
                except:
                    pass  # ? no exception for wrong data type !!
                if not (wasValidData or wasValidDescription):
                    print("INVALID PROTODATA")
                    pass  # invalid data leave parsing routine

                if SensorID in self.AllSensors:
                    try:
                        self.AllSensors[SensorID].send_output(message)
                    except:
                        tmp = self.packestlosforsensor[SensorID] = (
                            self.packestlosforsensor[SensorID] + 1)
                        if tmp == 1:
                            print("!!!! FATAL PERFORMANCE PROBLEMS !!!!")
                            print("FIRSTTIME packet lost for sensor ID:" +
                                  str(SensorID))
                            print(
                                "DROP MESSAGES ARE ONLY PRINTETD EVERY 1000 DROPS FROM NOW ON !!!!!!!! "
                            )
                        if tmp % 1000 == 0:
                            print("oh no lost an other  thousand packets :(")
                else:
                    self.AllSensors[SensorID] = self.agentNetwork.add_agent(
                        agentType=SensorAgent, log_mode=False, ID=SensorID)
                    self.agentNetwork.add_coalition(
                        "Sensor_Group_1",
                        [self] + list(self.AllSensors.values()))
                    print("FOUND NEW SENSOR WITH ID=hex" + hex(SensorID) +
                          "==>dec:" + str(SensorID))
                    self.packestlosforsensor[
                        SensorID] = 0  # initing lost packet counter
                self.msgcount = self.msgcount + 1

                if self.msgcount % self.params["PacketrateUpdateCount"] == 0:
                    print("received " +
                          str(self.params["PacketrateUpdateCount"]) +
                          " packets")
                    if self.lastTimestamp != 0:
                        timeDIFF = datetime.now() - self.lastTimestamp
                        timeDIFF = timeDIFF.seconds + timeDIFF.microseconds * 1e-6
                        self.Datarate = (self.params["PacketrateUpdateCount"] /
                                         timeDIFF)
                        print("Update rate is " + str(self.Datarate) + " Hz")
                        self.lastTimestamp = datetime.now()
                    else:
                        self.lastTimestamp = datetime.now()
        elif data[:4] == b"DSCP":
            while BytesProcessed < len(data):
                msg_len, new_pos = _DecodeVarint32(data, BytesProcessed)
                BytesProcessed = new_pos
                try:
                    msg_buf = data[new_pos:new_pos + msg_len]
                    ProtoDescription.ParseFromString(msg_buf)
                    # print(msg_buf)
                    wasValidData = True
                    SensorID = ProtoDescription.id
                    message = {"ProtMsg": msg_buf, "Type": "Description"}
                    BytesProcessed += msg_len
                except:
                    pass  # ? no exception for wrong data type !!
                if not (wasValidData or wasValidDescription):
                    print("INVALID PROTODATA")
                    pass  # invalid data leave parsing routine

                if SensorID in self.AllSensors:
                    try:
                        self.AllSensors[SensorID].buffer.put_nowait(message)
                    except:
                        print("packet lost for sensor ID:" + hex(SensorID))
                else:
                    self.AllSensors[SensorID] = self.agentNetwork.add_agent(
                        agentType=SensorAgent, log_mode=False, ID=SensorID)
                    self.agentNetwork.add_coalition(
                        "Sensor_Group_1",
                        [self] + list(self.AllSensors.values()))
                    print("FOUND NEW SENSOR WITH ID=hex" + hex(SensorID) +
                          " dec==>:" + str(SensorID))
                self.msgcount = self.msgcount + 1

                if self.msgcount % self.params["PacketrateUpdateCount"] == 0:
                    print("received " +
                          str(self.params["PacketrateUpdateCount"]) +
                          " packets")
                    if self.lastTimestamp != 0:
                        timeDIFF = datetime.now() - self.lastTimestamp
                        timeDIFF = timeDIFF.seconds + timeDIFF.microseconds * 1e-6
                        self.Datarate = (self.params["PacketrateUpdateCount"] /
                                         timeDIFF)
                        print("Update rate is " + str(self.Datarate) + " Hz")
                        self.lastTimestamp = datetime.now()
                    else:
                        self.lastTimestamp = datetime.now()
        else:
            print("unrecognized packed preamble" + str(data[:5]))
Ejemplo n.º 6
0
    def on_received_message(self, message):
        self.timeoutOccured = False
        self.ProcessedPacekts = self.ProcessedPacekts + 1
        if self.flags["PrintProcessedCounts"]:
            if self.ProcessedPacekts % 10000 == 0:
                print("processed 10000 packets in receiver for Sensor ID:" +
                      hex(self.params["ID"]) + " Packets in Que " +
                      str(self.buffer.qsize()) + " -->" +
                      str((self.buffer.qsize() / self.buffersize) * 100) + "%")
        if message["Type"] == "Description":
            ProtoDescription = messages_pb2.DescriptionMessage()
            Description = ProtoDescription.ParseFromString(message["ProtMsg"])
            try:
                if (not any(self.DescriptionsProcessed.values())
                        and Description.IsInitialized()):
                    # run only if no description packed has been procesed ever
                    # self.Description.SensorName=message.Sensor_name
                    print("Found new description " + Description.Sensor_name +
                          " sensor with ID:" + str(self.params["ID"]))
                    # print(str(Description.Description_Type))
                if (self.DescriptionsProcessed[Description.Description_Type] ==
                        False):

                    if self.Description.SensorName == "Name not Set":
                        self.Description.SensorName = Description.Sensor_name
                    # we havent processed thiss message before now do that
                    if Description.Description_Type in [
                            0,
                            1,
                            2,
                    ]:  # ["PHYSICAL_QUANTITY","UNIT","UNCERTAINTY_TYPE"]
                        # print(Description)
                        # string Processing

                        FieldNumber = 1
                        for StrField in self.StrFieldNames:
                            if Description.HasField(StrField):
                                self.Description.setChannelParam(
                                    FieldNumber,
                                    self.DescriptionTypNames[
                                        Description.Description_Type],
                                    Description.__getattribute__(StrField),
                                )
                                # print(str(FieldNumber)+' '+Description.__getattribute__(StrField))
                            FieldNumber = FieldNumber + 1

                        self.DescriptionsProcessed[
                            Description.Description_Type] = True
                        # print(self.DescriptionsProcessed)
                    if Description.Description_Type in [
                            3,
                            4,
                            5,
                    ]:  # ["RESOLUTION","MIN_SCALE","MAX_SCALE"]
                        self.DescriptionsProcessed[
                            Description.Description_Type] = True
                        FieldNumber = 1
                        for FloatField in self.FFieldNames:
                            if Description.HasField(FloatField):
                                self.Description.setChannelParam(
                                    FieldNumber,
                                    self.DescriptionTypNames[
                                        Description.Description_Type],
                                    Description.__getattribute__(FloatField),
                                )
                                # print(str(FieldNumber)+' '+str(Description.__getattribute__(FloatField)))
                            FieldNumber = FieldNumber + 1
                        # print(self.DescriptionsProcessed)
                        # string Processing
            except Exception:
                print(" Sensor id:" + hex(self.params["ID"]) +
                      "Exception in user Description parsing:")
                print("-" * 60)
                traceback.print_exc(file=sys.stdout)
                print("-" * 60)
        if message["Type"] == "Data":
            ProtoData = messages_pb2.DataMessage()
            Data = ProtoData.ParseFromString(message["ProtMsg"])
        if self.flags["callbackSet"]:
            if message["Type"] == "Data":
                try:
                    self.callback(Data, self.Description)
                except Exception:
                    print(" Sensor id:" + hex(self.params["ID"]) +
                          "Exception in user callback:")
                    print("-" * 60)
                    traceback.print_exc(file=sys.stdout)
                    print("-" * 60)
                    pass

        if self.flags["DumpToFileProto"]:
            if message["Type"] == "Data":
                try:
                    self.__dumpMsgToFileProto(Data)
                except Exception:
                    print(" Sensor id:" + hex(self.params["ID"]) +
                          "Exception in user datadump:")
                    print("-" * 60)
                    traceback.print_exc(file=sys.stdout)
                    print("-" * 60)
                    pass
        if self.flags["DumpToFileASCII"]:
            if message["Type"] == "Data":
                try:
                    self.__dumpMsgToFileASCII(Data)
                except Exception:
                    print(" Sensor id:" + hex(self.params["ID"]) +
                          "Exception in user datadump:")
                    print("-" * 60)
                    traceback.print_exc(file=sys.stdout)
                    print("-" * 60)
                    pass