def __init__(self): self.nodes={} self.path = Path(Setting.path+"./Settings/").absolute() self.path=self.path.joinpath("NodeRegistry.yaml") if self.path.is_file() == False : exit(-1) else: self.nodes=yaml.load(open(str(self.path),'r')) for node in self.nodes['node_templates']: self.nodes['node_templates'][node]['id']=Setting.getHostName() self.nodes['node_templates'][node]['attributes']['public_address']=Setting.getIp() self.nodes['node_templates'][node]['attributes']['broker_address']=Setting.getIp() if node == "node": self.nodes['node_templates'][Setting.getHostName()]=self.nodes['node_templates']['node'] self.nodes['node_templates'].pop('node') self.permanent() RaspberryPi.make_active(RaspberryPi()) print("Node loaded") def on_message_add(client, userdata, message, obj): serial_frame=str(message.payload.decode("utf-8")) yaml_frame=str(serial_frame) opt=subprocess.Popen("/opt/emqttd/bin/emqttd_ctl cluster join emqttd@"+yaml_frame+"." , stdout=subprocess.PIPE, shell=True) opt.wait() obj.publish() def on_message_remove(client, userdata, message, obj): print("Request to leave cluster") obj.client.publish("/"+Setting.getNodeId()+"/model/node/status",'''node_templates: {}\n''',qos=0,retain=True) opt=subprocess.Popen("/opt/emqttd/bin/emqttd_ctl cluster leave", stdout=subprocess.PIPE, shell=True) opt.wait() obj.publish() def on_message_read(client, userdata, message, obj): obj.publish() self.client = mqtt.Client() self.client.will_set("/"+Setting.getNodeId()+"/model/node/status",'''node_templates: {}\n''', 0, True) #self.client.message_callback_add("/"+Setting.getNodeId()+"/model/node/add", partial(on_message_add, obj=self)) self.client.message_callback_add("/"+Setting.getNodeId()+"/model/node/remove", partial(on_message_remove, obj=self)) self.client.message_callback_add("/"+Setting.getNodeId()+"/model/node/read", partial(on_message_read, obj=self)) self.client.connect(Setting.getBrokerIp()) self.client.loop_start() #self.client.subscribe("/"+Setting.getNodeId()+"/model/node/add", qos=0) self.client.subscribe("/"+Setting.getNodeId()+"/model/node/remove", qos=0) self.client.subscribe("/"+Setting.getNodeId()+"/model/node/read", qos=0) self.publish()
def adder(): start_time = time.perf_counter() serial_frame = str(message.payload.decode("utf-8")) yaml_frame = yaml.load(serial_frame) print("Time 1 : ", (time.perf_counter() - start_time)) for dev in yaml_frame['node_templates']: device = Factory.decode( json.dumps(yaml_frame['node_templates'][dev])) print("Time 2: ", (time.perf_counter() - start_time)) if device != None and device.id not in obj.devices[ 'node_templates']: obj.links[dev] = type(device).make_active(device) obj.devices['node_templates'][dev] = yaml_frame[ 'node_templates'][dev] self.client.publish( "/" + Setting.getNodeId() + "/model/device/status/" + dev, yaml.dump(obj.devices['node_templates'][dev]), qos=0, retain=True) print("Time 3: ", (time.perf_counter() - start_time)) #obj.permanent() print("Time 4: ", (time.perf_counter() - start_time)) #obj.publish() print("Time 5: ", (time.perf_counter() - start_time))
def publish(self): for i in self.devices['node_templates']: self.client.publish("/" + Setting.getNodeId() + "/model/device/status/" + i, yaml.dump(self.devices['node_templates'][i]), qos=0, retain=True)
def __init__(self): self.balancers = {} self.path = Path(Setting.path + "./Settings/").absolute() self.path = self.path.joinpath("LoadRegistry.yaml") if self.path.is_file() == False: yaml.dump(self.balancers, open(str(self.path), 'w')) else: self.balancers = yaml.load(open(str(self.path), 'r')) self.update_balancer() print("Balancer loaded") def on_message_add(client, userdata, message, obj): serial_frame = str(message.payload.decode("utf-8")) yaml_frame = yaml.load(serial_frame) for app in yaml_frame['node_templates']: if app not in obj.balancers['node_templates']: obj.balancers['node_templates'][app] = yaml_frame[ 'node_templates'][app] obj.permanent() obj.publish() obj.update_balancer() def on_message_remove(client, userdata, message, obj): serial_frame = str(message.payload.decode("utf-8")) yaml_frame = yaml.load(serial_frame) for app in yaml_frame['node_templates']: if app in obj.balancers['node_templates']: obj.balancers['node_templates'].pop(app) obj.permanent() obj.publish() obj.update_balancer() def on_message_read(client, userdata, message, obj): obj.publish() self.client = mqtt.Client() self.client.will_set( "/" + Setting.getNodeId() + "/model/balancer/status", '''node_templates: {}\n''', 0, True) self.client.message_callback_add( "/" + Setting.getNodeId() + "/model/balancer/add", partial(on_message_add, obj=self)) self.client.message_callback_add( "/" + Setting.getNodeId() + "/model/balancer/remove", partial(on_message_remove, obj=self)) self.client.message_callback_add( "/" + Setting.getNodeId() + "/model/balancer/read", partial(on_message_read, obj=self)) self.client.connect(Setting.getBrokerIp()) self.client.loop_start() self.client.subscribe("/" + Setting.getNodeId() + "/model/balancer/add", qos=0) self.client.subscribe("/" + Setting.getNodeId() + "/model/balancer/remove", qos=0) self.client.subscribe("/" + Setting.getNodeId() + "/model/balancer/read", qos=0) self.publish()
def on_message_add(client, userdata, message, obj): serial_frame = str(message.payload.decode("utf-8")) yaml_frame = yaml.load(serial_frame) for dev in yaml_frame['node_templates']: device = Factory.decode( json.dumps(yaml_frame['node_templates'][dev])) if device != None and device.id not in obj.devices[ 'node_templates']: obj.links[dev] = type(device).make_active(device) obj.devices['node_templates'][dev] = yaml_frame[ 'node_templates'][dev] self.client.publish( "/" + Setting.getNodeId() + "/model/device/status/" + dev, yaml.dump(obj.devices['node_templates'][dev]), qos=0, retain=True)
def publish(self): self.client.publish("/"+Setting.getNodeId()+"/model/node/status",yaml.dump(self.nodes),qos=0,retain=True)
def on_message_remove(client, userdata, message, obj): print("Request to leave cluster") obj.client.publish("/"+Setting.getNodeId()+"/model/node/status",'''node_templates: {}\n''',qos=0,retain=True) opt=subprocess.Popen("/opt/emqttd/bin/emqttd_ctl cluster leave", stdout=subprocess.PIPE, shell=True) opt.wait() obj.publish()
def __init__(self): self.devices = {} self.links = {} self.locker = threading.RLock() self.executor = ThreadPoolExecutor(max_workers=10) self.path = Path(Setting.path + "./Settings/").absolute() self.path = self.path.joinpath("DeviceRegistry.yaml") if self.path.is_file() == False: yaml.dump(self.devices, open(str(self.path), 'w')) else: self.devices = yaml.load(open(str(self.path), 'r')) for dev in self.devices['node_templates']: device = Factory.decode( json.dumps(self.devices['node_templates'][dev])) self.links[dev] = type(device).make_active(device) print("Device loaded") def on_message_add(client, userdata, message, obj): print("--------- New ADD message ---------------------") with self.locker: def adder(): start_time = time.perf_counter() serial_frame = str(message.payload.decode("utf-8")) yaml_frame = yaml.load(serial_frame) print("Time 1 : ", (time.perf_counter() - start_time)) for dev in yaml_frame['node_templates']: device = Factory.decode( json.dumps(yaml_frame['node_templates'][dev])) print("Time 2: ", (time.perf_counter() - start_time)) if device != None and device.id not in obj.devices[ 'node_templates']: obj.links[dev] = type(device).make_active(device) obj.devices['node_templates'][dev] = yaml_frame[ 'node_templates'][dev] self.client.publish( "/" + Setting.getNodeId() + "/model/device/status/" + dev, yaml.dump(obj.devices['node_templates'][dev]), qos=0, retain=True) print("Time 3: ", (time.perf_counter() - start_time)) #obj.permanent() print("Time 4: ", (time.perf_counter() - start_time)) #obj.publish() print("Time 5: ", (time.perf_counter() - start_time)) self.executor.submit(adder) def on_message_remove(client, userdata, message, obj): serial_frame = str(message.payload.decode("utf-8")) yaml_frame = yaml.load(serial_frame) for dev in yaml_frame['node_templates']: if dev in obj.devices['node_templates']: obj.devices['node_templates'].pop(dev) obj.links[dev].terminate() #obj.links[dev].kill() obj.links.pop(dev) obj.permanent() obj.publish() def on_message_read(client, userdata, message, obj): obj.publish() self.client = mqtt.Client() self.client.will_set( "/" + Setting.getNodeId() + "/model/device/status/all", '''node_templates: {}\n''', 0, True) self.client.message_callback_add( "/" + Setting.getNodeId() + "/model/device/add", partial(on_message_add, obj=self)) self.client.message_callback_add( "/" + Setting.getNodeId() + "/model/device/remove", partial(on_message_remove, obj=self)) self.client.message_callback_add( "/" + Setting.getNodeId() + "/model/device/read", partial(on_message_read, obj=self)) self.client.connect(Setting.getBrokerIp()) self.client.loop_start() self.client.subscribe("/" + Setting.getNodeId() + "/model/device/add", qos=0) self.client.subscribe("/" + Setting.getNodeId() + "/model/device/remove", qos=0) self.client.subscribe("/" + Setting.getNodeId() + "/model/device/read", qos=0) self.publish()
''' Created on 15 gen 2017 @author: Conny ''' from Model import Setting import paho.mqtt.client as mqtt import time yaml = """ node_templates: dev1: id: dev1 type: my.Device.TempSensor location: bathroom device_type: TempSensor requirements: host: py_3""" client = mqtt.Client() client.connect(Setting.getBrokerIp()) client.loop_start() client.publish("/" + Setting.getNodeId() + "/model/device/remove", yaml, 0) time.sleep(5)