ReportUpload.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. #!/usr/bin/env python
  2. import datetime
  3. import json
  4. from typing import List, Any
  5. import pymysql
  6. import time
  7. from sanic import Sanic
  8. from sanic.response import text, json
  9. from sanic_cors import CORS, cross_origin
  10. from sanic import response
  11. from sanic.response import file
  12. import math
  13. import base64
  14. import logging
  15. from video_capture import LOG_LEVEL
  16. from configparser import ConfigParser
  17. config = ConfigParser()
  18. config.read(r'./config.ini',encoding='utf-8')
  19. #Mysql
  20. MYSQL_HOST = config['mysql']['MYSQL_HOST']
  21. MYSQL_PORT = int(config['mysql']['MYSQL_PORT'])
  22. MYSQL_USER = config['mysql']['MYSQL_USER']
  23. MYSQL_PASSWORD = config['mysql']['MYSQL_PASSWORD']
  24. MYSQL_DATABASE = config['mysql']['MYSQL_DATABASE']
  25. SONAR_DATABASE = config['mysql']['SONAR_DATABASE']
  26. def logger_init(LOG_LEVEL):
  27. logger = logging.getLogger('mylogger.c')
  28. #set log level WARNING
  29. logger.setLevel(LOG_LEVEL)
  30. return logger
  31. logger = logger_init(LOG_LEVEL)
  32. app = Sanic(__name__)
  33. CORS(app)
  34. @app.route('/readReal', methods=['POST'])
  35. async def post_handler(request):
  36. # print(request)
  37. # print(request.json['dataB'])
  38. # print(request.json['dataE'])
  39. dataB = request.json["dataB"] + " 00:00:00"
  40. dataE = request.json["dataE"] + " 23:59:59"
  41. cameraid = request.json["camera"]
  42. specices = request.json["specices"]
  43. # print("this0")
  44. # print(dataB)
  45. # print(dataE)
  46. # print(cameraid)
  47. # print(specices)
  48. # print("this1")
  49. list1 = []
  50. list2: List[Any] = []
  51. list3 = []
  52. list4 = []
  53. # print("this2")
  54. try:
  55. db = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=MYSQL_DATABASE)
  56. cursor = db.cursor()
  57. # print("read database0")
  58. #尺寸分布横坐标固定
  59. size_section_num = 10
  60. size_Xaxis_str = []
  61. # print("read database3")
  62. size_Xaxis_str.append("<100")
  63. # print("read database0")
  64. size_Xaxis_str.append("100~150")
  65. size_Xaxis_str.append("150~200")
  66. size_Xaxis_str.append("200~250")
  67. size_Xaxis_str.append("250~300")
  68. size_Xaxis_str.append("300~350")
  69. size_Xaxis_str.append("350~400")
  70. size_Xaxis_str.append("400~450")
  71. size_Xaxis_str.append("450~500")
  72. size_Xaxis_str.append(">500")
  73. # print("read database1")
  74. size_Xaxis = [100,150,200,250,300,350,400,450,500]
  75. # 重量分布横坐标固定
  76. # print("read database")
  77. weight_section_num = 11
  78. weight_Xaxis_str = []
  79. weight_Xaxis_str.append("<100")
  80. weight_Xaxis_str.append("100~200")
  81. weight_Xaxis_str.append("200~300")
  82. weight_Xaxis_str.append("300~400")
  83. weight_Xaxis_str.append("400~500")
  84. weight_Xaxis_str.append("500~600")
  85. weight_Xaxis_str.append("600~700")
  86. weight_Xaxis_str.append("700~800")
  87. weight_Xaxis_str.append("800~900")
  88. weight_Xaxis_str.append("900~1000")
  89. weight_Xaxis_str.append(">1000")
  90. weight_Xaxis = [100,200,300,400,500,600,700,800,900,1000]
  91. print("the sql2")
  92. ######**************************** list1:查询刷选条件,更新详情报表 **********************************
  93. sql = "select id, DATE_FORMAT(datetime, '%Y-%m-%d %H:%i:%s') as datetime, binocular_id, fish_species, fish_" \
  94. "size, fish_weight from binocular_data where datetime > " + "'" + dataB + "'" + " and datetime < " + "'" \
  95. + dataE + "'" + "and binocular_id in (" + cameraid + ")" + 'and fish_species in (' + specices + ");"
  96. # print("the sql1 is: " + sql)
  97. count = cursor.execute(sql)
  98. # print("select the number of sql is")
  99. # print(count)
  100. results = cursor.fetchall()
  101. for row in results:
  102. dict = {}
  103. dict['id'] = row[0]
  104. dict['datetime'] = row[1]
  105. dict['binocular_id'] = row[2]
  106. dict['fish_species'] = row[3]
  107. dict['fish_size'] = row[4]
  108. dict['fish_weight'] = row[5]
  109. list1.append(dict)
  110. # print("the final dict is:")
  111. # print(dict)
  112. # #####**************************** 拆分有多少种鱼 **********************************
  113. specicesList = str(specices).split(",")
  114. specicesNumber = len(specicesList)
  115. specicesListStr = []
  116. for i in range(specicesNumber):
  117. str1 = specicesList[i].replace('"','')
  118. specicesListStr.append(str1.replace(" ",""))
  119. # #####**************************** list4:百分比重图表 **********************************
  120. # 查询黑头鱼数量
  121. percent_dict = {}
  122. sql = "select *from binocular_data where datetime > " + "'" + dataB + "'" + " and datetime < " + \
  123. "'" + dataE + "'" + " and fish_species in ('许氏平鲉');"
  124. # print("the sql6 is: " + sql)
  125. percent_dict["许氏平鲉"] = cursor.execute(sql)
  126. # sql = "select *from binocular_data where datetime > " + "'" + dataB + "'" + " and datetime < " + \
  127. # "'" + dataE + "'" + " and fish_species in ('鲈鱼');"
  128. # print("the sql6 is: " + sql)
  129. # percent_dict["鲈鱼"] = cursor.execute(sql)
  130. list4.append(percent_dict)
  131. # print("the percent graph list4 is :")
  132. # print(list4)
  133. # #####**************************** list2:尺寸分布图表 **********************************
  134. # for j in range(specicesNumber):0
  135. list2_single = []
  136. #筛选各分段尺寸鱼数量
  137. for i in range(0, size_section_num):
  138. if i == 0:
  139. sql = "select *from binocular_data where fish_size < " + str(size_Xaxis[i]) + " and datetime > " +\
  140. "'" + dataB + "'" + " and datetime < " + "'" + dataE + "'" + " and binocular_id in (" + \
  141. cameraid + ")" + " and fish_species in (" + specicesList[0] + ");"
  142. elif i < (size_section_num - 1):
  143. sql = "select *from binocular_data where fish_size between " + str(size_Xaxis[i-1]) + " and " + \
  144. str(size_Xaxis[i]) + " and datetime > " + "'" + dataB + "'" + " and datetime < " + \
  145. "'" + dataE + "'" + " and binocular_id in (" + cameraid + ")" + " and fish_species in (" + \
  146. specicesList[0] + ");"
  147. else:
  148. sql = "select *from binocular_data where fish_size > " + str(size_Xaxis[i-1]) + " and datetime > " + \
  149. "'" + dataB + "'" + " and datetime < " + "'" + dataE + "'" + " and binocular_id in (" + \
  150. cameraid + ")" + " and fish_species in (" + specicesList[0] + ");"
  151. # print(sql)
  152. # print("the sql count is")
  153. # print(i)
  154. size_dict = {}
  155. if percent_dict[specicesListStr[0]] == 0:
  156. size_dict[size_Xaxis_str[i]] = 0
  157. else:
  158. size_dict[size_Xaxis_str[i]] = round(cursor.execute(sql) * 100 / percent_dict[specicesListStr[0]],2)
  159. list2_single.append(size_dict)
  160. list2.append({specicesListStr[0]: list2_single})
  161. # print(list2)
  162. # print("the size graph list2 is :")
  163. # print(list2)
  164. ######**************************** list3:重量分布图表 **********************************
  165. # print("fish size over")
  166. #for j in range(specicesNumber):
  167. list3_single = []
  168. i = 0
  169. for i in range(0, weight_section_num):
  170. # print("the sql weight count is")
  171. # print(i)
  172. if i == 0:
  173. sql = "select *from binocular_data where fish_weight < " + str(weight_Xaxis[i]) + " and datetime > " +\
  174. "'" + dataB + "'" + " and datetime < " + "'" + dataE + "'" + " and binocular_id in (" + \
  175. cameraid + ")" + " and fish_species in (" + specicesList[0] + ");"
  176. elif i < (weight_section_num - 1):
  177. sql = "select *from binocular_data where fish_weight between " + str(weight_Xaxis[i-1]) + " and " + \
  178. str(weight_Xaxis[i]) + " and datetime > " + "'" + dataB + "'" + " and datetime < " + \
  179. "'" + dataE + "'" + " and binocular_id in (" + cameraid + ")" + " and fish_species in (" + \
  180. specicesList[0] + ");"
  181. else:
  182. sql = "select *from binocular_data where fish_weight > "+ str(weight_Xaxis[i-1]) + " and datetime > "+ \
  183. "'" + dataB + "'" + " and datetime < " + "'" + dataE + "'" + " and binocular_id in (" + \
  184. cameraid + ")" + " and fish_species in (" + specicesList[0] + ");"
  185. # print(sql)
  186. weight_dict = {}
  187. # print("0")
  188. weight_value = cursor.execute(sql)
  189. # print(weight_value)
  190. if percent_dict[specicesListStr[0]] == 0:
  191. weight_dict[weight_Xaxis_str[i]] = 0
  192. else:
  193. weight_dict[weight_Xaxis_str[i]] = round(cursor.execute(sql) * 100 / percent_dict[specicesListStr[0]],2)
  194. # print(weight_dict[weight_Xaxis_str[i]])
  195. list3_single.append(weight_dict)
  196. # print("2")
  197. list3.append({specicesListStr[0]: list3_single})
  198. # print("the weight graph list3 is :")
  199. # print(list3)
  200. ######**************************** list1-4数据整合成js **********************************
  201. result_list = {"reports": list1, "size_graph": list2, "weight_graph": list3, "percent_graph": list4}
  202. # print("the result_list is:")
  203. # print(result_list)
  204. result_json = json(result_list)
  205. cursor.close()
  206. db.close()
  207. return result_json
  208. except Exception as e:
  209. #print(e)
  210. logger.warning(e)
  211. list1 = []
  212. return json(list1)
  213. return False
  214. @app.route('/get_pic', methods=['POST'])
  215. async def post_handler(request):
  216. imageID = request.json["id"]
  217. #print("the image id is: "+str(imageID))
  218. try:
  219. db = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=MYSQL_DATABASE)
  220. cursor = db.cursor()
  221. # #####**************************** list1:查询刷选条件,更新详情报表 **********************************
  222. sql = "select id, DATE_FORMAT(datetime, '%Y-%m-%d %H:%i:%s.%f') from binocular_data where id = " + str(imageID) + " ;"
  223. #print("the sql1 is: " + sql)
  224. count = cursor.execute(sql)
  225. #print("select the number of sql is")
  226. #print(count)
  227. results = cursor.fetchall()
  228. cursor.close()
  229. db.close()
  230. for row in results:
  231. datestr = row[1]
  232. #print("the final date str is:")
  233. #print(datestr)
  234. imageNameStr = datestr.replace(":","-")
  235. imageNameStr = imageNameStr.replace(" ","-")
  236. imageNameStr = imageNameStr[:21]
  237. #print(imageNameStr)
  238. #imagePath = '/home/sencott/binocular_project_1/image_path/'+imageNameStr+'.jpg'
  239. imagePath = '/data/image_path/'+imageNameStr+'.jpg'
  240. #print(imagePath)
  241. return await file(imagePath)
  242. except Exception as e:
  243. logger.warning(e)
  244. list1 = []
  245. return json(list1)
  246. return False
  247. @app.route('/setSpecices', methods=['POST'])
  248. async def post_handler(request):
  249. imageID = request.json["id"]
  250. specices = request.json["specices"]
  251. #print(id)
  252. #print(specices)
  253. list1 = []
  254. try:
  255. db = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=MYSQL_DATABASE)
  256. cursor = db.cursor()
  257. # #####**************************** list1:查询刷选条件,更新统计报表 **********************************
  258. sql = "update binocular_data set species_add = '"+ specices +"' WHERE id = "+ str(imageID) +";";
  259. #print("the sql1 is: " + sql)
  260. count = cursor.execute(sql)
  261. db.commit()
  262. #print("the sql count is "+str(count))
  263. dict = {}
  264. if count == 1:
  265. dict['state'] = "sucess"
  266. else:
  267. dict['state'] = "failed"
  268. #print(dict)
  269. cursor.close()
  270. db.close()
  271. list1.append(dict)
  272. #print(list1)
  273. listJson = json(list1)
  274. #print(listJson)
  275. return listJson
  276. except Exception as e:
  277. logger.warning(e)
  278. list1 = []
  279. return json(list1)
  280. return False
  281. @app.route('/statisticsReport', methods=['POST'])
  282. async def post_handler(request):
  283. dataB = request.json["dataB"]
  284. dataE = request.json["dataE"]
  285. specices = request.json["specices"]
  286. #print(dataB)
  287. #print(dataE)
  288. #print(specices)
  289. list1 = []
  290. try:
  291. db = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=MYSQL_DATABASE)
  292. cursor = db.cursor()
  293. # #####**************************** list1:查询刷选条件,更新统计报表 **********************************
  294. sql = "select id,DATE_FORMAT(datetime, '%Y-%m-%d') as datetime,fish_species,size_average," \
  295. "size_median,weight_average,weight_median from binocular_dailyreport where " \
  296. "datetime between " + "'" + dataB + "'" + " and " + "'" \
  297. + dataE + "'" + 'and fish_species in (' + specices + ");"
  298. #print("the sql1 is: " + sql)
  299. count = cursor.execute(sql)
  300. #print("select the number of sql is")
  301. #print(count)
  302. results = cursor.fetchall()
  303. for row in results:
  304. dict = {}
  305. dict['id'] = row[0]
  306. dict['datetime'] = row[1]
  307. dict['fish_species'] = row[2]
  308. dict['size_average'] = row[3]
  309. dict['size_median'] = row[4]
  310. dict['weight_average'] = row[5]
  311. dict['weight_median'] = row[6]
  312. list1.append(dict)
  313. #print("the final dict is:")
  314. #print(dict)
  315. result_json = json(list1)
  316. cursor.close()
  317. db.close()
  318. return result_json
  319. except Exception as e:
  320. logger.warning(e)
  321. list1 = []
  322. return json(list1)
  323. return False
  324. @app.route('/growTimeSave', methods=['POST'])
  325. async def post_handler(request):
  326. dataB = request.json["dataB"]
  327. dataE = request.json["dataE"]
  328. list1 = []
  329. try:
  330. db = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=SONAR_DATABASE)
  331. cursor = db.cursor()
  332. # #####**************************** list1:查询刷选条件,更新统计报表 **********************************
  333. sql = "insert into growtimesave(growTime_begin,growTime_end)values(" + "'" + dataB + "'," + "" + "'" + dataE + "'" + ");"
  334. # print("the sql1 is: " + sql)
  335. cursor.execute(sql)
  336. db.commit()
  337. result_json = json(list1)
  338. cursor.close()
  339. db.close()
  340. return result_json
  341. except Exception as e:
  342. logger.warning(e)
  343. list1 = []
  344. return json(list1)
  345. return False
  346. @app.route('/growTimeRead', methods=['POST'])
  347. async def post_handler(request):
  348. list1 = []
  349. try:
  350. db = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=SONAR_DATABASE)
  351. cursor = db.cursor()
  352. # #####**************************** list1:查询刷选条件,更新统计报表 **********************************
  353. sql = "select id,DATE_FORMAT(growTime_begin,'%Y-%m-%d') as growTime_begin,DATE_FORMAT(growTime_end,'%Y-%m-%d') as growTime_end from growtimesave ORDER BY id desc LIMIT 1;"
  354. # print("the sql1 is: " + sql)
  355. count = cursor.execute(sql)
  356. # print("select the number of sql is")
  357. # print(count)
  358. results = cursor.fetchall()
  359. for row in results:
  360. dict = {}
  361. dict['id'] = row[0]
  362. dict['growTime_begin'] = row[1]
  363. dict['growTime_end'] = row[2]
  364. list1.append(dict)
  365. # print("the final dict is:")
  366. # print(dict)
  367. result_json = json(list1)
  368. cursor.close()
  369. db.close()
  370. return result_json
  371. except Exception as e:
  372. logger.warning(e)
  373. list1 = []
  374. return json(list1)
  375. return False
  376. @app.route('/compassDataRead', methods=['POST'])
  377. async def post_handler(request):
  378. list1 = []
  379. try:
  380. db = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=SONAR_DATABASE)
  381. cursor = db.cursor()
  382. # #####**************************** list1:查询刷选条件,更新统计报表 **********************************
  383. sql = "select *from sonar_sensor_data LIMIT 1;"
  384. # print("the sql1 is: " + sql)
  385. count = cursor.execute(sql)
  386. # print("select the number of sql is")
  387. # print(count)
  388. results = cursor.fetchall()
  389. for row in results:
  390. dict = {}
  391. dict['depth'] = row[0]
  392. dict['pitch'] = row[1]
  393. dict['roll'] = row[2]
  394. dict['heading'] = row[3]
  395. list1.append(dict)
  396. # print("the final dict is:")
  397. # print(dict)
  398. result_json = json(list1)
  399. cursor.close()
  400. db.close()
  401. return result_json
  402. except Exception as e:
  403. logger.warning(e)
  404. list1 = []
  405. return json(list1)
  406. return False
  407. @app.route('/homePage', methods=['POST'])
  408. async def post_handler(request):
  409. dataB_growCycle = request.json["dataB_growCycle"] + " 00:00:00"
  410. dataE_growCycle = request.json["dataE_growCycle"] + " 23:59:59"
  411. dataB = request.json["dataB_distribute"] + " 00:00:00"
  412. dataE = request.json["dataE_distribute"] + " 23:59:59"
  413. cameraid = '"1","2","3","4","5"'
  414. specices = '"许氏平鲉"'
  415. # print(dataB)
  416. # print(dataE)
  417. list1 = [] #统计报表数据,鱼尺寸与重量生长曲线
  418. list2 = [] #详情报表尺寸分布
  419. list3 = [] #详情报表的重量分布
  420. list4 = [] #种类分布
  421. list5 = [] #鱼群数量变化曲线
  422. try:
  423. db = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=MYSQL_DATABASE)
  424. cursor = db.cursor()
  425. sql = "SELECT MAX(datetime) FROM binocular_data;"
  426. count = cursor.execute(sql)
  427. results = cursor.fetchall()[0][0].strftime("%Y-%m-%d")
  428. dataB = results + " 00:00:00"
  429. dataE = results + " 23:59:59"
  430. # #####**************************** list1:查询刷选条件,更新统计报表 **********************************
  431. sql = "select id,DATE_FORMAT(datetime, '%Y-%m-%d') as datetime,fish_species,size_average," \
  432. "size_median,weight_average,weight_median from binocular_dailyreport where datetime > " + "'" + dataB_growCycle + "'" + " and datetime < " + \
  433. "'" + dataE_growCycle + "'" + ";"
  434. #print("the sql1 is: " + sql)
  435. count = cursor.execute(sql)
  436. #print("select the number of sql is")
  437. #print(count)
  438. results = cursor.fetchall()
  439. for row in results:
  440. dict = {}
  441. dict['id'] = row[0]
  442. dict['datetime'] = row[1]
  443. dict['fish_species'] = row[2]
  444. dict['size_average'] = row[3]
  445. dict['size_median'] = row[4]
  446. dict['weight_average'] = row[5]
  447. dict['weight_median'] = row[6]
  448. list1.append(dict)
  449. # print("the final dict is:")
  450. # print(dict)
  451. # 尺寸分布横坐标固定
  452. size_section_num = 10
  453. size_Xaxis_str = []
  454. # print("read database3")
  455. size_Xaxis_str.append("<100")
  456. # print("read database0")
  457. size_Xaxis_str.append("100~150")
  458. size_Xaxis_str.append("150~200")
  459. size_Xaxis_str.append("200~250")
  460. size_Xaxis_str.append("250~300")
  461. size_Xaxis_str.append("300~350")
  462. size_Xaxis_str.append("350~400")
  463. size_Xaxis_str.append("400~450")
  464. size_Xaxis_str.append("450~500")
  465. size_Xaxis_str.append(">500")
  466. # print("read database1")
  467. size_Xaxis = [100, 150, 200, 250, 300, 350, 400, 450, 500]
  468. # 重量分布横坐标固定
  469. # print("read database")
  470. weight_section_num = 11
  471. weight_Xaxis_str = []
  472. weight_Xaxis_str.append("<100")
  473. weight_Xaxis_str.append("100~200")
  474. weight_Xaxis_str.append("200~300")
  475. weight_Xaxis_str.append("300~400")
  476. weight_Xaxis_str.append("400~500")
  477. weight_Xaxis_str.append("500~600")
  478. weight_Xaxis_str.append("600~700")
  479. weight_Xaxis_str.append("700~800")
  480. weight_Xaxis_str.append("800~900")
  481. weight_Xaxis_str.append("900~1000")
  482. weight_Xaxis_str.append(">1000")
  483. weight_Xaxis = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
  484. # print("the sql2")
  485. # #####**************************** list4:百分比重图表 **********************************
  486. # 查询黑头鱼数量
  487. percent_dict = {}
  488. sql = "select *from binocular_data where datetime > " + "'" + dataB + "'" + " and datetime < " + \
  489. "'" + dataE + "'" + " and fish_species in ('许氏平鲉');"
  490. # print("the sql6 is: " + sql)
  491. percent_dict["许氏平鲉"] = cursor.execute(sql)
  492. # sql = "select *from binocular_data where datetime > " + "'" + dataB + "'" + " and datetime < " + \
  493. # "'" + dataE + "'" + " and fish_species in ('鲈鱼');"
  494. # print("the sql6 is: " + sql)
  495. # percent_dict["鲈鱼"] = cursor.execute(sql)
  496. list4.append(percent_dict)
  497. # print("the percent graph list4 is :")
  498. # print(list4)
  499. # #####**************************** list2:尺寸分布图表 **********************************
  500. # for j in range(specicesNumber):0
  501. list2_single = []
  502. # 筛选各分段尺寸鱼数量
  503. for i in range(0, size_section_num):
  504. if i == 0:
  505. sql = "select *from binocular_data where fish_size < " + str(size_Xaxis[i]) + " and datetime > " + \
  506. "'" + dataB + "'" + " and datetime < " + "'" + dataE + "'" + " and binocular_id in (" + \
  507. cameraid + ")" + " and fish_species in ('许氏平鲉');"
  508. elif i < (size_section_num - 1):
  509. sql = "select *from binocular_data where fish_size between " + str(size_Xaxis[i - 1]) + " and " + \
  510. str(size_Xaxis[i]) + " and datetime > " + "'" + dataB + "'" + " and datetime < " + \
  511. "'" + dataE + "'" + " and binocular_id in (" + cameraid + ")" + " and fish_species in " \
  512. "('许氏平鲉');"
  513. else:
  514. sql = "select *from binocular_data where fish_size > " + str(
  515. size_Xaxis[i - 1]) + " and datetime > " + \
  516. "'" + dataB + "'" + " and datetime < " + "'" + dataE + "'" + " and binocular_id in (" + \
  517. cameraid + ")" + " and fish_species in ('许氏平鲉');"
  518. # print(sql)
  519. # print("the sql count is")
  520. # print(i)
  521. size_dict = {}
  522. if percent_dict["许氏平鲉"]==0:
  523. size_dict[size_Xaxis_str[i]] = 0
  524. else:
  525. size_dict[size_Xaxis_str[i]] = round(cursor.execute(sql) * 100 / percent_dict["许氏平鲉"], 2)
  526. list2_single.append(size_dict)
  527. list2.append({"许氏平鲉": list2_single})
  528. # print(list2)
  529. # print("the size graph list2 is :")
  530. # print(list2)
  531. ######**************************** list3:重量分布图表 **********************************
  532. # print("fish size over")
  533. # for j in range(specicesNumber):
  534. list3_single = []
  535. i = 0
  536. for i in range(0, weight_section_num):
  537. # print("the sql weight count is")
  538. # print(i)
  539. if i == 0:
  540. sql = "select *from binocular_data where fish_weight < " + str(
  541. weight_Xaxis[i]) + " and datetime > " + \
  542. "'" + dataB + "'" + " and datetime < " + "'" + dataE + "'" + " and binocular_id in (" + \
  543. cameraid + ")" + " and fish_species in ('许氏平鲉');"
  544. elif i < (weight_section_num - 1):
  545. sql = "select *from binocular_data where fish_weight between " + str(
  546. weight_Xaxis[i - 1]) + " and " + \
  547. str(weight_Xaxis[i]) + " and datetime > " + "'" + dataB + "'" + " and datetime < " + \
  548. "'" + dataE + "'" + " and binocular_id in (" + cameraid + ")" + " and fish_species in " \
  549. "('许氏平鲉');"
  550. else:
  551. sql = "select *from binocular_data where fish_weight > " + str(
  552. weight_Xaxis[i - 1]) + " and datetime > " + \
  553. "'" + dataB + "'" + " and datetime < " + "'" + dataE + "'" + " and binocular_id in (" + \
  554. cameraid + ")" + " and fish_species in ('许氏平鲉');"
  555. # print(sql)
  556. weight_dict = {}
  557. # print("0")
  558. weight_value = cursor.execute(sql)
  559. # print(weight_value)
  560. if percent_dict["许氏平鲉"] == 0:
  561. weight_dict[weight_Xaxis_str[i]] = 0
  562. else:
  563. weight_dict[weight_Xaxis_str[i]] = round(cursor.execute(sql) * 100 / percent_dict["许氏平鲉"], 2)
  564. # print(weight_dict[weight_Xaxis_str[i]])
  565. list3_single.append(weight_dict)
  566. # print("2")
  567. list3.append({"许氏平鲉": list3_single})
  568. # print("the weight graph list3 is :")
  569. # print(list3)
  570. cursor.close()
  571. db.close()
  572. # print("database close")
  573. db2 = pymysql.connect(host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=SONAR_DATABASE)
  574. # print("database open")
  575. cursor2 = db2.cursor()
  576. # print("sql")
  577. # #####**************************** list5:查询刷选条件,更新鱼群数量曲线 **********************************
  578. sql5 = "select id,DATE_FORMAT(datetime, '%Y-%m-%d') as datetime,assess_total_number from fish_distribution_dailyReport " \
  579. "where datetime > " + "'" + dataB_growCycle + "'" + " and datetime < " + "'" + dataE_growCycle + "';"
  580. # print(sql5)
  581. count = cursor2.execute(sql5)
  582. # print("select the number of sql is")
  583. # print(count)
  584. results = cursor2.fetchall()
  585. # print("sql work")
  586. for row in results:
  587. dict = {}
  588. dict['id'] = row[0]
  589. dict['datetime'] = row[1]
  590. dict['assess_total_number'] = row[2]
  591. list5.append(dict)
  592. # print(list5)
  593. ######**************************** list1-4数据整合成json **********************************
  594. result_list = {"statisticsReport": list1, "size_graph": list2, "weight_graph": list3, "percent_graph": list4, "fishNumber": list5}
  595. # print("the result_list is:")
  596. # print(result_list)
  597. result_json = json(result_list)
  598. cursor2.close()
  599. db2.close()
  600. return result_json
  601. except Exception as e:
  602. logger.warning(e)
  603. list1 = []
  604. return json(list1)
  605. return False
  606. def sanic_server():
  607. app.run(host="0.0.0.0", port=7000)
  608. if __name__ == "__main__":
  609. app.run(host="0.0.0.0", port=7000)