qxz_zhijian_debug.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. class SCDThread(QtCore.QThread):
  2. """涉及进度条需主界面动态执行GUI,线程执行业务逻辑,避免主页面卡死"""
  3. def __init__(self,device_list,save_path,start_time,end_time,set_plat,set_stm8vs,set_order):
  4. super(SCDThread, self).__init__()
  5. self.device_list = device_list
  6. self.save_path = save_path
  7. # self.start_time = time.mktime(start_time.timetuple())
  8. # self.end_time = time.mktime(end_time.timetuple())
  9. self.start_time = 1689993000
  10. self.end_time = 1690008000
  11. self.start_time_str = start_time.strftime("%y-%m-%d %H:%M:%S")
  12. self.end_time_str = end_time.strftime("%y-%m-%d %H:%M:%S")
  13. self.set_plat = set_plat
  14. self.set_stm8vs = set_stm8vs
  15. self.set_order = set_order
  16. self.user = parse.quote_plus("root")
  17. self.passwd = parse.quote_plus("yfkj@6020")
  18. self.myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(self.user,self.passwd))
  19. self.db = self.myclient.smartfarming
  20. self.device_collection = self.db.sa_device
  21. self.qxz_collection = self.db.sa_device_qxz_data
  22. self.config = {
  23. 'host': '120.27.222.26',
  24. 'port': 3306,
  25. 'user': 'yfwlw',
  26. 'password': 'sql_yfkj_6019',
  27. 'db': 'yfwlw',
  28. 'charset': 'utf8mb4',
  29. 'cursorclass': pymysql.cursors.DictCursor,
  30. }
  31. self.connection = pymysql.connect(**self.config)
  32. self.cursor = self.connection.cursor()
  33. self.cond = QxzCand()
  34. self.mongo_ping()
  35. def mongo_ping(self):
  36. """mongo-ping预防连接失效"""
  37. # try:
  38. # self.myclient.admin.command('ping')
  39. # except: # "ConnectionFailure"
  40. self.myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(self.user,self.passwd))
  41. self.db = self.myclient.smartfarming
  42. self.device_collection = self.db.sa_device
  43. self.qxz_collection = self.db.sa_qxz_data
  44. self.qxz_info_record_collection = self.db.sa_qxz_info_record
  45. self.qxz_base_info_collection = self.db.sa_qxz_base_info
  46. # self.sa_qxz_conf = self.db.
  47. def sql_ping(self):
  48. """mysql-ping 预防连接失效"""
  49. try:
  50. self.connection.ping()
  51. except:
  52. self.connection = pymysql.connect(**self.config)
  53. self.cursor = self.connection.cursor()
  54. def __time_dif(self,checkdatetime):
  55. """计算时间差"""
  56. nowdatetime = datetime.datetime.now()
  57. checkdatetime = datetime.datetime.strptime(checkdatetime, "%Y-%m-%d %H:%M:%S")
  58. timedif = checkdatetime - nowdatetime
  59. return timedif.days
  60. def device_their_platform(self,shortId):
  61. """确定设备所在平台以及完整设备号"""
  62. self.mongo_ping()
  63. self.sql_ping()
  64. regex = re.compile('.*{}$'.format(shortId))
  65. bd_device_dict = self.device_collection.find_one(
  66. filter = {"device_id":regex,"device_type_id":5},
  67. projection = {'_id': 0},
  68. sort = [('uptime', pymongo.DESCENDING)]
  69. )
  70. device_sql = "SELECT * FROM AppInfoManage_qxzstatus_new WHERE equip_id_id LIKE '%{}' ORDER BY upl_time DESC LIMIT 1;".format(shortId)
  71. self.cursor.execute(device_sql)
  72. sq_device_dict = self.cursor.fetchone()
  73. if bd_device_dict and sq_device_dict:
  74. bd_upltime = bd_device_dict["uptime"]
  75. sq_upltime = time.mktime(sq_device_dict["upl_time"].timetuple())
  76. if bd_upltime >= sq_upltime:
  77. d_id = bd_device_dict["id"]
  78. deviceId = bd_device_dict["device_id"]
  79. platform = "大数据平台"
  80. else:
  81. d_id = ""
  82. deviceId = sq_device_dict["equip_id_id"]
  83. platform = "四情平台"
  84. elif bd_device_dict and not sq_device_dict:
  85. d_id = bd_device_dict["id"]
  86. deviceId = bd_device_dict["device_id"]
  87. platform = "大数据平台"
  88. return d_id,deviceId,platform
  89. elif not bd_device_dict and sq_device_dict:
  90. d_id = ""
  91. deviceId = sq_device_dict["equip_id_id"]
  92. platform = "四情平台"
  93. else:
  94. d_id = ""
  95. deviceId = "平台无此设备"
  96. platform = "未知"
  97. print("deviceId:",deviceId)
  98. return d_id,deviceId,platform
  99. def sim_info(self, iccid):
  100. """查询卡信息"""
  101. url = "http://8.136.98.49:10001/iotcard/platsimview/inquiries/"
  102. try:
  103. response = requests.request("POST", url, data={"iccid": iccid})
  104. except:
  105. return [0, "查询卡信息异常稍后重试"]
  106. else:
  107. res_data = json.loads(response.text)
  108. if res_data["msg"] == "success" and res_data["data"]:
  109. expiry_date = res_data["data"]["expiry_date"]
  110. if expiry_date == "未知":
  111. return [0, "未查询到卡信息"]
  112. else:
  113. time_difference = self.__time_dif(expiry_date)
  114. if time_difference >= 180:
  115. return [1, "有效期剩余{}天".format(time_difference)]
  116. elif 30 <= time_difference < 180:
  117. return [2, "有效期剩余{}天".format(time_difference)]
  118. else:
  119. return [0, "有效期剩余{}天".format(time_difference)]
  120. else:
  121. return [0, "查询无结果"]
  122. def _get_data_new(self):
  123. """获取最新设备数据信息"""
  124. pipeline = [
  125. {
  126. "$match": {
  127. "device_id": {"$in": self.device_list}
  128. }
  129. },
  130. {
  131. "$group": {
  132. "_id": {"device_id": "$device_id"},
  133. "last_msg": {"$last": "$$ROOT"}
  134. }
  135. },
  136. {
  137. "$replaceRoot": {"newRoot": "$last_msg"}
  138. }
  139. ]
  140. cursor = self.db.sa_qxz_data.aggregate(pipeline, allowDiskUse=True)
  141. data_dict = {item['device_id']: item for item in cursor}
  142. return data_dict
  143. def _get_bigdata_qxz_conf(self):
  144. data_dict = self._get_data_new()
  145. cursor = self.db.sa_qxz_conf.find({"device_id": {"$in": self.device_list}})
  146. title_name_set = set()
  147. qxz_conf_dict = {}
  148. for item in cursor:
  149. device_id = item['device_id']
  150. qxz_data = data_dict[device_id]
  151. config_dict = {}
  152. counter = Counter()
  153. for k, v in item.items():
  154. if k.startswith('e') and v and qxz_data.get(k, None):
  155. t_name = v.split('#')[0]
  156. t_index = counter.get(t_name, "")
  157. config_dict[k] = f"{t_name}#{t_index}"
  158. counter[t_name] += 1
  159. n_list = list(config_dict.values())
  160. title_name_set.update(n_list)
  161. qxz_conf_dict[device_id] = config_dict
  162. title_name_list = sorted(list(title_name_set))
  163. for i in title_name_list:
  164. print(i)
  165. for k, v in qxz_conf_dict.items():
  166. print(k, v)
  167. return title_name_list, qxz_conf_dict
  168. def get_qxz_element_data(self, device_id, conf_dict):
  169. group_dict = {
  170. "_id": "$device_id",
  171. "total_count": {"$count": {}},
  172. "uptime_list": {"$push": "$uptime"}
  173. }
  174. project_dict = {
  175. "_id": 0,
  176. "device_id": "$_id",
  177. "total_count": "$total_count",
  178. "uptime_info": {
  179. "$function": {
  180. "args": ["$uptime_list"],
  181. "lang": "js",
  182. "body": """
  183. function(uptime_list){
  184. let old_uptime = uptime_list[0];
  185. let uptime_20 = 0;
  186. let uptime_40 = 0;
  187. for (let i = 1; i < uptime_list.length; i++) {
  188. let uptime = uptime_list[i];
  189. let diff_time = uptime - old_uptime;
  190. if (diff_time < 1200){
  191. uptime_20 += 1;
  192. } else if (diff_time > 2400){
  193. uptime_40 += 1;
  194. }
  195. old_uptime = uptime;
  196. }
  197. return {"uptime_20": uptime_20, "uptime_40": uptime_40}
  198. }
  199. """
  200. }
  201. }
  202. }
  203. for ek in conf_dict.keys():
  204. group_dict[ek] = {"$push": f"${ek}"}
  205. try:
  206. k = conf_dict[ek]
  207. func_name = self.get_conf_key(k)
  208. project_dict[ek] = self.cond.func_dict[func_name](ek)
  209. except KeyError as e:
  210. continue
  211. pipeline = [
  212. {
  213. "$match": {
  214. "device_id": device_id,
  215. "uptime": {
  216. "$gte": self.start_time,
  217. "$lt": self.end_time
  218. }
  219. }
  220. },
  221. {
  222. "$sort": {"uptime": 1}
  223. },
  224. {
  225. "$project": {
  226. "device_id": "$device_id",
  227. "uptime": "$uptime",
  228. "tmp_list": {
  229. "$filter": {
  230. "input": {"$objectToArray": "$$ROOT"},
  231. "as": "item",
  232. "cond": {
  233. "$and": [
  234. {
  235. "$regexMatch": {
  236. "input": "$$item.k",
  237. "regex": "^e\d+",
  238. "options": "i"
  239. }
  240. },
  241. {
  242. "$regexMatch": {
  243. "input": "$$item.v",
  244. "regex": ".*#.*",
  245. "options": "i"
  246. }
  247. }
  248. ]
  249. }
  250. }
  251. }
  252. }
  253. },
  254. {
  255. "$addFields": {
  256. "tmp_obj": {
  257. "$arrayToObject": {
  258. "$map": {
  259. "input": "$tmp_list",
  260. "as": "item",
  261. "in": {
  262. "k": "$$item.k",
  263. "v": {
  264. "$toInt": {
  265. "$toDouble": {"$arrayElemAt": [{"$split": ["$$item.v", "#"]}, 0]}
  266. }
  267. }
  268. }
  269. }
  270. }
  271. }
  272. }
  273. },
  274. {
  275. "$replaceRoot": {
  276. "newRoot": {
  277. "$mergeObjects": ["$tmp_obj", {"device_id": "$device_id", "uptime": "$uptime"}]
  278. }
  279. }
  280. },
  281. {
  282. "$group": group_dict
  283. },
  284. {
  285. "$project": project_dict
  286. }
  287. ]
  288. cursor = self.db.sa_qxz_data.aggregate(pipeline, allowDiskUse=True)
  289. result = {k: v for item in cursor for k, v in item.items()}
  290. return result
  291. def get_qxz_volt_or_rssi_data(self):
  292. pipeline = [
  293. {
  294. "$match": {
  295. "device_id": {
  296. "$in": self.device_list
  297. }
  298. },
  299. },
  300. {
  301. "$project": {
  302. "device_id": "$device_id",
  303. "volt": {
  304. "$toDouble": "$volt"
  305. },
  306. "rssi": {
  307. "$toDouble": "$rssi"
  308. }
  309. }
  310. },
  311. {
  312. "$group": {
  313. "_id": "$device_id",
  314. "max_volt": {
  315. "$max": "$volt"
  316. },
  317. "min_volt": {
  318. "$min": "$volt"
  319. },
  320. "max_rssi": {
  321. "$max": "$rssi"
  322. },
  323. "min_rssi": {
  324. "$min": "$rssi"
  325. }
  326. }
  327. },
  328. {
  329. "$project": {
  330. "_id": 0,
  331. "device_id": "$_id",
  332. "volt_info": {
  333. 'max_value': "$max_volt", 'min_value': "$min_volt",
  334. 'status': {
  335. "$cond": {
  336. "if": {
  337. "$and": [
  338. {"$lte": ["$max_volt", 15]},
  339. {"$gte": ["$min_volt", 11]}
  340. ]
  341. },
  342. "then": 1,
  343. "else": 0
  344. }
  345. }
  346. },
  347. "rssi_info": {
  348. 'max_value': "$max_rssi", 'min_value': "$min_rssi",
  349. 'status': {
  350. "$cond": {
  351. "if": {
  352. "$gte": ["$min_rssi", 14]
  353. },
  354. "then": 1,
  355. "else": 0
  356. }
  357. }
  358. }
  359. }
  360. }
  361. ]
  362. cursor = self.qxz_info_record_collection.aggregate(pipeline, allowDiskUse=True)
  363. result = {item['device_id']: item for item in cursor}
  364. return result
  365. def get_qxz_device_data(self):
  366. pipeline = [
  367. {
  368. "$match": {
  369. "device_id": {
  370. "$in": self.device_list
  371. }
  372. },
  373. },
  374. {
  375. "$project": {
  376. "device_id": "$device_id",
  377. "dver_num": "$dver_num",
  378. "lng": {
  379. "$convert": {
  380. "input": "$lng",
  381. "to": "double",
  382. "onError": 0,
  383. "onNull": 0
  384. }
  385. },
  386. "lat": {
  387. "$convert": {
  388. "input": "$lat",
  389. "to": "double",
  390. "onError": 0,
  391. "onNull": 0
  392. }
  393. }
  394. }
  395. },
  396. {
  397. "$project": {
  398. "_id": 0,
  399. "device_id": "$device_id",
  400. "dver_info": {
  401. "status": {
  402. "$cond": {
  403. "if": {
  404. "$eq": ["$dver_num", self.set_stm8vs]
  405. },
  406. "then": 1,
  407. "else": 0
  408. }
  409. },
  410. "dver_num": "$dver_num",
  411. "old_value": self.set_stm8vs
  412. },
  413. "lng_info": {
  414. "status": {
  415. "$cond": {
  416. "if": {
  417. "$and": [
  418. {"$lte": ["$lng", 113.7869444]},
  419. {"$gte": ["$lng", 113.7536111]}
  420. ]
  421. },
  422. "then": 1,
  423. "else": 0
  424. }
  425. },
  426. "lng": "$lng"
  427. },
  428. "lat_info": {
  429. "status": {
  430. "$cond": {
  431. "if": {
  432. "$and": [
  433. {"$lte": ["$lat", 35.0458333]},
  434. {"$gte": ["$lat", 35.0125]}
  435. ]
  436. },
  437. "then": 1,
  438. "else": 0
  439. }
  440. },
  441. "lat": "$lat"
  442. }
  443. }
  444. }
  445. ]
  446. cursor = self.device_collection.aggregate(pipeline, allowDiskUse=True)
  447. result = {item['device_id']: item for item in cursor}
  448. return result
  449. def get_sim_data(self):
  450. pipeline = [
  451. {
  452. "$match": {
  453. "device_id": {
  454. "$in": self.device_list
  455. }
  456. }
  457. },
  458. {
  459. "$project": {
  460. "_id": 0,
  461. "device_id": "$device_id",
  462. "iccid": "$iccid"
  463. }
  464. }
  465. ]
  466. cursor = self.qxz_base_info_collection.aggregate(pipeline, allowDiskUse=True)
  467. result = {item['device_id']: item["iccid"] for item in cursor}
  468. return result
  469. def parse_data(self, device_id, qxz_conf_dict, vr_dict, device_data_dict, sim_data):
  470. """解析获取元素数据"""
  471. element_dict = {}
  472. element_dict["ID#"] = device_id
  473. element_dict["检验项目#"] = device_id
  474. try:
  475. conf_dict = qxz_conf_dict[device_id]
  476. device_data = self.get_qxz_element_data(device_id, conf_dict)
  477. element_dict.update({conf_dict[k]: v for k, v in device_data.items() if k in conf_dict})
  478. vr_info = vr_dict[device_id]
  479. dd_info = device_data_dict[device_id]
  480. element_dict["经度#"] = dd_info["lng_info"]
  481. element_dict["纬度#"] = dd_info["lat_info"]
  482. element_dict["固件版本号#"] = dd_info["dver_info"]
  483. element_dict["电压#"] = vr_info["volt_info"]
  484. element_dict["信号强度#"] = vr_info["rssi_info"]
  485. sim_info = self.sim_info(sim_data[device_id])
  486. element_dict["sim卡信息#"] = {
  487. "status": sim_info[0],
  488. "msg": sim_info[1]
  489. }
  490. element_dict["上传数据条数#"] = {
  491. "total_count": device_data['total_count'],
  492. "uptime_20": device_data["uptime_info"]["uptime_20"],
  493. "uptime_40": device_data["uptime_info"]["uptime_40"]
  494. }
  495. except KeyError as e:
  496. pass
  497. return element_dict
  498. def get_conf_key(self, k):
  499. key = k.split('#')[0]
  500. if "土壤含水率" in key:
  501. key = "土壤含水率"
  502. if "土壤温度" in key:
  503. key = "土壤温度"
  504. return key
  505. def get_position(self, lng, lat):
  506. if lng and lat:
  507. try:
  508. ret = requests.post("http://api.map.baidu.com/geocoder?location=%s,%s&coord_type=gcj02&output=json"%(lat,lng))
  509. ret_json = json.loads(ret.text)
  510. province, city, district = ret_json["result"]["addressComponent"]["province"], \
  511. ret_json["result"]["addressComponent"]["city"], \
  512. ret_json["result"]["addressComponent"]["district"]
  513. return province + city + district
  514. except Exception as e:
  515. return False
  516. else:
  517. return False
  518. def get_conf_data(self, device_config, device_data, keys):
  519. device_config = dict(device_config)
  520. for iname, ivalue in device_config.items():
  521. if ivalue and isinstance(ivalue, str):
  522. if keys in ivalue:
  523. idevice_key_sun = iname
  524. idevice_data = []
  525. for d in device_data:
  526. print(d, "++")
  527. idevice_data.append(float((d.get(idevice_key_sun)).split("#")[0]))
  528. return idevice_data
  529. def run(self):
  530. """主业务逻辑,涉及进度条不能模块化,慢慢捋"""
  531. title_name_list = ["ID#", "检验项目#", "电压#", "信号强度#", "经度#", "纬度#", "固件版本号#", "sim卡信息#",
  532. "上传数据条数#"]
  533. default_func_dict = {
  534. "电压": self.cond.get_cond_volt_msg,
  535. "信号强度": self.cond.get_cond_rssi_msg,
  536. "经度": self.cond.get_cond_lng_msg,
  537. "纬度": self.cond.get_cond_lat_msg,
  538. "固件版本号": self.cond.get_cond_version_msg,
  539. "上传数据条数": self.cond.get_time_uptime_msg,
  540. "sim卡信息": self.cond.get_cond_sim_msg
  541. }
  542. head_name_list, qxz_conf_dict = self._get_bigdata_qxz_conf()
  543. for n in head_name_list:
  544. if n not in title_name_list:
  545. title_name_list.append(n)
  546. title_name_list.append("位置信息#")
  547. title_name_list.append("单台合格数#")
  548. device_data_dict = self.get_qxz_device_data()
  549. vr_dict = self.get_qxz_volt_or_rssi_data()
  550. sim_data = self.get_sim_data()
  551. proess = 0
  552. now_time = datetime.datetime.now()
  553. global save_filename
  554. save_filename = self.set_order + "_" + now_time.strftime("%m%d") + ".xlsx"
  555. save_path = os.path.join(self.save_path,save_filename)
  556. workbook = Workbook(save_path)
  557. worksheet = workbook.add_worksheet()
  558. merge_title_style = workbook.add_format(merge_title_format)
  559. toji_style = workbook.add_format(toji_format)
  560. default_style = workbook.add_format(default_formal)
  561. red_style = workbook.add_format(error_format)
  562. green_style = workbook.add_format(formal_format)
  563. yellow_style = workbook.add_format(common_format)
  564. style_dict = {
  565. 0: red_style,
  566. 1: green_style,
  567. 2: yellow_style,
  568. 3: default_style
  569. }
  570. head_list = [i.split("#")[0] for i in title_name_list]
  571. worksheet.merge_range(0, 0, 0, len(head_list) - 1, "物联网气象站设备质检表格", merge_title_style)
  572. for index, k in enumerate(title_name_list):
  573. value = k.split('#')[0]
  574. worksheet.write(2, index, value, default_style)
  575. for index, k in enumerate(title_name_list):
  576. key = self.get_conf_key(k)
  577. try:
  578. value = self.cond.head_dict[key]
  579. except KeyError as e:
  580. value = "无判定条件"
  581. worksheet.write(3, index, value, default_style)
  582. row_index = 4
  583. qualified = 0
  584. for device_id in self.device_list:
  585. self.mongo_ping()
  586. element_dict = self.parse_data(device_id, qxz_conf_dict, vr_dict, device_data_dict, sim_data)
  587. device_config = self.db.sa_qxz_conf.find_one({"device_id": device_id}, {'_id':0,'id':0})
  588. device_data = self.db.sa_qxz_data.find({"device_id": device_id, "uptime": {"$gte": self.start_time, "$lte": self.end_time}})
  589. # device_machine = self.db.sa_device.find_one({"device_id": device_id})
  590. not_qualified = 0
  591. lng = 0
  592. lat = 0
  593. for index, k in enumerate(title_name_list):
  594. status, msg = 3, "无数据"
  595. try:
  596. value = element_dict[k]
  597. key = self.get_conf_key(k)
  598. if key in ["ID", "检验项目"]:
  599. rt = {"status": 1, "msg": value}
  600. elif key in default_func_dict:
  601. print(key, "-----------------")
  602. if key == "电压":
  603. print("----", device_data.count())
  604. result = self.get_conf_data(device_config, device_data, "电压")
  605. print(result, "************")
  606. rt = default_func_dict[key](value)
  607. if key == "经度":
  608. lng = value.get("lng")
  609. if key == "纬度":
  610. lat = value.get("lat")
  611. else:
  612. if key == "日照时数":
  613. idevice_key_sun = ""
  614. device_config = dict(device_config)
  615. for iname, ivalue in device_config.items():
  616. if ivalue and isinstance(ivalue, str):
  617. if "日照时数" in ivalue:
  618. idevice_key_sun = iname
  619. idevice_data = []
  620. for d in device_data:
  621. idevice_data.append(float((d.get(idevice_key_sun)).split("#")[0]))
  622. if max(idevice_data) > 0.1:
  623. rt = {'status': 1, 'msg': f'最大值:{str(max(idevice_data))},最小值:{str(min(idevice_data))}'}
  624. else:
  625. rt = {'status': 0, 'msg': f'最大值:{str(max(idevice_data))},最小值:{str(min(idevice_data))}'}
  626. elif key == "降雨量累计":
  627. idevice_key_rain = ""
  628. device_config = dict(device_config)
  629. for iname, ivalue in device_config.items():
  630. if ivalue and isinstance(ivalue, str):
  631. if "降雨量累计" in ivalue:
  632. idevice_key_rain = iname
  633. idevice_data = []
  634. for d in device_data:
  635. idevice_data.append(float((d.get(idevice_key_rain)).split("#")[0]))
  636. if max(idevice_data) > 0.1:
  637. rt = {'status': 1, 'msg': f'最大值:{str(max(idevice_data))},最小值:{str(min(idevice_data))}'}
  638. else:
  639. rt = {'status': 0, 'msg': f'最大值:{str(max(idevice_data))},最小值:{str(min(idevice_data))}'}
  640. else:
  641. rt = self.cond.cond_msg_dict[key](value)
  642. status, msg = rt['status'], rt['msg']
  643. except Exception as e:
  644. pass
  645. if status == 0:
  646. not_qualified += 1
  647. if k == "位置信息#":
  648. # 使用经纬度获取地理
  649. result = self.get_position(lng, lat)
  650. if result and result == "河南省新乡市原阳县":
  651. status = 1
  652. msg = result
  653. else:
  654. status = 0
  655. msg = result
  656. if k == "单台合格数#":
  657. status, msg = 0, f"不合格: {not_qualified}"
  658. if not_qualified == 0:
  659. status, msg = 1, "合格"
  660. column_style = style_dict[status]
  661. worksheet.write(row_index, index, str(msg), column_style)
  662. proess = (index + 1) / len(self.device_list) * 100
  663. if int(proess) == 100:
  664. self.proess_signal.emit(99)
  665. else:
  666. self.proess_signal.emit(int(proess))
  667. if not_qualified == 0:
  668. qualified += 1
  669. row_index += 1
  670. toji_data = [
  671. "任务单号", self.set_order, "检验时间", self.start_time_str, self.end_time_str,
  672. "报告日期", now_time.strftime("%y-%m-%d %H:%M:%S"), "合格数", qualified
  673. ]
  674. for i in range(len(head_list) - len(toji_data)):
  675. toji_data.append(" ")
  676. for index, k in enumerate(toji_data):
  677. value = str(k)
  678. worksheet.write(1, index, value, toji_style)
  679. c_n = len(value) + 15
  680. worksheet.set_column(index, index, c_n)
  681. worksheet.protect(pwd_str)
  682. workbook.close()
  683. self.cursor.close()
  684. self.connection.close()
  685. self.myclient.close()
  686. self.proess_signal.emit(100)