Exemple #1
0
    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()
Exemple #2
0
 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()
Exemple #3
0
 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)
Exemple #4
0
                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))
Exemple #5
0
    def __init__(self,
                 dev,
                 runnable=None,
                 handlers=[],
                 broker_ip=Setting.getBrokerIp()):

        super(ActiveDevice, self).__init__()
        self.isAlive = True
        self.pool = DeviceJobPool()
        self.dev = dev
        self.locker = threading.RLock()
        self.lock_id = ""
        self.lock_stack = []
        self.client = MqttClient()
        self.runnable = runnable
        self.handlers = handlers

        def lock(message, act):
            with self.locker:
                self.lock_stack.append(message['client_id'])
                if act.lock_id == "":
                    act.lock_id = self.lock_stack.pop()
                    act.publish()

        def unlock(message, act):
            with self.locker:
                for item in enumerate(self.lock_stack):
                    if message['client_id'] == item:
                        self.lock_stack.remove(item)
                if message['client_id'] == act.lock_id:
                    if len(self.lock_stack) > 0:
                        act.lock_id = self.lock_stack.pop()
                    else:
                        act.lock_id = ""
                    act.publish()

        def update(message, act):
            with self.locker:
                act.publish()

        def write_wrapp(client, userdata, message, act, func):
            json_frame = json.loads(str(message.payload.decode("utf-8")))
            if json_frame['client_id'] == act.lock_id or act.lock_id == "":
                with self.locker:
                    func(json_frame, act)

        for i in handlers:
            self.client.subscribe(i[0], 0,
                                  partial(write_wrapp, act=self, func=i[1]))
        self.client.subscribe("/device/" + self.dev.id + "/lock", 0,
                              partial(write_wrapp, act=self, func=lock))
        self.client.subscribe("/device/" + self.dev.id + "/unlock", 0,
                              partial(write_wrapp, act=self, func=unlock))
        self.client.subscribe("/device/" + self.dev.id + "/update", 0,
                              partial(write_wrapp, act=self, func=update))
        partial(self.runnable, act=self)

        self.pool.schedule(self.runnable, self.dev.time_resolution, self)
Exemple #6
0
    def __init__(self):
        '''
        Constructor
        '''
        self.locker = threading.RLock()
        self.table = {}

        self.client = mqtt.Client()
        self.client.connect(Setting.getBrokerIp())
        self.client.on_message = partial(on_message, obj=self)
        self.client.loop_start()
        self.client.subscribe("/application/shared/+", qos=0)
        time.sleep(1)
Exemple #7
0
 def __init__(self):
     #Device Pool
     self.cache = []
     self.topics = []
     #Mqtt connection
     self.client = mqtt.Client()
     #Comunicare che sono morto
     #self.client.will_set("/client/"+self.client._client_id+"/status","offline", 0, True)
     self.client.connect(Setting.getBrokerIp())
     self.client.on_message = partial(on_message, cache=self.cache)
     self.client.loop_start()
     #self.client.publish("/client/"+self.client._client_id+"/status","online", 0, True)
     self.executor = ThreadPoolExecutor(max_workers=2)
Exemple #8
0
 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)
Exemple #9
0
 def __init__(self, template=None):
     if template != None:
         self.template = yaml.load(template)
     else:
         self.template = yaml.load('''node_templates:
                                       dev:
                                         type_dev: TempSensor
                                         id_dev: dev
                                         location_dev: bathroom
                                         time_resolution: 0.5
                                         timestamp: 0
                                         requirements: {host: raspy3-A}
                                         type: my.Device.TempSensor
                                         temperature: 0
                                         unit: celsius
                                         gps: [0.0,0.0]''')
     self.client = mqtt.Client()
     self.client.connect(Setting.getBrokerIp())
     self.client.loop_start()
