def test_100_dhcp_heartbeat_bad(self): self.topic = "/dhcp/heartbeat" self.mqttc.publish(self.topic, 'ONLINE') self.mqttc.publish(self.topic+'/', 'ONLINE') self.mqttc.publish(self.topic+'/'+'a%s0'%HADD_SEP, 'ONLINE') self.mqttc.publish(self.topic+'/'+'0%sa'%HADD_SEP, 'ONLINE') self.mqttc.publish(self.topic+'/'+'a%sa'%HADD_SEP, 'ONLINE') self.mqttc.publish(self.topic+'/'+'None%sNone'%HADD_SEP, 'ONLINE') msg = {'add_ctrl':'a', 'add_node':'a'} self.mqttc.publish(self.topic, json_dumps(msg)) msg = {'add_ctrl':None, 'add_node':None} self.mqttc.publish(self.topic, json_dumps(msg))
def mqtt_on_resolv_hadd(self, client, userdata, message): """On resolv hadd message :param client: the Client instance that is calling the callback. :type client: paho.mqtt.client.Client :param userdata: user data of any type and can be set when creating a new client instance or with user_data_set(userdata). :type userdata: all :param message: The message variable is a MQTTMessage that describes all of the message parameters. :type message: paho.mqtt.client.MQTTMessage """ #~ print "mqtt_on_resolv_hadd receive %s" logger.debug("mqtt_on_resolv_hadd receive %s", message.payload) res = {} res['msg_status'] = 200 data = json_loads(message.payload) if 'rep_uuid' not in data: logger.debug("mqtt_on_resolv_hadd receive a request with no rep_uuid") return for ffield in ['add_ctrl', 'add_node']: if ffield not in data: res['msg_status'] = 400 res['msg_error'] = "Missing field %s in request" % ffield res['rep_uuid'] = data['rep_uuid'] if res['msg_status'] == 200: lease = self.lease_mgr.resolv_hadd(data['add_ctrl'], data['add_node']) #print lease res.update(lease) #print res self.publish_reply(uuid=data['rep_uuid'], payload=json_dumps(res))
def to_json_with_indexes(self): """Retrieve a json version of the value """ try: res = self.to_dict_with_indexes() #~ print "to_json_with_indexes", res return json_dumps(res) except Exception: logger.exception('[%s] - Exception in to_json_with_indexes', self.__class__.__name__) return None
def to_json_with_indexes(self): """Retrieve a json version of the value """ try: res = self.to_dict_with_indexes() #~ print "to_json_with_indexes", res return json_dumps(res) except: logger.exception('Exception in to_json_with_indexes') return None
def request_info_commands(self, reply_topic, resp): """ """ resp['data'] = {} for kvalue in self._controller.values.keys(): value = self._controller.values[kvalue] if value.genre == 0x04: if value.hadd not in resp['data']: resp['data'][value.hadd] = {} resp['data'][value.hadd][value.uuid] = value.to_dict() msg = json_dumps(resp) self.publish_request(reply_topic, msg)
def assertUpdateValue( self, type="user", data=None, cmd_class=0, genre=0x04, uuid="request_info_nodes", node_hadd=None, client_hadd=None, is_writeonly=False, is_readonly=False, timeout=5, ): self.message_received = False self.message = None print "Waiting for %s : %s" % (node_hadd, uuid) def mqtt_on_message(client, userdata, message): """On generic message """ msg = json_loads(message.payload) print "Received message %s" % msg if msg["uuid"] == uuid and msg["hadd"] == node_hadd: self.message = message self.message_received = True self.mqttc.subscribe(topic="/values/%s/%s/#" % (type, node_hadd), callback=mqtt_on_message) print "Subscribe to /values/%s/%s/#" % (type, node_hadd) time.sleep(0.5) msg = { "cmd_class": cmd_class, "genre": genre, "uuid": uuid, "reply_hadd": client_hadd, "data": data, "hadd": node_hadd, "is_writeonly": is_writeonly, "is_readonly": is_readonly, } self.mqttc.publish("/nodes/%s/request" % (node_hadd), json_dumps(msg)) i = 0 while i < timeout * 10000 and not self.message_received: time.sleep(0.0001) i += 1 self.assertTrue(self.message_received) self.assertTrue(self.message is not None) self.assertTrue(self.message.payload is not None) if data is not None: msg = json_loads(self.message.payload) self.assertEqual(msg["data"], data) self.mqttc.unsubscribe(topic="/values/%s/%s/#" % (type, node_hadd)) time.sleep(0.5)
def test_101_dhcp_lease_lock_bad(self): uuid = self.mqttc.subscribe_reply(callback=self.on_message) msg = {'rep_uuid' : uuid, 'add_ctrl':'a', 'add_node':'a'} self.mqttc.publish("/dhcp/lease/lock", json_dumps(msg)) i = 0 while self.message is None and i<100000: i += 1 time.sleep(0.0001) self.mqttc.unsubscribe_reply(uuid) print self.payload self.assertEqual(self.payload['msg_status'], 400) self.message = None uuid = self.mqttc.subscribe_reply(callback=self.on_message) msg = {'rep_uuid' : uuid, 'add_ctrl':None, 'add_node':None} self.mqttc.publish("/dhcp/lease/lock", json_dumps(msg)) i = 0 while self.message is None and i<100000: i += 1 time.sleep(0.0001) self.mqttc.unsubscribe_reply(uuid) print self.payload self.assertEqual(self.payload['msg_status'], 400)
def test_120_dhcp_lease_repair(self): self.topic = "/dhcp/lease/repair" uuid = self.mqttc.subscribe_reply(callback=self.on_message) msg = {'rep_uuid' : uuid, 'add_ctrl':11, 'add_node':1,'options' : {'name':'test', 'location':'location_test'}} self.mqttc.publish(self.topic, json_dumps(msg)) time.sleep(2) uuid = self.mqttc.unsubscribe_reply(uuid) print self.payload self.assertEqual(self.payload['msg_status'], 200) self.assertEqual(self.payload['name'], 'test') self.assertEqual(self.payload['location'], 'location_test') self.assertEqual(self.payload['add_ctrl'], 11) self.assertEqual(self.payload['add_node'], 1) self.mqttc.publish_heartbeat(self.payload['add_ctrl'], self.payload['add_node'])
def send(self, event=None): """ """ msg = { 'cmd_class':int(self.var_value_cmdclass.get()), 'genre':int(self.var_value_genre.get()), 'uuid':self.var_vuuid.get(), 'reply_hadd':self.my_hadd, 'is_writeonly':True, 'is_readonly':False, 'hadd':self.var_nhadd.get(), 'data':self.var_value_data.get(), } logger.debug('Send MQTT msg %s', msg) self.mqttc.publish(jnt_utils.TOPIC_NODES_REQUEST%self.var_nhadd.get(), jnt_utils.json_dumps(msg))
def resolv_heartbeat(self): """ """ logger.debug("[%s] - Send heartbeat on resolv", self.__class__.__name__) if self.resolv_timer is not None: #The manager is started self.resolv_timer.cancel() self.resolv_timer = None self.resolv_timer = threading.Timer(self.network.resolv_timeout, self.resolv_heartbeat) self.resolv_timer.start() if self.get_controller_hadd() is not None: #~ print self.nodes[node].hadd add_ctrl, add_node = self.get_controller().split_hadd() msg = {'add_ctrl':add_ctrl, 'add_node':add_node, 'state':'ONLINE'} self.mqtt_resolv.publish(topic="/dhcp/resolv/heartbeat", payload=json_dumps(msg))
def publish_heartbeat_resolv_msg(self, msg, qos=0, retain=False): """Publish an heartbeat message. This causes a message to be sent to the broker and subsequently from the broker to any clients subscribing to matching topics. :param uuid: The uuid sent in the request. :type uuid: str :param payload: The actual message to send. If not given, or set to None a zero length message will be used. Passing an int or float will result in the payload being converted to a string representing that number. If you wish to send a true int/float, use struct.pack() to create the payload you require. :type payload: message :param qos: The quality of service level to use. :type qos: int :param retain: If set to true, the message will be set as the "last known good"/retained message for the topic. :type retain: bool """ self.publish(topic="/resolv/dhcp/heartbeat/", payload=json_dumps(msg), qos=qos, retain=retain)
def assertNodeRequest( self, cmd_class=0, genre=0x04, uuid="request_info_nodes", node_hadd=None, client_hadd=None, data=None, is_writeonly=False, is_readonly=False, timeout=5, ): self.message_received = False print "Waiting for %s : %s" % (node_hadd, uuid) def mqtt_on_message(client, userdata, message): """On generic message """ self.message = message self.message_received = True self.mqttc.subscribe(topic=TOPIC_NODES_REPLY % client_hadd, callback=mqtt_on_message) time.sleep(0.5) msg = { "cmd_class": cmd_class, "genre": genre, "uuid": uuid, "reply_hadd": client_hadd, "data": data, "hadd": node_hadd, "is_writeonly": is_writeonly, "is_readonly": is_readonly, } self.mqttc.publish("/nodes/%s/request" % (node_hadd), json_dumps(msg)) i = 0 while i < timeout * 10000 and not self.message_received: time.sleep(0.0001) i += 1 self.assertTrue(self.message_received) self.mqttc.unsubscribe(topic=TOPIC_NODES_REPLY % client_hadd) time.sleep(0.5)
def test_160_dhcp_resolv_cmd_classes(self): self.start() self.assertHeartbeatNode() options={'name':'name','location':'location','cmd_classes':'0x1050','otherkey':'other','otherkey2':'other2',} res = self.server.lease_mgr.new_lease(-1, 3, options) options={'name':'name','location':'location','cmd_classes':'0x0027','otherkey':'other','otherkey2':'other2',} res = self.server.lease_mgr.new_lease(10, -1, options) options={'name':'name','location':'location','cmd_classes':'0x0027','otherkey':'other','otherkey2':'other2',} res = self.server.lease_mgr.new_lease(10, -1, options) options={'name':'name','location':'location','cmd_classes':'0x0028,0x0027','otherkey':'other','otherkey2':'other2',} res = self.server.lease_mgr.new_lease(10, -1, options) res = self.server.lease_mgr.resolv_cmd_classes(cmd_classes="0x0027") print res self.assertEqual(len(res), 3) self.assertNotEqual(json_dumps(res), None) res = self.server.lease_mgr.resolv_cmd_classes(cmd_classes=["0x1050","0x0028"]) print res self.assertEqual(len(res), 2) res = self.server.lease_mgr.resolv_cmd_classes() print res self.assertEqual(len(res), 4)
def test_170_dhcp_resolv_name(self): self.start() self.assertHeartbeatNode() options={'name':'name1','location':'location','cmd_classes':'0x1050','otherkey':'other','otherkey2':'other2',} res = self.server.lease_mgr.new_lease(-1, 3, options) options={'name':'name2','location':'kitchen.location','cmd_classes':'0x0027','otherkey':'other','otherkey2':'other2',} res = self.server.lease_mgr.new_lease(10, -1, options) options={'name':'name3','location':'lights.kitchen.location','cmd_classes':'0x0027','otherkey':'other','otherkey2':'other2',} res = self.server.lease_mgr.new_lease(10, -1, options) options={'name':'name4','location':'garden.location','cmd_classes':'0x0028,0x0027','otherkey':'other','otherkey2':'other2',} res = self.server.lease_mgr.new_lease(10, -1, options) res = self.server.lease_mgr.resolv_name(name="name1") print res self.assertEqual(len(res), 1) self.assertNotEqual(json_dumps(res), None) res = self.server.lease_mgr.resolv_name(location="location") print res self.assertEqual(len(res), 4) res = self.server.lease_mgr.resolv_name(location="kitchen.location") print res self.assertEqual(len(res), 2)
def to_json(self): """Retrieve a json version of the value """ res = self.to_dict() return json_dumps(res)
def request_info_nodes(self, reply_topic, resp): """ """ resp['data'] = self._controller.to_dict() msg = json_dumps(resp) self.publish_request(reply_topic, msg)
def test_110_dhcp_lease_new_release_remove(self): self.topic = "/dhcp/lease/new" uuid = self.mqttc.subscribe_reply(callback=self.on_message) msg = {'rep_uuid' : uuid, 'add_ctrl':-1, 'add_node':-1,'options' : {'name':'test', 'location':'location_test'}} self.mqttc.publish(self.topic, json_dumps(msg)) i = 0 while self.message is None and i<100000: i += 1 time.sleep(0.0001) self.mqttc.unsubscribe_reply(uuid) print self.payload self.assertEqual(self.payload['msg_status'], 200) self.assertEqual(self.payload['name'], 'test') self.assertEqual(self.payload['location'], 'location_test') self.assertEqual(self.payload['add_ctrl'], 10) self.assertEqual(self.payload['add_node'], 0) add_ctrl = self.payload['add_ctrl'] add_node = self.payload['add_node'] self.message = None uuid = self.mqttc.subscribe_reply(callback=self.on_message) msg = {'rep_uuid' : uuid, 'add_ctrl':add_ctrl, 'add_node':add_node} self.mqttc.publish("/dhcp/resolv/hadd", json_dumps(msg)) i = 0 while self.message is None and i<100000: i += 1 time.sleep(0.0001) self.mqttc.unsubscribe_reply(uuid) print self.payload self.assertEqual(self.payload['msg_status'], 200) self.assertEqual(self.payload['name'], 'test') self.assertEqual(self.payload['location'], 'location_test') self.assertEqual(self.payload['add_ctrl'], add_ctrl) self.assertEqual(self.payload['add_node'], add_node) self.assertEqual(self.payload['state'], 'BOOT') self.message = None self.mqttc.publish_heartbeat(add_ctrl, add_node) uuid = self.mqttc.subscribe_reply(callback=self.on_message) msg = {'rep_uuid' : uuid, 'add_ctrl':add_ctrl, 'add_node':add_node} self.mqttc.publish("/dhcp/resolv/hadd", json_dumps(msg)) i = 0 while self.message is None and i<100000: i += 1 time.sleep(0.0001) self.mqttc.unsubscribe_reply(uuid) print self.payload self.assertEqual(self.payload['msg_status'], 200) self.assertEqual(self.payload['name'], 'test') self.assertEqual(self.payload['location'], 'location_test') self.assertEqual(self.payload['add_ctrl'], add_ctrl) self.assertEqual(self.payload['add_node'], add_node) self.assertEqual(self.payload['state'], 'ONLINE') self.message = None uuid = self.mqttc.subscribe_reply(callback=self.on_message) msg = {'rep_uuid' : uuid, 'add_ctrl':add_ctrl, 'add_node':add_node} self.mqttc.publish("/dhcp/lease/release", json_dumps(msg)) i = 0 while self.message is None and i<100000: i += 1 time.sleep(0.0001) self.mqttc.unsubscribe_reply(uuid) print self.payload self.assertEqual(self.payload['msg_status'], 200) self.assertEqual(self.payload['add_ctrl'], add_ctrl) self.assertEqual(self.payload['add_node'], add_node) self.assertEqual(self.payload['state'], 'OFFLINE') self.message = None uuid = self.mqttc.subscribe_reply(callback=self.on_message) msg = {'rep_uuid' : uuid, 'add_ctrl':add_ctrl, 'add_node':add_node} self.mqttc.publish("/dhcp/resolv/hadd", json_dumps(msg)) i = 0 while self.message is None and i<100000: i += 1 time.sleep(0.0001) self.mqttc.unsubscribe_reply(uuid) print self.payload self.assertEqual(self.payload['msg_status'], 200) self.assertEqual(self.payload['name'], 'test') self.assertEqual(self.payload['location'], 'location_test') self.assertEqual(self.payload['add_ctrl'], add_ctrl) self.assertEqual(self.payload['add_node'], add_node) self.assertEqual(self.payload['state'], 'OFFLINE') self.message = None uuid = self.mqttc.subscribe_reply(callback=self.on_message) msg = {'rep_uuid' : uuid, 'add_ctrl':add_ctrl, 'add_node':add_node} self.mqttc.publish("/dhcp/lease/remove", json_dumps(msg)) i = 0 while self.message is None and i<100000: i += 1 time.sleep(0.0001) self.mqttc.unsubscribe_reply(uuid) print self.payload self.assertEqual(self.payload['msg_status'], 200) self.assertEqual(self.payload['add_ctrl'], add_ctrl) self.assertEqual(self.payload['add_node'], add_node)