tasks.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. from celery import shared_task
  2. import time
  3. import os
  4. from imutils import paths
  5. import stat
  6. import datetime
  7. import re
  8. import copy
  9. from django.utils.decorators import method_decorator
  10. from django.views.decorators.csrf import csrf_exempt
  11. # import xlrd
  12. from apps.SimInfo.views import get_siminfo
  13. import requests
  14. from io import BytesIO
  15. import sys
  16. from PIL import Image
  17. import argparse
  18. import cv2
  19. import shutil
  20. import glob
  21. from apps.AppInfoManage.models import Equip, User_Log, MyUser,Equip_Forward
  22. from apps.AppInfoManage.models import SCDdata, SCDstatus, RecentSCDdata, YBQdata, YBQstatus, QXZstatus, QXZdata, Trapstatus, Trapdata, Trapphoto, Equip_SimInfo
  23. from apps.AppInfoManage.models import CBDstatus, CBDdata, CBDphoto, CBDpest, CBDphoto_desc, YBQphoto, BZYstatus, BZYdata, BZYphoto, Spore_Exist, QXZswitchstatus, QXZswitchdata,\
  24. RecentCBDdata, QXZTimingswitch, QXZAutoswitch, Msg_Conf
  25. import json
  26. import time
  27. import paho.mqtt.client as mqtt
  28. from apps.EquipManage.views import CJSONEncoder
  29. # 复制
  30. def mycopyfile(srcfile,dstfile):
  31. if not os.path.isfile(srcfile):
  32. print("%s not exist!"%(srcfile))
  33. else:
  34. fpath,fname=os.path.split(dstfile) #分离文件名和路径
  35. if not os.path.exists(fpath):
  36. os.makedirs(fpath) #创建路径
  37. shutil.copyfile(srcfile,dstfile) #复制文件
  38. print("copy %s -> %s"%(srcfile,dstfile))
  39. def mkdir(path):
  40. # 去除首位空格
  41. path=path.strip()
  42. # 去除尾部 \ 符号
  43. path=path.rstrip("\\")
  44. # 判断路径是否存在
  45. # 存在 True
  46. # 不存在 False
  47. isExists=os.path.exists(path)
  48. # 判断结果
  49. if not isExists:
  50. # 如果不存在则创建目录
  51. # 创建目录操作函数
  52. os.makedirs(path)
  53. print(path+' 创建成功')
  54. return True
  55. else:
  56. # 如果目录存在则不创建,并提示目录已存在
  57. print(path+' 目录已存在')
  58. return False
  59. def variance_of_laplacian(image):
  60. # 拉普拉斯的方差
  61. return cv2.Laplacian(image, cv2.CV_64F).var()
  62. @shared_task
  63. def photo_transpond(imei,a_path,a_name):
  64. # FTP转发图片
  65. nowtime = datetime.datetime.now().strftime('%Y%m%d')
  66. origin = sys.stdout
  67. mkdir('logs/%s'%imei)
  68. f = open('logs/%s/'%imei + nowtime +'.txt','a')
  69. sys.stdout = f
  70. img_addr = "pyftp/" + os.path.join(a_path, a_name)
  71. try:
  72. # if imei == "868575028869008":
  73. #丁艳青 云洋 更换设备号
  74. if imei == "869300039229913" or imei == "860344048751540":
  75. # if imei == "868956044831497" or imei == "865650043175898":
  76. print("img_addr",img_addr)
  77. time.sleep(20)
  78. a = "/data/yfwlw/" + img_addr
  79. # a = "/home/web/yfwlw/" + img_addr
  80. # c = event.name
  81. # b = c.replace(':','-')
  82. # 切割文件名
  83. # aa = img_addr.split("/")
  84. # 拼接文件夹
  85. # b = aa[3] + "/" + aa[4] + "/" + b
  86. n_time = datetime.datetime.now()
  87. b = imei + "/" + n_time.strftime('%Y%m%d') + "/" + n_time.strftime('%H-%M-%S') + ".jpg"
  88. # print("bbbbbbb",b)
  89. print("target_ftp_name:",b)
  90. ftp_url = os.system(r'curl ftp://47.95.247.86:8022/%s --ftp-create-dirs -u "hnyfkj:hnyfkj19416" -T "%s" -# -v'%(b,a))
  91. print("=====>>",ftp_url)
  92. else:
  93. pass
  94. if imei == "869300039255967" or imei == "869300039253798" or imei == "868575028876029":
  95. print("img_addr",img_addr)
  96. time.sleep(20)
  97. a = "/data/yfwlw/" + img_addr
  98. # a = "/home/web/yfwlw/" + img_addr
  99. # c = event.name
  100. # b = c.replace(':','-')
  101. # 切割文件名
  102. # aa = img_addr.split("/")
  103. # 拼接文件夹
  104. # b = aa[3] + "/" + aa[4] + "/" + b
  105. n_time = datetime.datetime.now()
  106. b = imei + "/" + n_time.strftime('%Y%m%d') + "/" + n_time.strftime('%H-%M-%S') + ".jpg"
  107. print("target_ftp_name:",b)
  108. ftp_url = os.system(r'curl ftp://47.95.247.86:8022/%s --ftp-create-dirs -u "xjhnyfkj:xjhnyfkj511" -T "%s" -# -v'%(b,a))
  109. print("=====>>",ftp_url)
  110. else:
  111. pass
  112. except Exception as eee:
  113. print(eee)
  114. try:
  115. time.sleep(10)
  116. # insect_indentify 昆虫识别
  117. # # 保存图片之后调用害虫识别接口
  118. # 正则切割文件名 去掉.jpg后缀
  119. a_name = a_name.replace(".jpg","")
  120. # pattern = r'[.]'
  121. # res = a_name
  122. # result = re.split(pattern, res)
  123. # insect_pic_name = result[0] + 'result' + '.jpg'
  124. insect_pic_name = a_name + 'result' + '.jpg'
  125. # url = 'http://60.166.14.184:9143/upload'
  126. url = 'http://60.166.14.184:9143/upload'
  127. # 测试服务器路径
  128. # imageFile = {'imageFile':('pic.jpg',open("/home/web/yfwlw/" + img_addr,'rb'),'image/jpeg')}
  129. lng = CBDstatus.objects.get(equip_id=imei).lng
  130. lat = CBDstatus.objects.get(equip_id=imei).lat
  131. # 正式服务器路径
  132. imageFile = {'imageFile':('pic.jpg',open("/data/yfwlw/" + img_addr,'rb'),'image/jpeg')}
  133. data = {'longitude':lng,'latitude':lat}
  134. print('data:',data)
  135. res = requests.post(url,data=data,files=imageFile,timeout=10)
  136. print("res:",res.text)
  137. resp = json.loads(res.text)
  138. print("insect_pic_name:", insect_pic_name)
  139. print("-------------------------接口调用成功")
  140. except Exception as e:
  141. print(e)
  142. print("---------------------识别失败")
  143. try:
  144. if resp["returnResult"]["returnStatus"] == "0":
  145. print("识别结果为空,不进行保存")
  146. else:
  147. # 将识别结果中的链接中的图片存入本地 测试服务器
  148. # pic_dir_1 = "/home/web/yfwlw/result_pyftp/" + a_path + "/"
  149. pic_dir_1 = "/data/yfwlw/result_pyftp/" + a_path + "/"
  150. print("pic_dir_1:",pic_dir_1)
  151. if os.path.exists(pic_dir_1) == False:
  152. os.makedirs(pic_dir_1)
  153. img_src = resp["returnResult"]["returnImageUrl"]
  154. response = requests.get(img_src)
  155. image = Image.open(BytesIO(response.content))
  156. insect_image_addr = pic_dir_1 + insect_pic_name
  157. print("insect_image_addr:",insect_image_addr)
  158. image.save(insect_image_addr)
  159. print("------------------图片保存成功")
  160. try:
  161. photo_obj = CBDphoto.objects.get(addr=img_addr)
  162. if resp["returnResult"]["returnStatus"] == "0":
  163. print("识别结果为空,不进行保存")
  164. else:
  165. # 将识别结果存入数据库
  166. pic_dir = "result_pyftp/" + os.path.join(a_path, insect_pic_name)
  167. photo_obj.indentify_photo = pic_dir
  168. photo_obj.indentify_result = resp["returnResult"]["returnString"]
  169. photo_obj.save()
  170. print("图片路径:", pic_dir)
  171. print("---------------数据库保存识别结果成功")
  172. except Exception as ex:
  173. print(ex)
  174. print("---------------数据库保存识别结果失败")
  175. except Exception as exc:
  176. print(exc)
  177. print("----------------识别结果图片保存失败")
  178. try:
  179. if imei == "868956044807455" or imei == "868956044806671" or imei == "868956044831604" or imei == "868956044831711" or imei == "868956044807471" or imei == "868956044830994" or imei == "868956044824930" or imei == "868956044807455" or imei == "865650043209341" or imei == "868956044806705" or imei == "868956044830929" or imei == "868956044824898" or imei == "868956044830887" or imei == "868956044830911":
  180. try:
  181. if CBDphoto.objects.get(addr=img_addr).indentify_photo == None:
  182. indentify_photo_1 = ""
  183. indentify_result_1 = ""
  184. img_addr_1 = "http://120.27.222.26/" + img_addr
  185. else:
  186. r_photo = CBDphoto.objects.get(addr=img_addr).indentify_photo
  187. indentify_result_1 = CBDphoto.objects.get(addr=img_addr).indentify_result
  188. img_addr_1 = "http://120.27.222.26/" + img_addr
  189. indentify_photo_1 = "http://120.27.222.26/" + r_photo
  190. data = {
  191. "devId": imei,
  192. "ossPath": img_addr_1,
  193. "collectDT": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),
  194. "manuCode": "10014",
  195. "result": indentify_result_1,
  196. "resultImage": indentify_photo_1
  197. }
  198. # data = {
  199. # "cmd": "Imageresult",
  200. # "ext": {
  201. # "Image": img_addr_1,
  202. # "Result_image": indentify_photo_1,
  203. # "Result": indentify_result_1,
  204. # "imei": imei
  205. # }
  206. # }
  207. except Exception as eee:
  208. print("====京东错误信息为",eee)
  209. data = {
  210. "devId": imei,
  211. "ossPath": "",
  212. "collectDT": "",
  213. "manuCode": "10014",
  214. "result": "",
  215. "resultImage": ""
  216. }
  217. # data = {
  218. # "cmd": "Imageresult",
  219. # "ext": {
  220. # "Image": "",
  221. # "Result_image": "",
  222. # "Result": "",
  223. # "imei": imei
  224. # }
  225. # }
  226. # ----------------------------------------------------------------------------
  227. TASK_TOPIC = 'api/v1/iot/dragon/' + imei + "/telemetry" # 客户端发布消息主题
  228. """
  229. 客户端发布消息
  230. :param message: 消息主体
  231. :return:
  232. """
  233. payload = data
  234. # publish(主题:Topic; 消息内容)
  235. client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
  236. client = mqtt.Client(client_id, transport='tcp')
  237. client.username_pw_set(imei, imei)
  238. client.connect("aftrnmynhcbc5elv-master-nlb-FI.jvessel-open-hb.jdcloud.com", 1883, 60) # 此处端口默认为1883,通信端口期keepalive默认60
  239. # client.connect("120.27.222.26", 1883, 60) # 此处端口默认为1883,通信端口期keepalive默认60
  240. client.loop_start()
  241. client.publish(TASK_TOPIC, json.dumps(payload, ensure_ascii=False, qos=0))
  242. print("京东信息发送成功",data)
  243. print(datetime.datetime.now())
  244. # -------------------------------------------------------------------
  245. # TASK_TOPIC = '/yfkj/cbd/sub/%s'%imei # 客户端发布消息主题
  246. # """
  247. # 客户端发布消息
  248. # :param message: 消息主体
  249. # :return:
  250. # """
  251. # payload = data
  252. # # publish(主题:Topic; 消息内容)
  253. # client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
  254. # client = mqtt.Client(client_id, transport='tcp')
  255. # client.connect("120.27.222.26", 1883, 60) # 此处端口默认为1883,通信端口期keepalive默认60
  256. # client.loop_start()
  257. # client.publish(TASK_TOPIC, json.dumps(payload, ensure_ascii=False))
  258. # print("京东信息发送成功",data)
  259. # equip = Equip.objects.get(equip_id=imei)
  260. # if equip.ftp_addr:
  261. # # http://120.27.222.26/cbd/pyftp/ftp_file/ykm_cbd/868575028845867/20190305/20:56:15.jpg
  262. # ftp_addr = equip.ftp_addr
  263. # pattern = r'[#]'
  264. # split_result = re.split(pattern, ftp_addr)
  265. # print("split_result:",split_result)
  266. # try:
  267. # need_result = split_result[1]
  268. # except:
  269. # print("对接设备的地址填写有误!请检查!")
  270. # need_result = "0"
  271. print("imei------------------------>>",imei)
  272. try:
  273. equip_list = Equip_Forward.objects.get(equip_id=imei)
  274. ftp_addr = equip_list.equip_img_url
  275. pattern = r'[#]'
  276. split_result = re.split(pattern, ftp_addr)
  277. print("split_result:",split_result)
  278. need_result = split_result[1]
  279. except:
  280. print("对接设备的地址填写有误!请检查!")
  281. need_result = "400"
  282. #害虫识别
  283. if need_result == "1":
  284. if CBDphoto.objects.get(addr=img_addr).indentify_result == None:
  285. indentify_photo_1 = CBDphoto.objects.get(addr=img_addr).indentify_result
  286. else:
  287. r_photo = CBDphoto.objects.get(addr=img_addr).indentify_photo
  288. indentify_photo_1 = "http://120.27.222.26/" + r_photo
  289. try:
  290. # r_photo = CBDphoto.objects.get(addr=img_addr).indentify_photo
  291. indentify_result_1 = CBDphoto.objects.get(addr=img_addr).indentify_result
  292. img_addr_1 = "http://120.27.222.26/" + img_addr
  293. # indentify_photo_1 = "http://120.27.222.26/" + r_photo
  294. print("img_addr_1:",img_addr_1)
  295. print("indentify_photo_1:",indentify_photo_1)
  296. print("indentify_result_1:",indentify_result_1)
  297. if indentify_result_1 == None:
  298. data = {
  299. "Image":img_addr_1,
  300. "imei":imei,
  301. "Result_image":"0",
  302. "Result":"0",
  303. }
  304. else:
  305. data = {
  306. "Image":img_addr_1,
  307. "Result_image":indentify_photo_1,
  308. "Result":indentify_result_1,
  309. "imei":imei
  310. }
  311. data = json.dumps(data, cls=CJSONEncoder)
  312. print("data:",data)
  313. if equip_list.equip_img_between == "1":
  314. res_1 = requests.post(split_result[0],data=data,timeout=2)
  315. elif equip_list.equip_img_between == "2":
  316. headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
  317. res_1 = requests.post(split_result[0], data=data, timeout=3,headers=headers)
  318. print("res_1:",res_1)
  319. print("------------------识别结果发送成功!!!")
  320. except Exception as aex:
  321. print(aex)
  322. try:
  323. img_addr_1 = "http://120.27.222.26/" + img_addr
  324. print("img_addr_1:",img_addr_1)
  325. data = {
  326. "Image":img_addr_1,
  327. "imei":imei,
  328. "Result_image":"0",
  329. "Result":"0",
  330. }
  331. data = json.dumps(data, cls=CJSONEncoder)
  332. print("data:",data)
  333. print("url:",split_result[0])
  334. if equip_list.equip_img_between == "1":
  335. res_1 = requests.post(split_result[0],data=data,timeout=2)
  336. elif equip_list.equip_img_between == "2":
  337. headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
  338. res_1 = requests.post(split_result[0], data=data, timeout=3,headers=headers)
  339. print(res_1.text)
  340. except Exception as e:
  341. print("错误信息为",e)
  342. print("------------------识别结果发送失败!!!")
  343. #害虫计数
  344. elif need_result == "2":
  345. try:
  346. # r_photo = CBDphoto.objects.get(addr=img_addr).indentify_photo
  347. indentify_result_1 = CBDphoto.objects.get(addr=img_addr).indentify_result
  348. img_addr_1 = "http://120.27.222.26/" + img_addr
  349. # indentify_photo_1 = "http://120.27.222.26/" + r_photo
  350. print("img_addr_1:",img_addr_1)
  351. print("indentify_result_1:",indentify_result_1)
  352. data = {
  353. "Image":img_addr_1,
  354. "Result":indentify_result_1,
  355. "imei":imei
  356. }
  357. data = json.dumps(data, cls=CJSONEncoder)
  358. print("data:",data)
  359. if equip_list.equip_img_between == "1":
  360. res_1 = requests.post(split_result[0],data=data,timeout=2)
  361. elif equip_list.equip_img_between == "2":
  362. headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
  363. res_1 = requests.post(split_result[0], data=data, timeout=3,headers=headers)
  364. print("res_1:",res_1)
  365. print("------------------识别结果发送成功!!!")
  366. except Exception as aex:
  367. print(aex)
  368. try:
  369. img_addr_1 = "http://120.27.222.26/" + img_addr
  370. print("img_addr_1:",img_addr_1)
  371. data = {
  372. "Image":img_addr_1,
  373. "imei":imei,
  374. "Result":"0",
  375. }
  376. data = json.dumps(data, cls=CJSONEncoder)
  377. print("data:",data)
  378. print("url:",split_result[0])
  379. if equip_list.equip_img_between == "1":
  380. res_1 = requests.post(split_result[0],data=data,timeout=2)
  381. elif equip_list.equip_img_between == "2":
  382. headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
  383. res_1 = requests.post(split_result[0], data=data, timeout=3,headers=headers)
  384. print(res_1.text)
  385. except Exception as e:
  386. print("错误信息为",e)
  387. print("------------------识别结果发送失败!!!")
  388. #不带害虫识别
  389. elif need_result == "0":
  390. try:
  391. img_addr_1 = "http://120.27.222.26/" + img_addr
  392. print("img_addr_1:",img_addr_1)
  393. data = {
  394. "Image":img_addr_1,
  395. "imei":imei
  396. }
  397. data = json.dumps(data, cls=CJSONEncoder)
  398. print("data:",data)
  399. print("url:",split_result[0])
  400. if equip_list.equip_img_between == "1":
  401. res_1 = requests.post(split_result[0],data=data,timeout=2)
  402. elif equip_list.equip_img_between == "2":
  403. headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
  404. res_1 = requests.post(split_result[0], data=data, timeout=3,headers=headers)
  405. print("res_1:",res_1.text)
  406. # 判断文件大小的方法
  407. # path = 'F:\YFWLW\yfwlw\\16_19_21.jpg'
  408. # size = os.path.getsize(path)/1024
  409. print("------------------对接设备图片发送成功!!!")
  410. except Exception as aexc:
  411. print(aexc)
  412. print("------------------对接设备图片发送失败!!!")
  413. except Exception as exce:
  414. print(exce)
  415. print("!!!")
  416. equip = Equip.objects.get(equip_id=imei)
  417. if equip.scene == "1":
  418. mycopyfile("../pyftp/" + os.path.join(a_path, a_name),"../pyftp/scene1/" + os.path.join(a_path, a_name))
  419. # img_addr = "pyftp/scene1/" + os.path.join(event.path, event.name)
  420. elif equip.scene == "2":
  421. mycopyfile("../pyftp/" + os.path.join(a_path, a_name),"../pyftp/scene2/" + os.path.join(a_path, a_name))
  422. # img_addr = "pyftp/scene1/" + os.path.join(event.path, event.name)
  423. elif equip.scene == "3":
  424. mycopyfile("../pyftp/" + os.path.join(a_path, a_name),"../pyftp/scene3/" + os.path.join(a_path, a_name))
  425. # img_addr = "pyftp/scene1/" + os.path.join(event.path, event.name)
  426. elif equip.scene == "4":
  427. mycopyfile("../pyftp/" + os.path.join(a_path, a_name),"../pyftp/scene4/" + os.path.join(a_path, a_name))
  428. # img_addr = "pyftp/scene1/" + os.path.join(event.path, event.name)
  429. sys.stdout = origin
  430. f.close()
  431. return
  432. @shared_task
  433. def test(imei):
  434. # time.sleep(20)
  435. nowtime = datetime.datetime.now().strftime('%Y%m%d')
  436. origin = sys.stdout
  437. f = open('logs/'+"bzyphoto" + nowtime +'.txt','a')
  438. sys.stdout = f
  439. print('=================================================')
  440. print("<-----imei:\n" + imei + ';\n')
  441. print("Message:\n" + "http:bzyphoto" + "----->\n")
  442. print("%s"%datetime.datetime.now())
  443. print('=================================================')
  444. pic_name = datetime.datetime.now().strftime('%Y%m')
  445. try:
  446. args = "pyftp/ftp_file/bzy_test/" + imei
  447. cv_num_list = []
  448. file_list1 = list(paths.list_images(args))
  449. print("\n===========================")
  450. print("file_list1:",file_list1)
  451. print("type file_list1:",type(file_list1))
  452. cv_num_list1 = []
  453. for imagePath1 in file_list1[::]:
  454. # fm = get_value(imagePath)
  455. # 孢子仪图片过小删除
  456. if os.path.getsize(imagePath1) <= 10:
  457. file_list1.remove(imagePath1)
  458. img1 = cv2.imread(imagePath1)
  459. img1 = cv2.resize(img1, (800, 600))
  460. gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
  461. # 创建SIFT对象
  462. sift = cv2.xfeatures2d.SIFT_create()
  463. keypoints, descriptor = sift.detectAndCompute(gray, None)
  464. img1 = cv2.drawKeypoints(image=img1, outImage=img1, keypoints=keypoints,flags=cv2.DRAW_MATCHES_FLAGS_DEFAULT, color=(0, 0, 255))
  465. fm1 = len(keypoints)
  466. cv_num_list1.append(fm1)
  467. # 打印清晰度值:
  468. # print("fm1",fm1)
  469. # # 打印文件路径
  470. # print("imagePath1",imagePath1)
  471. file_cv_tuple1 = tuple(zip(cv_num_list1,file_list1))
  472. print("file_cv_tuple1:\n",file_cv_tuple1)
  473. print("=========================================/n")
  474. file_cv_tuple_list1 = list(file_cv_tuple1)
  475. print("file_cv_tuple_list1:\n",file_cv_tuple_list1)
  476. print("*****************************************/n")
  477. # 按照清晰度排序:, reverse=True降序
  478. result1 = sorted(file_cv_tuple_list1, key=lambda s: s[0],reverse=True)
  479. print("result1:\n",result1)
  480. # 切片取清晰度最高的前五张图片:
  481. result1_5 = result1[0:3]
  482. # result5 = result[0:1]
  483. print("result1_5:\n",result1_5)
  484. pic_name = datetime.datetime.now().strftime('%Y%m')
  485. # 定义要创建的目录:
  486. mkpath="./pyftp/ftp_file/bzy_photo/" + imei + "/" + pic_name
  487. # 调用函数,创建目录:
  488. mkdir(mkpath)
  489. for i in result1_5:
  490. # for i in result5:
  491. #i值:(137.06453470539032, 'images\\192.168.2.64_01_20190802095315882_ALARM_INPUT.jpg')
  492. print("file path:",i[1])
  493. #复制文件:
  494. shutil.copy(i[1],mkpath)
  495. # os.system("rm ./pyftp/ftp_file/bzy_test/%s/*.jpg -rf"%imei)
  496. os.system("mv ./pyftp/ftp_file/bzy_test/%s ./pyftp/ftp_file/bzy_test/%s_%s"%(imei,imei,datetime.datetime.now().strftime("%Y%m%d%H%M%S")))
  497. print("-----------------image move success----------------------")
  498. equip = Equip.objects.get(equip_id=imei)
  499. file_dir="pyftp/ftp_file/bzy_photo/" + imei + "/" + pic_name
  500. for root, dirs, files in os.walk(file_dir):
  501. print("当前目录为=========>>",root) #当前目录路径
  502. print("当前子目录为=========>>",dirs) #当前路径下所有子目录
  503. print("当前目录文件为=========>>",files) #当前路径下所有非目录子文件
  504. for i in files:
  505. photo_addr = file_dir + "/" + i
  506. print("文件夹图片路径===========》》",photo_addr)
  507. if BZYphoto.objects.filter(addr=photo_addr).exists():
  508. print("照片存在")
  509. else:
  510. bzys = BZYphoto.objects.create(equip_id=equip, addr=photo_addr)
  511. print("照片不存在")
  512. if equip.ftp_addr:
  513. bzy_imgs = "http://www.yfzhwlw.com/bzy/" + bzys.addr
  514. data = {
  515. "Image":bzy_imgs,
  516. "imei":bzys.equip_id.equip_id,
  517. }
  518. data = json.dumps(data)
  519. headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
  520. bzy_photo = requests.post(equip.ftp_addr,data=data,headers=headers,timeout=3)
  521. print("转发成功")
  522. except Exception as a:
  523. print('=================================================')
  524. print("<-----imei:\n" + imei + ';\n')
  525. print("Message:\n" + "http:bzyphoto" + "----->\n")
  526. print("%s"%datetime.datetime.now())
  527. print("==========%s========"%a)
  528. print("-----------------image move default----------------------")
  529. sys.stdout = origin
  530. f.close()
  531. return