def car_list_request(): """获取车辆列表信息 type: 2只显示在线的, 3只显示不在线的,其他显示全部的 """ type = request.args.get("type", "1") try: conns = ConnectDevice.query.all() conn_car_id_list = [conn.car_id for conn in conns] if type == "2": cars = CarInfo.query.filter(CarInfo.online == 1).all() elif type == "3": cars = CarInfo.query.filter(CarInfo.online == 0).all() else: cars = CarInfo.query.all() car_list = list() for car in cars: item = car.to_dict() if car.carId in conn_car_id_list: item["is_conn"] = 1 else: item["is_conn"] = 0 item["app_key"] = car.get_app_key() item["app_secret"] = car.get_app_secret() car_list.append(item) return jsonify(code=RET.OK, msg=error_map.get(RET.OK), data=car_list[::-1]) except Exception as e: current_app.logger.error(e) return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR))
def get_platform_list_request(): """获取台架列表 type: 2:只显示在线的,3:只显示不在线的,其他全显示 """ type = request.args.get("type", "1") try: conns = ConnectDevice.query.all() conn_platform_id_list = [conn.platform_id for conn in conns] if type == "2": platforms = PlatformInfo.query.filter(PlatformInfo.online == 1).all() elif type == "3": platforms = PlatformInfo.query.filter(PlatformInfo.online == 0).all() else: platforms = PlatformInfo.query.all() platform_list = list() for platform in platforms: item = platform.to_dict() if platform.platformId in conn_platform_id_list: item["is_conn"] = 1 else: item["is_conn"] = 0 item["app_key"] = platform.get_app_key() item["app_secret"] = platform.get_app_secret() platform_list.append(item) return jsonify(code=RET.OK, msg=error_map.get(RET.OK), data=platform_list[::-1]) except Exception as e: # print(e) current_app.logger.error(e) return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR))
def check_login(*args, **kwargs): token = request.headers.get("token") if not token: return jsonify(code=RET.NOTLOGIN, msg=error_map.get(RET.NOTLOGIN)) user = UserInfo.verify_user_token(token=token) if not user: return jsonify(code=RET.NOTLOGIN, msg=error_map.get(RET.NOTLOGIN)) else: g.user = user return func(*args, **kwargs)
def car_update_request(): """修改车辆信息""" # carId = request.form.get("carId") id = request.form.get("id") carLicense = request.form.get("carLicense") if not id: return jsonify(code=RET.PARAMERR, msg="id参数不足") # if not carId: # return jsonify(code=RET.PARAMERR, msg="车辆ID不可为空") if not carLicense: return jsonify(code=RET.PARAMERR, msg="车辆牌照不可为空") # res_carId = re.match("^[0-9]{3,6}$", carId) # if not res_carId: # return jsonify(code=RET.PARAMERR, msg='车辆ID格式错误(只可包含数字)') re_carLicense = re.match( '^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$', carLicense) if not re_carLicense: return jsonify(code=RET.PARAMERR, msg='车辆牌照格式错误') try: car3 = CarInfo.query.filter(CarInfo.id == id).first() if not car3: return jsonify(code=RET.PARAMERR, msg="车辆不存在") if car3.online: return jsonify(code=RET.PARAMERR, msg="车辆在线不能操作") # car1 = CarInfo.query.filter(and_(CarInfo.carId == carId, CarInfo.id != id)).first() # if car1: # return jsonify(code=RET.PARAMERR, msg="车辆ID已存在,请更换有效ID。") car2 = CarInfo.query.filter( and_(CarInfo.carLicense == carLicense, CarInfo.id != id)).first() if car2: return jsonify(code=RET.PARAMERR, msg="车辆牌照已存在,请更换有效名称。") old_carId = car3.carId # app = AppInfo.query.filter(and_(AppInfo.type == "1", AppInfo.deviceId == old_carId)).first() # app.deviceId = carId # db.session.add(app) # car3.carId = carId car3.carLicense = carLicense db.session.add(car3) db.session.commit() write_redis_message_order(type="1", msg={ "type": "1", "online": 0, "deviceid": old_carId }) return jsonify(code=RET.OK, msg="修改保存成功") except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR))
def connect_get_list_request(): """获取所有连接的详情""" try: conns = ConnectDevice.query.all() data_list = list() for conn in conns: car = CarInfo.query.filter(CarInfo.carId == conn.car_id).first() platform = PlatformInfo.query.filter( PlatformInfo.platformId == conn.platform_id).first() data_list.append({ "carInfo": car.to_dict(), "platformInfo": platform.to_dict(), "create_at": conn.create_at.strftime("%Y-%m-%d %H:%M:%S") }) return jsonify(code=RET.OK, msg=error_map.get(RET.OK), data=data_list) except Exception as e: current_app.looger.error(e) return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR))
def user_login_request(): username = request.form.get("username") pwd = request.form.get("pwd") if not all([username, pwd]): return jsonify(code=RET.PARAMERR, msg=error_map.get(RET.PARAMERR)) try: user = UserInfo.query.filter(UserInfo.username==username).first() if not user: return jsonify(code=RET.PARAMERR, msg="用户名不存在") if user.pwd != pwd: return jsonify(code=RET.PARAMERR, msg="密码不正确") session["userinfo"] = user.to_dict() session["userid"] = user.id g.user = user token = user.generate_user_token() except Exception as e: # print(e) current_app.logger.error(e) return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR)) return jsonify(code=RET.OK, msg=error_map.get(RET.OK), token=token)
def platform_update_request(): """修改架台信息""" # platformId = request.form.get("platformId") platformName = request.form.get("platformName") id = request.form.get("id") if not id: return jsonify(code=RET.PARAMERR, msg="id不能为空") # if not platformId: # return jsonify(code=RET.PARAMERR, msg="台架ID不可为空") if not platformName: return jsonify(code=RET.PARAMERR, msg="台架名称不可为空") # res_platformId = re.match("^[0-9]{3,6}$", platformId) # if not res_platformId: # return jsonify(code=RET.PARAMERR, msg='台架ID格式错误(3-6位纯数字)') re_platformName = re.match(r"^[\u4E00-\u9FA5a-zA-Z0-9]{1}[\u4E00-\u9FA5a-zA-Z0-9_]{2,7}$", platformName) if not re_platformName: return jsonify(code=RET.PARAMERR, msg='台架名称格式错误') try: plat1 = PlatformInfo.query.filter(PlatformInfo.id==id).first() if not plat1: return jsonify(code=RET.PARAMERR, msg='该台架不存在') if plat1.online: return jsonify(code=RET.PARAMERR, msg='台架在线不能操作') # plat3 = PlatformInfo.query.filter(and_(PlatformInfo.id != id, # PlatformInfo.platformId == platformId)).first() # if plat3: # return jsonify(code=RET.PARAMERR, msg="台架ID已存在,请更换有效ID") plat2 = PlatformInfo.query.filter(and_(PlatformInfo.id!=id, PlatformInfo.platformName==platformName)).first() if plat2: return jsonify(code=RET.PARAMERR, msg="台架名称已存在,请更换有效名称") old_platformid = plat1.platformId # app = AppInfo.query.filter(and_(AppInfo.type=="2", AppInfo.deviceId==old_platformid)).first() # app.deviceId = platformId # db.session.add(app) # plat1.platformId = platformId plat1.platformName = platformName db.session.add(plat1) db.session.commit() write_redis_message_order(type="1", msg={"type": "2", "online": 0, "deviceid": old_platformid}) except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR)) return jsonify(code=RET.OK, msg="修改保存成功")
def platform_delete_request(): """删除架台""" platformId = request.args.get("platformId") if not platformId: return jsonify(code=RET.PARAMERR, msg=error_map.get(RET.PARAMERR)) try: conn = ConnectDevice.query.filter(ConnectDevice.platform_id==platformId).first() if conn: return jsonify(code=RET.PARAMERR, msg="台架已连接,请先断开") plat = PlatformInfo.query.filter(PlatformInfo.platformId==platformId).first() if not plat: return jsonify(code=RET.PARAMERR, msg=error_map.get(RET.PARAMERR)) app = AppInfo.query.filter(and_(AppInfo.deviceId==platformId, AppInfo.type=="2")).first() if app: db.session.delete(app) db.session.delete(plat) db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR)) return jsonify(code=RET.OK, msg=error_map.get(RET.OK))
def get_platforminfo_request(): """获取架台信息""" platformId = request.args.get("platformId", None) if not platformId: return jsonify(code=RET.PARAMERR, msg=error_map.get(RET.PARAMERR)) try: conns = ConnectDevice.query.all() conn_platform_id_list = [conn.platform_id for conn in conns] platform = PlatformInfo.query.filter(PlatformInfo.platformId==platformId).first() if not platform: return jsonify(code=RET.PARAMERR, msg=error_map.get(RET.PARAMERR)) data = platform.to_dict() if platformId in conn_platform_id_list: data["is_conn"] = 1 else: data["is_conn"] = 0 data["app_key"] = platform.get_app_key() data["app_secret"] = platform.get_app_secret() except Exception as e: # print(e) current_app.logger.error(e) return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR)) return jsonify(code=RET.OK, msg=error_map.get(RET.OK), data=data)
def get_carinfo_request(): """获取车辆详情""" carId = request.args.get("carId", None) if not carId: return jsonify(code=RET.PARAMERR, msg=error_map.get(RET.PARAMERR)) try: car = CarInfo.query.filter(CarInfo.carId == carId).first() if not car: return jsonify(code=RET.PARAMERR, msg=error_map.get(RET.PARAMERR)) conns = ConnectDevice.query.all() conn_car_id_list = [conn.car_id for conn in conns] data = car.to_dict() if carId in conn_car_id_list: data["is_conn"] = 1 else: data["is_conn"] = 0 data["app_key"] = car.get_app_key() data["app_secret"] = car.get_app_secret() except Exception as e: # print(e) current_app.logger.error(e) return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR)) return jsonify(code=RET.OK, msg=error_map.get(RET.OK), data=data)
def car_delete_request(): """删除车辆""" try: carId = request.args.get("carId") if not carId: return jsonify(code=RET.PARAMERR, msg=error_map.get(RET.PARAMERR)) car = CarInfo.query.filter(CarInfo.carId == carId).first() if not car: return jsonify(code=RET.PARAMERR, msg=error_map.get(RET.PARAMERR)) conn = ConnectDevice.query.filter( ConnectDevice.car_id == carId).first() if conn: return jsonify(code=RET.PARAMERR, msg="车辆已连接,请先断开连接") app = AppInfo.query.filter( and_(AppInfo.type == "1", AppInfo.deviceId == carId)).first() if app: db.session.delete(app) db.session.delete(car) db.session.commit() return jsonify(code=RET.OK, msg=error_map.get(RET.OK)) except Exception as e: current_app.logger.error(e) db.session.rollback() return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR))
def platform_add_request(): """新增架台""" # platformId = request.form.get("platformId") platformName = request.form.get("platformName") # platformIp = request.form.get("platformIp") # parsePort = request.form.get("parsePort") plat_last = PlatformInfo.query.filter().order_by(PlatformInfo.platformId.desc()).first() if not plat_last: platformId = '0001' else: platformId = '%04d' % (int(plat_last.platformId) + 1) if not platformId: return jsonify(code=RET.PARAMERR, msg="台架ID不可为空") if not platformName: return jsonify(code=RET.PARAMERR, msg="台架名称不可为空") res_platformId = re.match("^[0-9]{3,6}$", platformId) if not res_platformId: return jsonify(code=RET.PARAMERR, msg='台架ID格式错误(只可包含数字)') re_platformName = re.match(r"^[\u4E00-\u9FA5a-zA-Z0-9]{1}[\u4E00-\u9FA5a-zA-Z0-9_]{2,7}$", platformName) if not re_platformName: return jsonify(code=RET.PARAMERR, msg='台架名称格式错误') parsePort = "6089" print('ssss', platformId, type(platformId)) try: platformall = PlatformInfo.query.all() allips = [p.platformIp for p in platformall] while True: platformIp = "192.168.50.{}".format(random.randint(0, 256)) if platformIp not in allips: break plat1 = PlatformInfo.query.filter(PlatformInfo.platformId==platformId).first() if plat1: return jsonify(code=RET.PARAMERR, msg="台架ID已存在,请更换有效ID。") plat2 = PlatformInfo.query.filter(PlatformInfo.platformName==platformName).first() if plat2: return jsonify(code=RET.PARAMERR, msg="台架名称已存在,请更换有效名称。") plat = PlatformInfo() plat.platformId = platformId plat.platformName = platformName plat.platformIp = platformIp plat.parsePort = parsePort plat.online = 0 plat.create_at = datetime.datetime.now() app = AppInfo() app.type = "2" print(platformId, type(platformId)) app.deviceId = platformId # app.app_key = get_16_md5(f"{int(time.time()*100)}{platformId}") # app.app_secret = str(uuid.uuid4().hex) app_key = get_16_md5("{}{}".format(int(time.time()*100), platformId)) app.app_key = app_key # app.app_secret = get_16_md5(f"{app_key}{platformId}") app.app_secret = get_16_md5("{}{}".format(app_key, platformId)) db.session.add(plat) db.session.add(app) db.session.commit() msg = {"type": "2", "online": 0, "deviceid": platformId} write_redis_message_order(type="1", msg=msg) except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR)) return jsonify(code=RET.OK, msg="台架添加成功")
def conn_dstatus_device(): carId = request.form.get("carId") platformId = request.form.get("platformId") is_conn = request.form.get("is_conn") try: if is_conn not in ["1", "0"]: return jsonify(code=RET.PARAMERR, msg="is_conn:" + error_map.get(RET.PARAMERR)) if not all([carId, platformId, is_conn]): return jsonify(code=RET.PARAMERR, msg=error_map.get(RET.PARAMERR)) car = CarInfo.query.filter( and_(CarInfo.carId == carId, CarInfo.online == 1)).first() plat = PlatformInfo.query.filter( and_(PlatformInfo.platformId == platformId, PlatformInfo.online == 1)).first() if not all([car, plat]): return jsonify(code=RET.PARAMERR, msg="车辆或架台不在线无法操作") conn_real = ConnectDevice.query.all() car_id_list = [conn.car_id for conn in conn_real] platform_id_list = [conn.platform_id for conn in conn_real] if is_conn == "1": # 需要连接 if carId in car_id_list: return jsonify(code=RET.PARAMERR, msg="车辆 已连接请先断开") if platformId in platform_id_list: return jsonify(code=RET.PARAMERR, msg="架台 已连接请先断开") newconn = ConnectDevice() newconn.car_id = carId newconn.platform_id = platformId newconn.create_at = datetime.datetime.now() db.session.add(newconn) elif is_conn == "0": # 断开 if carId not in car_id_list: return jsonify(code=RET.PARAMERR, msg="车辆 未连接") if platformId not in platform_id_list: return jsonify(code=RET.PARAMERR, msg="架台 未连接") delconn = ConnectDevice.query.filter( and_(ConnectDevice.platform_id == platformId, ConnectDevice.car_id == carId)).first() if not delconn: return jsonify(code=RET.PARAMERR, msg="车辆与架台 未连接") db.session.delete(delconn) db.session.commit() except Exception as e: # print(e) current_app.logger.error(e) db.session.rollback() return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR)) conn = { "carId": carId, "platformId": platformId, "is_conn": is_conn, "platformIp": plat.platformIp, "parsePort": int(plat.parsePort) } try: conn_socket(conn["carId"], conn) except Exception as e: # print(e) current_app.logger.error(e) db.session.rollback() return jsonify(code=RET.DBERR, msg='链接失败') # 下达命令 同步数据向数据中心 write_redis_message_order( "2", { "carId": carId, "platformId": platformId, "is_conn": is_conn, "platformIp": plat.platformIp, "parsePort": plat.parsePort }) # 将消息放入socketio的消息队列中 write_redis_message_order("1", { "type": "1", "deviceid": carId, "online": 1 }) write_redis_message_order("1", { "type": "2", "deviceid": platformId, "online": 1 }) return jsonify(code=RET.OK, msg=error_map.get(RET.OK))
def user_logout_request(): session.clear() return jsonify(code=RET.OK, msg=error_map.get(RET.OK))
def car_add_request(): """新增车辆""" # carId = request.form.get("carId") carLicense = request.form.get("carLicense") car_last = CarInfo.query.filter().order_by(CarInfo.carId.desc()).first() if not car_last: carId = '0001' else: carId = '%04d' % (int(car_last.carId) + 1) if not carId: return jsonify(code=RET.PARAMERR, msg="车辆ID不可为空") if not carLicense: return jsonify(code=RET.PARAMERR, msg="车辆牌照不可为空") res_carId = re.match("^[0-9]{3,6}$", carId) if not res_carId: return jsonify(code=RET.PARAMERR, msg="车辆ID格式错误(3-6位纯数字)") re_carLicense = re.match( '^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$', carLicense) if not re_carLicense: return jsonify(code=RET.PARAMERR, msg="车辆牌照格式错误") try: caripall = [car.carIp for car in CarInfo.query.all()] while True: carIp = "192.168.50.{}".format(random.randint(0, 256)) if carIp not in caripall: break car1 = CarInfo.query.filter(CarInfo.carId == carId).first() if car1: return jsonify(code=RET.PARAMERR, msg="车辆ID已存在,请更换有效ID") car2 = CarInfo.query.filter(CarInfo.carLicense == carLicense).first() if car2: return jsonify(code=RET.PARAMERR, msg="车辆牌照已存在,请更换有效名称") car = CarInfo() car.carId = carId car.carIp = carIp car.carLicense = carLicense car.online = 0 car.create_at = datetime.datetime.now() app = AppInfo() app.deviceId = carId app.type = "1" # app.app_key = get_16_md5(f"{int(time.time()*100)}{carId}") # app.app_secret = str(uuid.uuid4().hex) # app_key = get_16_md5(f"{int(time.time()*100)}{carId}") app_key = get_16_md5("{}{}".format(int(time.time() * 100), carId)) app.app_key = app_key # app.app_secret = get_16_md5(f"{app_key}{carId}") app.app_secret = get_16_md5("{}{}".format(app_key, carId)) db.session.add(app) db.session.add(car) db.session.commit() msg = {"type": "1", "online": 0, "deviceid": carId} write_redis_message_order(type="1", msg=msg) return jsonify(code=RET.OK, msg="车辆添加成功") except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(code=RET.DBERR, msg=error_map.get(RET.DBERR))