device.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter web
  5. name:device.py 设备相关
  6. author:rabin
  7. """
  8. from __load__ import *
  9. class index_path(Load):
  10. """
  11. @api {get} /device 获取网关首页信息
  12. @apiVersion 1.0.0
  13. @apiName getDevice
  14. @apiGroup Device
  15. @apiParam {String} id 网关id
  16. @apiSuccess {Object[]} setting 基本信息
  17. @apiSuccess {String} setting.name 站点名
  18. @apiSuccess {Number} setting.farm 当前的农场ID
  19. @apiSuccess {Object[]} info 网关状态基本信息
  20. @apiSuccess {String} info.id 网关ID
  21. @apiSuccess {String} info.name 网关名称
  22. @apiSuccess {Bool} info.status 网关状态
  23. @apiSuccess {Number} info.power 电量
  24. @apiSuccess {Object[]} device 设备数据
  25. @apiSuccess {Number} device.id 设备ID
  26. @apiSuccess {String} device.name 设备名称
  27. @apiSuccess {String} device.show 显示的数据
  28. @apiSuccess {String} device.unit 设备的单位
  29. @apiSuccess {Number} device.max 设备的上限
  30. @apiSuccess {Number} device.min 设备的下限
  31. @apiSuccess {Bool} device.status 设备状态
  32. @apiSuccess {Object[]} pic 图像数据
  33. @apiSuccess {Number} pic.id 图片ID
  34. @apiSuccess {String} pic.name 图片名称
  35. @apiSuccess {String} pic.show 图片地址
  36. @apiSuccess {String} pic.cdates 采集时间
  37. @apiSuccess {String} pic.cdate 采集时间戳
  38. @apiErrorExample 操作成功
  39. {"status": 1, "msg": "yes", "code": 0, "data": {"info": {"status": true, "hardware_id": 10086, "name": "\u9ed8\u8ba4\u7f51\u5173", "farm_id": 1, "cdate": 1504233947, "state": true, "power_num": "", "power_status": "", "id": "ce37636b-3ce7-5de2-9160-0f3283f7d392"}, "device": [{"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100009, "name": "\u6e7f\u5ea6#100009", "type_id": 2, "max": 0, "farm_id": 1, "cdate": 1504253837, "state": true, "num": 30, "status": true, "min": 0, "show": "30", "id": "6d8a4c0e-4f85-524a-87f8-382c333c44da", "unit": "%RH"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100000, "name": "\u6e7f\u5ea6#100000", "type_id": 2, "max": 0, "farm_id": 1, "cdate": 1504253836, "state": true, "num": 5, "status": true, "min": 0, "show": "5", "id": "d644b079-9d4d-521c-8e65-8940c96ba9a7", "unit": "%RH"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100001, "name": "\u6e7f\u5ea6#100001", "type_id": 2, "max": 0, "farm_id": 1, "cdate": 1504253835, "state": true, "num": 4, "status": true, "min": 0, "show": "4", "id": "dc24428c-eafc-5715-b7d1-f3222d28c3bb", "unit": "%RH"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100004, "name": "\u6e7f\u5ea6#100004", "type_id": 2, "max": 0, "farm_id": 1, "cdate": 1504253834, "state": true, "num": 17, "status": true, "min": 0, "show": "17", "id": "62199416-cca0-5114-b900-a10f51e7e447", "unit": "%RH"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100005, "name": "\u6e7f\u5ea6#100005", "type_id": 2, "max": 0, "farm_id": 1, "cdate": 1504253833, "state": true, "num": 7, "status": true, "min": 0, "show": "7", "id": "456daa98-a1f0-59e6-a337-7ca1fa7b2527", "unit": "%RH"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100003, "name": "\u6e7f\u5ea6#100003", "type_id": 2, "max": 0, "farm_id": 1, "cdate": 1504253832, "state": true, "num": 39, "status": true, "min": 0, "show": "39", "id": "11bd4ad1-b4ff-5b1f-948a-935d859b408c", "unit": "%RH"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100007, "name": "\u6e7f\u5ea6#100007", "type_id": 2, "max": 0, "farm_id": 1, "cdate": 1504253828, "state": true, "num": 43, "status": true, "min": 0, "show": "43", "id": "f8e3a42a-7fa3-575a-b99e-2dab4f992239", "unit": "%RH"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100010, "name": "\u6e7f\u5ea6#100010", "type_id": 2, "max": 0, "farm_id": 1, "cdate": 1504253827, "state": true, "num": 20, "status": true, "min": 0, "show": "20", "id": "138876d7-1710-56fa-824b-10402cba17f8", "unit": "%RH"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100002, "name": "\u6e7f\u5ea6#100002", "type_id": 2, "max": 0, "farm_id": 1, "cdate": 1504253826, "state": true, "num": 43, "status": true, "min": 0, "show": "43", "id": "eecfe61f-de83-588f-8cbd-41519d4ebef3", "unit": "%RH"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100006, "name": "\u6e7f\u5ea6#100006", "type_id": 2, "max": 0, "farm_id": 1, "cdate": 1504253818, "state": true, "num": 4, "status": true, "min": 0, "show": "4", "id": "78bd90ac-6af3-5091-83e5-f3397063f743", "unit": "%RH"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100008, "name": "\u6e7f\u5ea6#100008", "type_id": 2, "max": 0, "farm_id": 1, "cdate": 1504253815, "state": true, "num": 37, "status": true, "min": 0, "show": "37", "id": "5fda8633-aed8-5a3b-8175-2c61e7553d91", "unit": "%RH"}], "setting": {"farm": 1, "user": 0, "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "pic": [{"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 5, "hardware_id": 200001, "name": "pic.jpg", "type_id": null, "farm_id": 1, "cdate": 1504262668, "cdates": "2017-09-01 10:44:28", "state": true, "num": 0, "status": true, "show": "/camera/2017/09/01/01c323ac0208580601252e2ce9eb3286.jpg", "id": "2f612150-f670-54e9-9886-a044cfb18e20"}]}}
  40. @apiErrorExample 操作失败
  41. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  42. """
  43. @auth
  44. def get(self):
  45. id = self.input('id', None)
  46. if id:
  47. self.data['info'] = self.service('common').one('device_gateway', id=id)
  48. if not self.data['info']:
  49. self.out('no')
  50. return
  51. self.data['device'] = self.service('common').list('device_info', state=True, search={'hardware_type':2, 'gateway_id':id})
  52. if self.data['device']:
  53. for key, value in enumerate(self.data['device']):
  54. type = self.service('common').one('device_type', id=value['type_id'])
  55. self.data['device'][key]['unit'] = type['unit']
  56. set = self.service('common').one('device_set', device_id=value['id'])
  57. if set:
  58. self.data['device'][key]['max'] = set['max']
  59. self.data['device'][key]['min'] = set['min']
  60. else:
  61. self.data['device'][key]['max'] = 0
  62. self.data['device'][key]['min'] = 0
  63. self.data['pic'] = self.service('common').list('device_info', state=True, search={'hardware_type':6, 'gateway_id':id})
  64. if self.data['pic']:
  65. for key, value in enumerate(self.data['pic']):
  66. self.data['pic'][key]['cdates'] = Demeter.date(value['cdate'])
  67. self.out('yes', self.data)
  68. else:
  69. self.out('no')
  70. class stat_path(Load):
  71. """
  72. @api {get} /device/stat 获取数据统计
  73. @apiVersion 1.0.0
  74. @apiName getDeviceStat
  75. @apiGroup Device
  76. @apiParam {String} id 网关id
  77. @apiParam {String} device 设备id
  78. @apiParam {String} search_group 周期ID
  79. @apiParam {String} search_start 开始时间:2017-10-10 10:10:10
  80. @apiParam {String} search_end 结束时间:2017-10-10 10:10:10
  81. @apiSuccess {Object[]} setting 基本信息
  82. @apiSuccess {String} setting.name 站点名
  83. @apiSuccess {Number} setting.farm 当前的农场ID
  84. @apiSuccess {Object[]} info 网关状态基本信息
  85. @apiSuccess {String} info.id 网关ID
  86. @apiSuccess {String} info.name 网关名称
  87. @apiSuccess {Object[]} data 设备选择
  88. @apiSuccess {Number} data.id 设备ID
  89. @apiSuccess {String} data.name 设备名称
  90. @apiSuccess {Object[]} group 周期选择
  91. @apiSuccess {Number} group.id 周期ID
  92. @apiSuccess {String} group.name 周期名称
  93. @apiSuccess {Object[]} cur_device 当前选择的设备
  94. @apiSuccess {Number} cur_device.id 设备ID
  95. @apiSuccess {String} cur_device.name 设备名称
  96. @apiSuccess {Object[]} data 设备数据,两条线
  97. @apiSuccess {String} data.name 本条数据名
  98. @apiSuccess {Object[]} data.data 本条数据的具体数据
  99. @apiSuccess {String} data.data.time 数据时间
  100. @apiSuccess {String} data.data.value 数据具体的值
  101. @apiSuccess {Object[]} search 当前搜索的值
  102. @apiSuccess {String} search.group 周期的值
  103. @apiSuccess {String} search.start 开始时间
  104. @apiSuccess {String} search.end 结束时间
  105. @apiErrorExample 操作成功
  106. {"status": 1, "msg": "yes", "code": 0, "data": {"info": {"status": true, "hardware_id": 10086, "name": "\u9ed8\u8ba4\u7f51\u5173", "farm_id": 1, "cdate": 1504233947, "state": true, "power_num": "", "power_status": "", "id": "ce37636b-3ce7-5de2-9160-0f3283f7d392"}, "search": {"group": "1"}, "group": [{"id": "1", "name": "\u4e00\u5929"}, {"id": "7", "name": "\u4e00\u5468"}, {"id": "30", "name": "\u4e00\u4e2a\u6708"}, {"id": "180", "name": "\u534a\u5e74"}], "setting": {"farm": 1, "user": 0, "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "cur_device": {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": "", "hardware_type": 2, "hardware_id": 100001, "name": "\u6e7f\u5ea6#100001", "type_id": 2, "farm_id": 1, "cdate": 1504253835, "state": true, "num": 4, "status": true, "show": "4", "id": "dc24428c-eafc-5715-b7d1-f3222d28c3bb"}, "device": [{"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100009, "name": "\u6e7f\u5ea6#100009", "type_id": 2, "farm_id": 1, "cdate": 1504253837, "state": true, "num": 30, "status": true, "show": "30", "id": "6d8a4c0e-4f85-524a-87f8-382c333c44da"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100000, "name": "\u6e7f\u5ea6#100000", "type_id": 2, "farm_id": 1, "cdate": 1504253836, "state": true, "num": 5, "status": true, "show": "5", "id": "d644b079-9d4d-521c-8e65-8940c96ba9a7"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100001, "name": "\u6e7f\u5ea6#100001", "type_id": 2, "farm_id": 1, "cdate": 1504253835, "state": true, "num": 4, "status": true, "show": "4", "id": "dc24428c-eafc-5715-b7d1-f3222d28c3bb"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100004, "name": "\u6e7f\u5ea6#100004", "type_id": 2, "farm_id": 1, "cdate": 1504253834, "state": true, "num": 17, "status": true, "show": "17", "id": "62199416-cca0-5114-b900-a10f51e7e447"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100005, "name": "\u6e7f\u5ea6#100005", "type_id": 2, "farm_id": 1, "cdate": 1504253833, "state": true, "num": 7, "status": true, "show": "7", "id": "456daa98-a1f0-59e6-a337-7ca1fa7b2527"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100003, "name": "\u6e7f\u5ea6#100003", "type_id": 2, "farm_id": 1, "cdate": 1504253832, "state": true, "num": 39, "status": true, "show": "39", "id": "11bd4ad1-b4ff-5b1f-948a-935d859b408c"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100007, "name": "\u6e7f\u5ea6#100007", "type_id": 2, "farm_id": 1, "cdate": 1504253828, "state": true, "num": 43, "status": true, "show": "43", "id": "f8e3a42a-7fa3-575a-b99e-2dab4f992239"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100010, "name": "\u6e7f\u5ea6#100010", "type_id": 2, "farm_id": 1, "cdate": 1504253827, "state": true, "num": 20, "status": true, "show": "20", "id": "138876d7-1710-56fa-824b-10402cba17f8"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100002, "name": "\u6e7f\u5ea6#100002", "type_id": 2, "farm_id": 1, "cdate": 1504253826, "state": true, "num": 43, "status": true, "show": "43", "id": "eecfe61f-de83-588f-8cbd-41519d4ebef3"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100006, "name": "\u6e7f\u5ea6#100006", "type_id": 2, "farm_id": 1, "cdate": 1504253818, "state": true, "num": 4, "status": true, "show": "4", "id": "78bd90ac-6af3-5091-83e5-f3397063f743"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": null, "hardware_type": 2, "hardware_id": 100008, "name": "\u6e7f\u5ea6#100008", "type_id": 2, "farm_id": 1, "cdate": 1504253815, "state": true, "num": 37, "status": true, "show": "37", "id": "5fda8633-aed8-5a3b-8175-2c61e7553d91"}], "data": [{"data": {"rows": [["2017-09-01 11:15:00", 45], ["2017-09-01 11:20:00", 50], ["2017-09-01 11:25:00", 49], ["2017-09-01 11:30:00", 49], ["2017-09-01 11:35:00", 50], ["2017-09-01 11:40:00", 48], ["2017-09-01 11:45:00", 49], ["2017-09-01 11:50:00", 47], ["2017-09-01 11:55:00", 49], ["2017-09-01 12:00:00", 49], ["2017-09-01 12:05:00", 47], ["2017-09-01 12:10:00", 50], ["2017-09-01 12:15:00", 49], ["2017-09-01 12:20:00", 50], ["2017-09-01 12:25:00", 48], ["2017-09-01 12:30:00", 50], ["2017-09-01 12:35:00", 48], ["2017-09-01 12:40:00", 50], ["2017-09-01 12:45:00", 47], ["2017-09-01 12:50:00", 50], ["2017-09-01 12:55:00", 49], ["2017-09-01 13:00:00", 46], ["2017-09-01 13:05:00", 50], ["2017-09-01 13:10:00", 50], ["2017-09-01 13:15:00", 49], ["2017-09-01 13:20:00", 50], ["2017-09-01 13:25:00", 49], ["2017-09-01 13:30:00", 50], ["2017-09-01 13:35:00", 50], ["2017-09-01 13:40:00", 48], ["2017-09-01 13:45:00", 49], ["2017-09-01 13:50:00", 49], ["2017-09-01 13:55:00", 49], ["2017-09-01 14:00:00", 49], ["2017-09-01 14:05:00", 47], ["2017-09-01 14:10:00", 49], ["2017-09-01 14:15:00", 48], ["2017-09-01 14:20:00", 45], ["2017-09-01 14:25:00", 50], ["2017-09-01 14:30:00", 50], ["2017-09-01 14:35:00", 49], ["2017-09-01 14:40:00", 49], ["2017-09-01 14:45:00", 49], ["2017-09-01 14:50:00", 49], ["2017-09-01 14:55:00", 47], ["2017-09-01 15:00:00", 49], ["2017-09-01 15:05:00", 50], ["2017-09-01 15:10:00", 49], ["2017-09-01 15:15:00", 47], ["2017-09-01 15:25:00", 50], ["2017-09-01 15:30:00", 50], ["2017-09-01 15:35:00", 49], ["2017-09-01 15:40:00", 49], ["2017-09-01 15:45:00", 49], ["2017-09-01 15:50:00", 48], ["2017-09-01 15:55:00", 47], ["2017-09-01 16:05:00", 46], ["2017-09-01 16:10:00", 49], ["2017-09-01 16:15:00", 49]], "value": "[45, 50, 49, 49, 50, 48, 49, 47, 49, 49, 47, 50, 49, 50, 48, 50, 48, 50, 47, 50, 49, 46, 50, 50, 49, 50, 49, 50, 50, 48, 49, 49, 49, 49, 47, 49, 48, 45, 50, 50, 49, 49, 49, 49, 47, 49, 50, 49, 47, 50, 50, 49, 49, 49, 48, 47, 46, 49, 49]", "time": "[\"2017-09-01 11:15:00\", \"2017-09-01 11:20:00\", \"2017-09-01 11:25:00\", \"2017-09-01 11:30:00\", \"2017-09-01 11:35:00\", \"2017-09-01 11:40:00\", \"2017-09-01 11:45:00\", \"2017-09-01 11:50:00\", \"2017-09-01 11:55:00\", \"2017-09-01 12:00:00\", \"2017-09-01 12:05:00\", \"2017-09-01 12:10:00\", \"2017-09-01 12:15:00\", \"2017-09-01 12:20:00\", \"2017-09-01 12:25:00\", \"2017-09-01 12:30:00\", \"2017-09-01 12:35:00\", \"2017-09-01 12:40:00\", \"2017-09-01 12:45:00\", \"2017-09-01 12:50:00\", \"2017-09-01 12:55:00\", \"2017-09-01 13:00:00\", \"2017-09-01 13:05:00\", \"2017-09-01 13:10:00\", \"2017-09-01 13:15:00\", \"2017-09-01 13:20:00\", \"2017-09-01 13:25:00\", \"2017-09-01 13:30:00\", \"2017-09-01 13:35:00\", \"2017-09-01 13:40:00\", \"2017-09-01 13:45:00\", \"2017-09-01 13:50:00\", \"2017-09-01 13:55:00\", \"2017-09-01 14:00:00\", \"2017-09-01 14:05:00\", \"2017-09-01 14:10:00\", \"2017-09-01 14:15:00\", \"2017-09-01 14:20:00\", \"2017-09-01 14:25:00\", \"2017-09-01 14:30:00\", \"2017-09-01 14:35:00\", \"2017-09-01 14:40:00\", \"2017-09-01 14:45:00\", \"2017-09-01 14:50:00\", \"2017-09-01 14:55:00\", \"2017-09-01 15:00:00\", \"2017-09-01 15:05:00\", \"2017-09-01 15:10:00\", \"2017-09-01 15:15:00\", \"2017-09-01 15:25:00\", \"2017-09-01 15:30:00\", \"2017-09-01 15:35:00\", \"2017-09-01 15:40:00\", \"2017-09-01 15:45:00\", \"2017-09-01 15:50:00\", \"2017-09-01 15:55:00\", \"2017-09-01 16:05:00\", \"2017-09-01 16:10:00\", \"2017-09-01 16:15:00\"]"}, "name": "\u6700\u9ad8"}, {"data": {"rows": [["2017-09-01 11:15:00", 3], ["2017-09-01 11:20:00", 3], ["2017-09-01 11:25:00", 1], ["2017-09-01 11:30:00", 1], ["2017-09-01 11:35:00", 1], ["2017-09-01 11:40:00", 2], ["2017-09-01 11:45:00", 2], ["2017-09-01 11:50:00", 1], ["2017-09-01 11:55:00", 3], ["2017-09-01 12:00:00", 3], ["2017-09-01 12:05:00", 1], ["2017-09-01 12:10:00", 1], ["2017-09-01 12:15:00", 2], ["2017-09-01 12:20:00", 1], ["2017-09-01 12:25:00", 1], ["2017-09-01 12:30:00", 2], ["2017-09-01 12:35:00", 1], ["2017-09-01 12:40:00", 3], ["2017-09-01 12:45:00", 1], ["2017-09-01 12:50:00", 4], ["2017-09-01 12:55:00", 1], ["2017-09-01 13:00:00", 3], ["2017-09-01 13:05:00", 1], ["2017-09-01 13:10:00", 1], ["2017-09-01 13:15:00", 1], ["2017-09-01 13:20:00", 7], ["2017-09-01 13:25:00", 1], ["2017-09-01 13:30:00", 1], ["2017-09-01 13:35:00", 1], ["2017-09-01 13:40:00", 1], ["2017-09-01 13:45:00", 1], ["2017-09-01 13:50:00", 4], ["2017-09-01 13:55:00", 5], ["2017-09-01 14:00:00", 2], ["2017-09-01 14:05:00", 2], ["2017-09-01 14:10:00", 3], ["2017-09-01 14:15:00", 4], ["2017-09-01 14:20:00", 1], ["2017-09-01 14:25:00", 1], ["2017-09-01 14:30:00", 1], ["2017-09-01 14:35:00", 1], ["2017-09-01 14:40:00", 2], ["2017-09-01 14:45:00", 2], ["2017-09-01 14:50:00", 2], ["2017-09-01 14:55:00", 1], ["2017-09-01 15:00:00", 1], ["2017-09-01 15:05:00", 1], ["2017-09-01 15:10:00", 1], ["2017-09-01 15:15:00", 1], ["2017-09-01 15:25:00", 3], ["2017-09-01 15:30:00", 1], ["2017-09-01 15:35:00", 1], ["2017-09-01 15:40:00", 9], ["2017-09-01 15:45:00", 2], ["2017-09-01 15:50:00", 5], ["2017-09-01 15:55:00", 8], ["2017-09-01 16:05:00", 5], ["2017-09-01 16:10:00", 1], ["2017-09-01 16:15:00", 4]], "value": "[3, 3, 1, 1, 1, 2, 2, 1, 3, 3, 1, 1, 2, 1, 1, 2, 1, 3, 1, 4, 1, 3, 1, 1, 1, 7, 1, 1, 1, 1, 1, 4, 5, 2, 2, 3, 4, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 3, 1, 1, 9, 2, 5, 8, 5, 1, 4]", "time": "[\"2017-09-01 11:15:00\", \"2017-09-01 11:20:00\", \"2017-09-01 11:25:00\", \"2017-09-01 11:30:00\", \"2017-09-01 11:35:00\", \"2017-09-01 11:40:00\", \"2017-09-01 11:45:00\", \"2017-09-01 11:50:00\", \"2017-09-01 11:55:00\", \"2017-09-01 12:00:00\", \"2017-09-01 12:05:00\", \"2017-09-01 12:10:00\", \"2017-09-01 12:15:00\", \"2017-09-01 12:20:00\", \"2017-09-01 12:25:00\", \"2017-09-01 12:30:00\", \"2017-09-01 12:35:00\", \"2017-09-01 12:40:00\", \"2017-09-01 12:45:00\", \"2017-09-01 12:50:00\", \"2017-09-01 12:55:00\", \"2017-09-01 13:00:00\", \"2017-09-01 13:05:00\", \"2017-09-01 13:10:00\", \"2017-09-01 13:15:00\", \"2017-09-01 13:20:00\", \"2017-09-01 13:25:00\", \"2017-09-01 13:30:00\", \"2017-09-01 13:35:00\", \"2017-09-01 13:40:00\", \"2017-09-01 13:45:00\", \"2017-09-01 13:50:00\", \"2017-09-01 13:55:00\", \"2017-09-01 14:00:00\", \"2017-09-01 14:05:00\", \"2017-09-01 14:10:00\", \"2017-09-01 14:15:00\", \"2017-09-01 14:20:00\", \"2017-09-01 14:25:00\", \"2017-09-01 14:30:00\", \"2017-09-01 14:35:00\", \"2017-09-01 14:40:00\", \"2017-09-01 14:45:00\", \"2017-09-01 14:50:00\", \"2017-09-01 14:55:00\", \"2017-09-01 15:00:00\", \"2017-09-01 15:05:00\", \"2017-09-01 15:10:00\", \"2017-09-01 15:15:00\", \"2017-09-01 15:25:00\", \"2017-09-01 15:30:00\", \"2017-09-01 15:35:00\", \"2017-09-01 15:40:00\", \"2017-09-01 15:45:00\", \"2017-09-01 15:50:00\", \"2017-09-01 15:55:00\", \"2017-09-01 16:05:00\", \"2017-09-01 16:10:00\", \"2017-09-01 16:15:00\"]"}, "name": "\u6700\u4f4e"}]}}
  107. @apiErrorExample 操作失败
  108. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  109. """
  110. @auth
  111. def get(self):
  112. id = self.input('id', None)
  113. device = self.input('device', None)
  114. if id and device:
  115. self.data['info'] = self.service('common').one('device_gateway', id=id)
  116. if not self.data['info']:
  117. self.out('no')
  118. return
  119. self.data['cur_device'] = self.service('common').one('device_info', id=device)
  120. if not self.data['cur_device']:
  121. self.out('no')
  122. return
  123. self.data['device'] = self.service('common').list('device_info', state=True, search={'hardware_type':2, 'gateway_id':id})
  124. model = Demeter.model('data', 'tsdb')
  125. self.data['group'] = model.dateConfig()
  126. search_group = self.input('search_group')
  127. search_start = self.input('search_start')
  128. search_end = self.input('search_end')
  129. search = {}
  130. if search_group:
  131. search['group'] = search_group
  132. else:
  133. search['group'] = '1'
  134. if search_start:
  135. search['start'] = search_start
  136. if search_end:
  137. search['end'] = search_end
  138. method = 'maxmin'
  139. self.data['search'] = search
  140. self.data['data'] = model.getData(method, search, self.data['cur_device'])
  141. self.out('yes', self.data)
  142. else:
  143. self.out('no')
  144. class pic_path(Load):
  145. """
  146. @api {get} /device/pic 获取图像记录
  147. @apiVersion 1.0.0
  148. @apiName getDevicePic
  149. @apiGroup Device
  150. @apiParam {String} page 页数
  151. @apiParam {String} id 网关id
  152. @apiParam {String} search_date 拍照时间:2017-10-10
  153. @apiSuccess {Object[]} setting 基本信息
  154. @apiSuccess {String} setting.name 站点名
  155. @apiSuccess {Number} setting.farm 当前的农场ID
  156. @apiSuccess {Object[]} info 网关状态基本信息
  157. @apiSuccess {String} info.id 网关ID
  158. @apiSuccess {String} info.name 网关名称
  159. @apiSuccess {Object[]} pic 图像数据
  160. @apiSuccess {Number} pic.id 图片ID
  161. @apiSuccess {String} pic.name 图片名称
  162. @apiSuccess {String} pic.show 图片地址
  163. @apiSuccess {String} pic.cdates 采集时间
  164. @apiSuccess {String} pic.cdate 采集时间戳
  165. @apiSuccess {Object[]} search 当前搜索的值
  166. @apiSuccess {String} search.date 拍照时间
  167. @apiSuccess {String} url 网站url
  168. @apiSuccess {Object[]} page 分页信息
  169. @apiSuccess {String} page.current 当前页
  170. @apiSuccess {String} page.total 总页数
  171. @apiSuccess {String} page.num 每页数据条数
  172. @apiSuccess {String} page.totalNum 总数据条数
  173. @apiErrorExample 操作成功
  174. {"status": 1, "msg": "yes", "code": 0, "data": {"info": {"status": true, "hardware_id": 10086, "name": "\u9ed8\u8ba4\u7f51\u5173", "farm_id": 1, "cdate": 1504233947, "state": true, "power_num": "", "power_status": "", "id": "ce37636b-3ce7-5de2-9160-0f3283f7d392"}, "url": "http://192.168.15.10:8088/", "pic": [{"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "pic": "/camera/2017/09/01/01c323ac0208580601252e2ce9eb3286.jpg", "farm_id": 1, "cdate": 1504262668, "cdates": "2017-09-01 10:44:28", "state": true, "id": "5ea5d688-e092-5131-a382-686298da4f94", "device_id": "2f612150-f670-54e9-9886-a044cfb18e20"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "pic": "/camera/2017/09/01/f5e1509cf0d6b1cb2f048a058dabd75b.jpg", "farm_id": 1, "cdate": 1504262666, "cdates": "2017-09-01 10:44:26", "state": true, "id": "bd36fa61-5210-57c5-b8b8-a81e5797ee85", "device_id": "2f612150-f670-54e9-9886-a044cfb18e20"}, {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "pic": "/camera/2017/09/01/8f3a6d9d614cd280d2cb4a78f90d121a.jpg", "farm_id": 1, "cdate": 1504262665, "cdates": "2017-09-01 10:44:25", "state": true, "id": "162ac864-b2e9-5a7a-84cb-ac57e31ef99c", "device_id": "2f612150-f670-54e9-9886-a044cfb18e20"}], "search_date": null, "setting": {"farm": 1, "user": 0, "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "page": {"current": 1, "ajax": false, "total": 1.0, "num": 15, "totalNum": 3}}}
  175. @apiErrorExample 操作失败
  176. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  177. """
  178. @auth
  179. def get(self):
  180. id = self.input('id', None)
  181. if id:
  182. self.data['info'] = self.service('common').one('device_gateway', id=id)
  183. if not self.data['info']:
  184. self.out('no')
  185. return
  186. self.data['url'] = self.request.protocol + "://" + self.request.host + '/'
  187. self.data['search_date'] = self.input('search_date')
  188. search = {}
  189. search['gateway_id'] = id
  190. if self.data['search_date']:
  191. search['cdate'] = Demeter.mktime(self.data['search_date'])
  192. self.data['pic'] = self.service('common').list('device_pic', state=True, search=search, page=True)
  193. if self.data['pic']:
  194. for key, value in enumerate(self.data['pic']):
  195. self.data['pic'][key]['cdates'] = Demeter.date(value['cdate'])
  196. self.out('yes', self.data)
  197. else:
  198. self.out('no')
  199. class msg_path(Load):
  200. """
  201. @api {get} /device/msg 获取设备日志
  202. @apiVersion 1.0.0
  203. @apiName getDeviceMsg
  204. @apiGroup Device
  205. @apiParam {String} page 页数
  206. @apiParam {String} id 网关id
  207. @apiParam {String} device 设备id
  208. @apiSuccess {Object[]} setting 基本信息
  209. @apiSuccess {String} setting.name 站点名
  210. @apiSuccess {Number} setting.farm 当前的农场ID
  211. @apiSuccess {Object[]} msg 日志列表
  212. @apiSuccess {Number} msg.id 日志ID
  213. @apiSuccess {String} msg.content 日志内容
  214. @apiSuccess {String} msg.cdates 日志时间
  215. @apiSuccess {String} msg.cdate 日志时间戳
  216. @apiSuccess {Object[]} page 分页信息
  217. @apiSuccess {String} page.current 当前页
  218. @apiSuccess {String} page.total 总页数
  219. @apiSuccess {String} page.num 每页数据条数
  220. @apiSuccess {String} page.totalNum 总数据条数
  221. @apiErrorExample 操作成功
  222. {"status": 1, "msg": "yes", "code": 0, "data": {"info": {"status": true, "hardware_id": 10086, "name": "\u9ed8\u8ba4\u7f51\u5173", "farm_id": 1, "cdate": 1504233947, "state": true, "power_num": "", "power_status": "", "id": "ce37636b-3ce7-5de2-9160-0f3283f7d392"}, "msg": [], "setting": {"farm": 1, "user": 0, "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "cur_device": {"gateway_id": "ce37636b-3ce7-5de2-9160-0f3283f7d392", "hardware_station": "", "hardware_type": 2, "hardware_id": 100001, "name": "\u6e7f\u5ea6#100001", "type_id": 2, "farm_id": 1, "cdate": 1504253835, "state": true, "num": 4, "status": true, "show": "4", "id": "dc24428c-eafc-5715-b7d1-f3222d28c3bb"}}}
  223. @apiErrorExample 操作失败
  224. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  225. """
  226. @auth
  227. def get(self):
  228. id = self.input('id', None)
  229. device = self.input('device', None)
  230. if id and device:
  231. self.data['info'] = self.service('common').one('device_gateway', id=id)
  232. if not self.data['info']:
  233. self.out('no')
  234. return
  235. self.data['cur_device'] = self.service('common').one('device_info', id=device)
  236. if not self.data['cur_device']:
  237. self.out('no')
  238. return
  239. search = {}
  240. search['device_id'] = device
  241. self.data['msg'] = self.service('common').list('msg', state=True, search=search, page=True)
  242. if self.data['msg']:
  243. for key, value in enumerate(self.data['msg']):
  244. self.data['msg'][key]['cdates'] = Demeter.date(value['cdate'])
  245. self.out('yes', self.data)
  246. else:
  247. self.out('no')
  248. class set_path(Load):
  249. """
  250. @api {get} /device/set 获取设备极限值信息
  251. @apiVersion 1.0.0
  252. @apiName getDeviceSet
  253. @apiGroup Device
  254. @apiParam {String} id 网关id
  255. @apiParam {String} device 设备id
  256. @apiSuccess {Object[]} setting 基本信息
  257. @apiSuccess {String} setting.name 站点名
  258. @apiSuccess {Number} setting.farm 当前的农场ID
  259. @apiSuccess {Object[]} device 设备数据
  260. @apiSuccess {Number} device.id 设备ID
  261. @apiSuccess {String} device.name 设备名称
  262. @apiSuccess {String} device.show 显示的数据
  263. @apiSuccess {String} device.unit 设备的单位
  264. @apiSuccess {Number} device.max 设备的上限
  265. @apiSuccess {Number} device.min 设备的下限
  266. @apiSuccess {Bool} device.status 设备状态
  267. @apiErrorExample 操作成功
  268. {"status": 1, "msg": "yes", "code": 0, "data": {"setting": {"farm": 1, "user": 0, "name": "\u519c\u5c0f\u76d2", "copyright": "2017 nongxiaohe.com v1.0.0", "site": "http://www.nongxiaohe.com/"}, "list": [{"pic": "http://192.168.15.10:8087/upload/2017/08/30/455f9d6c26b5c989b32eda1809e58d5f.jpg", "method_id": null, "land_id": "249be20d-5759-5af0-9859-b770d9f44e2a", "farm_id": 1, "state": true, "amount": "100ml", "workdate": 1504105397, "category_id": "9365ea66-be55-56f7-a511-059fc85a9478", "cdate": 1504076614, "id": "19e05f00-ae55-5605-be12-86dcd2866df9"}], "page": {"current": 1, "ajax": false, "total": 1.0, "num": 15, "totalNum": 1}}}
  269. @apiErrorExample 操作失败
  270. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  271. """
  272. @auth
  273. def get(self):
  274. id = self.input('id', None)
  275. device = self.input('device', None)
  276. if id and device:
  277. self.data['info'] = self.service('common').one('device_gateway', id=id)
  278. if not self.data['info']:
  279. self.out('no')
  280. return
  281. self.data['device'] = self.service('common').one('device_info', id=device)
  282. if not self.data['device']:
  283. self.out('no')
  284. return
  285. type = self.service('common').one('device_type', id=self.data['device']['type_id'])
  286. self.data['device']['unit'] = type['unit']
  287. set = self.service('common').one('device_set', device_id=self.data['device']['id'])
  288. if set:
  289. self.data['device']['max'] = set['max']
  290. self.data['device']['min'] = set['min']
  291. else:
  292. self.data['device']['max'] = 0
  293. self.data['device']['min'] = 0
  294. self.out('yes', self.data)
  295. else:
  296. self.out('no')
  297. """
  298. @api {post} /device/set 更新极限值设置
  299. @apiVersion 1.0.0
  300. @apiName updateDeviceSet
  301. @apiGroup Farm
  302. @apiParam {String} id 设备id
  303. @apiParam {String} update_max 最大上限
  304. @apiParam {String} update_min 最小下限
  305. @apiSuccess {Bool} status 状态
  306. @apiSuccess {String} msg 描述
  307. @apiSuccess {Number} code 状态码
  308. @apiErrorExample 操作成功
  309. {"status": 1, "msg": "操作成功", "code": 0, "data": {}}
  310. @apiErrorExample 操作失败
  311. {"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
  312. """
  313. @auth
  314. def post(self):
  315. id = self.input('id', None)
  316. if not id:
  317. self.out('no')
  318. return
  319. device = self.service('common').one('device_info', id=id)
  320. if not self.data['device']:
  321. self.out('no')
  322. return
  323. update = {}
  324. update['max'] = self.input('update_max')
  325. update['min'] = self.input('update_min')
  326. if not update['max']:
  327. self.out(u'请输入最大上限')
  328. return
  329. if not update['min']:
  330. self.out(u'请输入最小下限')
  331. return
  332. data = self.service('common').one('device_info', id=id)
  333. if not data:
  334. state = self.service('common').update('device_info', None, update)
  335. else:
  336. state = self.service('common').update('device_info', data['id'], update)
  337. self.out('yes', {'id':state})