💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
云丁Saas系统openAPI提供了丰富的电表管理接口,本章介绍常用的接口,并且使用它们实现常见的电表管理功能。如:电表详情页面展示,计算电笔当前剩余电量,电表异常记录页面展示等。 # 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" } ``` # 4.3 计算电表当前剩余电量 电费的计量涉及几个概念需要先了解: 电表的付费模式分为**预付费**和**后付费**。不同的付费模式所需要调用的接口也是不同的,需要根据自己公司的业务确定付费模式。 **预付费**: 即先付费后用电,主要涉及剩余电量计算等。 **后付费**: 先用电后结算,由于不涉及费控,其主要涉及电表用电量的获取等。 **公摊**:即将公共区间的用电量分摊给各房间用电量。 本节的示例采用预付费的方式。 当房间付费模式为预付费,并且开启开启公摊后,此时可通过elemeter\_fetch\_power\_history\_with\_pool接口获取用电记录。并且 剩余电量的计算公式为: left = total\_amount - consume\_amount + charge\_pooling\_amount - pooling\_amount。 total_amont : 房间充值电量 consume_amount:房间用电量 charge_pooling_amount:公摊充值电量 pooling_amount:公摊用电量 具体的代码示例: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字段中含有该电表的用电量历史记录,可以根据该数据进行房间用电量展示以及剩余电量的计算。 ``` { "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" } ] } ``` # 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字段中含有该电表的异常记录,可以将其发送给前端进行异常记录页面展示。 ``` ~~~ { "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" } ] } ``` # 4.5 小结 本章通过几个简单的例子说明了常见的电表管理功能的实现。云丁Saas系统openAPI关于电表管理提供了丰富的接口,其使用方法也大同小异。开发者可根据需求自行对其进行组合。