Exemple #10
0
    def __init__(self,
                 dev,
                 runnable=None,
                 handlers=[],
                 broker_ip=Setting.getBrokerIp()):

        super(ActiveDevice, self).__init__()
        self.isAlive = True
        self.dev = dev
        self.locker = threading.RLock()
        self.lock_id = ""
        self.lock_stack = []
        self.client = mqtt.Client()
        self.client.will_set(self.dev.topic(),
                             '["' + self.dev.id + '","offline","",""]', 0,
                             True)
        self.runnable = runnable

        def lock(message, act):
            with self.locker:
                self.lock_stack.append(message['client_id'])
                if act.lock_id == "":
                    act.lock_id = self.lock_stack.pop()
                    act.publish()

        def unlock(message, act):
            with self.locker:
                for item in enumerate(self.lock_stack):
                    if message['client_id'] == item:
                        self.lock_stack.remove(item)
                if message['client_id'] == act.lock_id:
                    if len(self.lock_stack) > 0:
                        act.lock_id = self.lock_stack.pop()
                    else:
                        act.lock_id = ""
                    act.publish()

        def update(message, act):
            with self.locker:
                act.publish()

        def write_wrapp(client, userdata, message, act, func):
            json_frame = json.loads(str(message.payload.decode("utf-8")))
            if json_frame['client_id'] == act.lock_id or act.lock_id == "":
                with self.locker:
                    func(json_frame, act)

        for i in handlers:
            self.client.message_callback_add(
                i[0], partial(write_wrapp, act=self, func=i[1]))
        self.client.message_callback_add(
            "/device/" + self.dev.id + "/lock",
            partial(write_wrapp, act=self, func=lock))
        self.client.message_callback_add(
            "/device/" + self.dev.id + "/unlock",
            partial(write_wrapp, act=self, func=unlock))
        self.client.message_callback_add(
            "/device/" + self.dev.id + "/update",
            partial(write_wrapp, act=self, func=update))

        self.client.connect(broker_ip)
        self.client.loop_start()
        for i in handlers:
            self.client.subscribe(i[0], qos=0)
        self.client.subscribe("/device/" + self.dev.id + "/lock", qos=0)
        self.client.subscribe("/device/" + self.dev.id + "/unlock", qos=0)
        self.client.subscribe("/device/" + self.dev.id + "/update", qos=0)

        self.start()
Exemple #11
0
    def get_msg(self):
        with self.locker:
            return self.msg

    def zero(self):
        with self.locker:
            self.counter = 0
            self.msg = 0


#Init Test
gen = DeviceGenerator()
c = Counter()
client = mqtt.Client()
client.connect(Setting.getBrokerIp())
client.loop_start()
client.on_message = partial(on_message, counter=c)

size_gen = 1
time_wait = 5
list_location = ['bathroom', 'bedroom', 'living', 'kitchen', 'closet', 'box']
time_resolution = [
    0.0005, 0.001, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1,
    0.2, 0.5, 1
]
for i in range(len(time_resolution)):
    print("------         Test :", i, " with resolution :", time_resolution[i])
    model_dev, id_dev = gen.make(size_gen, list_location, ['raspy3-A'],
                                 time_resolution[i])
    time.sleep(time_wait)
Exemple #12
0
 def publish(self):
     self.client.publish("/"+Setting.getNodeId()+"/model/node/status",yaml.dump(self.nodes),qos=0,retain=True)
Exemple #13
0
 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()
Exemple #14
0
 def __init__(self):
     self.locker = threading.RLock()
     self.client = mqtt.Client()
     self.client.connect(Setting.getBrokerIp())
     self.client.loop_start()
