getconf.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import datetime
  2. import json
  3. import pymysql
  4. import os
  5. import sys
  6. import time
  7. import requests
  8. import base64
  9. import configparser
  10. from sanic import Sanic
  11. from sanic.response import text, json
  12. from sanic_cors import CORS, cross_origin
  13. from sanic import response
  14. from mysqlDataBase import MysqldbOperational
  15. from configobj import ConfigObj
  16. app = Sanic(__name__)
  17. CORS(app)
  18. @app.route('/marinePlatform/v1/devices/frameExtractionSetting', methods=['POST'])
  19. async def capture_getconf(request):
  20. result = updata_conf(request.json)
  21. return response.text(result)
  22. def update_sql_splicing(tablename, attrs_dict, cond_dict):
  23. """更新数据
  24. args:
  25. tablename :表名字
  26. attrs_dict :更改属性键值对字典
  27. cond_dict :更新条件字典
  28. example:
  29. params = {"name" : "caixinglong", "age" : "38"}
  30. cond_dict = {"name" : "liuqiao", "age" : "18"}
  31. mydb.update(table, params, cond_dict)
  32. """
  33. attrs_list = []
  34. consql = ' '
  35. for y in attrs_dict:
  36. if not isinstance(attrs_dict[y], str):
  37. attrs_dict[y] = str(attrs_dict[y])
  38. for x in cond_dict:
  39. if not isinstance(cond_dict[x], str):
  40. cond_dict[x] = str(cond_dict[x])
  41. for tmpkey, tmpvalue in attrs_dict.items():
  42. attrs_list.append("`" + tmpkey + "`" + "=" + "\'" + tmpvalue + "\'")
  43. attrs_sql = ",".join(attrs_list)
  44. #print("attrs_sql:", attrs_sql)
  45. if cond_dict != '':
  46. for k, v in cond_dict.items():
  47. if isinstance(v, str):
  48. v = "\'" + v + "\'"
  49. consql = consql + "`" + tablename + "`." + "`" + k + "`" + '=' + v + ' and '
  50. consql = consql + ' 1=1 '
  51. sql = "UPDATE %s SET %s where%s" % (tablename, attrs_sql, consql)
  52. #print(sql)
  53. return sql
  54. def updata_conf(json_in):
  55. cond_dict = {"conf_type": 'camera_capture'}
  56. json_in["is_read"] = "0"
  57. try:
  58. db = pymysql.connect(mysql_host, mysql_username, mysql_password, mysql_database)
  59. sql = update_sql_splicing('camera_strategy_table', json_in, cond_dict)
  60. cursor = db.cursor()
  61. cursor.execute(sql)
  62. db.commit()
  63. db.close()
  64. return 'SUCCESS'
  65. except:
  66. return 'ERROR'
  67. @app.route('/marinePlatform/v2/data/history', methods=['POST'])
  68. async def delete_file(request):
  69. try:
  70. if request.json['token'] == token:
  71. result = find_and_delete_file(file_dir,request.json['fileNames'])
  72. return response.text(result)
  73. else:
  74. #return response.text('ERROR')
  75. return response.text('Token not exist')
  76. except:
  77. #return response.text('ERROR')
  78. return response.text('data format error!')
  79. def find_and_delete_file(file_dir,name_list):
  80. #result_list = []
  81. file_exists = True
  82. for index in range(0,len(name_list)):
  83. if os.path.exists(file_dir+name_list[index]) == False:
  84. file_exists = False
  85. #return "ERROR"
  86. return "error " + file_dir+name_list[index] + " not find"
  87. if file_exists == True:
  88. for index in range(0,len(name_list)):
  89. #result_dict = {}
  90. result = delete_single_file(file_dir,name_list[index])
  91. #result_dict["file_name"] = result_dict
  92. #result_dict["delete"] = result
  93. #result_list.append(result_dict)
  94. return "SUCCESS"
  95. #return result_list
  96. def delete_single_file(file_dir,file_name):
  97. try:
  98. os.remove(file_dir+file_name)
  99. return True
  100. except:
  101. return False
  102. # ===============================================================================
  103. # =================================== 新增部分 ===================================
  104. # ===============================================================================
  105. @app.route('/marinePlatform/v1/app/appToken', methods=['POST'])
  106. async def update_token(request):
  107. """
  108. 下行数据:下发平台验证token
  109. :param request:
  110. :return:
  111. """
  112. print("下发平台验证token request.json=", request.json)
  113. if request.json['old-token'] == token:
  114. # noinspection PyBroadException
  115. try:
  116. change_config = ConfigObj("config.ini", encoding='UTF8')
  117. # 修改配置文件中的token
  118. change_config['General']['token'] = request.json['token']
  119. change_config.write()
  120. result = "SUCCESS"
  121. except Exception as e:
  122. print("e=", e)
  123. result = "ERROR"
  124. else:
  125. result = "token 验证失败!"
  126. print("下发平台验证token result", result)
  127. return response.text(result)
  128. @app.route('/marinePlatform/v1/data/extremeModeSetting', methods=['POST'])
  129. async def set_extreme_mode(request):
  130. """
  131. 设置极端模式规则
  132. :param request:
  133. :return:
  134. """
  135. if request.json['token'] == token:
  136. extreme_case_make_id = request.json["extreme_case_make_id"]
  137. posterize_rule = request.json["posterize_rule"]
  138. '''未完成...'''
  139. else:
  140. print("token 验证失败!")
  141. @app.route('/marinePlatform/v1/devices/delFrameExtraction', methods=['POST'])
  142. async def del_camera(request):
  143. """
  144. 下行数据:删除摄像头
  145. :param request:
  146. :return:
  147. """
  148. print("删除摄像头 request.json=", request.json)
  149. receive_args = request.json
  150. if receive_args['token'] == token:
  151. if "id" in request.json.keys():
  152. camera_id = receive_args["id"]
  153. del_sql = "DELETE FROM camera_configuration_table WHERE camera_id in %s;"
  154. if camera_configuration_sql.commit_sql(del_sql, camera_id):
  155. result = "SUCCESS"
  156. else:
  157. result = "ERROR"
  158. else:
  159. result = "未传入摄像头id"
  160. else:
  161. result = "token 验证失败!"
  162. print("删除摄像头 result", result)
  163. return response.text(result)
  164. @app.route('/marinePlatform/v1/devices/setVideoRecognize', methods=['POST'])
  165. async def camera_image_recognize_conf(request):
  166. """
  167. 下行数据:设置摄像头图像识别
  168. :param request:
  169. :return:
  170. """
  171. print("设置摄像头图像识别 request.json=", request.json)
  172. result = process_args(request.json)
  173. print("设置摄像头图像识别 result", result)
  174. return response.text(result)
  175. def process_args(receive_args):
  176. """
  177. 处理接收到的参数
  178. :param receive_args:
  179. :return:
  180. """
  181. if receive_args['token'] == token:
  182. value = []
  183. value.append(receive_args["camera_id"])
  184. value.append(int(receive_args["recognize_rate"]))
  185. value.append(receive_args["camera_ip"])
  186. value.append(int(receive_args["nvr_port"]))
  187. value.append(receive_args["user_name"])
  188. value.append(receive_args["password"])
  189. value.append(receive_args["channel_id"])
  190. insert_sql = "INSERT INTO camera_configuration_table " \
  191. "(camera_id,recognize_rate,camera_ip,nvr_port, user_name,password,channel_id) " \
  192. "VALUES %s;"
  193. if camera_configuration_sql.commit_sql(insert_sql, value):
  194. result = "SUCCESS"
  195. else:
  196. result = "ERROR"
  197. else:
  198. result = "token 验证失败!"
  199. return result
  200. if __name__ == "__main__":
  201. file_dir = r'/home/sencott/imageCapture/HDU/'
  202. # 创建读取配置文件对象
  203. config = configparser.ConfigParser()
  204. config.read("config.ini", encoding="utf-8")
  205. # 获取通用配置项
  206. section = "General" # 读取的部section标签
  207. token = config.get(section, 'token')
  208. mysql_host = config.get(section, 'mysqlHost')
  209. mysql_username = config.get(section, 'mysqlUsername')
  210. mysql_password = config.get(section, 'mysqlPassword')
  211. mysql_port = config.getint(section, 'mysqlPort')
  212. # 获取特有配置项
  213. mysql_database = config.get('api', 'mysqlDatabase')
  214. camera_configuration_sql = MysqldbOperational(host=mysql_host,
  215. username=mysql_username,
  216. password=mysql_password,
  217. port=mysql_port,
  218. database=mysql_database)
  219. app.run(host="0.0.0.0", port=8765, workers=1, debug=False, access_log=False)