main.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. # -*- coding: utf-8 -*-
  2. import subprocess
  3. import time
  4. import oss2
  5. from rosbag import Bag, Compression
  6. import logging
  7. import pymysql
  8. import os
  9. import urllib2
  10. import json
  11. path1 = '/root/cicv_data_closedloop-data_processing/python2-algorithm-exam/'
  12. path2 = '/root/cicv_data_closedloop-data_processing/python2-algorithm-exam/json/'
  13. path3 = '/root/cicv_data_closedloop-data_processing/python2-algorithm-exam/bag/'
  14. path4 = '/root/cicv_data_closedloop-data_processing/python2-algorithm-exam/bag/merged/'
  15. logging.basicConfig(filename=path1 + 'log/algorithm-exam.log', level=logging.INFO,
  16. format='%(asctime)s - %(levelname)s - %(message)s')
  17. key1 = 'pjisuv/'
  18. key2 = 'data/'
  19. key3 = 'data_merge/'
  20. key4 = 'data_parse/'
  21. sleep_time = 5 # 每多少秒扫描一次
  22. compress_way = Compression.BZ2
  23. score_script_path = '/root/competition/Final_Compet_619.py'
  24. def merge(source_bag_files, target_bag_file, target_bag_file2):
  25. try:
  26. with Bag(target_bag_file, 'w', compression=compress_way) as o:
  27. for i in range(len(source_bag_files)):
  28. with Bag(source_bag_files[i], 'r') as ib:
  29. for topic, msg, t in ib:
  30. o.write(topic, msg, t)
  31. with Bag(target_bag_file2, 'w', compression=compress_way) as o2:
  32. for i in range(len(source_bag_files)):
  33. with Bag(source_bag_files[i], 'r') as ib:
  34. for topic, msg, t in ib:
  35. if topic == '/pj_vehicle_fdb_pub':
  36. o2.write(topic, msg, t)
  37. except Exception as e:
  38. logging.exception("bag包合并报错: %s", e)
  39. def convert_unix_timestamp(timestamp):
  40. return time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(float(timestamp)))
  41. def format_timestamp(timestamp):
  42. return time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(float(timestamp)))
  43. def seconds_between_timestamps(time_begin, time_end):
  44. begin_seconds = int(time_begin)
  45. end_seconds = int(time_end)
  46. current_seconds = begin_seconds
  47. formatted_times = []
  48. while current_seconds <= end_seconds:
  49. formatted_time = format_timestamp(current_seconds)
  50. formatted_times.append(formatted_time)
  51. current_seconds += 1
  52. return formatted_times
  53. def parse_light_6(time_begin1, time_end1):
  54. a = 6
  55. result1 = []
  56. try:
  57. # 定义 API 地址
  58. url = 'http://10.14.65.102:30087/event_rsu_rsu2cloud_spat_alias/_search'
  59. logging.info('信号灯开始时间为: %s', str(int(time_begin1) * 1000))
  60. logging.info('信号灯结束时间为: %s', str(int(time_end1) * 1000))
  61. # 定义请求体
  62. data1 = {
  63. "query": {
  64. "bool": {
  65. "must": [
  66. {
  67. "term": {
  68. "deviceId": {
  69. "value": "R-HK0511"
  70. }
  71. }
  72. },
  73. {
  74. "range": {
  75. "timestamp": {
  76. "gte": int(time_begin1) * 1000,
  77. "lte": int(time_end1) * 1000
  78. }
  79. }
  80. }
  81. ]
  82. }
  83. },
  84. "size": 1000,
  85. "sort": [
  86. {
  87. "timestamp": {
  88. "order": "asc"
  89. }
  90. }
  91. ]
  92. }
  93. # 将请求体转换为 JSON 格式
  94. json_data = json.dumps(data1)
  95. # 发起 HTTP POST 请求
  96. request = urllib2.Request(url, json_data)
  97. request.add_header('Content-Type', 'application/json; charset=UTF-8')
  98. # 接收响应
  99. response = urllib2.urlopen(request)
  100. # 读取响应内容
  101. response_data = response.read()
  102. logging.info("信号灯数据结果为:%s", response_data)
  103. # 将响应内容转换为 JSON 对象
  104. json_response = json.loads(response_data)
  105. hits_array = list(json_response.get('hits').get('hits'))
  106. data_array = []
  107. for hits in hits_array:
  108. _source = hits.get('_source')
  109. intersections = list(_source.get('intersections'))
  110. for intersection in intersections:
  111. intersectionTimestamp = intersection.get('intersectionTimestamp')
  112. phases = list(intersection.get('phases'))
  113. for phase in phases:
  114. if phase.get('phaseId') == a:
  115. phaseStates = list(phase.get('phaseStates'))
  116. for phaseState in phaseStates:
  117. startTime = phaseState.get('startTime')
  118. if startTime == 0:
  119. data_array.append(
  120. {'intersectionTimestamp': intersectionTimestamp, 'light': phaseState.get('light')})
  121. currentIntersectionTimestamp = data_array[0].get('intersectionTimestamp')
  122. if data_array[0].get('light') == 2 or data_array[0].get('light') == 3:
  123. currentLightCustom = 1
  124. else:
  125. currentLightCustom = 0
  126. # logging.info("原始信号灯数据为 %s" % data_array)
  127. # 0 绿灯 1 红灯
  128. for e in data_array:
  129. intersectionTimestamp = e.get('intersectionTimestamp')
  130. tempLight = e.get('light')
  131. if tempLight == 2 or tempLight == 3: # 1红灯 0绿灯
  132. tempCurrentLightCustom = 1
  133. else:
  134. tempCurrentLightCustom = 0
  135. if tempCurrentLightCustom == currentLightCustom:
  136. continue
  137. else:
  138. result1.append({'beginTime': currentIntersectionTimestamp, 'endTime': intersectionTimestamp,
  139. 'light': tempCurrentLightCustom})
  140. currentIntersectionTimestamp = intersectionTimestamp
  141. currentLightCustom = tempCurrentLightCustom
  142. if currentLightCustom == 1:
  143. result1.append(
  144. {'beginTime': currentIntersectionTimestamp, 'endTime': currentIntersectionTimestamp + 10000,
  145. 'light': 1})
  146. else:
  147. result1.append(
  148. {'beginTime': currentIntersectionTimestamp, 'endTime': currentIntersectionTimestamp + 10000,
  149. 'light': 0})
  150. except Exception as e:
  151. logging.exception('无法获取信号灯数据,默认全程绿灯 %s' % str(e))
  152. result1.append({'beginTime': int(time_begin1) * 1000, 'endTime': int(time_end1) * 1000 + 10000, 'light': 0})
  153. # 打印JSON文件的内容
  154. logging.info("信号灯数据为 %s" % result1)
  155. # 定义要保存为JSON文件的列表
  156. # 指定要保存的JSON文件路径
  157. file_path = path2 + str(time_begin1) + '_6.json'
  158. # 将列表保存为JSON文件
  159. with open(file_path, "w") as json_file:
  160. json.dump(result1, json_file)
  161. logging.info("信号灯数据文件路径为 %s" % json_file)
  162. return file_path
  163. def parse_light_13(time_begin1, time_end1):
  164. a = 13
  165. result1 = []
  166. try:
  167. # 定义 API 地址
  168. url = 'http://10.14.65.102:30087/event_rsu_rsu2cloud_spat_alias/_search'
  169. logging.info('信号灯开始时间为: %s', str(int(time_begin1) * 1000))
  170. logging.info('信号灯结束时间为: %s', str(int(time_end1) * 1000))
  171. # 定义请求体
  172. data1 = {
  173. "query": {
  174. "bool": {
  175. "must": [
  176. {
  177. "term": {
  178. "deviceId": {
  179. "value": "R-HK0511"
  180. }
  181. }
  182. },
  183. {
  184. "range": {
  185. "timestamp": {
  186. "gte": int(time_begin1) * 1000,
  187. "lte": int(time_end1) * 1000
  188. }
  189. }
  190. }
  191. ]
  192. }
  193. },
  194. "size": 1000,
  195. "sort": [
  196. {
  197. "timestamp": {
  198. "order": "asc"
  199. }
  200. }
  201. ]
  202. }
  203. # 将请求体转换为 JSON 格式
  204. json_data = json.dumps(data1)
  205. # 发起 HTTP POST 请求
  206. request = urllib2.Request(url, json_data)
  207. request.add_header('Content-Type', 'application/json; charset=UTF-8')
  208. # 接收响应
  209. response = urllib2.urlopen(request)
  210. # 读取响应内容
  211. response_data = response.read()
  212. # 将响应内容转换为 JSON 对象
  213. json_response = json.loads(response_data)
  214. hits_array = list(json_response.get('hits').get('hits'))
  215. data_array = []
  216. for hits in hits_array:
  217. _source = hits.get('_source')
  218. intersections = list(_source.get('intersections'))
  219. for intersection in intersections:
  220. intersectionTimestamp = intersection.get('intersectionTimestamp')
  221. phases = list(intersection.get('phases'))
  222. for phase in phases:
  223. if phase.get('phaseId') == a:
  224. phaseStates = list(phase.get('phaseStates'))
  225. for phaseState in phaseStates:
  226. startTime = phaseState.get('startTime')
  227. if startTime == 0:
  228. data_array.append(
  229. {'intersectionTimestamp': intersectionTimestamp, 'light': phaseState.get('light')})
  230. currentIntersectionTimestamp = data_array[0].get('intersectionTimestamp')
  231. if data_array[0].get('light') == 2 or data_array[0].get('light') == 3:
  232. currentLightCustom = 1
  233. else:
  234. currentLightCustom = 0
  235. # logging.info("原始信号灯数据为 %s" % data_array)
  236. # 0 绿灯 1 红灯
  237. for e in data_array:
  238. intersectionTimestamp = e.get('intersectionTimestamp')
  239. tempLight = e.get('light')
  240. if tempLight == 2 or tempLight == 3: # 1红灯 0绿灯
  241. tempCurrentLightCustom = 1
  242. else:
  243. tempCurrentLightCustom = 0
  244. if tempCurrentLightCustom == currentLightCustom:
  245. continue
  246. else:
  247. result1.append({'beginTime': currentIntersectionTimestamp, 'endTime': intersectionTimestamp,
  248. 'light': tempCurrentLightCustom})
  249. currentIntersectionTimestamp = intersectionTimestamp
  250. currentLightCustom = tempCurrentLightCustom
  251. if currentLightCustom == 1:
  252. result1.append(
  253. {'beginTime': currentIntersectionTimestamp, 'endTime': currentIntersectionTimestamp + 10000,
  254. 'light': 1})
  255. else:
  256. result1.append(
  257. {'beginTime': currentIntersectionTimestamp, 'endTime': currentIntersectionTimestamp + 10000,
  258. 'light': 0})
  259. except Exception as e:
  260. logging.exception('无法获取信号灯数据,默认全程绿灯 %s' % str(e))
  261. result1.append({'beginTime': int(time_begin1) * 1000, 'endTime': int(time_end1) * 1000 + 10000, 'light': 0})
  262. # 打印JSON文件的内容
  263. logging.info("信号灯数据为 %s" % result1)
  264. # 定义要保存为JSON文件的列表
  265. # 指定要保存的JSON文件路径
  266. file_path = path2 + str(time_begin1) + '_13.json'
  267. # 将列表保存为JSON文件
  268. with open(file_path, "w") as json_file:
  269. json.dump(result1, json_file)
  270. logging.info("信号灯数据文件路径为 %s" % json_file)
  271. return file_path
  272. def parse_light_1(time_begin1, time_end1):
  273. a = 1
  274. result1 = []
  275. try:
  276. # 定义 API 地址
  277. url = 'http://10.14.65.102:30087/event_rsu_rsu2cloud_spat_alias/_search'
  278. logging.info('信号灯开始时间为: %s', str(int(time_begin1) * 1000))
  279. logging.info('信号灯结束时间为: %s', str(int(time_end1) * 1000))
  280. # 定义请求体
  281. data1 = {
  282. "query": {
  283. "bool": {
  284. "must": [
  285. {
  286. "term": {
  287. "deviceId": {
  288. "value": "R-HK0511"
  289. }
  290. }
  291. },
  292. {
  293. "range": {
  294. "timestamp": {
  295. "gte": int(time_begin1) * 1000,
  296. "lte": int(time_end1) * 1000
  297. }
  298. }
  299. }
  300. ]
  301. }
  302. },
  303. "size": 1000,
  304. "sort": [
  305. {
  306. "timestamp": {
  307. "order": "asc"
  308. }
  309. }
  310. ]
  311. }
  312. # 将请求体转换为 JSON 格式
  313. json_data = json.dumps(data1)
  314. # 发起 HTTP POST 请求
  315. request = urllib2.Request(url, json_data)
  316. request.add_header('Content-Type', 'application/json; charset=UTF-8')
  317. # 接收响应
  318. response = urllib2.urlopen(request)
  319. # 读取响应内容
  320. response_data = response.read()
  321. # 将响应内容转换为 JSON 对象
  322. json_response = json.loads(response_data)
  323. hits_array = list(json_response.get('hits').get('hits'))
  324. data_array = []
  325. for hits in hits_array:
  326. _source = hits.get('_source')
  327. intersections = list(_source.get('intersections'))
  328. for intersection in intersections:
  329. intersectionTimestamp = intersection.get('intersectionTimestamp')
  330. phases = list(intersection.get('phases'))
  331. for phase in phases:
  332. if phase.get('phaseId') == a:
  333. phaseStates = list(phase.get('phaseStates'))
  334. for phaseState in phaseStates:
  335. startTime = phaseState.get('startTime')
  336. if startTime == 0:
  337. data_array.append(
  338. {'intersectionTimestamp': intersectionTimestamp, 'light': phaseState.get('light')})
  339. currentIntersectionTimestamp = data_array[0].get('intersectionTimestamp')
  340. if data_array[0].get('light') == 2 or data_array[0].get('light') == 3:
  341. currentLightCustom = 1
  342. else:
  343. currentLightCustom = 0
  344. # logging.info("原始信号灯数据为 %s" % data_array)
  345. # 0 绿灯 1 红灯
  346. for e in data_array:
  347. intersectionTimestamp = e.get('intersectionTimestamp')
  348. tempLight = e.get('light')
  349. if tempLight == 2 or tempLight == 3: # 1红灯 0绿灯
  350. tempCurrentLightCustom = 1
  351. else:
  352. tempCurrentLightCustom = 0
  353. if tempCurrentLightCustom == currentLightCustom:
  354. continue
  355. else:
  356. result1.append({'beginTime': currentIntersectionTimestamp, 'endTime': intersectionTimestamp,
  357. 'light': tempCurrentLightCustom})
  358. currentIntersectionTimestamp = intersectionTimestamp
  359. currentLightCustom = tempCurrentLightCustom
  360. if currentLightCustom == 1:
  361. result1.append(
  362. {'beginTime': currentIntersectionTimestamp, 'endTime': currentIntersectionTimestamp + 10000,
  363. 'light': 1})
  364. else:
  365. result1.append(
  366. {'beginTime': currentIntersectionTimestamp, 'endTime': currentIntersectionTimestamp + 10000,
  367. 'light': 0})
  368. except Exception as e:
  369. logging.exception('无法获取信号灯数据,默认全程绿灯 %s' % str(e))
  370. result1.append({'beginTime': int(time_begin1) * 1000, 'endTime': int(time_end1) * 1000 + 10000, 'light': 0})
  371. # 打印JSON文件的内容
  372. logging.info("信号灯数据为 %s" % result1)
  373. # 定义要保存为JSON文件的列表
  374. # 指定要保存的JSON文件路径
  375. file_path = path2 + str(time_begin1) + '_1.json'
  376. # 将列表保存为JSON文件
  377. with open(file_path, "w") as json_file:
  378. json.dump(result1, json_file)
  379. logging.info("信号灯数据文件路径为 %s" % json_file)
  380. return file_path
  381. '''
  382. 1-东进口左转
  383. 2-东进口直行
  384. 3-东进口右转
  385. 4-东进口非机动车
  386. 5–南进口左转
  387. 6-南进口直行
  388. 7-南进口右转
  389. 8-南进口非机动车
  390. 9-西进口左转
  391. 10-西进口直行
  392. 11-西进口右转
  393. 12-西进口非机动车
  394. 13-北进口左转
  395. 14-北进口直行
  396. 15-北进口右转
  397. 16-北进口非机动车
  398. '''
  399. '''
  400. 1.南进口直行 6
  401. 2.北进口左转 13
  402. 3.东进口左转 1
  403. '''
  404. if __name__ == '__main__':
  405. # 1 创建阿里云对象
  406. auth = oss2.Auth('n8glvFGS25MrLY7j', 'xZ2Fozoarpfw0z28FUhtg8cu0yDc5d')
  407. # cname = 'http://open-bucket.oss.icvdc.com'
  408. # bucket = oss2.Bucket(auth, cname, 'open-bucket', is_cname=True)
  409. endpoint = 'oss-cn-beijing-gqzl-d01-a.ops.gqzl-cloud.com'
  410. bucket = oss2.Bucket(auth, endpoint, 'open-bucket')
  411. while True:
  412. try:
  413. # 连接到MySQL数据库
  414. conn = pymysql.connect(
  415. host='10.14.85.241',
  416. # host='36.110.106.156',
  417. port=3306, # 默认MySQL端口为3306
  418. user='root',
  419. password='1qaz2wsx!',
  420. database='dataclosedloop',
  421. charset='utf8mb4', # 设置字符集
  422. cursorclass=pymysql.cursors.DictCursor, # 设置游标类型为字典型
  423. connect_timeout=60000, # 连接超时,单位是秒
  424. read_timeout=60000, # 读取超时,单位是秒
  425. write_timeout=60000 # 写入超时,单位是秒
  426. )
  427. # 创建一个游标对象
  428. with conn.cursor() as cursor:
  429. # 编写你的SQL查询语句,这里以连接查询为例
  430. sql = """
  431. SELECT *
  432. FROM exam
  433. where score_online = 0
  434. and (details is null or details = '')
  435. and end_time != '2006-01-02 15:04:05'
  436. """
  437. # 执行查询
  438. cursor.execute(sql)
  439. # 获取查询结果
  440. result = cursor.fetchall()
  441. if len(result) == 0:
  442. logging.info("未查询到待评分的记录。")
  443. time.sleep(1)
  444. continue
  445. conn.close()
  446. # 输出结果
  447. for row in result:
  448. equipment_no = str(row['equipment_no']) # 车辆编号
  449. logging.info("即将对队伍 %s 进行评分。", row['team_name'].encode('utf-8'))
  450. timestamp_begin = time.mktime(row['begin_time'].timetuple())
  451. timestamp_end = time.mktime(row['end_time'].timetuple())
  452. time_begin_for_light = str(int(timestamp_begin))
  453. time_begin_for_bag = str(int(timestamp_begin) - (8 * 60 * 60))
  454. time_end_for_light = str(int(timestamp_end))
  455. time_end_for_bag = str(int(time_end_for_light) - (8 * 60 * 60))
  456. interval = seconds_between_timestamps(time_begin_for_light, time_end_for_light)
  457. interval_for_bag = seconds_between_timestamps(time_begin_for_bag, time_end_for_bag)
  458. logging.info('时间戳 %s 到 %s 改为自定义格式 %s ', time_begin_for_bag, time_end_for_bag,
  459. interval_for_bag)
  460. oss_bags = []
  461. local_bags = []
  462. local_delete_list = []
  463. for time_prefix in interval_for_bag:
  464. # 添加待下载的bag列表
  465. for obj1 in oss2.ObjectIterator(bucket,
  466. prefix='competition/' + equipment_no + '/' + time_prefix):
  467. oss_bags.append(str(obj1.key))
  468. # 下载准备merge
  469. logging.info("合并区间内的bag包:%s -> %s,共 %s 个", interval_for_bag[0],
  470. interval_for_bag[len(interval_for_bag) - 1],
  471. len(interval_for_bag))
  472. for oss_bag in oss_bags:
  473. bag_name = str(oss_bag).split('/')[-1]
  474. equipment_dir = path3 + equipment_no
  475. if not os.path.exists(equipment_dir) or not os.path.isdir(equipment_dir):
  476. os.makedirs(equipment_dir)
  477. local_bag_path = equipment_dir + '/' + bag_name
  478. bucket.get_object_to_file(oss_bag, local_bag_path)
  479. local_bags.append(local_bag_path)
  480. local_delete_list.append(local_bag_path)
  481. # 3 合并
  482. merged_bag_dir = path4 + str(interval[0]) + '/'
  483. if not os.path.exists(merged_bag_dir) or not os.path.isdir(merged_bag_dir):
  484. os.makedirs(merged_bag_dir)
  485. merged_bag_path = merged_bag_dir + str(interval[0]) + '.bag'
  486. merged_bag_path2 = merged_bag_dir + str(interval[0]) + '_2.bag'
  487. merge(local_bags, merged_bag_path, merged_bag_path2)
  488. # 4 清理临时文件
  489. if len(local_delete_list) > 0:
  490. for local_delete in local_delete_list:
  491. try:
  492. os.remove(local_delete)
  493. except Exception as e:
  494. logging.exception("删除本地临时文件报错: %s" % str(e))
  495. logging.info("合并后的数据包为 %s" % merged_bag_path)
  496. # 5 查询信号灯数据
  497. try:
  498. json_path_6 = parse_light_6(time_begin_for_light, time_end_for_light)
  499. json_path_13 = parse_light_13(time_begin_for_light, time_end_for_light)
  500. json_path_1 = parse_light_1(time_begin_for_light, time_end_for_light)
  501. except Exception as e:
  502. logging.exception("未获取信号灯数据: %s" % str(e))
  503. continue
  504. # 6 调用bag解析程序
  505. command1 = 'rosrun carcompetition jiexi_node ' + merged_bag_path
  506. logging.info("生成csv: %s" % str(command1))
  507. process1 = subprocess.Popen(command1, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  508. output, error = process1.communicate()
  509. if process1.returncode == 0:
  510. print("Output:", output)
  511. else:
  512. print("Error:", error)
  513. # 7 调用评分脚本获取评分结果
  514. # command2 = 'python /root/competition/demo.py /root/candata.csv /root/gpsdata.csv'
  515. command2 = 'python3 ' + score_script_path + ' ' + merged_bag_dir + 'candata.csv ' + merged_bag_dir + 'gpsdata.csv ' + json_path_6 + ' ' + json_path_13 + ' ' + json_path_1 + ' ' + merged_bag_path2
  516. logging.info("执行打分命令 %s", command2)
  517. process2 = subprocess.Popen(command2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  518. output, error = process2.communicate()
  519. if process2.returncode == 0:
  520. text = str(output)
  521. logging.info('评分结果为:%s' % str(output))
  522. # 使用 json.loads() 方法将 JSON 字符串解析为 Python 字典
  523. data = json.loads(str(output))
  524. # 8 计算 TotalScore 和 DeductedScore 字段的总和
  525. total_score = 0 # 初始化总分数为 0
  526. for item in data: # 遍历每个场景
  527. # 计算当前场景的实际分数(总分数减去扣减分数)
  528. actual_score = item['TotalScore'] + item['DeductedScore']
  529. # 累加实际分数到总分数
  530. total_score += actual_score
  531. conn2 = pymysql.connect(
  532. host='10.14.85.241',
  533. # host='36.110.106.156',
  534. port=3306, # 默认MySQL端口为3306
  535. user='root',
  536. password='1qaz2wsx!',
  537. database='dataclosedloop',
  538. charset='utf8mb4', # 设置字符集
  539. cursorclass=pymysql.cursors.DictCursor, # 设置游标类型为字典型
  540. connect_timeout=60000, # 连接超时,单位是秒
  541. read_timeout=60000, # 读取超时,单位是秒
  542. write_timeout=60000 # 写入超时,单位是秒
  543. )
  544. # 9 将解析结果回传到数据库
  545. with conn2.cursor() as cursor2:
  546. # 编写UPDATE语句,更新指定字段
  547. sql = """
  548. UPDATE exam
  549. SET score_online = %s, details = %s
  550. WHERE id = %s
  551. """
  552. cursor2.execute(sql, (total_score, text, row['id']))
  553. conn2.commit()
  554. conn2.close()
  555. else:
  556. print("执行命令报错:", error)
  557. logging.info("队伍 %s 评分结束。", row['team_name'].encode('utf-8'))
  558. # try:
  559. # os.remove(merged_bag_path)
  560. # except Exception as e:
  561. # logging.exception("删除本地临时文件报错: %s" % str(e))
  562. except Exception as e:
  563. logging.exception("报错: %s" % str(e))
  564. continue