Exemplo n.º 1
0
                    sys.stderr.write("unsupported LED color: " + color)

                resp['color'] = color
            else:
                sys.stderr.write("unknown daqc(2) cmd: " + cmd)
            print(json.dumps(resp))
        elif (plate_type == "MOTOR"):
            break
        elif (plate_type == "THERMO"):
            if (cmd == "getTEMP"):
                channel = args['channel']
                value = TP.getTEMP(addr, channel)
                resp['channel'] = channel
                resp['value'] = value
            elif (cmd == "getCOLD"):
                value = TP.getCOLD(addr)
                resp['value'] = value
            elif (cmd == "setLED"):
                TP.setLED(addr)
                resp['LED'] = 1
            elif (cmd == "clrLED"):
                TP.clrLED(addr)
                resp['LED'] = 0
            elif (cmd == "toggleLED"):
                TP.toggleLED(addr)
                resp['LED'] = TP.getLED(addr)
            else:
                sys.stderr.write("unknown or unimplemented thermo cmd: " + cmd)
            print(json.dumps(resp))
        else:
            sys.stderr.write("unknown plate_type: " + plate_type)
Exemplo n.º 2
0
    async def run(self):

        msgCnt = 0

        # Set device client from Azure IoT SDK and connect
        device_client = None

        try:

            while True:

                GPIO.output(self.Wait, GPIO.HIGH)
                await asyncio.sleep(self.config["TelemetryFrequencyInSeconds"])

                msgCnt = msgCnt + 1

                GPIO.output(self.Wait, GPIO.LOW)
                GPIO.output(self.Good, GPIO.HIGH)

                print("[%s]: Reading Thermocoupler Values" %
                      self.config["NameSpace"])
                print("[%s]: Plate Cold Value" %
                      ThermoPlate.getCOLD(self.ThermoPlate_addr))

                # READ FIREBOX
                print("[%s]: Reading the FIRE BOX TEMPERATURE" %
                      self.config["NameSpace"])
                DAQCPlate.setDOUTbit(self.DAQCPlate_addr, self.FireBoxStatus)
                self.TemperatureFireBox = ThermoPlate.getTEMP(
                    self.ThermoPlate_addr, self.FireBox)
                await asyncio.sleep(
                    self.config["ReadTimeBetweenTemperatureInSeconds"])
                DAQCPlate.clrDOUTbit(self.DAQCPlate_addr, self.FireBoxStatus)

                # READ WARMING BOX
                print("[%s]: Reading the WARMING BOX TEMPERATURE" %
                      self.config["NameSpace"])
                DAQCPlate.setDOUTbit(self.DAQCPlate_addr,
                                     self.WarmingBoxStatus)
                self.TemperatureWarmingBox = ThermoPlate.getTEMP(
                    self.ThermoPlate_addr, self.WarmingBox)
                await asyncio.sleep(
                    self.config["ReadTimeBetweenTemperatureInSeconds"])
                DAQCPlate.clrDOUTbit(self.DAQCPlate_addr,
                                     self.WarmingBoxStatus)

                # READ LEFT BACK CHAMBER
                print("[%s]: Reading the LEFT BACK CHAMBER TEMPERATURE" %
                      self.config["NameSpace"])
                DAQCPlate.setDOUTbit(self.DAQCPlate_addr, self.LeftBackStatus)
                self.TemperatureLeftBack = ThermoPlate.getTEMP(
                    self.ThermoPlate_addr, self.LeftBack)
                await asyncio.sleep(
                    self.config["ReadTimeBetweenTemperatureInSeconds"])
                DAQCPlate.clrDOUTbit(self.DAQCPlate_addr, self.LeftBackStatus)

                # READ RIGHT BACK CHAMBER
                print("[%s]: Reading the RIGHT BACK CHAMBER TEMPERATURE" %
                      self.config["NameSpace"])
                DAQCPlate.setDOUTbit(self.DAQCPlate_addr, self.RightBackStatus)
                self.TemperatureRightBack = ThermoPlate.getTEMP(
                    self.ThermoPlate_addr, self.RightBack)
                await asyncio.sleep(
                    self.config["ReadTimeBetweenTemperatureInSeconds"])
                DAQCPlate.clrDOUTbit(self.DAQCPlate_addr, self.RightBackStatus)

                # READ LEFT FRONT CHAMBER
                print("[%s]: Reading the LEFT FRONT CHAMBER TEMPERATURE" %
                      self.config["NameSpace"])
                DAQCPlate.setDOUTbit(self.DAQCPlate_addr, self.LeftFrontStatus)
                self.TemperatureLeftFront = ThermoPlate.getTEMP(
                    self.ThermoPlate_addr, self.LeftFront)
                await asyncio.sleep(
                    self.config["ReadTimeBetweenTemperatureInSeconds"])
                DAQCPlate.clrDOUTbit(self.DAQCPlate_addr, self.LeftFrontStatus)

                # READ RIGHT FRONT CHAMBER
                print("[%s]: Reading the RIGHT FRONT CHAMBER TEMPERATURE" %
                      self.config["NameSpace"])
                DAQCPlate.setDOUTbit(self.DAQCPlate_addr,
                                     self.RightFrontStatus)
                self.TemperatureRightFront = ThermoPlate.getTEMP(
                    self.ThermoPlate_addr, self.RightFront)
                await asyncio.sleep(
                    self.config["ReadTimeBetweenTemperatureInSeconds"])
                DAQCPlate.clrDOUTbit(self.DAQCPlate_addr,
                                     self.RightFrontStatus)

                self.TemperatureAmbient = ThermoPlate.getTEMP(
                    self.ThermoPlate_addr, self.Ambient, "f")

                table = Texttable()
                table.set_deco(Texttable.HEADER)
                table.set_cols_dtype(["t", "f", "f", "f"])
                table.set_cols_align(["l", "r", "r", "r"])
                table.add_rows([
                    [
                        "Sensor Name", "Temperature", "Last Temperature",
                        "Smoothing"
                    ],
                    [
                        self.AmbientDisplayText, self.TemperatureAmbient,
                        self.LastTemperatureAmbient,
                        self.TemperatureAmbient / self.smoothing_value
                    ],
                    [
                        self.FireBoxDisplayText, self.TemperatureFireBox,
                        self.LastTemperatureFireBox,
                        self.TemperatureFireBox / self.smoothing_value
                    ],
                    [
                        self.WarmingBoxDisplayText, self.TemperatureWarmingBox,
                        self.LastTemperatureWarmingBox,
                        self.TemperatureWarmingBox / self.smoothing_value
                    ],
                    [
                        self.LeftBackDisplayText, self.TemperatureLeftBack,
                        self.LastTemperatureLeftBack,
                        self.TemperatureLeftBack / self.smoothing_value
                    ],
                    [
                        self.RightBackDisplayText, self.TemperatureRightBack,
                        self.LastTemperatureRightBack,
                        self.TemperatureRightBack / self.smoothing_value
                    ],
                    [
                        self.LeftFrontDisplayText, self.TemperatureLeftFront,
                        self.LastTemperatureLeftFront,
                        self.TemperatureLeftFront / self.smoothing_value
                    ],
                    [
                        self.RightFrontDisplayText, self.TemperatureRightFront,
                        self.LastTemperatureRightFront,
                        self.TemperatureRightFront / self.smoothing_value
                    ]
                ])

                print(table.draw())
                print("***")

                # Smooth Values
                self.TemperatureAmbient = self.TemperatureAmbient / self.smoothing_value
                self.TemperatureFireBox = self.TemperatureFireBox / self.smoothing_value
                self.TemperatureWarmingBox = self.TemperatureWarmingBox / self.smoothing_value
                self.TemperatureLeftBack = self.TemperatureLeftBack / self.smoothing_value
                self.TemperatureRightBack = self.TemperatureRightBack / self.smoothing_value
                self.TemperatureLeftFront = self.TemperatureLeftFront / self.smoothing_value
                self.TemperatureRightFront = self.TemperatureLeftFront / self.smoothing_value

                # Capture Last Values
                self.LastTemperatureAmbient = self.TemperatureAmbient
                self.LastTemperatureFireBox = self.TemperatureFireBox
                self.LastTemperatureWarmingBox = self.TemperatureWarmingBox
                self.LastTemperatureLeftBack = self.TemperatureLeftBack
                self.LastTemperatureRightBack = self.TemperatureRightBack
                self.LastTemperatureLeftFront = self.TemperatureLeftFront
                self.LastTemperatureRightFront = self.TemperatureLeftFront

                # Send Data to IoT Central
                for device in self.map_telemetry["Devices"]:

                    if device_client == None:
                        self.logger.info(
                            "[BBQ MONITOR] CONNECTING IOT CENTRAL: %s" %
                            device_client)
                        device_client = DeviceClient(self.logger,
                                                     device["Name"])
                        print(device_client)
                        await device_client.connect()

                    for interface in device["Interfaces"]:

                        self.logger.info("[BBQ MONITOR] InterfacelId: %s" %
                                         interface["InterfacelId"])
                        self.logger.info(
                            "[BBQ MONITOR] InterfaceInstanceName: %s" %
                            interface["InterfaceInstanceName"])

                        self.telemetry_dict = {}

                        for capability in interface["Capabilities"]:

                            # Assign variable name and value to dictionary
                            if capability[
                                    "Name"] == self.TemperatureAmbientMapName:
                                self.telemetry_dict[capability[
                                    "Name"]] = self.TemperatureAmbient
                            elif capability[
                                    "Name"] == self.TemperatureFireBoxMapName:
                                self.telemetry_dict[capability[
                                    "Name"]] = self.TemperatureFireBox
                            elif capability[
                                    "Name"] == self.TemperatureWarmingBoxMapName:
                                self.telemetry_dict[capability[
                                    "Name"]] = self.TemperatureWarmingBox
                            elif capability[
                                    "Name"] == self.TemperatureLeftBackMapName:
                                self.telemetry_dict[capability[
                                    "Name"]] = self.TemperatureLeftBack
                            elif capability[
                                    "Name"] == self.TemperatureRightBackMapName:
                                self.telemetry_dict[capability[
                                    "Name"]] = self.TemperatureRightBack
                            elif capability[
                                    "Name"] == self.TemperatureLeftFrontMapName:
                                self.telemetry_dict[capability[
                                    "Name"]] = self.TemperatureLeftFront
                            elif capability[
                                    "Name"] == self.TemperatureRightFrontMapName:
                                self.telemetry_dict[capability[
                                    "Name"]] = self.TemperatureRightFront
                            else:
                                self.telemetry_dict[capability["Name"]] = 0

                            self.logger.info("[BBQ MONITOR] DICTIONARY: %s" %
                                             self.telemetry_dict)

                        self.logger.info(
                            "[BBQ MONITOR] SENDING PAYLOAD IOT CENTRAL")
                        await device_client.send_telemetry(
                            self.telemetry_dict, interface["InterfacelId"],
                            interface["InterfaceInstanceName"])
                        self.logger.info("[BBQ MONITOR] SUCCESS")

                GPIO.output(self.Good, GPIO.LOW)

            return

        except Exception as ex:
            self.logger.error("[ERROR] %s" % ex)
            self.logger.error(
                "[TERMINATING] We encountered an error in BBQ Monitor Run::run()"
            )

        finally:
            await device_client.disconnect()