예제 #1
0
 def encode_state(self):
     return fos_utils.encode_state({
         "threshold": self.sensor.threshold,
         "delay": self.sensor.delay,
         "state": self.sensor.state,
         "um": self.sensor.um,
         "value": self.sensor.value,
         "label": self.label
     })
예제 #2
0
 def payload_handler(self, payload):
     # update state
     content = {}
     with self.lock:
         content = super().payload_handler(payload)
         if self.critic:
             self.devices[content["label"]]["last_update"] = int(time.time())
     # execute actions
     mappings = self.mappings[content["label"]]
     for mapping in mappings:
         self.devices[mapping["label"]]["connector"].send("PUT", fos_utils.encode_state(mapping))
     if self.critic and (content["label"] == "A" or content["label"] == B):
         time_delta = self.devices["A"].get("last_update", 100) - self.devices["A"].get("last_update", 0)
         if -5 < time_delta < 5:
             print("deploy maintenance fog05")
             for node in self.critic_nodes:
                 self.fos_api.fdu.instantiate(self.config["fdu_control"], node, wait=False)
             # reset timestamps so that two more alarms have to arrive to trigger another control
             self.devices["A"]["last_update"] = 100
             self.devices["A"]["last_update"] = 0
     return content
 def encode_state(self):
     return fos_utils.encode_state({
         "execution_time": self.actuator.execution_time,
         "state": self.actuator.state,
         "label": self.label
     })
예제 #4
0
 def send(self, action, payload):
     msg_json = fos_utils.decode_state(payload)
     if "action" not in msg_json:
         msg_json["action"] = action
     self.mqttc.publish(self.topic + "/in", payload=fos_utils.encode_state(msg_json))
     print("Mqtt05Device sent to " + self.label + " at " + self.topic)
예제 #5
0
def main_test():
    # simulated deploy
    os.environ["config"] = "deploy/system_instance.json"
    dev = Coap05Device(prints, "1", "2", "", "B", "name", "localhost", "9500")
    dev.send("PUT", fos_utils.encode_state({"state": "off"}))
    asyncio.get_event_loop().run_forever()
예제 #6
0
    def __init__(self):
        # fog05 env parameters loading
        super().__init__()
        self.nuuid = os.environ["nuuid"] if "nuuid" in os.environ else "no_nuiid"
        self.fuuid = os.environ["fuuid"] if "fuuid" in os.environ else "no_fuuid"
        self.iuuid = os.environ["iuuid"] if "iuuid" in os.environ else "manager05"
        self.label = os.environ["label"] if "label" in os.environ else "manager05"
        # if system instance description is given through Env variable
        if "system" in os.environ:
            self.config = json.loads(os.environ["system"].replace("^", ", ").replace("%", "\"").replace(":", ": "))
        else:
            self.config = json.loads(fos_utils.read_file("/var/fos/demo/deploy/system_instance.json"))
        self.mappings = json.loads(fos_utils.read_file(os.environ["config"]))
        self.lock = threading.Lock()

        self.critic = False
        if "A" in self.mappings.keys() and "B" in self.mappings.keys():
            from fog05 import FIMAPI
            from fog05_sdk.interfaces.FDU import FDU
            self.critic = True
            self.critic_nodes = {self.config["devices"]["A"]["nuuid"], self.config["devices"]["B"]["nuuid"]}
            self.fos_api = FIMAPI(self.config.get("fimapi", "192.168.31.232"))
            print("+++ this is the critic manager +++")
            if "json" in self.config["fdu_control"]:  # if descriptor is a filename, load it in fog05
                print("fog05 onboarding " + self.config["fdu_control"])
                fdu_descriptor = FDU(json.loads(fos_utils.read_file(self.config["fdu_control"])))
                fduD = self.fos_api.fdu.onboard(fdu_descriptor)
                self.config["fdu_control"] = fduD.get_uuid()

        # rebuild with factory
        # iterate on keys (i.e. labels)
        # create a set of all the managed labels
        devices = set(self.mappings.keys())
        for label in self.mappings.keys():
            for mapping in self.mappings[label]:
                devices.add(mapping["label"])
        print(str(devices))
        # for device in self.config["devices"]:
        for device in devices:
            self.devices[device] = {}
            self.devices[device]["connector"] = get_connector(self.config["devices"][device], self.payload_handler)
            # self.devices[device["label"]] = get_connector(device["connector"], self.payload_handler)

        for label in self.devices.keys():
            if "sens" in self.config["devices"][label]["desc"]:
                payload = {"action": "PUT", "state": "on"}
                # print("Manager turning on sensor " + label + " with msg " + str(payload))
                self.devices[label]["connector"].send("PUT", fos_utils.encode_state(payload))
        # for device in self.config["devices"]:
        # (self, nuuid, fuuid, iuuid, broker, payload_handler)
        # self.devices[device["label"]] = Mqtt05_comm(device["nuuid"],
        #                                             device["fuuid"],
        #                                             device["iuuid"],
        #                                             device["broker"],
        #                                             self.payload_handler)
        # # (self, ip, port, label, payload_handler)
        # self.devices[device["label"]] = Coap05_comm(device["ip"],
        #                                             device["port"],
        #                                             device["iuuid"],
        #                                             device["label"],
        #                                             self.payload_handler)
        asyncio.get_event_loop().run_forever()