123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- import datetime
- import json
- import pymysql
- import os
- import sys
- import time
- import requests
- import base64
- import configparser
- from sanic import Sanic
- from sanic.response import text, json
- from sanic_cors import CORS, cross_origin
- from sanic import response
- from mysqlDataBase import MysqldbOperational
- from configobj import ConfigObj
- app = Sanic(__name__)
- CORS(app)
- @app.route('/marinePlatform/v1/devices/frameExtractionSetting', methods=['POST'])
- async def capture_getconf(request):
- result = updata_conf(request.json)
- return response.text(result)
- def update_sql_splicing(tablename, attrs_dict, cond_dict):
- """更新数据
- args:
- tablename :表名字
- attrs_dict :更改属性键值对字典
- cond_dict :更新条件字典
- example:
- params = {"name" : "caixinglong", "age" : "38"}
- cond_dict = {"name" : "liuqiao", "age" : "18"}
- mydb.update(table, params, cond_dict)
- """
- attrs_list = []
- consql = ' '
- for y in attrs_dict:
- if not isinstance(attrs_dict[y], str):
- attrs_dict[y] = str(attrs_dict[y])
- for x in cond_dict:
- if not isinstance(cond_dict[x], str):
- cond_dict[x] = str(cond_dict[x])
- for tmpkey, tmpvalue in attrs_dict.items():
- attrs_list.append("`" + tmpkey + "`" + "=" + "\'" + tmpvalue + "\'")
- attrs_sql = ",".join(attrs_list)
- #print("attrs_sql:", attrs_sql)
- if cond_dict != '':
- for k, v in cond_dict.items():
- if isinstance(v, str):
- v = "\'" + v + "\'"
- consql = consql + "`" + tablename + "`." + "`" + k + "`" + '=' + v + ' and '
- consql = consql + ' 1=1 '
- sql = "UPDATE %s SET %s where%s" % (tablename, attrs_sql, consql)
- #print(sql)
- return sql
- def updata_conf(json_in):
- cond_dict = {"conf_type": 'camera_capture'}
- json_in["is_read"] = "0"
- try:
- db = pymysql.connect(mysql_host, mysql_username, mysql_password, mysql_database)
- sql = update_sql_splicing('camera_strategy_table', json_in, cond_dict)
- cursor = db.cursor()
- cursor.execute(sql)
- db.commit()
- db.close()
- return 'SUCCESS'
- except:
- return 'ERROR'
- @app.route('/marinePlatform/v2/data/history', methods=['POST'])
- async def delete_file(request):
- try:
- if request.json['token'] == token:
- result = find_and_delete_file(file_dir,request.json['fileNames'])
- return response.text(result)
- else:
- #return response.text('ERROR')
- return response.text('Token not exist')
- except:
- #return response.text('ERROR')
- return response.text('data format error!')
- def find_and_delete_file(file_dir,name_list):
- #result_list = []
- file_exists = True
- for index in range(0,len(name_list)):
- if os.path.exists(file_dir+name_list[index]) == False:
- file_exists = False
- #return "ERROR"
- return "error " + file_dir+name_list[index] + " not find"
- if file_exists == True:
- for index in range(0,len(name_list)):
- #result_dict = {}
- result = delete_single_file(file_dir,name_list[index])
- #result_dict["file_name"] = result_dict
- #result_dict["delete"] = result
- #result_list.append(result_dict)
- return "SUCCESS"
- #return result_list
- def delete_single_file(file_dir,file_name):
- try:
- os.remove(file_dir+file_name)
- return True
- except:
- return False
- # ===============================================================================
- # =================================== 新增部分 ===================================
- # ===============================================================================
- @app.route('/marinePlatform/v1/app/appToken', methods=['POST'])
- async def update_token(request):
- """
- 下行数据:下发平台验证token
- :param request:
- :return:
- """
- print("下发平台验证token request.json=", request.json)
- if request.json['old-token'] == token:
- # noinspection PyBroadException
- try:
- change_config = ConfigObj("config.ini", encoding='UTF8')
- # 修改配置文件中的token
- change_config['General']['token'] = request.json['token']
- change_config.write()
- result = "SUCCESS"
- except Exception as e:
- print("e=", e)
- result = "ERROR"
- else:
- result = "token 验证失败!"
- print("下发平台验证token result", result)
- return response.text(result)
- @app.route('/marinePlatform/v1/data/extremeModeSetting', methods=['POST'])
- async def set_extreme_mode(request):
- """
- 设置极端模式规则
- :param request:
- :return:
- """
- if request.json['token'] == token:
- extreme_case_make_id = request.json["extreme_case_make_id"]
- posterize_rule = request.json["posterize_rule"]
- '''未完成...'''
- else:
- print("token 验证失败!")
- @app.route('/marinePlatform/v1/devices/delFrameExtraction', methods=['POST'])
- async def del_camera(request):
- """
- 下行数据:删除摄像头
- :param request:
- :return:
- """
- print("删除摄像头 request.json=", request.json)
- receive_args = request.json
- if receive_args['token'] == token:
- if "id" in request.json.keys():
- camera_id = receive_args["id"]
- del_sql = "DELETE FROM camera_configuration_table WHERE camera_id in %s;"
- if camera_configuration_sql.commit_sql(del_sql, camera_id):
- result = "SUCCESS"
- else:
- result = "ERROR"
- else:
- result = "未传入摄像头id"
- else:
- result = "token 验证失败!"
- print("删除摄像头 result", result)
- return response.text(result)
- @app.route('/marinePlatform/v1/devices/setVideoRecognize', methods=['POST'])
- async def camera_image_recognize_conf(request):
- """
- 下行数据:设置摄像头图像识别
- :param request:
- :return:
- """
- print("设置摄像头图像识别 request.json=", request.json)
- result = process_args(request.json)
- print("设置摄像头图像识别 result", result)
- return response.text(result)
- def process_args(receive_args):
- """
- 处理接收到的参数
- :param receive_args:
- :return:
- """
- if receive_args['token'] == token:
- value = []
- value.append(receive_args["camera_id"])
- value.append(int(receive_args["recognize_rate"]))
- value.append(receive_args["camera_ip"])
- value.append(int(receive_args["nvr_port"]))
- value.append(receive_args["user_name"])
- value.append(receive_args["password"])
- value.append(receive_args["channel_id"])
- insert_sql = "INSERT INTO camera_configuration_table " \
- "(camera_id,recognize_rate,camera_ip,nvr_port, user_name,password,channel_id) " \
- "VALUES %s;"
- if camera_configuration_sql.commit_sql(insert_sql, value):
- result = "SUCCESS"
- else:
- result = "ERROR"
- else:
- result = "token 验证失败!"
- return result
- if __name__ == "__main__":
- file_dir = r'/home/sencott/imageCapture/HDU/'
- # 创建读取配置文件对象
- config = configparser.ConfigParser()
- config.read("config.ini", encoding="utf-8")
- # 获取通用配置项
- section = "General" # 读取的部section标签
- token = config.get(section, 'token')
- mysql_host = config.get(section, 'mysqlHost')
- mysql_username = config.get(section, 'mysqlUsername')
- mysql_password = config.get(section, 'mysqlPassword')
- mysql_port = config.getint(section, 'mysqlPort')
- # 获取特有配置项
- mysql_database = config.get('api', 'mysqlDatabase')
- camera_configuration_sql = MysqldbOperational(host=mysql_host,
- username=mysql_username,
- password=mysql_password,
- port=mysql_port,
- database=mysql_database)
- app.run(host="0.0.0.0", port=8765, workers=1, debug=False, access_log=False)
|