Exemple #15
0
    def __init__(self):
        super(Dashboard, self).__init__()
        self.nodes = {'node_templates': {}}
        self.devices = {'node_templates': {}}
        self.apps = {'node_templates': {}}
        self.balancers = {'node_templates': {}}

        def on_message_node(client, userdata, message, obj):
            serial_frame = str(message.payload.decode("utf-8"))
            yaml_frame = yaml.load(serial_frame)
            for node in yaml_frame['node_templates']:
                if node not in obj.nodes['node_templates']:
                    obj.nodes['node_templates'][node] = yaml_frame[
                        'node_templates'][node]
                else:
                    pass

        def on_message_device(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 not in obj.devices['node_templates']:
                    obj.devices['node_templates'][dev] = yaml_frame[
                        'node_templates'][dev]
                else:
                    pass

        def on_message_app(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']:
                obj.apps['node_templates'][app] = yaml_frame['node_templates'][
                    app]

        def on_message_balancer(client, userdata, message, obj):
            serial_frame = str(message.payload.decode("utf-8"))
            yaml_frame = yaml.load(serial_frame)
            for balancer in yaml_frame['node_templates']:
                obj.balancers['node_templates'][balancer] = yaml_frame[
                    'node_templates'][balancer]

        self.client = mqtt.Client()
        self.client.message_callback_add("/+/model/node/status",
                                         partial(on_message_node, obj=self))
        self.client.message_callback_add("/+/model/device/status",
                                         partial(on_message_device, obj=self))
        self.client.message_callback_add("/+/model/apps/status",
                                         partial(on_message_app, obj=self))
        self.client.message_callback_add(
            "/+/model/balancer/status", partial(on_message_balancer, obj=self))
        self.client.connect(Setting.getBrokerIp())
        self.client.loop_start()
        self.client.subscribe("/+/model/node/status", qos=0)
        self.client.subscribe("/+/model/device/status", qos=0)
        self.client.subscribe("/+/model/apps/status", qos=0)
        self.client.subscribe("/+/model/balancer/status", qos=0)
        '''
        <script>
        function timedRefresh(timeoutPeriod) {
            setTimeout("location.reload(true);",timeoutPeriod);
        }
        window.onload = timedRefresh(5000);
        </script>
        '''

        self.structure = """<html>
Exemple #16
0
    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()
Exemple #17
0
    def __init__(self):
        super(Dashboard, self).__init__()
        self.nodes = {'node_templates': {}}
        self.devices = {'node_templates': {}}
        self.apps = {'node_templates': {}}
        self.balancers = {'node_templates': {}}
        self.lock_nodes = threading.RLock()
        self.lock_devices = threading.RLock()
        self.lock_apps = threading.RLock()
        self.lock_balancers = threading.RLock()

        def on_message_node(client, userdata, message, obj):
            with self.lock_nodes:
                serial_frame = str(message.payload.decode("utf-8"))
                yaml_frame = yaml.load(serial_frame)
                if (yaml_frame['node_templates']):
                    source = message.topic.split('/')[1]
                    if (source in obj.nodes['node_templates']):
                        obj.nodes['node_templates'].pop(source)
                for node in yaml_frame['node_templates']:
                    obj.nodes['node_templates'][node] = yaml_frame[
                        'node_templates'][node]

        def on_message_device(client, userdata, message, obj):
            with self.lock_devices:
                serial_frame = str(message.payload.decode("utf-8"))
                yaml_frame = yaml.load(serial_frame)
                source = message.topic.split('/')[1]
                if (yaml_frame['node_templates']):
                    if (source in obj.devices['node_templates']):
                        obj.devices['node_templates'].pop(source)
                obj.devices['node_templates'][source] = yaml_frame[
                    'node_templates']

        def on_message_app(client, userdata, message, obj):
            with self.lock_apps:
                serial_frame = str(message.payload.decode("utf-8"))
                yaml_frame = yaml.load(serial_frame)
                source = message.topic.split('/')[1]
                if (yaml_frame['node_templates']):
                    if (source in obj.apps['node_templates']):
                        obj.apps['node_templates'].pop(source)
                obj.apps['node_templates'][source] = yaml_frame[
                    'node_templates']

        def on_message_balancer(client, userdata, message, obj):
            with self.lock_balancers:
                serial_frame = str(message.payload.decode("utf-8"))
                yaml_frame = yaml.load(serial_frame)
                source = message.topic.split('/')[1]
                if (yaml_frame['node_templates']):
                    if (source in obj.balancers['node_templates']):
                        obj.balancers['node_templates'].pop(source)
                obj.balancers['node_templates'][source] = yaml_frame[
                    'node_templates']

        self.client = mqtt.Client()
        self.client.message_callback_add("/+/model/node/status",
                                         partial(on_message_node, obj=self))
        self.client.message_callback_add("/+/model/device/status",
                                         partial(on_message_device, obj=self))
        self.client.message_callback_add("/+/model/apps/status",
                                         partial(on_message_app, obj=self))
        self.client.message_callback_add(
            "/+/model/balancer/status", partial(on_message_balancer, obj=self))
        self.client.connect(Setting.getBrokerIp())
        self.client.loop_start()
        self.client.subscribe("/+/model/node/status", qos=0)
        self.client.subscribe("/+/model/device/status", qos=0)
        self.client.subscribe("/+/model/apps/status", qos=0)
        self.client.subscribe("/+/model/balancer/status", qos=0)

        self.structure = """<html>
Exemple #18
0
'''
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)