Bladeren bron

增加门头沟一张图数据

yf_yzl 2 jaren geleden
bovenliggende
commit
8f9b952dca
2 gewijzigde bestanden met toevoegingen van 163 en 1 verwijderingen
  1. 148 0
      apps/Equipment/men_tou_gou.py
  2. 15 1
      apps/Equipment/urls.py

+ 148 - 0
apps/Equipment/men_tou_gou.py

@@ -0,0 +1,148 @@
+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})
+
+
+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 = {}
+        for k, v in data.items():
+            tp = conf.get(k)
+            if tp:
+                tp_m = tp.split("#")
+                tmp[tp_m[0]] = 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({
+            "气象站":tmp,
+            "墒情站": data
+        })
+
+
+class QxWarning(APIView):
+    # 气象预警数据
+    def post(self, request):
+        myclient = get_table()
+        db = myclient.smartfarming
+        qxz_alarm = db.sa_qxz_alarm_log_new
+        query = {'device_id': "864865060482180"}
+        data = qxz_alarm.find(query, {"_id":0, "warning_content": 1}).sort("uptime", pymongo.DESCENDING).limit(5)
+        myclient.close()
+        return Response(list(data))
+    
+
+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
+        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://")
+            result[device_id] = addr
+        myclient.close()
+        return Response(result)
+        

+ 15 - 1
apps/Equipment/urls.py

@@ -2,6 +2,14 @@ from django.conf.urls import url
 
 from . import views
 
+from .men_tou_gou import (
+    CountDeviceStatus,
+    QxData,
+    QxWarning,
+    CameraDetail
+)
+
+
 urlpatterns = [
     url(r'^search/$', views.SearchEquip.as_view(), name='equip_search'),
 
@@ -28,5 +36,11 @@ urlpatterns = [
 
     url(r'^gssq/list/$', views.GssqDeviceListView.as_view(), name='gssq_list'),
     url(r'^gssq/detail/$', views.GssqDeviceDetailView.as_view(), name='gssq_detail'),
-    
+
+    # 门头沟一张图
+    url(r'^mtg/device/$', CountDeviceStatus.as_view(), name='mtg_device'),
+    url(r'^mtg/qxz/$', QxData.as_view(), name='mtg_qxz'),
+    url(r'^mtg/qxz_warning/$', QxWarning.as_view(), name='mtg_qxz_warning'),
+    url(r'^mtg/camera/$', CameraDetail.as_view(), name='mtg_camera'),
+
 ]