💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
本章主要实现智能设备管理系统中的电表管理功能,主要包括**电表详情页面展示**,**电笔用电记录展示**,**电表异常记录页面展示**等功能。 # 4.1 电表管理接口实现 本节将实现电表管理的基础功能,如:设置付费模式(set\_room\_pay\_type),获取电表详情(get\_elemeter\_info),获取电表用电记录(含公摊)(elemeter\_fetch\_power\_history\_with\_pool),获取电表操作记录(search_device_op_log), 获取电表异常记录(device_fetch_exceptions)。 具体实现:在apis文件夹下新建elemeter.py, vim elemeter.py. ``` ~~~ import requests def set_room_pay_type(access_token, uuid, pay_type): ''' 设置房间付费模式 :param access_token: 调用接口凭证 :param uuid: 电表设备uuid, 唯一 :param pay_type: 付费类型 1:预付费 2:后付费 ''' url = 'https://saas-openapi.dding.net/v2/set_room_pay_type' headers = { 'Content-type': 'Application/json', 'User-Agent': 'PostmanRuntime/7.13.0', } payload = { 'access_token': access_token, 'uuid': uuid, 'pay_type': pay_type } response = requests.request('POST', url, headers=headers, json=payload) return response.json() def get_elemeter_info(access_token, home_id, uuid): ''' 获取电表详情 :param access_token: 调用接口凭证 :param home_id: 电表所在房源id :param uuid: 电表设备的uuid ''' url = 'https://saas-openapi.dding.net/v2/get_elemeter_info' headers = { 'Content-type': 'Application/json', 'User-Agent': 'PostmanRuntime/7.13.0', } query_string = { 'access_token': access_token, 'home_id': home_id, 'uuid': uuid } response = requests.request('GET', url, headers=headers, params=query_string) return response.json() def elemeter_fetch_power_history_with_pool(access_token, home_id, uuid): ''' 获取电表用电记录(含公摊) :param access_token: 调用接口凭证 :param home_id: 电表所在房源id :param uuid: 电表设备的uuid ''' url = 'https://saas-openapi.dding.net/v2/elemeter_fetch_power_history_with_pool' headers = { 'Content-type': 'Application/json', 'User-Agent': 'PostmanRuntime/7.13.0', } query_string = { 'access_token': access_token, 'home_id': home_id, 'uuid': uuid } response = requests.request('GET', url, headers=headers, params=query_string) return response.json() def search_elemeter_op_log(access_token, uuid): ''' 查询电表操作记录 :param access_token: :param uuid: :return: ''' url = 'https://saas-openapi.dding.net/v2/search_device_op_log' headers = { 'Content-type': 'Application/json', 'User-Agent': 'PostmanRuntime/7.13.0', } query_string = { 'access_token': access_token, 'uuid': uuid } response = requests.request('GET', url, headers=headers, params=query_string) return response.json() def elemeter_fetch_exceptions(access_token, uuid): ''' 电表异常记录获取 :param access_token: 接口调用凭证 :param uuid: 电表设备uuid ''' url = 'https://saas-openapi.dding.net/v2/device_fetch_exceptions' headers = { 'Content-type': 'Application/json', 'User-Agent': 'PostmanRuntime/7.13.0', } query_string = { 'access_token': access_token, 'uuid': uuid } response = requests.request('GET', url, headers=headers, params=query_string) return response.json() ~~~ ``` 实现接口函数后,新建elemeter_model.py, 实现Elemeter类的定义以及相关实例方法。vim elemeter_model.py ``` from apis.elemeter import set_room_pay_type, get_elemeter_info, elemeter_fetch_exceptions, \ elemeter_fetch_power_history_with_pool, search_elemeter_op_log class Elemeter: def __init__(self, access_token, uuid): self.access_token = access_token self.uuid = uuid def set_room_pay_type(self, pay_type): res = set_room_pay_type(self.access_token, self.uuid, pay_type) return res def get_elemeter_info(self, home_id): res = get_elemeter_info(self.access_token, home_id, self.uuid) return res def elemeter_fetch_power_history_with_pool(self, home_id): res = elemeter_fetch_power_history_with_pool(self.access_token, home_id, self.uuid) return res def search_elemeter_op_log(self): res = search_elemeter_op_log(self.access_token, self.uuid) return res def elemeter_fetch_exceptions(self): res = elemeter_fetch_exceptions(self.access_token, self.uuid) return res ``` # 4.2 电表详情页面展示 这一节实现电表详情页面展示,vim main.py ``` from elemeter_model import Elemeter def main(): access_token = 'xxxxxxxxxxx' #请填写商户账号的access_token uuid = 'f0a0c5abd9769ebe2efae3e27a5fbea7' home_id = 'cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a' elemeter = Elemeter(access_token, uuid) res = elemeter.get_elemeter_info(home_id) print(res) if __name__ == '__main__': main() ``` 先实例化电表,然后使用get_elemeter_info实例化方法,获取该电表的详情。 返回结果如下:包括电表版本信息,信号值等等基本信息。可以将其展示给前端。 ``` { "ReqID": "1SSFPZJWbyf", "ErrNo": 0, "ErrMsg": "成功", "sn": "190215103584", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7", "versions": { "elemeter_potocol_version": "07", "elemeter_potocol_type": "01" }, "name": "北川电表", "power": -1, "status": 1, "model": "DDSU1225", "model_name": "A1P", "onoff_line": 1, "onoff_time": 1567395901032, "time": 1563628490452, "bind_time": 1563628490641, "overdraft": 0, "overdraft_time": 1567408957336, "capacity": 14.52, "capacity_time": 1567408957336, "consume_amount": 314.34, "consume_amount_time": 1567407600000, "power_total": 0, "power_total_time": 1567408957336, "enable_state": 1, "enable_state_time": 1564632109395, "charge_stage": 3, "overdraft_stage": 3, "capacity_stage": 3, "trans_status": 1, "trans_status_time": 1566478811023, "switch_stage": 3, "switch_stage_time": 1567408957336, "control_switch": 1, "syn_stage_time": 1567408957336, "syn_stage": 3, "reset_stage_time": 1564648064289, "reset_stage": 3, "elecollector_uuid": "bc516a8cb656de8a33aa2c6b4bc8ce70" } ``` 电表详情页面展示效果如下: ![](https://img.kancloud.cn/10/9f/109f2ae94c1bcef7a796e83b7f40e076_2310x1188.png) # 4.3 用电记录页面展示 用电记录展示是电表智能管理的一项基本功能,通过elemeter\_fetch\_power\_history\_with\_pool接口可以获取该电表历史用电记录。 具体的代码示例:vim main.py ``` from elemeter_model import Elemeter def main(): access_token = 'xxxxxxxxxxxxxxxx' #请填写商户自己的access_token uuid = 'f0a0c5abd9769ebe2efae3e27a5fbea7' home_id = 'cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a' elemeter = Elemeter(access_token, uuid) # res = elemeter.get_elemeter_info(home_id) res = elemeter.elemeter_fetch_power_history_with_pool(home_id) print(res) if __name__ == '__main__': main() ``` 返回结果如下:history字段中含有该电表的用电量历史记录。使用history字段中的两条记录consume_amount相减,便可以计算出每小时的房间用电量;使用history字段中第一条记录,用total\_amount - consume\_amount + charge\_pooling\_amount - pooling\_amount便可以计算该电表当前的剩余电量。 ``` { "ReqID": "1SSIpPIQpOB", "ErrNo": 0, "ErrMsg": "", "history": [ { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.34, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567407600000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.34, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567404000000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.34, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567400400000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.33, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567396800000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.33, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567393200000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.33, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567389600000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.32, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567386000000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.32, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567382400000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.32, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567378800000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.31, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567375200000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.31, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567371600000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.31, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567368000000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.3, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567364400000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.3, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567360800000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.3, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567357200000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "home_id": "5d2949817c83c22451d14263", "room_id": "5d2949f7039d2a2452555664", "consume_amount": 314.29, "pooling_amount": 0, "total_amount": 0, "charge_pooling_amount": 0, "time": 1567353600000, "date": "2019-09-02", "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" } ] } ``` 房间用电记录展示效果如下: ![](https://img.kancloud.cn/6a/58/6a58642b72ed1bee9768ed0b6800c77d_2312x1240.png) # 4.4 异常记录页面展示 本节演示获取电表异常记录,vim main.py ``` from elemeter_model import Elemeter def main(): access_token = 'xxxxxxxxxxxxxxxxxxx' #请填写商户自己的access_token uuid = 'f0a0c5abd9769ebe2efae3e27a5fbea7' home_id = 'cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a' elemeter = Elemeter(access_token, uuid) # res = elemeter.get_elemeter_info(home_id) # res = elemeter.elemeter_fetch_power_history_with_pool(home_id) res = elemeter.elemeter_fetch_exceptions() print(res) if __name__ == '__main__': main() ``` 返回的结果如下:device_exceptions字段中含有该电表的异常记录,可以将其发送给前端进行异常记录页面展示。 返回结果如下:device_exceptions字段里面每一个元素都是一条异常记录。 ``` ~~~ { "ReqID": "1SSQr1vNHoI", "ErrNo": 0, "ErrMsg": "", "device_exceptions": [ { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1567128030000, "type": 1007, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1567042655000, "type": 1006, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1566634238000, "type": 1007, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1566633785000, "type": 1006, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1566470154000, "type": 1007, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1566469791000, "type": 1006, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1566465006000, "type": 1007, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1566464914000, "type": 1006, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1566311201000, "type": 1007, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1566309285000, "type": 1006, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1564632109000, "type": 2014, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1564560012000, "type": 2003, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1564557239000, "type": 1007, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" }, { "client_id": 100041, "device_type": "elemeter", "home_id": "cadc71cb-6dec-4155-8ddb-4e99ae8f9f5a", "time": 1563629395000, "type": 1006, "uuid": "f0a0c5abd9769ebe2efae3e27a5fbea7" } ] } ``` 异常页面展示效果如下: ![](https://img.kancloud.cn/55/78/55783789a00c5f0170df8a5efb6eb1c1_2272x1390.png) # 4.5 小结 本章实现了智能电表管理常见的功能,当然还有其他的电表管理接口可以使用。为避免重复在此不一一赘述,开发者可根据需求自行对其进行组合。