| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- # -*- coding: utf-8 -*-
- #!/usr/bin/env python
- # encoding:utf-8
- # 注意:import顺序很重要
- import os, sys
- # 将django项目根目录加入环境变量
- BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # 定位到你的django根目录
- sys.path.append(os.path.abspath(os.path.join(BASE_DIR, os.pardir)))
- # 引入django配置文件
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yfwlw_pro.settings") # 你的django的settings文件
- # 启动django
- import django
- django.setup()
- import re
- import shutil
- import json
- import requests
- from PIL import Image
- from io import BytesIO
- import time
- import paho.mqtt.client as mqtt
- import sys
- import datetime
- from pyinotify import WatchManager, Notifier, \
- ProcessEvent, IN_DELETE, IN_CREATE, IN_MODIFY
- from apps.AppInfoManage.models import Equip, Equip_type, CBDdata, CBDstatus, MyUser,Alarm_record,CBDphoto, RecentCBDdata
- from apps.EquipManage.views import CJSONEncoder
- from apps.ReportManage.tasks import photo_transpond,test
- # 剪切
- def mymovefile(srcfile,dstfile):
- if not os.path.isfile(srcfile):
- print("%s not exist!"%(srcfile))
- else:
- fpath,fname=os.path.split(dstfile) #分离文件名和路径
- if not os.path.exists(fpath):
- os.makedirs(fpath) #创建路径
- shutil.move(srcfile,dstfile) #移动文件
- print("move %s -> %s"%(srcfile,dstfile))
- # 复制
- def mycopyfile(srcfile,dstfile):
- if not os.path.isfile(srcfile):
- print("%s not exist!"%(srcfile))
- else:
- fpath,fname=os.path.split(dstfile) #分离文件名和路径
- if not os.path.exists(fpath):
- os.makedirs(fpath) #创建路径
- shutil.copyfile(srcfile,dstfile) #复制文件
- print("copy %s -> %s"%(srcfile,dstfile))
- class EventHandler(ProcessEvent):
- """事件处理"""
- def process_IN_CREATE(self, event):
- print ("Create file: %s " % os.path.join(event.path, event.name))
- if event.name.endswith(".jpg"):
- print("new file upload is ok!")
- print("type event.path:",type(event.path))
- print("event.path:",event.path)
- print("event.name:",event.name)
-
- # imei = re.findall(r"/(.+?)/",event.path)
- imei = re.findall(r"\d+\.?\d*",event.path)[0]
- print("imei:", imei)
- nowtime = datetime.datetime.now().strftime('%Y%m%d')
- # origin = sys.stdout
- # f = open('../logs/'+nowtime+'.txt','a+')
- # sys.stdout = f
- # 判断设备是否存在:
- if Equip.objects.filter(equip_id=imei).exists():
- equip = Equip.objects.get(equip_id=imei)
- img_addr = "pyftp/" + os.path.join(event.path, event.name)
- # print("设备场景为------->>",equip.scene)
- # print("设备场景为------->>",type(equip.scene))
- # time.sleep(10)
-
- data_obj = RecentCBDdata.objects.get(equip_id=equip)
- CBDphoto.objects.create(equip_id=equip, addr=img_addr,at=eval(data_obj.cbd_data)['at'],ah=eval(data_obj.cbd_data)['ah'])
- print("==================================================")
- print("图片增加链接成功!")
-
- r = photo_transpond.delay(imei,event.path,event.name)
- # r = test.delay(imei)
- print("---------------->>",r)
- # FTP转发图片
- # try:
- # # if imei == "868575028869008":
- # if imei == "869300039229913" or imei == "860344048751540":
- # print("img_addr",img_addr)
- # time.sleep(20)
- # a = "/data/yfwlw/" + img_addr
- # # a = "/home/web/yfwlw/" + img_addr
- # # c = event.name
- # # b = c.replace(':','-')
- # # 切割文件名
- # # aa = img_addr.split("/")
- # # 拼接文件夹
- # # b = aa[3] + "/" + aa[4] + "/" + b
- # n_time = datetime.datetime.now()
- # b = imei + "/" + n_time.strftime('%Y%m%d') + "/" + n_time.strftime('%H-%M-%S') + ".jpg"
- # # print("bbbbbbb",b)
- # print("target_ftp_name:",b)
- # ftp_url = os.system(r'curl ftp://47.95.247.86:8022/%s --ftp-create-dirs -u "hnyfkj:hnyfkj19416" -T "%s" -# -v'%(b,a))
- # print("=====>>",ftp_url)
- # else:
- # pass
- # if imei == "869300039255967" or imei == "869300039253798" or imei == "868575028876029":
- # print("img_addr",img_addr)
- # time.sleep(20)
- # a = "/data/yfwlw/" + img_addr
- # # a = "/home/web/yfwlw/" + img_addr
- # # c = event.name
- # # b = c.replace(':','-')
- # # 切割文件名
- # # aa = img_addr.split("/")
- # # 拼接文件夹
- # # b = aa[3] + "/" + aa[4] + "/" + b
- # n_time = datetime.datetime.now()
- # b = imei + "/" + n_time.strftime('%Y%m%d') + "/" + n_time.strftime('%H-%M-%S') + ".jpg"
- # print("target_ftp_name:",b)
- # ftp_url = os.system(r'curl ftp://47.95.247.86:8022/%s --ftp-create-dirs -u "xjhnyfkj:xjhnyfkj511" -T "%s" -# -v'%(b,a))
- # print("=====>>",ftp_url)
- # else:
- # pass
- # except Exception as eee:
- # print(eee)
-
-
- # try:
- # time.sleep(10)
- # # insect_indentify 昆虫识别
- # # # 保存图片之后调用害虫识别接口
- # # 正则切割文件名 去掉.jpg后缀
- # pattern = r'[.]'
- # res = event.name
- # result = re.split(pattern, res)
- # insect_pic_name = result[0] + 'result' + '.jpg'
- # # url = 'http://60.166.14.184:9143/upload'
- # url = 'http://60.166.14.184:9143/upload'
- # # 测试服务器路径
- # # imageFile = {'imageFile':('pic.jpg',open("/home/web/yfwlw/" + img_addr,'rb'),'image/jpeg')}
- # # 正式服务器路径
- # imageFile = {'imageFile':('pic.jpg',open("/data/yfwlw/" + img_addr,'rb'),'image/jpeg')}
- # data = {'longitude':'118.279643','latitude':'31.221456'}
- # print('data:',data)
- # res = requests.post(url,data=data,files=imageFile,timeout=10)
- # print("res:",res.text)
- # resp = json.loads(res.text)
- # print("insect_pic_name:", insect_pic_name)
- # print("-------------------------接口调用成功")
-
- # except Exception as e:
- # print(e)
- # print("---------------------识别失败")
- # try:
- # if resp["returnResult"]["returnStatus"] == "0":
- # print("识别结果为空,不进行保存")
- # else:
- # # 将识别结果中的链接中的图片存入本地 测试服务器
- # # pic_dir_1 = "/home/web/yfwlw/result_pyftp/" + event.path + "/"
- # pic_dir_1 = "/data/yfwlw/result_pyftp/" + event.path + "/"
- # print("pic_dir_1:",pic_dir_1)
- # if os.path.exists(pic_dir_1) == False:
- # os.makedirs(pic_dir_1)
- # img_src = resp["returnResult"]["returnImageUrl"]
- # response = requests.get(img_src)
- # image = Image.open(BytesIO(response.content))
- # insect_image_addr = pic_dir_1 + insect_pic_name
- # print("insect_image_addr:",insect_image_addr)
- # image.save(insect_image_addr)
- # print("------------------图片保存成功")
- # try:
- # photo_obj = CBDphoto.objects.get(addr=img_addr)
- # if resp["returnResult"]["returnStatus"] == "0":
- # print("识别结果为空,不进行保存")
- # else:
- # # 将识别结果存入数据库
- # pic_dir = "result_pyftp/" + os.path.join(event.path, insect_pic_name)
-
- # photo_obj.indentify_photo = pic_dir
- # photo_obj.indentify_result = resp["returnResult"]["returnString"]
- # photo_obj.save()
- # print("图片路径:", pic_dir)
- # print("---------------数据库保存识别结果成功")
- # except Exception as ex:
- # print(ex)
- # print("---------------数据库保存识别结果失败")
- # except Exception as exc:
- # print(exc)
- # print("----------------识别结果图片保存失败")
- # try:
- # 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":
- # try:
- # if CBDphoto.objects.get(addr=img_addr).indentify_photo == None:
- # indentify_photo_1 = ""
- # indentify_result_1 = ""
- # img_addr_1 = "http://120.27.222.26/" + img_addr
- # else:
- # r_photo = CBDphoto.objects.get(addr=img_addr).indentify_photo
- # indentify_result_1 = CBDphoto.objects.get(addr=img_addr).indentify_result
- # img_addr_1 = "http://120.27.222.26/" + img_addr
- # indentify_photo_1 = "http://120.27.222.26/" + r_photo
- # data = {
- # "cmd": "Imageresult",
- # "ext": {
- # "Image": img_addr_1,
- # "Result_image": indentify_photo_1,
- # "Result": indentify_result_1,
- # "imei": imei
- # }
- # }
-
- # except Exception as eee:
- # print("====京东错误信息为",eee)
- # data = {
- # "cmd": "Imageresult",
- # "ext": {
- # "Image": "",
- # "Result_image": "",
- # "Result": "",
- # "imei": imei
- # }
- # }
- # TASK_TOPIC = '/yfkj/cbd/sub/%s'%imei # 客户端发布消息主题
- # """
- # 客户端发布消息
- # :param message: 消息主体
- # :return:
- # """
- # payload = data
- # # publish(主题:Topic; 消息内容)
- # client_id = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
- # client = mqtt.Client(client_id, transport='tcp')
- # client.connect("120.27.222.26", 1883, 60) # 此处端口默认为1883,通信端口期keepalive默认60
- # client.loop_start()
- # client.publish(TASK_TOPIC, json.dumps(payload, ensure_ascii=False))
- # if equip.ftp_addr:
- # # http://120.27.222.26/cbd/pyftp/ftp_file/ykm_cbd/868575028845867/20190305/20:56:15.jpg
- # ftp_addr = equip.ftp_addr
- # pattern = r'[#]'
- # split_result = re.split(pattern, ftp_addr)
- # print("split_result:",split_result)
- # try:
- # need_result = split_result[1]
- # except:
- # print("对接设备的地址填写有误!请检查!")
- # need_result = "0"
- # if need_result == "1":
- # if CBDphoto.objects.get(addr=img_addr).indentify_result == None:
- # indentify_photo_1 = CBDphoto.objects.get(addr=img_addr).indentify_result
- # else:
- # r_photo = CBDphoto.objects.get(addr=img_addr).indentify_photo
- # indentify_photo_1 = "http://120.27.222.26/" + r_photo
- # try:
- # # r_photo = CBDphoto.objects.get(addr=img_addr).indentify_photo
- # indentify_result_1 = CBDphoto.objects.get(addr=img_addr).indentify_result
- # img_addr_1 = "http://120.27.222.26/" + img_addr
- # # indentify_photo_1 = "http://120.27.222.26/" + r_photo
- # print("img_addr_1:",img_addr_1)
- # print("indentify_photo_1:",indentify_photo_1)
- # print("indentify_result_1:",indentify_result_1)
- # data = {
- # "Image":img_addr_1,
- # "Result_image":indentify_photo_1,
- # "Result":indentify_result_1,
- # "imei":imei
- # }
-
- # data = json.dumps(data, cls=CJSONEncoder)
- # print("data:",data)
- # res_1 = requests.post(split_result[0],data=data,timeout=2)
- # print("res_1:",res_1)
- # print("------------------识别结果发送成功!!!")
- # except Exception as aex:
- # print(aex)
- # try:
- # img_addr_1 = "http://120.27.222.26/" + img_addr
- # print("img_addr_1:",img_addr_1)
- # data = {
- # "Image":img_addr_1,
- # "imei":imei,
- # "Result_image":img_addr_1,
- # "Result":"0",
- # }
- # data = json.dumps(data, cls=CJSONEncoder)
- # print("data:",data)
- # print("url:",split_result[0])
- # res_1 = requests.post(split_result[0],data=data)
- # print(res_1.text)
- # except Exception as e:
- # print("错误信息为",e)
- # print("------------------识别结果发送失败!!!")
- # else:
- # try:
- # img_addr_1 = "http://120.27.222.26/" + img_addr
- # print("img_addr_1:",img_addr_1)
- # data = {
- # "Image":img_addr_1,
- # "imei":imei
- # }
- # data = json.dumps(data, cls=CJSONEncoder)
- # print("data:",data)
- # print("url:",split_result[0])
- # res_1 = requests.post(split_result[0],data=data,timeout=2)
- # print("res_1:",res_1.text)
- # # 判断文件大小的方法
- # # path = 'F:\YFWLW\yfwlw\\16_19_21.jpg'
- # # size = os.path.getsize(path)/1024
- # print("------------------对接设备图片发送成功!!!")
- # except Exception as aexc:
- # print(aexc)
- # print("------------------对接设备图片发送失败!!!")
- # except Exception as exce:
- # print(exce)
- # print("!!!")
-
- else:
- print("该设备不存在")
- # sys.stdout = origin
- # f.close()
- def process_IN_DELETE(self, event):
- print ("Delete file: %s " % os.path.join(event.path, event.name))
- # imei = re.findall(r"\d+\.?\d*",event.path)[0]
- # img_addr = "pyftp/" + os.path.join(event.path, event.name)
- # img_addr = "/home/web/yfwlw/" + img_addr[0:-9]
- # print("image_addr:",img_addr)
- # print("判断是否存在:",os.path.exists(img_addr))
-
- if event.name.endswith(".jpg"):
- print("file delete is ok!")
- # def process_IN_MODIFY(self, event):
- # print ("Modify file: %s " % os.path.join(event.path, event.name))
- def FSMonitor(path='.'):
-
- wm = WatchManager()
- mask = IN_DELETE | IN_CREATE | IN_MODIFY
- notifier = Notifier(wm, EventHandler())
- wm.add_watch(path, mask, auto_add=True, rec=True)
- print('ftp file watchdog is running')
- print('now starting monitor %s' % (path))
- while True:
- try:
- notifier.process_events()
- if notifier.check_events():
- notifier.read_events()
- except KeyboardInterrupt:
- notifier.stop()
- break
- if __name__ == "__main__":
- FSMonitor('ftp_file/ykm_cbd')
|