Exemple #1
0
    def _send_to_storage(self, connector_name, data):
        if not connector_name == self.name:
            if not TBUtility.validate_converted_data(data):
                log.error("Data from %s connector is invalid.", connector_name)
                return
            if data["deviceName"] not in self.get_devices():
                self.add_device(
                    data["deviceName"],
                    {"connector": self.available_connectors[connector_name]},
                    wait_for_publish=True)
            if not self.__connector_incoming_messages.get(connector_name):
                self.__connector_incoming_messages[connector_name] = 0
            else:
                self.__connector_incoming_messages[connector_name] += 1

        telemetry = {}
        for item in data["telemetry"]:
            telemetry = {**telemetry, **item}
        data["telemetry"] = {
            "ts": int(time.time() * 1000),
            "values": telemetry
        }

        json_data = dumps(data)
        save_result = self._event_storage.put(json_data)
        if not save_result:
            log.error(
                'Data from device "%s" cannot be saved, connector name is %s.',
                data["deviceName"], connector_name)
Exemple #2
0
    def send_to_storage(self, connector_name, data):
        if not connector_name == self.name:
            if not TBUtility.validate_converted_data(data):
                log.error("Data from %s connector is invalid.", connector_name)
                return None
            if data["deviceName"] not in self.get_devices():
                self.add_device(data["deviceName"],
                                {"connector": self.available_connectors[connector_name]}, wait_for_publish=True, device_type=data["deviceType"])
            if not self.__connector_incoming_messages.get(connector_name):
                self.__connector_incoming_messages[connector_name] = 0
            else:
                self.__connector_incoming_messages[connector_name] += 1

        telemetry = {}
        telemetry_with_ts = []
        for item in data["telemetry"]:
            if item.get("ts") is None:
                telemetry = {**telemetry, **item}
            else:
                telemetry_with_ts.append({"ts": item["ts"], "values": {**item["values"]}})
        if telemetry_with_ts:
            data["telemetry"] = telemetry_with_ts
        else:
            data["telemetry"] = {"ts": int(time() * 1000), "values": telemetry}

        json_data = dumps(data)
        save_result = self._event_storage.put(json_data)
        if not save_result:
            log.error('Data from the device "%s" cannot be saved, connector name is %s.',
                      data["deviceName"],
                      connector_name)
    def __send_to_storage(self):
        while True:
            try:
                if not self.__converted_data_queue.empty():
                    connector_name, data = self.__converted_data_queue.get(
                        False)
                    if not connector_name == self.name:
                        if not TBUtility.validate_converted_data(data):
                            log.error("Data from %s connector is invalid.",
                                      connector_name)
                            return None
                        if data["deviceName"] not in self.get_devices(
                        ) and self.tb_client.is_connected():
                            self.add_device(data["deviceName"], {
                                "connector":
                                self.available_connectors[connector_name]
                            },
                                            device_type=data["deviceType"])
                        if not self.__connector_incoming_messages.get(
                                connector_name):
                            self.__connector_incoming_messages[
                                connector_name] = 0
                        else:
                            self.__connector_incoming_messages[
                                connector_name] += 1
                    else:
                        data["deviceName"] = "currentThingsBoardGateway"

                    telemetry = {}
                    telemetry_with_ts = []
                    for item in data["telemetry"]:
                        if item.get("ts") is None:
                            telemetry = {**telemetry, **item}
                        else:
                            telemetry_with_ts.append({
                                "ts": item["ts"],
                                "values": {
                                    **item["values"]
                                }
                            })
                    if telemetry_with_ts:
                        data["telemetry"] = telemetry_with_ts
                    else:
                        data["telemetry"] = {
                            "ts": int(time() * 1000),
                            "values": telemetry
                        }

                    json_data = dumps(data)
                    save_result = self._event_storage.put(json_data)
                    if not save_result:
                        log.error(
                            'Data from the device "%s" cannot be saved, connector name is %s.',
                            data["deviceName"], connector_name)
            except Exception as e:
                log.error(e)
Exemple #4
0
    def send_to_storage(self, connector_name, data):
        #print("send_to_storage received data now.................................")
        #print("data in send_to_storage is like this...........................", data)
        if not connector_name == self.name:
            if not TBUtility.validate_converted_data(data):
                log.error("Data from %s connector is invalid.", connector_name)
                return None
            if data["deviceName"] not in self.get_devices():
                self.add_device(
                    data["deviceName"],
                    {"connector": self.available_connectors[connector_name]},
                    wait_for_publish=True,
                    device_type=data["deviceType"])
            if not self.__connector_incoming_messages.get(connector_name):
                self.__connector_incoming_messages[connector_name] = 0
            else:
                self.__connector_incoming_messages[connector_name] += 1
        else:
            data["deviceName"] = "currentThingsBoardGateway"

        telemetry = {}
        telemetry_with_ts = []
        for item in data["telemetry"]:
            if item.get("ts") is None:
                telemetry = {**telemetry, **item}
            else:
                telemetry_with_ts.append({
                    "ts": item["ts"],
                    "values": {
                        **item["values"]
                    }
                })
        if telemetry_with_ts:
            data["telemetry"] = telemetry_with_ts
        else:
            data["telemetry"] = {"ts": int(time() * 1000), "values": telemetry}
        print("¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤")
        print(data)

        indexOfDevice = self.__dataCleaning.doesDeviceExist(
            self.__list_of_queues, data)

        ######### CLEANING SECTION BEGIN  #############

        if indexOfDevice == -1:  # -1 represents not a device yet
            self.__list_of_queues.append(
                self.__dataCleaning.createDevice(self.__list_of_queues, data))
        else:
            self.__dataCleaning.addTelemetry(self.__list_of_queues, data,
                                             indexOfDevice)

        ######### CLEANING SECTIO END #############

        i = 0
        testingString = '{"'
        for test in data["telemetry"]["values"]:
            testingString += test
            testingString += '":'
            testingString += str(self.__list_of_queues[indexOfDevice][i][
                self._index_of_telemetry_data][-1])
            if i < (len(data["telemetry"]["values"]) - 1):
                testingString += ', "'
            else:
                testingString += '}'
            i += 1

        newDictionary = eval(testingString)

        data["telemetry"]["values"].clear()
        data["telemetry"]["values"].update(newDictionary)

        json_data = dumps(data)

        save_result = self._event_storage.put(json_data)
        if not save_result:
            log.error(
                'Data from the device "%s" cannot be saved, connector name is %s.',
                data["deviceName"], connector_name)