device_manage.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import datetime
  2. from smartfarming.models.device import MongoDevice, MongoSCDData, MongoDeviceType, MongoXYCBData
  3. from kedong.decoration import kedong_deco, PortError
  4. from smartfarming.utils import get_address_by_lntlat, get_weather_info
  5. @kedong_deco(login_required=False)
  6. def weathers(request):
  7. """
  8. 天气接口
  9. 参数
  10. lat 必传(str) 纬度
  11. lng 必传(str) 经度
  12. 返回值:
  13. "data": [
  14. {
  15. "id": 1201,
  16. "province": "新疆维吾尔自治区", 省
  17. "city": "克孜勒苏柯尔克孜自治州", 市
  18. "district": "阿克陶县", 县
  19. "lng": "75.945159", 经度
  20. "lat": "39.147079", 纬度
  21. "at": "31", 温度
  22. "ah": "14", 湿度
  23. "upl_time": "2021-10-25 14:39:48", 时间
  24. "win": "东风", 风向
  25. "win_speed": "1级", 风速
  26. "win_meter": "5km/h", 风力
  27. "wea": "多云", 天气
  28. "visibility": "30km", 能见度
  29. "pressure": "863", 压力
  30. "air": "40", 空气
  31. "air_pm25": "19", 空气PM2.5
  32. "air_level": "优", 空气等级
  33. "air_tips": "空气很好,可以外出活动,呼吸新鲜空气,拥抱大自然!" 空气说明
  34. }
  35. ],
  36. "params": {
  37. "lat": "039.1850853",
  38. "lng": "075.8749465"
  39. }
  40. }
  41. """
  42. lat = request.POST.get('lat', '')
  43. lng = request.POST.get('lng', '')
  44. device_id = request.POST.get('device_id')
  45. if not lat and not lng:
  46. raise PortError('', '未传经纬度')
  47. province, city, district = "", "", ""
  48. if device_id:
  49. try:
  50. instance = MongoDevice.objects.get(device_id=device_id)
  51. province, city, district = instance.province, instance.city, instance.district
  52. if (not city) and district:
  53. city = district
  54. except Exception as e:
  55. pass
  56. if not (province and city):
  57. province, city, district = get_address_by_lntlat(lng, lat)
  58. result = []
  59. weather_info = get_weather_info(lng, lat)
  60. try:
  61. upl_time = weather_info.get('upl_time')
  62. upl_time = datetime.datetime.fromtimestamp(int(upl_time)).strftime("%Y-%m-%d %H:%M:%S")
  63. except Exception as e:
  64. upl_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  65. result.append({
  66. "province": province,
  67. "city": city,
  68. "district": district,
  69. "lng": lng,
  70. "lat": lat,
  71. "at": weather_info.get('at', ''),
  72. "ah": weather_info.get('ah', ''),
  73. "upl_time": upl_time,
  74. "win": weather_info.get('win', ''),
  75. "win_speed": weather_info.get('win_speed', ''),
  76. "win_meter": weather_info.get('win_meter', ''),
  77. "wea": weather_info.get('wea', ''),
  78. "visibility": weather_info.get('visibility', ''),
  79. "pressure": weather_info.get('pressure', ''),
  80. "air": weather_info.get('air', ''),
  81. "air_pm25": weather_info.get('air_pm25', ''),
  82. "air_level": weather_info.get('air_level', ''),
  83. "air_tips": weather_info.get('air_tips', ''),
  84. })
  85. return result
  86. @kedong_deco(login_required=True)
  87. def scd_gis_info(request):
  88. """
  89. 杀虫灯GIS展示信息
  90. 参数
  91. device_id 必传(str) 设备ID
  92. 返回值:
  93. "data": {
  94. "address": "河南省新乡市原阳县", 设备地址
  95. "uptime": "2022-01-02 12:12:12", 最新上传时间
  96. "device_name": "xxxxxx", 设备名称
  97. "device_id":"xxxxxxxxxxx", 设备ID
  98. 'device_status': "在线", 设备状态
  99. "at": "30", 温度
  100. "ah": "50", 湿度
  101. "ws_status": "待机", 工作状态
  102. "rps_status": "正常", 雨控状态
  103. "tt": "4", 定时时长
  104. "ct": "0", 电击次数
  105. "csq": "0" 信号强度
  106. }
  107. """
  108. device_id = request.POST.get('device_id')
  109. try:
  110. device_info = MongoDevice.objects.get(device_id=device_id)
  111. except Exception as e:
  112. raise PortError("device_id", "设备id参数异常")
  113. device_name = device_info.device_name
  114. if not device_name:
  115. device_name = MongoDeviceType.objects.get(id=4).type_name
  116. up_date = datetime.datetime.utcfromtimestamp(device_info.uptime)
  117. try:
  118. queryset = MongoSCDData.objects.filter(device_id=device_info.id).order_by('-id')[0]
  119. data_info = eval(queryset.device_data)
  120. except Exception as e:
  121. data_info = {}
  122. ws = data_info.get('ws', '0')
  123. ws_status = "待机"
  124. if ws == "1":
  125. ws_status = "工作"
  126. elif ws == "2":
  127. ws_status = "充电"
  128. tt = data_info.get('tt', '0')
  129. if tt == '0':
  130. tt = '常亮'
  131. rps_status = "正常" if data_info.get("rps", "0") == "0" else "保护"
  132. data = {
  133. "address": f"{device_info.province}{device_info.city}{device_info.district}",
  134. "uptime": up_date.strftime("%Y-%m-%d %H:%M:%S"),
  135. "device_name": device_name,
  136. "device_id": device_info.device_id,
  137. 'device_status': "在线" if device_info.device_status == 1 else "离线",
  138. "ws_status": ws_status,
  139. "rps_status": rps_status,
  140. "tt": tt,
  141. "at": data_info.get('at', ''),
  142. "ah": data_info.get('ah', ''),
  143. "ct": data_info.get('ct', '0'),
  144. "csq": data_info.get('csq', '0')
  145. }
  146. return data
  147. @kedong_deco(login_required=True)
  148. def xycb_gis_info(request):
  149. """
  150. 性诱测报GIS展示信息
  151. 参数
  152. device_id 必传(str) 设备ID
  153. 返回值:
  154. "data": {
  155. "address": "河南省新乡市原阳县", 设备地址
  156. "uptime": "2022-01-02 12:12:12", 最新上传时间
  157. "device_name": "xxxxxx", 设备名称
  158. "device_id":"xxxxxxxxxxx", 设备ID
  159. 'device_status': "在线", 设备状态
  160. "at": "30", 温度
  161. "ah": "50", 湿度
  162. "ws_status": "待机", 工作状态
  163. "cs_status": "非充电", 充电状态
  164. "bs_status": "正常", 电池状态
  165. "infr_ct": "0", 击虫次数
  166. "csq": "0" 信号强度
  167. }
  168. """
  169. device_id = request.POST.get('device_id')
  170. try:
  171. device_info = MongoDevice.objects.get(device_id=device_id)
  172. except Exception as e:
  173. raise PortError("device_id", "设备id参数异常")
  174. device_name = device_info.device_name
  175. if not device_name:
  176. device_name = MongoDeviceType.objects.get(id=4).type_name
  177. up_date = datetime.datetime.utcfromtimestamp(device_info.uptime)
  178. try:
  179. queryset = MongoXYCBData.objects.filter(device_id=device_info.id).order_by('-id')[0]
  180. data_info = eval(queryset.device_data)
  181. except Exception as e:
  182. data_info = {}
  183. ws = data_info.get('ws', '0')
  184. ws_status = "待机"
  185. if ws == "1":
  186. ws_status = "工作"
  187. elif ws == "2":
  188. ws_status = "充电"
  189. bs = data_info.get('bs', '0')
  190. bs_status = '正常'
  191. if bs == '1':
  192. bs_status = '欠压'
  193. elif bs == '2':
  194. bs_status = '过压'
  195. cs_status = '非充电' if data_info.get('cs', '0') == '0' else '充电'
  196. data = {
  197. "address": f"{device_info.province}{device_info.city}{device_info.district}",
  198. "uptime": up_date.strftime("%Y-%m-%d %H:%M:%S"),
  199. "device_name": device_name,
  200. "device_id": device_info.device_id,
  201. 'device_status': "在线" if device_info.device_status == 1 else "离线",
  202. "at": data_info.get('at', ''),
  203. "ah": data_info.get('ah', ''),
  204. "ws_status": ws_status,
  205. "cs_status": cs_status,
  206. "bs_status": bs_status,
  207. "infr_ct": data_info.get('infr_ct', '0'),
  208. "csq": data_info.get('csq')
  209. }
  210. return data