Ejemplo n.º 1
0
class Service(Daemon):
    def __init__(self):
        self.cls = super().__init__()
        self.breathe = Breathe()

    def run(self):
        self.breathe.start()

    def shutdown(self):
        self.breathe.stop()
        self.cls.stop()
Ejemplo n.º 2
0
    def loop(self, interval: int = 60):
        from breathe import Breathe
        sleep_indicator = Breathe()
        while True:
            pycom.rgbled(0x001100)
            data = self.prepare_data()

            print(json.dumps(data))
            # send data to Cumulocity
            try:
                print("** sending measurements ...")
                self.c8y.measurement(data)
            except Exception as e:
                pycom.rgbled(0x110000)
                print("!! error sending data to backend: " + repr(e))
                time.sleep(2)
            else:
                pycom.rgbled(0x001100)

            # send data certificate (UPP) to UBIRCH
            try:
                print("** sending measurement certificate ...")
                (response, r) = self.ubirch.send(data)
            except Exception as e:
                pycom.rgbled(0x440000)
                print("!! response: verification failed: {}".format(e))
                time.sleep(2)
            else:
                if r.status_code != 202:
                    pycom.rgbled(0x550000)
                    print("!! request failed with {}: {}".format(
                        r.status_code, r.content.decode()))
                    time.sleep(2)
                else:
                    print(response)
                    if isinstance(response, dict):
                        interval = response.get("i", interval)

            # everything okay
            pycom.rgbled(0x004400)
            print("** done")

            sleep_indicator.start()
            time.sleep(interval)
            sleep_indicator.stop()