def saveMotionSensorData(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_ix = int(l_json['Key']) l_delete = l_json['Delete'] if l_delete: try: del self.m_pyhouse_obj.House.Security.MotionSensors[l_ix] except AttributeError: LOG.error( "web_controllers - Failed to delete - JSON: {}".FORMAT( l_json)) return try: l_obj = self.m_pyhouse_obj.House.Security.MotionSensors[l_ix] except KeyError: l_obj = MotionSensorData() web_utils.get_base_info(l_obj, l_json) l_obj.Comment = l_json['Comment'] l_obj.DeviceFamily = l_json['DeviceFamily'] l_obj.DeviceType = 3 l_obj.DeviceSubType = 2 l_obj.Status = l_json['Status'] web_family.get_family_json_data(l_obj, l_json) web_utils.get_room_info(l_obj, l_json) self.m_pyhouse_obj.House.Security.MotionSensors[l_ix] = l_obj LOG.info('Motion Sensor Added - {}\n{}'.format(l_obj.Name, l_json))
def saveButtonData(self, p_json): """A new/changed button is returned. """ l_json = json_tools.decode_json_unicode(p_json) l_delete = l_json['Delete'] l_ix = int(l_json['Key']) if l_delete: try: del self.m_pyhouse_obj.House.Lighting.Buttons[l_ix] except AttributeError: LOG.error("Failed to delete - JSON: {}".format(l_json)) return l_obj = lighting_buttons.ButtonData() try: l_obj = self.m_pyhouse_obj.House.Lighting.Buttons[l_ix] except KeyError: LOG.warning('Creating a new button {}'.format(l_ix)) # web_utils.get_base_info(l_obj, l_json) l_obj.Comment = l_json['Comment'] l_obj.DeviceType = 1 l_obj.DeviceSubType = 1 l_obj.DeviceFamily = l_json['DeviceFamily'] web_family.get_family_json_data(l_obj, l_json) web_utils.get_room_info(l_obj, l_json) self.m_pyhouse_obj.House.Lighting.Buttons[l_obj.Key] = l_obj LOG.info('Button Added - {}'.format(l_obj.Name))
def test_02_Decode(self): # l_json = json_tools.encode_json(self.m_pyhouse_obj.Computer) l_json = json_tools.encode_json(MSG_DICT) print(PrettyFormatAny.form(l_json, 'A1-02-A - Encoded Info')) l_dict = json_tools.decode_json_unicode(l_json) print(l_dict) print(PrettyFormatAny.form(l_dict, 'A1-02-B - Decoded Info'))
def saveWebData(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) l_obj = WebInformation() l_obj.Port = l_json['Port'] self.m_pyhouse_obj._APIs.Computer.WebAPI.SaveXml(l_obj)
def saveMqttData(self, p_json): """Mqtt data is returned, so update the info. """ l_json = json_tools.decode_json_unicode(p_json) l_delete = l_json['Delete'] l_ix = int(l_json['Key']) if l_delete: try: del self.m_pyhouse_obj.Core.Mqtt.Brokers except AttributeError: LOG.error( "web_mqtt - Failed to delete - JSON: {}".format(l_json)) return try: l_obj = self.m_pyhouse_obj.Core.Mqtt.Brokers[l_ix] except KeyError: LOG.warning('Creating a new Mqtt Broker Key: {}'.format(l_ix)) l_obj = MqttBrokerInformation() # LOG.info('JSON {}'.format(l_json)) l_obj.Name = l_json['Name'] l_obj.Active = l_json['Active'] l_obj.Key = l_ix l_obj.UUID = Uuid.make_valid(l_json['UUID']) l_obj.BrokerAddress = l_json['BrokerAddress'] l_obj.BrokerPort = l_json['BrokerPort'] self.m_pyhouse_obj.Core.Mqtt.Brokers[l_obj.Key] = l_obj
def saveLightData(self, p_json): """A new/changed light is returned. Process it and update the internal data via light_xxxx.py """ l_json = json_tools.decode_json_unicode(p_json) l_delete = l_json['Delete'] l_ix = int(l_json['Key']) if l_delete: try: del self.m_pyhouse_obj.House.Lighting.Lights[l_ix] except AttributeError: LOG.error( "web_lights - Failed to delete - JSON: {}".format(l_json)) return l_obj = lighting_lights.LightData() try: l_obj = self.m_pyhouse_obj.House.Lighting.Lights[l_ix] except KeyError: l_obj = lighting_lights.LightData() LOG.warning('Creating a new light {}'.format(l_ix)) # LOG.debug(PrettyFormatAny.form(l_json, 'json')) web_utils.get_base_info(l_obj, l_json) l_obj.Comment = l_json['Comment'] l_obj.IsDimmable = l_json['IsDimmable'] l_obj.DeviceFamily = l_json['DeviceFamily'] l_obj.DeviceType = 1 l_obj.DeviceSubType = 3 web_family.get_family_json_data(l_obj, l_json) web_utils.get_room_info(l_obj, l_json) self.m_pyhouse_obj.House.Lighting.Lights[l_ix] = l_obj LOG.info('Light Added - {}'.format(l_obj.Name))
def saveButtonData(self, p_json): """A new/changed button is returned. """ l_json = json_tools.decode_json_unicode(p_json) l_delete = l_json['Delete'] l_ix = int(l_json['Key']) if l_delete: try: del self.m_pyhouse_obj.House.Lighting.Buttons[l_ix] except AttributeError: LOG.error("Failed to delete - JSON: {}".format(l_json)) return l_obj = lighting_buttons.ButtonData() try: l_obj = self.m_pyhouse_obj.House.Lighting.Buttons[l_ix] except KeyError: LOG.warning('Creating a new button {}'.format(l_ix)) # web_utils.get_base_info(l_obj, l_json) l_obj.Comment = l_json['Comment'] l_obj.DeviceType = 1 l_obj.DeviceSubType = 1 l_obj.DeviceFamily = l_json['DeviceFamily'] web_family.get_family_json_data(l_obj, l_json) web_utils.get_room_info(l_obj, l_json) self.m_pyhouse_obj.House.Lighting.Buttons[l_obj.Key] = l_obj LOG.info('Button Added - {}'.format(l_obj.Name))
def saveLightData(self, p_json): """A new/changed light is returned. Process it and update the internal data via light_xxxx.py """ l_json = json_tools.decode_json_unicode(p_json) l_delete = l_json['Delete'] l_ix = int(l_json['Key']) if l_delete: try: del self.m_pyhouse_obj.House.Lighting.Lights[l_ix] except AttributeError: LOG.error("web_lights - Failed to delete - JSON: {}".format(l_json)) return l_obj = lighting_lights.LightData() try: l_obj = self.m_pyhouse_obj.House.Lighting.Lights[l_ix] except KeyError: l_obj = lighting_lights.LightData() LOG.warning('Creating a new light {}'.format(l_ix)) # LOG.debug(PrettyFormatAny.form(l_json, 'json')) web_utils.get_base_info(l_obj, l_json) l_obj.Comment = l_json['Comment'] l_obj.IsDimmable = l_json['IsDimmable'] l_obj.DeviceFamily = l_json['DeviceFamily'] l_obj.DeviceType = 1 l_obj.DeviceSubType = 3 web_family.get_family_json_data(l_obj, l_json) web_utils.get_room_info(l_obj, l_json) self.m_pyhouse_obj.House.Lighting.Lights[l_ix] = l_obj LOG.info('Light Added - {}'.format(l_obj.Name))
def saveScheduleData(self, p_json): """A new/changed schedule is returned. Process it and update the internal data via schedule.py """ l_json = json_tools.decode_json_unicode(p_json) l_delete = l_json['Delete'] l_schedule_ix = int(l_json['Key']) if l_delete: try: del self.m_pyhouse_obj.House.Schedules[l_schedule_ix] except AttributeError as e: LOG.warning('Failed to delete schedule: {}, ix:{}').format( e, l_schedule_ix) return try: l_obj = self.m_pyhouse_obj.House.Schedules[l_schedule_ix] except KeyError: l_obj = ScheduleBaseData() l_obj.Name = l_json['Name'] l_obj.Active = l_json['Active'] l_obj.Key = l_schedule_ix # l_obj.ScheduleType = l_json['ScheduleType'] l_obj.Time = l_json['Time'] l_obj.DayOfWeek = l_json['DayOfWeek'] l_obj.ScheduleMode = l_json['ScheduleMode'] # if l_obj.ScheduleType == 'Lighting': l_obj = self._save_light(l_obj, l_json) elif l_obj.ScheduleType == 'Irrigation': l_obj = self._save_irrigation(l_obj, l_json) # l_obj._DeleteFlag = l_json['Delete'] self.m_pyhouse_obj.House.Schedules[l_schedule_ix] = l_obj self.m_pyhouse_obj._APIs.House.ScheduleAPI.RestartSchedule()
def test_02_Decode(self): # l_json = json_tools.encode_json(self.m_pyhouse_obj.Computer) l_json = json_tools.encode_json(MSG_DICT) print(PrettyFormatAny.form(l_json, 'A1-02-A - Encoded Info')) l_dict = json_tools.decode_json_unicode(l_json) print(l_dict) print(PrettyFormatAny.form(l_dict, 'A1-02-B - Decoded Info'))
def saveControllerInformation(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_ix = int(l_json['Key']) l_delete = l_json['Delete'] if l_delete: try: del self.m_pyhouse_obj.House.Lighting.Controllers[l_ix] except AttributeError: LOG.error( "web_controllers - Failed to delete - JSON: {}".FORMAT( l_json)) return l_obj = ControllerInformation() try: l_obj = self.m_pyhouse_obj.House.Lighting.Controllers[l_ix] except KeyError: LOG.warning('Creating a new controller {}'.format(l_ix)) web_utils.get_base_info(l_obj, l_json) l_obj.Comment = l_json['Comment'] l_obj.DeviceFamily = l_json['DeviceFamily'] l_obj.DeviceType = 1 l_obj.DeviceSubType = 2 l_obj.InterfaceType = l_json['InterfaceType'] l_obj.Port = l_json['Port'] if l_obj.InterfaceType == 'Serial': l_obj.BaudRate = l_json['BaudRate'] web_family.get_family_json_data(l_obj, l_json) web_utils.get_room_info(l_obj, l_json) self.m_pyhouse_obj.House.Lighting.Controllers[l_ix] = l_obj LOG.info('Controller Added - {}'.format(l_obj.Name))
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_ix = int(l_json['Key']) l_delete = l_json['Delete'] if l_delete: try: del self.m_pyhouse_obj.House.Lighting.Controllers[l_ix] except AttributeError: LOG.error("web_controllers - Failed to delete - JSON: {}".FORMAT(l_json)) return l_obj = ControllerData() try: l_obj = self.m_pyhouse_obj.House.Lighting.Controllers[l_ix] except KeyError: LOG.warning('Creating a new controller {}'.format(l_ix)) web_utils.get_base_info(l_obj, l_json) l_obj.Comment = l_json['Comment'] l_obj.DeviceFamily = l_json['DeviceFamily'] l_obj.DeviceType = 1 l_obj.DeviceSubType = 2 l_obj.InterfaceType = l_json['InterfaceType'] l_obj.Port = l_json['Port'] if l_obj.InterfaceType == 'Serial': l_obj.BaudRate = l_json['BaudRate'] web_family.get_family_json_data(l_obj, l_json) web_utils.get_room_info(l_obj, l_json) self.m_pyhouse_obj.House.Lighting.Controllers[l_ix] = l_obj LOG.info('Controller Added - {}'.format(l_obj.Name))
def saveWebData(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) l_obj = WebData() l_obj.Port = l_json['Port'] self.m_pyhouse_obj.APIs.Computer.WebAPI.SaveXml(l_obj)
def saveThermostatsData(self, p_json): """Thermostat data is returned, so update the info. """ l_json = json_tools.decode_json_unicode(p_json) l_delete = l_json['Delete'] l_ix = int(l_json['Key']) if l_delete: try: del self.m_pyhouse_obj.House.Hvac.Thermostats[l_ix] except AttributeError: LOG.error("web_thermostats - Failed to delete - JSON: {}".format(l_json)) return try: l_obj = self.m_pyhouse_obj.House.Hvac.Thermostats[l_ix] except KeyError: LOG.warning('Creating a new Thermostat for Key:{}'.format(l_ix)) l_obj = ThermostatData() # web_utils.get_base_info(l_obj, l_json) l_obj.Comment = l_json['Comment'] l_obj.CoolSetPoint = l_json['CoolSetPoint'] l_obj.CurrentTemperature = 0 l_obj.DeviceFamily = l_json['DeviceFamily'] l_obj.DeviceSubType = 1 l_obj.DeviceType = 2 l_obj.HeatSetPoint = l_json['HeatSetPoint'] l_obj.ThermostatMode = 'Auto' # Cool | Heat | Auto | EHeat l_obj.ThermostatScale = 'F' # F | C l_obj.ThermostatStatus = 'Off' web_family.get_family_json_data(l_obj, l_json) web_utils.get_room_info(l_obj, l_json) self.m_pyhouse_obj.House.Hvac.Thermostats[l_ix] = l_obj # Put into internal data store LOG.info('Thermostat Added - {}'.format(l_obj.Name))
def saveMotionSensorData(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_ix = int(l_json['Key']) l_delete = l_json['Delete'] if l_delete: try: del self.m_pyhouse_obj.House.Security.MotionSensors[l_ix] except AttributeError: LOG.error("web_controllers - Failed to delete - JSON: {}".FORMAT(l_json)) return try: l_obj = self.m_pyhouse_obj.House.Security.MotionSensors[l_ix] except KeyError: l_obj = MotionSensorData() web_utils.get_base_info(l_obj, l_json) l_obj.Comment = l_json['Comment'] l_obj.DeviceFamily = l_json['DeviceFamily'] l_obj.DeviceType = 3 l_obj.DeviceSubType = 2 l_obj.Status = l_json['Status'] web_family.get_family_json_data(l_obj, l_json) web_utils.get_room_info(l_obj, l_json) self.m_pyhouse_obj.House.Security.MotionSensors[l_ix] = l_obj LOG.info('Motion Sensor Added - {}\n{}'.format(l_obj.Name, l_json))
def saveHouseData(self, p_json): """House data has been returned, so update the house info. """ l_json = json_tools.decode_json_unicode(p_json) LOG.info('Update House info - {}'.format(l_json)) l_delete = l_json['Delete'] if l_delete: try: del self.m_pyhouse_obj.House except AttributeError: LOG.error("Failed to delete - JSON: {}".format(l_json)) return self.m_pyhouse_obj.House.Name = l_json['Name'] self.m_pyhouse_obj.House.Key = int(l_json['Key']) self.m_pyhouse_obj.House.Active = True self.m_pyhouse_obj.House.UUID l_obj = self.m_pyhouse_obj.House.Location l_obj.Street = l_json['Location']['Street'] l_obj.City = l_json['Location']['City'] l_obj.State = l_json['Location']['State'] l_obj.ZipCode = l_json['Location']['ZipCode'] # l_obj.Region = l_json['Location']['Region'] l_obj.Phone = l_json['Location']['Phone'] l_obj.Latitude = l_json['Location']['Latitude'] l_obj.Longitude = l_json['Location']['Longitude'] l_obj.Elevation = l_json['Location']['Elevation'] l_obj.TimeZoneName = l_json['Location']['TimeZoneName']
def saveScheduleData(self, p_json): """A new/changed schedule is returned. Process it and update the internal data via schedule.py """ l_json = json_tools.decode_json_unicode(p_json) l_delete = l_json['Delete'] l_schedule_ix = int(l_json['Key']) if l_delete: try: del self.m_pyhouse_obj.House.Schedules[l_schedule_ix] except AttributeError as e: LOG.warning('Failed to delete schedule: {}, ix:{}').format(e, l_schedule_ix) return try: l_obj = self.m_pyhouse_obj.House.Schedules[l_schedule_ix] except KeyError: l_obj = ScheduleBaseData() l_obj.Name = l_json['Name'] l_obj.Active = l_json['Active'] l_obj.Key = l_schedule_ix # l_obj.ScheduleType = l_json['ScheduleType'] l_obj.Time = l_json['Time'] l_obj.DOW = l_json['DOW'] l_obj.ScheduleMode = l_json['ScheduleMode'] # if l_obj.ScheduleType == 'Lighting': l_obj = self._save_light(l_obj, l_json) elif l_obj.ScheduleType == 'Irrigation': l_obj = self._save_irrigation(l_obj, l_json) # l_obj._DeleteFlag = l_json['Delete'] self.m_pyhouse_obj.House.Schedules[l_schedule_ix] = l_obj self.m_pyhouse_obj.APIs.House.ScheduleAPI.RestartSchedule()
def saveMqttData(self, p_json): """Mqtt data is returned, so update the info. """ l_json = json_tools.decode_json_unicode(p_json) l_delete = l_json['Delete'] l_ix = int(l_json['Key']) if l_delete: try: del self.m_pyhouse_obj.Computer.Mqtt.Brokers except AttributeError: LOG.error("web_mqtt - Failed to delete - JSON: {}".format(l_json)) return try: l_obj = self.m_pyhouse_obj.Computer.Mqtt.Brokers[l_ix] except KeyError: LOG.warning('Creating a new Mqtt Broker Key: {}'.format(l_ix)) l_obj = MqttBrokerData() # LOG.info('JSON {}'.format(l_json)) l_obj.Name = l_json['Name'] l_obj.Active = l_json['Active'] l_obj.Key = l_ix l_obj.UUID = Uuid.make_valid(l_json['UUID']) l_obj.BrokerAddress = l_json['BrokerAddress'] l_obj.BrokerPort = l_json['BrokerPort'] self.m_pyhouse_obj.Computer.Mqtt.Brokers[l_obj.Key] = l_obj
def test_01_CreateJson(self): """ Create a JSON object for Rooms. """ self.m_pyhouse_obj.House.Rooms = l_rooms = self.m_api(self.m_pyhouse_obj).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 test_02_Who(self): l_node = nodesXml._read_one_node_xml(self.m_xml.node) # print(PrettyFormatAny.form(l_node, 'C1-01-A - Node')) l_json = json_tools.encode_json(l_node) # print(PrettyFormatAny.form(l_json, 'C1-01-B - 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, 'C1-01-C - PyHouse')) self.assertNotEqual(self.m_pyhouse_obj.Xml, TESTING_NODES_NODE_NAME_0)
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 test_02_Who(self): l_node = nodesXml._read_one_node_xml(self.m_xml.node) # print(PrettyFormatAny.form(l_node, 'C1-01-A - Node')) l_json = json_tools.encode_json(l_node) # print(PrettyFormatAny.form(l_json, 'C1-01-B - 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, 'C1-01-C - 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, 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_Create(self): """ Create a JSON object for Location.5 """ l_house = houseXml.read_house_xml(self.m_pyhouse_obj) # print(PrettyFormatAny.form(l_house, 'J1-01-A - House')) l_json = json_tools.encode_json(l_house) # print('J1-01-B - JSON: {}'.format(l_json)) l_decoded = json_tools.decode_json_unicode(l_json) # print(PrettyFormatAny.form(l_decoded, 'J1-01-C - Decoded')) self.assertEqual(l_decoded['Name'], TESTING_HOUSE_NAME)
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 startIrrigation(self, zone, minutes): self.logger.debug("Irrigation start requested for zone " + str(zone) + " for duration " + str(minutes)) resp = self.request("ManuallyRunStation", zone, minutes) if (resp != ""): jsonresult = json_tools.decode_json_unicode(resp) if (jsonresult["result"]["data"][:2] == "01"): self.logger.debug("Irrigation request acknowledged") return 1 else: self.logger.warning("Irrigation request NOT acknowledged") return -1 self.logger.warning("Request resulted in no response") return 0
def stopIrrigation(self): self.logger.debug("Irrigation stop requested") resp = self.request("StopIrrigation") if (resp != ""): jsonresult = json_tools.decode_json_unicode(resp) if (jsonresult["result"]["data"][:2] == "01"): self.logger.debug("Irrigation stop request acknowledged") return 1 else: self.logger.warning("Irrigation stop request NOT acknowledged") return -1 self.logger.warning("Request resulted in no response") return 0
def saveInternetData(self, p_json): """Internet data is returned, so update the computer info. """ l_json = json_tools.decode_json_unicode(p_json) # l_ix = int(l_json['Key']) try: l_obj = self.m_pyhouse_obj.Computer.InternetConnection except KeyError: l_obj = InternetConnectionData() l_obj.LastChanged = datetime.now() l_obj.LocateUrls = l_json['LocateUrls'] l_obj.UpdateUrls = l_json['UpdateUrls'] l_obj.UpdateInterval = l_json['UpdateInterval'] self.m_pyhouse_obj.Computer.InternetConnection = l_obj
def saveInternetData(self, p_json): """Internet data is returned, so update the computer info. """ l_json = json_tools.decode_json_unicode(p_json) # l_ix = int(l_json['Key']) try: l_obj = self.m_pyhouse_obj.Computer.InternetConnection except KeyError: l_obj = InternetConnectionInformation() l_obj.LastChanged = datetime.now() l_obj.LocateUrls = l_json['LocateUrls'] l_obj.UpdateUrls = l_json['UpdateUrls'] l_obj.UpdateInterval = l_json['UpdateInterval'] self.m_pyhouse_obj.Computer.InternetConnection = l_obj
def saveControlLightData(self, p_json): """A changed Light is returned. Process it and update the light level """ l_json = json_tools.decode_json_unicode(p_json) l_light_ix = int(l_json['Key']) l_light_obj = LightData() l_light_obj.Name = l_json['Name'] l_light_obj.Key = l_light_ix l_light_obj.BrightnessPct = l_brightness = l_json['BrightnessPct'] l_light_obj.UUID = l_json['UUID'] LOG.info('Control Light via Web - Change {} device to Brightness {}'.format(l_light_obj.Name, l_light_obj.BrightnessPct)) l_topic = 'lighting/web/{}/control'.format(l_light_obj.Name) self.m_pyhouse_obj.APIs.Computer.MqttAPI.MqttPublish(l_topic, l_light_obj) # lighting/web/{}/control self.m_pyhouse_obj.APIs.House.LightingAPI.ChangeLight(l_light_obj, 'web', l_brightness)
def saveIrrigationData(self, p_json): """Internet data is returned, so update the computer info. """ l_json = json_tools.decode_json_unicode(p_json) l_ix = int(l_json['Key']) _l_system = l_json['Name'] try: l_obj = self.m_pyhouse_obj.House.Irrigation[l_ix] except KeyError: l_obj = IrrigationData() l_obj.DynDns = {} l_obj.Name = l_json['Name'] l_obj.Key = 0 l_obj.Active = True self.m_pyhouse_obj.House.Irrigation[l_ix] = l_obj
def currentRainSensorState(self): self.logger.debug("Requesting current Rain Sensor State") resp = self.request("CurrentRainSensorState") if (resp != ""): jsonresult = json_tools.decode_json_unicode(resp) if (jsonresult["result"]["data"][:2] == "BE"): val = jsonresult["result"]["data"][3:4] self.logger.debug("Current rainsensor state: " + str(val)) return str(val) else: self.logger.warning("Status request failed with wrong respone") return -1 else: self.logger.warning("Request resulted in no response") return -1
def saveIrrigationData(self, p_json): """Internet data is returned, so update the computer info. """ l_json = json_tools.decode_json_unicode(p_json) l_ix = int(l_json['Key']) _l_system = l_json['Name'] try: l_obj = self.m_pyhouse_obj.House.Irrigation[l_ix] except KeyError: l_obj = IrrigationData() l_obj.DynDns = {} l_obj.Name = l_json['Name'] l_obj.Key = 0 l_obj.Active = True self.m_pyhouse_obj.House.Irrigation[l_ix] = l_obj
def doLogin(self, p_json): """ This will receive json of username, password when the user clicks on the login button in the browser. First, we validate the user If valid, display the user and then the root menu. If not - allow the user to retry the login. also allow user to check the change button and apply the change after logging in the user. @param p_json: is the username and password passed back by the client. """ LOG.info("doLogin called {}.".format(PrettyFormatAny.form(p_json, 'Login From Browser'))) l_obj = json_tools.decode_json_unicode(p_json) l_login_obj = self.validate_user(l_obj) l_json = json_tools.encode_json(l_login_obj) return l_json
def currentIrrigation(self): self.logger.debug("Requesting current Irrigation station") resp = self.request("CurrentStationsActive") if (resp != ""): jsonresult = json_tools.decode_json_unicode(resp) if (jsonresult["result"]["data"][:2] == "BF"): val = jsonresult["result"]["data"][4:8] if (int(val[:2]) != 0): self.logger.debug("Status request acknowledged") return round(math.log(int(val[:2]), 2) + 1) else: return 0 else: self.logger.warning("Status request NOT acknowledged") return -1 else: self.logger.warning("Request resulted in no response") return -1
def saveControlLightData(self, p_json): """A changed Light is returned. Process it and update the light level """ l_json = json_tools.decode_json_unicode(p_json) l_light_ix = int(l_json['Key']) l_light_obj = LightData() l_light_obj.Name = l_json['Name'] l_light_obj.Key = l_light_ix l_light_obj.BrightnessPct = l_brightness = l_json['BrightnessPct'] l_light_obj.UUID = l_json['UUID'] LOG.info( 'Control Light via Web - Change {} device to Brightness {}'.format( l_light_obj.Name, l_light_obj.BrightnessPct)) l_topic = 'house/lighting/web/{}/control'.format(l_light_obj.Name) self.m_pyhouse_obj._APIs.Core.MqttAPI.MqttPublish( l_topic, l_light_obj) # lighting/web/{}/control self.m_pyhouse_obj._APIs.House.LightingAPI.AbstractControlLight( l_light_obj, 'web', l_brightness)
def saveNodeData(self, p_json): """A new/changed/deleted node is returned. Process it and update the internal data. """ l_json = json_tools.decode_json_unicode(p_json) l_ix = int(l_json['Key']) l_delete = l_json['Delete'] if l_delete: try: del self.m_pyhouse_obj.Computer.Nodes[l_ix] except AttributeError: LOG.error("web_nodes - Failed to delete - JSON: {}".format(l_json)) return try: l_obj = self.m_pyhouse_obj.Computer.Nodes[l_ix] except KeyError: l_obj = NodeData() l_obj.Name = l_json['Name'] l_obj.Active = l_json['Active'] l_obj.Key = l_ix l_obj.Comment = l_json['Comment'] self.m_pyhouse_obj.Computer.Nodes[l_ix] = l_obj
def saveNodeData(self, p_json): """A new/changed/deleted node is returned. Process it and update the internal data. """ l_json = json_tools.decode_json_unicode(p_json) l_ix = int(l_json['Key']) l_delete = l_json['Delete'] if l_delete: try: del self.m_pyhouse_obj.Computer.Nodes[l_ix] except AttributeError: LOG.error( "web_nodes - Failed to delete - JSON: {}".format(l_json)) return try: l_obj = self.m_pyhouse_obj.Computer.Nodes[l_ix] except KeyError: l_obj = NodeInformation() l_obj.Name = l_json['Name'] l_obj.Active = l_json['Active'] l_obj.Key = l_ix l_obj.Comment = l_json['Comment'] self.m_pyhouse_obj.Computer.Nodes[l_ix] = l_obj
def _event_publish(self, packet, qos, dup, retain): """ Receive a "Published" message Here we get a published message from the broker. Extract the parts of the packet. @param packet: is a bytearray containing the variable header and payload combined. """ # Extract the topic portion of the packet. l_topic = EncodeDecode._decodeString(packet) packet = packet[len(l_topic) + 2:] # Extract the message ID if appropriate messageId = None if qos > 0: messageId = EncodeDecode._decodeValue(packet[:2]) packet = packet[2:] LOG.debug('Publish MsgID:{}'.format(messageId)) # Extract whatever remains as the message l_json = EncodeDecode._get_string(packet) # l_json = packet.decode('utf-8') l_message = json_tools.decode_json_unicode(l_json) # l_topic is a string # l_message is a string self.publishReceived(l_topic, l_message, qos, dup, retain, messageId)
def _event_publish(self, packet, qos, dup, retain): """ Receive a "Published" message Here we get a published message from the broker. Extract the parts of the packet. @param packet: is a bytearray containing the variable header and payload combined. """ # Extract the topic portion of the packet. l_topic = EncodeDecode._decodeString(packet) packet = packet[len(l_topic) + 2:] # Extract the message ID if appropriate messageId = None if qos > 0: messageId = EncodeDecode._decodeValue(packet[:2]) packet = packet[2:] LOG.debug('Publish MsgID:{}'.format(messageId)) # Extract whatever remains as the message l_json = EncodeDecode._get_string(packet) # l_json = packet.decode('utf-8') l_message = json_tools.decode_json_unicode(l_json) # l_topic is a string # l_message is a string self.publishReceived(l_topic, l_message, qos, dup, retain, messageId)
def putUsersData(self, p_json): """A new/changed/deleted user is returned. Process it and update the internal data. """ l_json = json_tools.decode_json_unicode(p_json) l_ix = int(l_json['Key']) l_delete = l_json['Delete'] if l_delete: try: del self.m_pyhouse_obj.Computer.Web.Logins[l_ix] except AttributeError: LOG.error("Failed to delete user - JSON: {}".format(l_json)) return try: l_obj = self.m_pyhouse_obj.Computer.Web.Logins[l_ix] except KeyError: l_obj = LoginData() l_obj.Name = l_json['Name'] l_obj.Active = l_json['Active'] l_obj.Key = l_ix l_obj.LoginFullName = l_json['FullName'] l_obj.LoginPasswordCurrent = l_json['Password_1'] l_obj.LoginRole = l_json['Role'] self.m_pyhouse_obj.Computer.Web.Logins[l_ix] = l_obj
def putUsersData(self, p_json): """A new/changed/deleted user is returned. Process it and update the internal data. """ l_json = json_tools.decode_json_unicode(p_json) l_ix = int(l_json['Key']) l_delete = l_json['Delete'] if l_delete: try: del self.m_pyhouse_obj.Computer.Web.Logins[l_ix] except AttributeError: LOG.error("Failed to delete user - JSON: {}".format(l_json)) return try: l_obj = self.m_pyhouse_obj.Computer.Web.Logins[l_ix] except KeyError: l_obj = LoginData() l_obj.Name = l_json['Name'] l_obj.Active = l_json['Active'] l_obj.Key = l_ix l_obj.LoginFullName = l_json['FullName'] l_obj.LoginPasswordCurrent = l_json['Password_1'] l_obj.LoginRole = l_json['Role'] self.m_pyhouse_obj.Computer.Web.Logins[l_ix] = l_obj
def saveThermostatsData(self, p_json): """Thermostat data is returned, so update the info. """ l_json = json_tools.decode_json_unicode(p_json) l_delete = l_json['Delete'] l_ix = int(l_json['Key']) if l_delete: try: del self.m_pyhouse_obj.House.Hvac.Thermostats[l_ix] except AttributeError: LOG.error( "web_thermostats - Failed to delete - JSON: {}".format( l_json)) return try: l_obj = self.m_pyhouse_obj.House.Hvac.Thermostats[l_ix] except KeyError: LOG.warning('Creating a new Thermostat for Key:{}'.format(l_ix)) l_obj = ThermostatData() # web_utils.get_base_info(l_obj, l_json) l_obj.Comment = l_json['Comment'] l_obj.CoolSetPoint = l_json['CoolSetPoint'] l_obj.CurrentTemperature = 0 l_obj.DeviceFamily = l_json['DeviceFamily'] l_obj.DeviceSubType = 1 l_obj.DeviceType = 2 l_obj.HeatSetPoint = l_json['HeatSetPoint'] l_obj.ThermostatMode = 'Auto' # Cool | Heat | Auto | EHeat l_obj.ThermostatScale = 'F' # F | C l_obj.ThermostatStatus = 'Off' web_family.get_family_json_data(l_obj, l_json) web_utils.get_room_info(l_obj, l_json) self.m_pyhouse_obj.House.Hvac.Thermostats[ l_ix] = l_obj # Put into internal data store LOG.info('Thermostat Added - {}'.format(l_obj.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, 'C1-02-A - JSON', 40)) self.assertEqual(l_test['Families'], VALID_FAMILIES)
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_03_ByteArray(self): print(PrettyFormatAny.form(MSG_JSON, 'A1-03-A - byte array')) l_json = MSG_JSON.decode('utf-8') print(PrettyFormatAny.form(l_json, 'A1-03-B - Ascii String', 5000)) l_dict = json_tools.decode_json_unicode(l_json) print(PrettyFormatAny.form(l_dict, 'A1-03-C - Decoded Info'))
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, 'C1-02-A - JSON', 40)) self.assertEqual(l_test['Families'], VALID_FAMILIES)
def __init__ (self): # Load JSON with commands self.rainbirdCommands = json_tools.decode_json_unicode(self.jsoncommands) self.rainbirdEncryption = self.RainbirdEncryption() self.rainbirdPassword = None self.rainbirdserver = None
def test_03_ByteArray(self): print(PrettyFormatAny.form(MSG_JSON, 'A1-03-A - byte array')) l_json = MSG_JSON.decode('utf-8') print(PrettyFormatAny.form(l_json, 'A1-03-B - Ascii String', 5000)) l_dict = json_tools.decode_json_unicode(l_json) print(PrettyFormatAny.form(l_dict, 'A1-03-C - Decoded Info'))
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 saveRoomData(self, p_json): """A new/changed/deleted room is returned. Process it and update the internal data. """ l_json = json_tools.decode_json_unicode(p_json) roomMaint().from_web(self.m_pyhouse_obj, l_json)