|
@@ -10,7 +10,7 @@ import time
|
|
|
from django.db import transaction
|
|
|
from django.views import View
|
|
|
|
|
|
-from Model.models import Device_Info, GatewaySubDevice, FamilyRoomDevice
|
|
|
+from Model.models import Device_Info, GatewaySubDevice, FamilyRoomDevice, SensorRecord
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
|
|
@@ -27,22 +27,23 @@ class GatewaySubDeviceView(View):
|
|
|
return self.validation(request.POST, request, operation)
|
|
|
|
|
|
def validation(self, request_dict, request, operation):
|
|
|
- token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
|
|
|
+ token_obj = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
|
|
|
lang = request_dict.get('lang', None)
|
|
|
- response = ResponseObject(lang) if lang else ResponseObject(token.lang)
|
|
|
+ response = ResponseObject(lang if lang else token_obj.lang)
|
|
|
|
|
|
- # if token.code != 0:
|
|
|
- # return response.json(token.code)
|
|
|
- # user_id = token.userID
|
|
|
- user_id = '154700384179113800138000'
|
|
|
+ if token_obj.code != 0:
|
|
|
+ return response.json(token_obj.code)
|
|
|
+ user_id = token_obj.userID
|
|
|
if operation == 'add': # 添加子设备
|
|
|
return self.add(request_dict, user_id, response)
|
|
|
elif operation == 'query': # 查询子设备
|
|
|
return self.query(request_dict, user_id, response)
|
|
|
elif operation == 'update': # 更新子设备信息
|
|
|
- return self.update(request_dict, user_id, response)
|
|
|
+ return self.sensor_update(request_dict, response)
|
|
|
elif operation == 'delete': # 删除子设备
|
|
|
return self.delete(request_dict, user_id, response)
|
|
|
+ elif operation == 'records': # 查询子设备记录数据
|
|
|
+ return self.records(request_dict, response)
|
|
|
else:
|
|
|
return response.json(414)
|
|
|
|
|
@@ -61,27 +62,34 @@ class GatewaySubDeviceView(View):
|
|
|
@param response: 响应对象
|
|
|
@return: response 响应对象
|
|
|
"""
|
|
|
- serial_number = request_dict.get('serial_number', None)
|
|
|
- device_type = int(request_dict.get('device_type', None))
|
|
|
- nickname = request_dict.get('nickname', None)
|
|
|
- src_addr = request_dict.get('src_addr', None)
|
|
|
- family_id = request_dict.get('family_id', None)
|
|
|
- room_id = request_dict.get('room_id', None)
|
|
|
-
|
|
|
- if not all([serial_number, device_type, nickname, src_addr, family_id, room_id]):
|
|
|
+ serial_number = request_dict.get('serialNumber', None)
|
|
|
+ device_type = int(request_dict.get('deviceType', None))
|
|
|
+ nickname = request_dict.get('nickName', None)
|
|
|
+ ieee_addr = request_dict.get('ieeeAddr', None)
|
|
|
+ src_addr = request_dict.get('srcAddr', None)
|
|
|
+ family_id = request_dict.get('familyId', None)
|
|
|
+ room_id = request_dict.get('roomId', 0)
|
|
|
+
|
|
|
+ if not all([serial_number, device_type, nickname, src_addr, family_id]):
|
|
|
return response.json(444)
|
|
|
now_time = int(time.time())
|
|
|
try:
|
|
|
+ # 查询网关设备数据
|
|
|
device_info_qs = Device_Info.objects.filter(userID_id=user_id, serial_number=serial_number).values('id')
|
|
|
if not device_info_qs.exists():
|
|
|
return response.json(14)
|
|
|
device_id = device_info_qs[0]['id']
|
|
|
+ # 查询是否已添加过该子设备
|
|
|
+ sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id, ieee_addr=ieee_addr)
|
|
|
+ if sub_device_qs.exists():
|
|
|
+ return response.json(174)
|
|
|
+
|
|
|
with transaction.atomic():
|
|
|
sub_device = GatewaySubDevice.objects.create(device_id=device_id, device_type=device_type,
|
|
|
- nickname=nickname, src_addr=src_addr, status=1,
|
|
|
- created_time=now_time, updated_time=now_time)
|
|
|
+ nickname=nickname, ieee_addr=ieee_addr, src_addr=src_addr,
|
|
|
+ status=1, created_time=now_time, updated_time=now_time)
|
|
|
FamilyRoomDevice.objects.create(family_id=family_id, room_id=room_id, device_id=device_id,
|
|
|
- sub_device=sub_device, created_time=now_time, updated_time=now_time)
|
|
|
+ sub_device=sub_device.id, created_time=now_time, updated_time=now_time)
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
return response.json(500, repr(e))
|
|
@@ -96,7 +104,7 @@ class GatewaySubDeviceView(View):
|
|
|
@param response: 响应对象
|
|
|
@return: response
|
|
|
"""
|
|
|
- serial_number = request_dict.get('serial_number', None)
|
|
|
+ serial_number = request_dict.get('serialNumber', None)
|
|
|
|
|
|
if not all([serial_number]):
|
|
|
return response.json(444)
|
|
@@ -107,8 +115,8 @@ class GatewaySubDeviceView(View):
|
|
|
device_id = device_info_qs[0]['id']
|
|
|
count = GatewaySubDevice.objects.filter(device_id=device_id).count()
|
|
|
gateway_sub_device_qs = GatewaySubDevice.objects.filter(device_id=device_id).values('device_type',
|
|
|
- 'nickname', 'src_addr',
|
|
|
- 'status')
|
|
|
+ 'nickname', 'ieee_addr',
|
|
|
+ 'src_addr', 'status')
|
|
|
gateway_sub_device_list = [gateway_sub_device for gateway_sub_device in gateway_sub_device_qs]
|
|
|
res = {
|
|
|
'count': count,
|
|
@@ -118,39 +126,33 @@ class GatewaySubDeviceView(View):
|
|
|
except Exception as e:
|
|
|
return response.json(500, repr(e))
|
|
|
|
|
|
- @staticmethod
|
|
|
- def update(request_dict, user_id, response):
|
|
|
+ @classmethod
|
|
|
+ def sensor_update(cls, request_dict, response):
|
|
|
"""
|
|
|
- 更新子设备信息
|
|
|
+ 传感器修改
|
|
|
@param request_dict: 请求参数
|
|
|
- @request_dict serial_number: 序列号
|
|
|
- @request_dict sub_device_id: 子设备id
|
|
|
- @param user_id: 用户id
|
|
|
- @param response: 响应对象
|
|
|
- @return: response
|
|
|
+ @request_dict gateway_sub_id: 子设备id
|
|
|
+ @request_dict nickName: 设备名
|
|
|
+ @request_dict room_id: 房间id
|
|
|
+ @param response: 响应结果
|
|
|
+ @return: list
|
|
|
"""
|
|
|
- serial_number = request_dict.get('serial_number', None)
|
|
|
- sub_device_id = request_dict.get('sub_device_id', None)
|
|
|
- nickname = request_dict.get('nickname', None)
|
|
|
- src_addr = request_dict.get('src_addr', None)
|
|
|
- status = int(request_dict.get('status', None))
|
|
|
- family_id = request_dict.get('family_id', None)
|
|
|
- room_id = request_dict.get('room_id', None)
|
|
|
-
|
|
|
- if not all([serial_number]):
|
|
|
+ gateway_sub_id = int(request_dict.get('gatewaySubId', None))
|
|
|
+ nickName = request_dict.get('nickName', None)
|
|
|
+ room_id = request_dict.get('roomId', None)
|
|
|
+ if not all([gateway_sub_id]):
|
|
|
return response.json(444)
|
|
|
try:
|
|
|
- device_info_qs = Device_Info.objects.filter(userID_id=user_id, serial_number=serial_number).values('id')
|
|
|
- if not device_info_qs.exists():
|
|
|
- return response.json(14)
|
|
|
- update_data = {
|
|
|
- 'nickname': nickname,
|
|
|
- 'src_addr': src_addr,
|
|
|
- 'status': status,
|
|
|
- }
|
|
|
- GatewaySubDevice.objects.filter(id=sub_device_id).update(**update_data)
|
|
|
+ gateway_sub_qs = GatewaySubDevice.objects.filter(id=gateway_sub_id)
|
|
|
+ if not gateway_sub_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ if nickName:
|
|
|
+ gateway_sub_qs.update(nickname=nickName)
|
|
|
+ elif room_id:
|
|
|
+ FamilyRoomDevice.objects.filter(sub_device=gateway_sub_id).update(room_id=room_id)
|
|
|
return response.json(0)
|
|
|
except Exception as e:
|
|
|
+ print(e.args)
|
|
|
return response.json(500, repr(e))
|
|
|
|
|
|
@staticmethod
|
|
@@ -158,12 +160,12 @@ class GatewaySubDeviceView(View):
|
|
|
"""
|
|
|
更新子设备信息
|
|
|
@param request_dict: 请求参数
|
|
|
- @request_dict sub_device_id: 子设备id
|
|
|
+ @request_dict sub_device_id: 子设备id
|
|
|
@param user_id: 用户id
|
|
|
@param response: 响应对象
|
|
|
@return: response
|
|
|
"""
|
|
|
- sub_device_id = request_dict.get('sub_device_id', None)
|
|
|
+ sub_device_id = request_dict.get('subDeviceId', None)
|
|
|
|
|
|
if not all([sub_device_id]):
|
|
|
return response.json(444)
|
|
@@ -173,3 +175,101 @@ class GatewaySubDeviceView(View):
|
|
|
except Exception as e:
|
|
|
return response.json(500, repr(e))
|
|
|
|
|
|
+ @staticmethod
|
|
|
+ def records(request_dict, response):
|
|
|
+ """
|
|
|
+ 查询子设备记录数据
|
|
|
+ @param request_dict: 请求参数
|
|
|
+ @request_dict gatewaySubId: 子设备id
|
|
|
+ @request_dict cycle: 时间周期
|
|
|
+ @request_dict page: 页数
|
|
|
+ @request_dict size: 条数
|
|
|
+ @param response: 响应对象
|
|
|
+ @return: response
|
|
|
+ """
|
|
|
+ sub_device_id = request_dict.get('gatewaySubId', None)
|
|
|
+ cycle = request_dict.get('cycle', None)
|
|
|
+ if not all([sub_device_id]):
|
|
|
+ return response.json(444)
|
|
|
+ now_time = int(time.time())
|
|
|
+ try:
|
|
|
+ if not cycle:
|
|
|
+ page = request_dict.get('page', None)
|
|
|
+ size = request_dict.get('size', None)
|
|
|
+ if not all([page, size]):
|
|
|
+ return response.json(444)
|
|
|
+ page, size = int(page), int(size)
|
|
|
+ sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id). \
|
|
|
+ values('alarm', 'created_time').order_by('-created_time')[
|
|
|
+ (page - 1) * size:page * size]
|
|
|
+ if not sensor_record_qs.exists():
|
|
|
+ return response.json(0)
|
|
|
+ return response.json(0, list(sensor_record_qs))
|
|
|
+ else:
|
|
|
+ record_dict = {}
|
|
|
+ record_list = []
|
|
|
+ if cycle == '24 hours':
|
|
|
+ start_time = now_time - 24 * 60 * 60
|
|
|
+ sensor_record_qs = SensorRecord.objects.filter(gateway_sub_device_id=sub_device_id,
|
|
|
+ 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)
|
|
|
+
|
|
|
+ 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,
|
|
|
+ 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)
|
|
|
+
|
|
|
+ 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,
|
|
|
+ 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)
|
|
|
+
|
|
|
+ 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]
|
|
|
+
|
|
|
+ # 组织响应数据列表,value为每 小时/天 的平均值
|
|
|
+ for k, v in record_dict.items():
|
|
|
+ record_list.append({'key': k, 'value': round(sum(v) / len(v), 1)})
|
|
|
+ return response.json(0, record_list)
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, repr(e))
|