|
|
@@ -213,111 +213,112 @@ class QxzDeviceAddAPIViw(APIView):
|
|
|
cmd = request_data.get("cmd")
|
|
|
up = uptime.replace(" ", "")
|
|
|
uptime_tp = int((datetime.datetime.strptime(up, "%Y-%m-%d%H:%M:%S")).timestamp())
|
|
|
- # 获取该设备的预警配置数据
|
|
|
- alarm = MongoQXZ_Alarm.objects.filter(device_id=device_id)
|
|
|
- qxz_e_conf = MongoQXZ_Conf.objects.filter(device_id=device_id)
|
|
|
- qxz_e_conf = qxz_e_conf.first() if qxz_e_conf else None
|
|
|
- mongo_device = MongoDevice.objects.filter(device_id=device_id)
|
|
|
- if mongo_device:
|
|
|
- mongo_device = mongo_device.first()
|
|
|
- if not qxz_e_conf:
|
|
|
- logger.error(f"该设备未配置预警阀值: {mongo_device.device_id}")
|
|
|
- return Response({"code": 2, "msg": "该设备未配置预警阀值"})
|
|
|
- if data:
|
|
|
- qxz_e_conf = model_to_dict(qxz_e_conf)
|
|
|
- qx_ek = {}
|
|
|
- result_tp_fin = ""
|
|
|
- for i in data:
|
|
|
- tp_value = i.get("eValue")
|
|
|
- if tp_value:
|
|
|
- ek = i.get("eKey")
|
|
|
- qx_ek[ek] = f"{tp_value}#{i.get('eNum')}#{ek}"
|
|
|
- if alarm:
|
|
|
- # 存在预警配置文件, 先查看预警配置文件中是否有配置 -- "0#6" 表示大于6则报警 "1#5" 表示小于5报警 "0#" 表示不配置
|
|
|
- alarms = alarm.first()
|
|
|
- alarm_config = eval(alarms.conf)
|
|
|
- dat = alarm_config.get("dat")
|
|
|
- for m, n in dat.items():
|
|
|
- n_sp = n.split("#")
|
|
|
- if n_sp[1]:
|
|
|
- if ek == m:
|
|
|
- # 查询具体含义
|
|
|
- zh = qxz_e_conf.get(m)
|
|
|
- zh_k = zh.split("#")
|
|
|
- result = ""
|
|
|
- if n_sp[0] == "1":
|
|
|
- if float(tp_value) > float(n_sp[1]):
|
|
|
- # 组织预警信息
|
|
|
- result = f"为{tp_value},大于{n_sp[1]}"
|
|
|
- elif n_sp[0] == "0":
|
|
|
- if float(tp_value) < float(n_sp[1]):
|
|
|
- result = f"为{tp_value},小于{n_sp[1]}"
|
|
|
- if result:
|
|
|
- QXZThresholdWarning.objects.create(
|
|
|
- device_id=device_id,
|
|
|
- warning_content= "大于预警" if n_sp[0] == "1" else "小于预警",
|
|
|
- ekey=zh,
|
|
|
- set_value=n_sp[1],
|
|
|
- current_value=tp_value,
|
|
|
- upltime=uptime_tp
|
|
|
- )
|
|
|
- result_tp = f"{zh_k[0]}{result}{zh_k[1]},"
|
|
|
- result_tp_fin += result_tp
|
|
|
- if result_tp_fin:
|
|
|
- alarm_new = MongoQXZ_Alarm_Log_New()
|
|
|
- alarm_new.warning_content = result_tp_fin
|
|
|
- alarm_new.upl_time = uptime_tp
|
|
|
- alarm_new.device_id = mongo_device.id
|
|
|
- alarm_new.warning_name = str(mongo_device.device_type_id) # 设备类型ID
|
|
|
- alarm_new.save()
|
|
|
- logger.error(f"产生预警:{device_id}")
|
|
|
- # 30分钟上报一次的数据
|
|
|
- qx_ek["device_id"] = device_id
|
|
|
- qx_ek["uptime"] = uptime_tp
|
|
|
- qxz_data = QXZdata_New(**qx_ek)
|
|
|
- qxz_data.save()
|
|
|
- mongo_device.uptime=uptime_tp
|
|
|
- mongo_device.device_status=1
|
|
|
- mongo_device.save()
|
|
|
- return Response({"code": 0, "msg": "success"})
|
|
|
- if terminalStatus:
|
|
|
- base_info_obj, is_created = MongoQXZ_Base_Info.objects.update_or_create(
|
|
|
- device_id=device_id,
|
|
|
- defaults={
|
|
|
- "volt": terminalStatus.get("VOLT"),
|
|
|
- "rssi": terminalStatus.get("RSSI"),
|
|
|
- "uptime": uptime_tp
|
|
|
- }
|
|
|
- )
|
|
|
- iccid = terminalStatus.get("ICCID")
|
|
|
- lng = terminalStatus.get("longitude")
|
|
|
- lat = terminalStatus.get("latitude")
|
|
|
- led = terminalStatus.get("Dotled")
|
|
|
- dver = terminalStatus.get("Version")
|
|
|
- device, is_created = MongoDevice.objects.get_or_create(device_id=device_id)
|
|
|
- if iccid:
|
|
|
- base_info_obj.iccid = iccid
|
|
|
- if lng:
|
|
|
- base_info_obj.lng = lng
|
|
|
- device.lng = lng
|
|
|
- if lat:
|
|
|
- base_info_obj.lat = lat
|
|
|
- device.lat = lat
|
|
|
- if led:
|
|
|
- base_info_obj.led = led
|
|
|
- if dver:
|
|
|
- base_info_obj.dver = dver
|
|
|
- # 如果经纬度均存在
|
|
|
- if lat and lng:
|
|
|
- is_success, province, city, district = get_addr_by_lag_lng(lat, lng)
|
|
|
- if is_success:
|
|
|
- device.province = province
|
|
|
- device.city = city
|
|
|
- device.district = district
|
|
|
- base_info_obj.save()
|
|
|
- device.uptime = uptime_tp
|
|
|
- device.save()
|
|
|
- return Response({"code": 0, "msg": "success"})
|
|
|
+ if device_id:
|
|
|
+ # 获取该设备的预警配置数据
|
|
|
+ alarm = MongoQXZ_Alarm.objects.filter(device_id=device_id)
|
|
|
+ qxz_e_conf = MongoQXZ_Conf.objects.filter(device_id=device_id)
|
|
|
+ qxz_e_conf = qxz_e_conf.first() if qxz_e_conf else None
|
|
|
+ mongo_device = MongoDevice.objects.filter(device_id=device_id)
|
|
|
+ if mongo_device:
|
|
|
+ mongo_device = mongo_device.first()
|
|
|
+ if not qxz_e_conf:
|
|
|
+ logger.error(f"该设备未配置预警阀值: {mongo_device.device_id}")
|
|
|
+ return Response({"code": 2, "msg": "该设备未配置预警阀值"})
|
|
|
+ if data:
|
|
|
+ qxz_e_conf = model_to_dict(qxz_e_conf)
|
|
|
+ qx_ek = {}
|
|
|
+ result_tp_fin = ""
|
|
|
+ for i in data:
|
|
|
+ tp_value = i.get("eValue")
|
|
|
+ if tp_value:
|
|
|
+ ek = i.get("eKey")
|
|
|
+ qx_ek[ek] = f"{tp_value}#{i.get('eNum')}#{ek}"
|
|
|
+ if alarm:
|
|
|
+ # 存在预警配置文件, 先查看预警配置文件中是否有配置 -- "0#6" 表示大于6则报警 "1#5" 表示小于5报警 "0#" 表示不配置
|
|
|
+ alarms = alarm.first()
|
|
|
+ alarm_config = eval(alarms.conf)
|
|
|
+ dat = alarm_config.get("dat")
|
|
|
+ for m, n in dat.items():
|
|
|
+ n_sp = n.split("#")
|
|
|
+ if n_sp[1]:
|
|
|
+ if ek == m:
|
|
|
+ # 查询具体含义
|
|
|
+ zh = qxz_e_conf.get(m)
|
|
|
+ zh_k = zh.split("#")
|
|
|
+ result = ""
|
|
|
+ if n_sp[0] == "1":
|
|
|
+ if float(tp_value) > float(n_sp[1]):
|
|
|
+ # 组织预警信息
|
|
|
+ result = f"为{tp_value},大于{n_sp[1]}"
|
|
|
+ elif n_sp[0] == "0":
|
|
|
+ if float(tp_value) < float(n_sp[1]):
|
|
|
+ result = f"为{tp_value},小于{n_sp[1]}"
|
|
|
+ if result:
|
|
|
+ QXZThresholdWarning.objects.create(
|
|
|
+ device_id=device_id,
|
|
|
+ warning_content= "大于预警" if n_sp[0] == "1" else "小于预警",
|
|
|
+ ekey=zh,
|
|
|
+ set_value=n_sp[1],
|
|
|
+ current_value=tp_value,
|
|
|
+ upltime=uptime_tp
|
|
|
+ )
|
|
|
+ result_tp = f"{zh_k[0]}{result}{zh_k[1]},"
|
|
|
+ result_tp_fin += result_tp
|
|
|
+ if result_tp_fin:
|
|
|
+ alarm_new = MongoQXZ_Alarm_Log_New()
|
|
|
+ alarm_new.warning_content = result_tp_fin
|
|
|
+ alarm_new.upl_time = uptime_tp
|
|
|
+ alarm_new.device_id = mongo_device.id
|
|
|
+ alarm_new.warning_name = str(mongo_device.device_type_id) # 设备类型ID
|
|
|
+ alarm_new.save()
|
|
|
+ logger.error(f"产生预警:{device_id}")
|
|
|
+ # 30分钟上报一次的数据
|
|
|
+ qx_ek["device_id"] = device_id
|
|
|
+ qx_ek["uptime"] = uptime_tp
|
|
|
+ qxz_data = QXZdata_New(**qx_ek)
|
|
|
+ qxz_data.save()
|
|
|
+ mongo_device.uptime=uptime_tp
|
|
|
+ mongo_device.device_status=1
|
|
|
+ mongo_device.save()
|
|
|
+ return Response({"code": 0, "msg": "success"})
|
|
|
+ if terminalStatus:
|
|
|
+ base_info_obj, is_created = MongoQXZ_Base_Info.objects.update_or_create(
|
|
|
+ device_id=device_id,
|
|
|
+ defaults={
|
|
|
+ "volt": terminalStatus.get("VOLT"),
|
|
|
+ "rssi": terminalStatus.get("RSSI"),
|
|
|
+ "uptime": uptime_tp
|
|
|
+ }
|
|
|
+ )
|
|
|
+ iccid = terminalStatus.get("ICCID")
|
|
|
+ lng = terminalStatus.get("longitude")
|
|
|
+ lat = terminalStatus.get("latitude")
|
|
|
+ led = terminalStatus.get("Dotled")
|
|
|
+ dver = terminalStatus.get("Version")
|
|
|
+ device, is_created = MongoDevice.objects.get_or_create(device_id=device_id)
|
|
|
+ if iccid:
|
|
|
+ base_info_obj.iccid = iccid
|
|
|
+ if lng:
|
|
|
+ base_info_obj.lng = lng
|
|
|
+ device.lng = lng
|
|
|
+ if lat:
|
|
|
+ base_info_obj.lat = lat
|
|
|
+ device.lat = lat
|
|
|
+ if led:
|
|
|
+ base_info_obj.led = led
|
|
|
+ if dver:
|
|
|
+ base_info_obj.dver = dver
|
|
|
+ # 如果经纬度均存在
|
|
|
+ if lat and lng:
|
|
|
+ is_success, province, city, district = get_addr_by_lag_lng(lat, lng)
|
|
|
+ if is_success:
|
|
|
+ device.province = province
|
|
|
+ device.city = city
|
|
|
+ device.district = district
|
|
|
+ base_info_obj.save()
|
|
|
+ device.uptime = uptime_tp
|
|
|
+ device.save()
|
|
|
+ return Response({"code": 0, "msg": "success"})
|
|
|
if cmd:
|
|
|
ext = request_data.get("ext")
|
|
|
imei = ext.get("imei")
|