def test_21_CreateJson(self): """ Create a JSON object for Location. """ l_location = self.m_api.read_location_xml(self.m_pyhouse_obj) l_json = encode_json(l_location) l_obj = decode_json_unicode(l_json) # print(PrettyFormatAny.form(l_obj, 'JSON', 80)) self.assertEqual(l_obj['Street'], TESTING_LOCATION_STREET) self.assertEqual(l_obj['City'], TESTING_LOCATION_CITY)
def MqttDispatch(self, p_topic, p_message): """Dispatch a received MQTT message according to the topic. TODO: This needs protection from poorly formed Mqtt messages. """ l_topic = p_topic.split('/')[2:] # Drop the pyhouse/housename/ as that is all we subscribed to. l_message = json_tools.decode_json_unicode(p_message) l_logmsg = Actions(self.m_pyhouse_obj).dispatch(l_topic, l_message) # ## LOG.info(l_logmsg)
def jsonPair(self, p_json, p_key): """ Extract key, value from json """ l_json = json_tools.decode_json_unicode(p_json) try: l_val = l_json[p_key] except (KeyError, ValueError) as e_err: l_val = 'ERRor on JsonPair for key "{}" {} {}'.format(p_key, e_err, l_json) print(l_val) return l_val
def test_01_Add(self): l_node = nodesXml._read_one_node_xml(self.m_xml.node) print(PrettyFormatAny.form(l_node, 'Node')) l_json = json_tools.encode_json(l_node) print(PrettyFormatAny.form(l_json, 'PyHouse')) l_msg = json_tools.decode_json_unicode(l_json) Util.add_node(self.m_pyhouse_obj, l_msg) print( PrettyFormatAny.form(self.m_pyhouse_obj.Computer.Nodes, 'PyHouse')) self.assertNotEqual(self.m_pyhouse_obj.Xml, TESTING_NODES_NODE_NAME_0)
def jsonPair(self, p_json, p_key): """ Extract key, value from json """ l_json = json_tools.decode_json_unicode(p_json) try: l_val = l_json[p_key] except KeyError as e_err: print('ERROR - {}'.format(e_err)) l_val = 'ERRor {}'.format(e_err) return l_val
def test_1_CreateJson(self): """ Create a JSON object for Rooms. """ self.m_pyhouse_obj.House.Rooms = l_rooms = self.m_api.read_rooms_xml( self.m_pyhouse_obj) l_json = json_tools.encode_json(l_rooms) l_obj = json_tools.decode_json_unicode(l_json) # print(PrettyFormatAny.form(l_json, 'JSON', 80)) # print(PrettyFormatAny.form(l_obj, 'JSON', 80)) self.assertEqual(len(l_obj), len(l_rooms))
def MqttDispatch(self, p_topic, p_message): """Dispatch a received MQTT message according to the topic. TODO: This needs protection from poorly formed Mqtt messages. """ l_topic = p_topic.split('/')[ 2:] # Drop the pyhouse/housename/ as that is all we subscribed to. l_message = json_tools.decode_json_unicode(p_message) l_logmsg = Actions(self.m_pyhouse_obj).mqtt_dispatch( l_topic, l_message) LOG.info(l_logmsg)
def jsonPair(self, p_json, p_key): """ Extract key, value from json """ l_json = json_tools.decode_json_unicode(p_json) try: l_val = l_json[p_key] except (KeyError, ValueError) as e_err: l_val = 'ERRor on JsonPair for key "{}" {} {}'.format( p_key, e_err, l_json) print(l_val) return l_val
def saveControllerData(self, p_json): """A new/changed controller is returned. Process it and update the internal data via controller.py """ l_json = json_tools.decode_json_unicode(p_json) l_controller_ix = int(l_json['Key']) l_delete = l_json['Delete'] if l_delete: try: del self.m_pyhouse_obj.House.Lighting.Controllers[l_controller_ix] except AttributeError: print("web_controllers - Failed to delete - JSON: {0:}".FORMAT(l_json)) return try: l_obj = self.m_pyhouse_obj.House.Lighting.Controllers[l_controller_ix] except KeyError: l_obj = ControllerData() l_obj.Name = l_json['Name'] l_obj.Active = l_json['Active'] l_obj.Key = l_controller_ix l_obj.Comment = l_json['Comment'] l_obj.RoomCoords = l_json['RoomCoords'] l_obj.IsDimmable = l_json['IsDimmable'] l_obj.DeviceFamily = l_json['DeviceFamily'] l_obj.RoomName = l_json['RoomName'] l_obj.LightingType = l_json['LightingType'] l_obj.UUID = l_json['UUID'] l_obj.InterfaceType = l_json['InterfaceType'] l_obj.Port = l_json['Port'] if l_obj.DeviceFamily == 'Insteon': Insteon_utils.Util().get_json_data(l_obj, l_json) elif l_obj.DeviceFamily == 'UPB': l_obj.UPBAddress = l_json['UPBAddress'] l_obj.UPBPassword = l_json['UPBPassword'] l_obj.UPBNetworkID = l_json['UPBNetworkID'] if l_obj.InterfaceType == 'Serial': l_obj.BaudRate = l_json['BaudRate'] self.m_pyhouse_obj.House.Lighting.Controllers[l_controller_ix] = l_obj LOG.info('Controller Added - {}'.format(l_obj))
def saveControllerData(self, p_json): """A new/changed controller is returned. Process it and update the internal data via controller.py """ l_json = json_tools.decode_json_unicode(p_json) l_controller_ix = int(l_json['Key']) l_delete = l_json['Delete'] if l_delete: try: del self.m_pyhouse_obj.House.Lighting.Controllers[l_controller_ix] except AttributeError: LOG.error("web_controllers - Failed to delete - JSON: {}".FORMAT(l_json)) return try: l_obj = self.m_pyhouse_obj.House.Lighting.Controllers[l_controller_ix] except KeyError: l_obj = ControllerData() l_obj.Name = l_json['Name'] l_obj.Active = l_json['Active'] l_obj.Key = l_controller_ix l_obj.Comment = l_json['Comment'] l_obj.RoomCoords = l_json['RoomCoords'] l_obj.IsDimmable = l_json['IsDimmable'] l_obj.DeviceFamily = l_json['DeviceFamily'] l_obj.RoomName = l_json['RoomName'] l_obj.LightingType = l_json['LightingType'] l_obj.UUID = l_json['UUID'] l_obj.InterfaceType = l_json['InterfaceType'] l_obj.Port = l_json['Port'] if l_obj.DeviceFamily == 'Insteon': Insteon_utils.Util().get_json_data(l_obj, l_json) elif l_obj.DeviceFamily == 'UPB': l_obj.UPBAddress = l_json['UPBAddress'] l_obj.UPBPassword = l_json['UPBPassword'] l_obj.UPBNetworkID = l_json['UPBNetworkID'] if l_obj.InterfaceType == 'Serial': l_obj.BaudRate = l_json['BaudRate'] self.m_pyhouse_obj.House.Lighting.Controllers[l_controller_ix] = l_obj LOG.info('Controller Added - {}'.format(l_obj))
def saveUpdateData(self, p_json): """A new/changed web is returned. Process it and update the internal data via ???.py """ l_json = json_tools.decode_json_unicode(p_json)
def test_02_Decode(self): l_json = json_tools.encode_json(self.m_pyhouse_obj.Computer) l_dict = json_tools.decode_json_unicode(l_json) # print(debug_tools.PrettyFormatAny.form(l_dict, 'Decoded Inof')) self.assertEqual(l_dict['Name'], self.m_pyhouse_obj.Computer.Name)
def test_02_ValidList(self): l_json = web_login.LoginElement(self.m_worksapce).getValidLists() l_test = json_tools.decode_json_unicode(l_json) print(PrettyFormatAny.form(l_test, 'JSON', 40)) self.assertEqual(l_test['ServerName'], TESTING_NODES_NODE_NAME_0)