| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- from rest_framework.views import APIView
- from rest_framework.response import Response
- import pymongo
- from urllib import parse
- import datetime
- import re
- import json
- def get_device(device_type=None):
- device = {
- "864865060482180": "5",
- "860181067701570": "15",
- "L31671797-1": "6",
- "L42185263-1": "6"
- }
- if not device_type:
- return device
- else:
- new_device = {}
- for k, v in device:
- if v == device_type:
- new_device[k] = v
- return new_device
- def get_table():
- passwd = parse.quote_plus("yfkj@6020")
- user = parse.quote_plus("root")
- myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
- return myclient
- class CountDeviceStatus(APIView):
- # 设备状态,在线与离线数量
- def post(self, request):
- devices = get_device()
- device_ids = devices.keys()
- # 获取设备的版本,经纬度
- myclient = get_table()
- db = myclient.smartfarming
- sa_device = db.sa_device
- query = {
- 'device_id': {
- "$in": list(device_ids)
- }
- }
- device = list(sa_device.find(query, {"device_id": 1,"lng":1, "lat": 1, "device_status": 1, "_id": 0, "uptime": 1, "province": 1, "city":1, "district": 1, "device_name": 1}))
- offline = 0
- online = 0
- new = []
- for k in device:
- if k.get("device_status") == 1:
- online += 1
- else:
- offline += 1
- new.append(k)
- myclient.close()
- return Response({"count": offline + online, "online": online, "offline": offline, "device": new, "mtg_name": True})
- class QxData(APIView):
- # 气象、墒情监测数据
- def post(self, request):
- result = {}
- device_id = {
- "气象站": "864865060482180",
- "墒情站": "860181067701570"
- }
- myclient = get_table()
- db = myclient.smartfarming
- qxz_conf = db.sa_qxz_conf
- query = {'device_id': "864865060482180"}
- conf = qxz_conf.find_one(query, {"_id": 0, "uptime": 0, "device_id": 0, "id": 0})
- conf = {k: v for k, v in conf.items() if v is not None and v != ""}
- qxz_data = db.sa_qxz_data
- query = {'device_id': "864865060482180"}
- field = conf.keys()
- fields = {i: 1 for i in field}
- fields.update({"_id": 0, "uptime": 1})
- data = qxz_data.find(query, fields).sort("uptime", pymongo.DESCENDING).limit(1)
- data = data[0]
- tmp = {}
- print(conf)
- for k, v in data.items():
- tp = conf.get(k)
- if tp:
- tp_m = tp.split("#")
- tmp[k] = v.split("#")[0] + tp_m[-1]
- sa_nd_qxz_status = db.sa_nd_qxz_status
- qxz_obj = sa_nd_qxz_status.find({"device_id": "860181067701570"}, {"_id": 0}).sort("upl_time", pymongo.DESCENDING).limit(1)
- qxz_obj = qxz_obj[0]
- if qxz_obj.get("depth"):
- depth = qxz_obj.get("depth")
- else:
- depth = ""
- len_ = len((qxz_obj.get("temp")).split(","))
- for i in range(len_):
- if i != 0:
- depth += ","
- depth += str((i+1)*10)
- data = {"depth":depth,"csq":qxz_obj.get("csq"),"temp":qxz_obj.get("temp"),"swc":qxz_obj.get("swc"),"atm":qxz_obj.get("atm"),"at":qxz_obj.get("at"),"ats":qxz_obj.get("ats"),"uptime":qxz_obj.get("upl_time")}
- myclient.close()
- return Response({
- "qxz":tmp,
- "qxz_conf": conf,
- "sqz": data,
- "mtg_name": True
- })
- class QxWarning(APIView):
- # 气象预警数据
- def post(self, request):
- myclient = get_table()
- db = myclient.smartfarming
- qxz_alarm = db.sa_qxz_alarm_log_new
- k = qxz_alarm.find_one(sort=[("id", -1)])["id"] + 1
- query = {'device_id': "864865060482180"}
- data = qxz_alarm.find(query, {"_id":0, "warning_content": 1, "upl_time": 1}).sort("upl_time", pymongo.DESCENDING).limit(5)
- myclient.close()
- return Response({"warning": list(data), "mtg_name": True})
-
- class CameraDetail(APIView):
- # 监控数据
- def post(self, request):
- myclient = get_table()
- db = myclient.smartfarming
- sa_device_camera = db.sa_device_camera
- sa_device_camera_account = db.sa_device_camera_account
- sa_device = db.sa_device
- result = []
- for device_id in ["L31671797-1", "L42185263-1"]:
- camera = sa_device_camera.find_one({"device_id": device_id}, {"device_info": 1, "account_id": 1, "_id": 0})
- addr = camera.get("device_info")
- account_id = camera.get("account_id")
- camera_account = sa_device_camera_account.find_one({"id": account_id}, {"account_type": 1, "_id": 0})
- if camera_account:
- account_type = camera_account.get("account_type")
- if account_type == 0:
- addr = addr.replace("http://", "https://")
- elif account_type == 1:
- pass
- else:
- addr = addr.replace("http://", "https://")
- device_info = sa_device.find_one({"device_id": device_id}, {"_id": 0, "device_name":1})
- device_name = device_info.get("device_name")
- result.append({
- "device_id": device_id,
- "device_name": device_name if device_name else device_id,
- "device_info": addr,
- "is_name": device_name,
-
- })
- myclient.close()
- return Response({"camera": result, "mtg_name": True})
-
|