def device_status(): """ 设备状态详细信息 :return: """ project_id = request.args.get("project_id") devices = list(Device.objects.filter(project_id=project_id).all()) clients = socketservice.get_instance().clients result = [] for device in devices: monitor_energy_max = Monitor.objects.filter(device_id=device["device_id"], line=0, type="energy", createdAt__gte=zero_time()).order_by('-createdAt').first() monitor_energy_min = Monitor.objects.filter(device_id=device["device_id"], line=0, type="energy", createdAt__gte=zero_time()).order_by('+createdAt').first() monitor_electricity = Monitor.objects.filter(device_id=device["device_id"], line=0, type="electricity").first() energy = monitor_energy_max.value - monitor_energy_min.value if monitor_energy_max else None electricity = monitor_electricity.value if monitor_electricity else None cloud_id = device["cloud_id"] data = jsonify(device) data["energy"] = energy data["electricity"] = electricity data["alarm_count"] = Alarm.objects.filter(device_id=device["device_id"], createdAt__gte=month_datetime(), status=0).count() if device["cloud_id"] in clients.keys(): socket = clients[cloud_id] data["online"] = True data["sign"] = socket.sign result.append(data) else: data["online"] = False data["sign"] = None result.append(data) return Success(result)
def img_urls(self): try: img_urls = Product2Image.query.filter_by( product_id=self.id).order_by(asc(Product2Image.order)).all() except Exception: return [] return list(map(lambda x: x['img_url'], jsonify(img_urls)))
def job_list(): """ 根据项目id返回计划列表 :return:[{ "count": 1, "id": "5e71dd0d3ae156497e114364", "off": [ { "hour": "12", "minute": "05" } ], "on": [ { "hour": "17", "minute": "01" } ], "plan_name": "测试计划1", "project_id": "5e69f8311366ea889c7cff4c" }] """ project_id = request.args.get("project_id") plans = jsonify(list(Plan.objects.filter(project_id=project_id).all())) for plan in plans: count = Line.objects.filter(choice_plan=plan["id"]).count() plan["count"] = count return Success(plans)
def __init__(self, data=None, code=None, error_code=None, msg=None): if data: self.data = jsonify(data) if error_code == 1: code = code if code else 201 msg = msg if msg else '创建 | 更新成功' if error_code == 2: code = code if code else 202 msg = msg if msg else '删除成功' super(Success, self).__init__(code, error_code, msg)
def __get_user_address(self): with db.auto_check_empty(UserException(error_code=6001, msg='用户地址不存在, 下单失败')): user_address = UserAddress.query.filter_by(user_id=self.uid).first_or_404() return jsonify(user_address)
def __get_user_address(self): user_address = UserAddress.query\ .filter_by(user_id=self.uid)\ .first_or_404(e=UserException(error_code=6001, msg='用户地址不存在, 下单失败')) order_address = jsonify(user_address) # 序列化,因为要存入到数据库 return order_address
def line_job(): """ 每条线路的计划分配情况 :return: "data": [ { "choice_plan": "5e71dd0d3ae156497e114364", "device_id": "7WIZya2wsIKGuitNpHyIWjCq", "id": "5e6ee2ddc1094a4d94ed0264", "limit": 100.0, "line": 1, "line_name": "线路一", "off": [ { "hour": "12", "minute": "05" } ], "on": [ { "hour": "17", "minute": "01" } ], "plan_name": "测试计划1", "plan_status": false, "standard": 78 }, { "choice_plan": null, "device_id": "7WIZya2wsIKGuitNpHyIWjCq", "id": "5e6ee2e8c1094a4d94ed0265", "limit": 160.0, "line": 2, "line_name": "线路二", "plan_status": null, "standard": 78 }, { "choice_plan": null, "device_id": "7WIZya2wsIKGuitNpHyIWjCq", "id": "5e6ee2f7c1094a4d94ed0266", "limit": 250.0, "line": 3, "line_name": "线路三", "plan_status": null, "standard": 78 } ] """ device_id = request.args.get("device_id") lines = jsonify(list( Line.objects.filter(device_id=device_id).get_or_404())) result = [] for line in lines: if line["choice_plan"]: plan = Plan.objects.filter(id=line["choice_plan"]).first() if plan: line["plan_name"] = plan["plan_name"] line["on"] = plan["on"] line["off"] = plan["off"] result.append(line) else: if line["line"] != 0: result.append(line) return Success(result)
def __init__(self, data=None, code=None, error_code=None, msg=None): if data: self.data = jsonify(data) super(Success, self).__init__(code, error_code, msg)