|
@@ -192,8 +192,8 @@ class GatewaySubDeviceView(View):
|
|
except Exception as e:
|
|
except Exception as e:
|
|
return response.json(500, repr(e))
|
|
return response.json(500, repr(e))
|
|
|
|
|
|
- @staticmethod
|
|
|
|
- def records_tem_hum(request_dict, response):
|
|
|
|
|
|
+ @classmethod
|
|
|
|
+ def records_tem_hum(cls, request_dict, response):
|
|
"""
|
|
"""
|
|
查询温湿度传感器记录
|
|
查询温湿度传感器记录
|
|
@param request_dict: 请求参数
|
|
@param request_dict: 请求参数
|
|
@@ -210,74 +210,12 @@ class GatewaySubDeviceView(View):
|
|
return response.json(444, {'error param': 'gatewaySubId or cycle or eventType'})
|
|
return response.json(444, {'error param': 'gatewaySubId or cycle or eventType'})
|
|
now_time = int(time.time())
|
|
now_time = int(time.time())
|
|
try:
|
|
try:
|
|
- record_dict = OrderedDict()
|
|
|
|
- record_list = []
|
|
|
|
- if cycle == 'Hours':
|
|
|
|
- start_time = now_time - 24 * 60 * 60
|
|
|
|
- sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
|
|
|
|
- event_type=event_type,
|
|
|
|
- created_time__range=(start_time, now_time)). \
|
|
|
|
- values('alarm', 'created_time').order_by('created_time')
|
|
|
|
- if not sensor_record_qs.exists():
|
|
|
|
- return response.json(0, {'records': [], 'time': now_time})
|
|
|
|
-
|
|
|
|
- for sensor_record in sensor_record_qs:
|
|
|
|
- created_time = time.strftime('%m/%d %H:%M %w', time.localtime(sensor_record['created_time']))
|
|
|
|
- hour = int(created_time[-7:-5])
|
|
|
|
- minute = int(created_time[-4:-2])
|
|
|
|
- if hour != 23 and minute > 30: # 不为23时且分钟大于30,hour+1
|
|
|
|
- hour += 1
|
|
|
|
- alarm = float(sensor_record['alarm'])
|
|
|
|
- # 组织数据,record_dict:{"0": [1.0, 2.0, 3.0], "1": [1.0, 2.0, 3.0]...}
|
|
|
|
- if str(hour) in record_dict:
|
|
|
|
- record_dict[str(hour)].append(alarm)
|
|
|
|
- else:
|
|
|
|
- record_dict[str(hour)] = [alarm]
|
|
|
|
-
|
|
|
|
- elif cycle == 'Week':
|
|
|
|
- start_time = now_time - 24 * 60 * 60 * 7
|
|
|
|
- sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
|
|
|
|
- event_type=event_type,
|
|
|
|
- created_time__range=(start_time, now_time)). \
|
|
|
|
- values('alarm', 'created_time').order_by('created_time')
|
|
|
|
- if not sensor_record_qs.exists():
|
|
|
|
- return response.json(0, {'records': [], 'time': now_time})
|
|
|
|
-
|
|
|
|
- for sensor_record in sensor_record_qs:
|
|
|
|
- created_time = time.strftime('%m/%d %H:%M %w', time.localtime(sensor_record['created_time']))
|
|
|
|
- week = int(created_time[-1:])
|
|
|
|
- alarm = float(sensor_record['alarm'])
|
|
|
|
- # 组织数据,record_dict:{"0": [1.0, 2.0, 3.0], "1": [1.0, 2.0, 3.0]...}
|
|
|
|
- if str(week) in record_dict:
|
|
|
|
- record_dict[str(week)].append(alarm)
|
|
|
|
- else:
|
|
|
|
- record_dict[str(week)] = [alarm]
|
|
|
|
-
|
|
|
|
- elif cycle == 'Month':
|
|
|
|
- start_time = now_time - 24 * 60 * 60 * 30
|
|
|
|
- sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
|
|
|
|
- event_type=event_type,
|
|
|
|
- created_time__range=(start_time, now_time)). \
|
|
|
|
- values('alarm', 'created_time').order_by('created_time')
|
|
|
|
- if not sensor_record_qs.exists():
|
|
|
|
- return response.json(0, {'records': [], 'time': now_time})
|
|
|
|
-
|
|
|
|
- for sensor_record in sensor_record_qs:
|
|
|
|
- created_time = time.strftime('%m/%d %H:%M %w', time.localtime(sensor_record['created_time']))
|
|
|
|
- month = int(created_time[:2])
|
|
|
|
- day = int(created_time[3:5])
|
|
|
|
- date = str(month) + '/' + str(day)
|
|
|
|
- alarm = float(sensor_record['alarm'])
|
|
|
|
- # 组织数据,record_dict:{"0": [1.0, 2.0, 3.0], "1": [1.0, 2.0, 3.0]...}
|
|
|
|
- if date in record_dict:
|
|
|
|
- record_dict[date].append(alarm)
|
|
|
|
- else:
|
|
|
|
- record_dict[date] = [alarm]
|
|
|
|
-
|
|
|
|
- else:
|
|
|
|
- return response.json(444, {'error param': 'invalid cycle'})
|
|
|
|
-
|
|
|
|
|
|
+ record_dict = cls.get_record_dict(cycle, now_time, sub_device_id, event_type, response)
|
|
|
|
+ except Exception as e:
|
|
|
|
+ return response.json(500, repr(e))
|
|
|
|
+ else:
|
|
# 组织响应数据列表,value为每 小时/天 的平均值
|
|
# 组织响应数据列表,value为每 小时/天 的平均值
|
|
|
|
+ record_list = []
|
|
for k, v in record_dict.items():
|
|
for k, v in record_dict.items():
|
|
record_list.append({'key': k, 'value': round(sum(v) / len(v), 1)})
|
|
record_list.append({'key': k, 'value': round(sum(v) / len(v), 1)})
|
|
|
|
|
|
@@ -286,8 +224,85 @@ class GatewaySubDeviceView(View):
|
|
'time': now_time
|
|
'time': now_time
|
|
}
|
|
}
|
|
return response.json(0, res)
|
|
return response.json(0, res)
|
|
- except Exception as e:
|
|
|
|
- return response.json(500, repr(e))
|
|
|
|
|
|
+
|
|
|
|
+ @staticmethod
|
|
|
|
+ def get_record_dict(cycle, now_time, sub_device_id, event_type, response):
|
|
|
|
+ """
|
|
|
|
+ 获取记录数据
|
|
|
|
+ @param cycle: 时间周期
|
|
|
|
+ @param now_time: 当前时间
|
|
|
|
+ @param sub_device_id: 子设备id
|
|
|
|
+ @param event_type: 事件类型, 18:温度,19:湿度
|
|
|
|
+ @param response: 响应对象
|
|
|
|
+ @return: record_dict: 记录数据
|
|
|
|
+ """
|
|
|
|
+ record_dict = OrderedDict()
|
|
|
|
+ if cycle == 'Hours':
|
|
|
|
+ start_time = now_time - 24 * 60 * 60
|
|
|
|
+ sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
|
|
|
|
+ event_type=event_type,
|
|
|
|
+ created_time__range=(start_time, now_time)). \
|
|
|
|
+ values('alarm', 'created_time').order_by('created_time')
|
|
|
|
+ if not sensor_record_qs.exists():
|
|
|
|
+ return response.json(0, {'records': [], 'time': now_time})
|
|
|
|
+
|
|
|
|
+ for sensor_record in sensor_record_qs:
|
|
|
|
+ created_time = time.strftime('%m/%d %H:%M %w', time.localtime(sensor_record['created_time']))
|
|
|
|
+ hour = int(created_time[-7:-5])
|
|
|
|
+ minute = int(created_time[-4:-2])
|
|
|
|
+ if hour != 23 and minute > 30: # 不为23时且分钟大于30,hour+1
|
|
|
|
+ hour += 1
|
|
|
|
+ alarm = float(sensor_record['alarm'])
|
|
|
|
+ # 组织数据,record_dict:{"0": [1.0, 2.0, 3.0], "1": [1.0, 2.0, 3.0]...}
|
|
|
|
+ if str(hour) in record_dict:
|
|
|
|
+ record_dict[str(hour)].append(alarm)
|
|
|
|
+ else:
|
|
|
|
+ record_dict[str(hour)] = [alarm]
|
|
|
|
+
|
|
|
|
+ elif cycle == 'Week':
|
|
|
|
+ start_time = now_time - 24 * 60 * 60 * 7
|
|
|
|
+ sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
|
|
|
|
+ event_type=event_type,
|
|
|
|
+ created_time__range=(start_time, now_time)). \
|
|
|
|
+ values('alarm', 'created_time').order_by('created_time')
|
|
|
|
+ if not sensor_record_qs.exists():
|
|
|
|
+ return response.json(0, {'records': [], 'time': now_time})
|
|
|
|
+
|
|
|
|
+ for sensor_record in sensor_record_qs:
|
|
|
|
+ created_time = time.strftime('%m/%d %H:%M %w', time.localtime(sensor_record['created_time']))
|
|
|
|
+ week = int(created_time[-1:])
|
|
|
|
+ alarm = float(sensor_record['alarm'])
|
|
|
|
+ # 组织数据,record_dict:{"0": [1.0, 2.0, 3.0], "1": [1.0, 2.0, 3.0]...}
|
|
|
|
+ if str(week) in record_dict:
|
|
|
|
+ record_dict[str(week)].append(alarm)
|
|
|
|
+ else:
|
|
|
|
+ record_dict[str(week)] = [alarm]
|
|
|
|
+
|
|
|
|
+ elif cycle == 'Month':
|
|
|
|
+ start_time = now_time - 24 * 60 * 60 * 30
|
|
|
|
+ sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
|
|
|
|
+ event_type=event_type,
|
|
|
|
+ created_time__range=(start_time, now_time)). \
|
|
|
|
+ values('alarm', 'created_time').order_by('created_time')
|
|
|
|
+ if not sensor_record_qs.exists():
|
|
|
|
+ return response.json(0, {'records': [], 'time': now_time})
|
|
|
|
+
|
|
|
|
+ for sensor_record in sensor_record_qs:
|
|
|
|
+ created_time = time.strftime('%m/%d %H:%M %w', time.localtime(sensor_record['created_time']))
|
|
|
|
+ month = int(created_time[:2])
|
|
|
|
+ day = int(created_time[3:5])
|
|
|
|
+ date = str(month) + '/' + str(day)
|
|
|
|
+ alarm = float(sensor_record['alarm'])
|
|
|
|
+ # 组织数据,record_dict:{"0": [1.0, 2.0, 3.0], "1": [1.0, 2.0, 3.0]...}
|
|
|
|
+ if date in record_dict:
|
|
|
|
+ record_dict[date].append(alarm)
|
|
|
|
+ else:
|
|
|
|
+ record_dict[date] = [alarm]
|
|
|
|
+
|
|
|
|
+ else:
|
|
|
|
+ return response.json(444, {'invalid cycle': cycle})
|
|
|
|
+
|
|
|
|
+ return record_dict
|
|
|
|
|
|
@staticmethod
|
|
@staticmethod
|
|
def records(request_dict, response):
|
|
def records(request_dict, response):
|