def start_device(): global mediatorClient, edgeClient, mqttLoop Log.v('start_device', 'Starting device on {} ...'.format(sys.platform)) Log.v('start_device', 'Connecting to mediator broker: {}'.format(mediatorBroker)) mediatorClient = mqtt.Client(client_id = mediatorClientId + deviceId) mediatorClient.username_pw_set(mediatorUsername, mediatorPassword) mediatorClient.on_connect = on_m_connect mediatorClient.on_message = on_m_message mediatorClient.on_subscribe = on_m_subscribe mediatorClient.on_publish = on_m_publish mediatorClient.connect(mediatorBroker) mediatorClient.loop_start() edgeClient = mqtt.Client(client_id = edgeClientId + deviceId) edgeClient.username_pw_set(edgeUsername, edgePassword) edgeClient.on_connect = on_e_connect edgeClient.on_message = on_e_message edgeClient.on_subscribe = on_e_subscribe mqttLoop = True try: while mqttLoop: pass except KeyboardInterrupt: print('Device stopped with Ctrl + C ...') exit()
def on_message(client, userdata, msg): rx_topic = msg.topic Log.v('on_message', 'topic: {}'.format(rx_topic)) if rx_topic == topic_reg_req: # register/request reg_handler(msg) elif rx_topic == topic_sch_req: # search/request sch_handler(msg) else: Log.w('on_message', 'Unknown topic: {}'.format(rx_topic))
def on_m_message(client, userdata, msg): global topic_off_req, edgeClient, edgeBroker, edgeId Log.v('on_m_message', 'topic: {}'.format(msg.topic)) Log.v('on_m_message', 'payload: {}'.format(msg.payload)) js = json.loads(msg.payload) edgeId = str(js['client_id']) edgeBroker = str(js['broker']['ip']) topic_off_req = topic_off_req_base + edgeId edgeClient.connect(edgeBroker) edgeClient.loop_start()
def reg_handler(msg): global topic_reg_rep, topic_reg_rep_base global edge_capability dict = json.loads(msg.payload) Log.v('reg_handler', dict) edgeId = dict['client_id'] topic_reg_rep = topic_reg_rep_base + edgeId if edgeId == None: Log.e('reg_handler', 'Request without edge id: ' + dict) payload = json.dumps({"code":100,"message":"without edge id"}) elif dict.get('disk') == None: payload = json.dumps({"code":100,"message":"without disk data"}) elif dict.get('memory') == None: payload = json.dumps({"code":100,"message":"without memory data"}) elif dict.get('cpu') == None: payload = json.dumps({"code":100,"message":"without cpu data"}) else: edge_capability[edgeId] = {} edge_capability[edgeId]['broker'] = dict['broker'] edge_capability[edgeId]['disk'] = dict['disk'] edge_capability[edgeId]['memory'] = dict['memory'] edge_capability[edgeId]['cpu'] = dict['cpu'] payload = json.dumps({"code":200, "message":"success"}) Log.v('reg_handler', 'Pub topic: {}'.format(topic_reg_rep)) Log.v('reg_handler', 'Pub payload: {}'.format(payload)) client.publish(topic_reg_rep, payload, qos = 2, retain = False)
def start_mediator(): global client, clientId global username, password global broker, port, loop Log.v('start_mediator', 'Starting mediator ...') # Connect to mediator broker Log.v('start_mediator', 'Connecting mediator broker ...') client = mqtt.Client(clientId) client.username_pw_set(username, password) client.on_connect = on_connect client.on_message = on_message client.on_subscribe = on_subscribe client.connect(broker, port) client.loop_start() loop = True try: while loop: pass except KeyboardInterrupt: Log.i('start_mediator', 'Mediator stopped with Ctrl + C ...') exit()
def sch_handler(msg): global topic_sch_rep, topic_sch_rep_base global edge_capability, device_requirement dict = json.loads(msg.payload) Log.v('sch_handler', dict) deviceId = dict['client_id'] topic_sch_rep = topic_sch_rep_base + deviceId device_requirement['disk'] = dict['disk'] device_requirement['memory'] = dict['memory'] device_requirement['cpu'] = dict['cpu'] code, message = co2_utils.select_edge(co2_utils.max_cpu, edge_capability, device_requirement) if code == 200: print message ip = edge_capability[message]['broker']['ip'] port = edge_capability[message]['broker']['port'] payload = json.dumps({"code":200, "client_id":message, "broker":{"ip":ip, "port":port}}) elif code == 100: payload = json.dumps({"code":100, "message":message}) Log.v('sch_handler', 'Pub: {}\n{}'.format(topic_sch_rep, payload)) client.publish(topic_sch_rep, payload, qos = 2, retain = False)
def on_subscribe(client, userdata, mid, qos): Log.v('on_subscribe', 'Subscribed {} with QoS ({})'.format(topic_reg_req, qos[0])) Log.v('on_subscribe', 'Subscribed {} with QoS ({})'.format(topic_sch_req, qos[1]))
def on_m_subscribe(client, userdata, mid, qos): Log.v('on_m_subscribe', 'Subscribed {} with QoS ({})'.format(topic_sch_rep, qos[0])) client.publish(topic_sch_req, generate_search_payload(), qos = 2)
def on_m_publish(client, userdata, result): Log.v('on_m_publish', 'pub ok')
def on_e_subscribe(client, userdata, mid, qos): Log.v('on_e_subscribe', 'Subscribed {} with QoS ({})'.format(topic_off_rep, qos[0])) topic_off_req = topic_off_req_base + edgeId client.publish(topic_off_req, message2, qos = 2)
def on_e_message(client, userdata, msg): Log.v('on_e_message', 'topic: {}'.format(msg.topic)) Log.v('on_e_message', 'payload: {}'.format(msg.payload)) edgeClient.disconnect() edgeClient.loop_stop()
def on_e_subscribe(client, userdata, mid, qos): Log.v('on_e_subscribe', 'Subscribed {} with QoS ({})'.format(topic_off_req, qos[0]))
def on_m_message(client, userdate, msg): Log.v('on_m_message', 'Sub topic: {}'.format(msg.topic)) Log.v('on_m_message', 'Sub payload: {}'.format(msg.payload))