| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- # -*- coding: utf-8 -*-
- # File Name:mqtt_chat_client.py
- # Python Version:3.5.1
- import os
- import django
- import sys
- from qcloudsms_py import SmsSingleSender
- from qcloudsms_py.httpclient import HTTPError
- from qcloudsms_py import SmsMultiSender
- BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # 定位到你的django根目录
- sys.path.append(os.path.abspath(os.path.join(BASE_DIR, os.pardir)))
- os.environ.setdefault("DJANGO_SETTINGS_MODULE",
- "yfwlw_pro.settings") # project_name 项目名称
- django.setup()
- import json
- from apps.AppInfoManage.models import Equip, Equip_type, MyUser, CBDphoto, CBDstatus, QXZdata, SCDstatus, CBDstatus, Equip_SimInfo, CBDdata, BZYphoto, RecentCBDdata,RecentSCDdata,\
- Msg_Conf, Msg_Send, BZYstatus
- import re
- import datetime
- import time
- import random
- from apps.SimInfo.views import get_siminfo
- from apps.ReportManage.all_dict import insect_dict_new, transpont_equip_scd
- import requests
- import oss2
- auth = oss2.Auth('LTAI4G7tFh5Nk4KXZoSPk1D8', 'RV4S2SfbLPoFNjlI4uIOoA0J1LQPQc')
- # Endpoint以杭州为例,其它Region请按实际情况填写。
- # bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
- # bucket = oss2.Bucket(auth, 'https://cbdphoto.oss-cn-hangzhou.aliyuncs.com', 'cbdphoto')
- bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', 'bzypic')
- def TimeStampToTime(timestamp):
- timeStruct = time.localtime(timestamp)
- str_p = time.strftime('%Y-%m-%d %H:%M:%S',timeStruct)
- dateTime_p = datetime.datetime.strptime(str_p,'%Y-%m-%d %H:%M:%S')
- return dateTime_p
- def get_FileCreateTime(filePath):
- # filePath = unicode(filePath,'utf8')
- t = os.path.getctime(filePath)
- return TimeStampToTime(t)
- # imei = request.POST.get("imei")
- for x in Equip.objects.filter(equip_type=7):
- imei = x.equip_id
- equip_obj = x
- file_dir = "../pyftp/ftp_file/bzy_photo/%s"%imei
- for root, dirs, file in os.walk(file_dir):
- if file != [] and "jpg" in file[0]:
- # print("当前路径下所有非目录子文件>>",file)
- for i in file:
- pic_addr_1 = root + "/" + i
-
- pic_addr = pic_addr_1.replace("../","")
- picname_1 = get_FileCreateTime(pic_addr_1)
- print("文件时间为",picname_1)
- # now = datetime.datetime.now()
-
- addr = pic_addr.split("/")[-1]
- if "192.168" in addr:
- print("长addr",addr)
- _time = addr.split("_")[2][0:14]
- print("------->>",len(_time))
- try:
- _time = datetime.datetime.strptime(_time,'%Y%m%d%H%M%S')
- except:
- _time = picname_1
- print(_time)
- # if i.upl_time.hour != _time.hour:
- # i.upl_time = _time
- # i.save()
- elif len(addr) == 18:
- print("短addr",addr)
- _time = addr.split(".")[0][0:14]
- print("------->>",len(_time))
- _time = _time.split("\r")[0]
- try:
- _time = datetime.datetime.strptime(_time,'%Y%m%d%H%M%S')
- except:
- _time = picname_1
-
- strnow = datetime.datetime.strftime(_time,'%Y%m')
- picname = datetime.datetime.strftime(_time,'%Y%m%d%H%M%S')
-
- # bucket_pic = bucket.put_object_from_file(strnow + '/' + imei + '/'+ picname + '.jpg', pic_addr_1)
- print("图片数据库路径为pic_addr",pic_addr)
-
- new_addr = "https://bzypic.oss-cn-hangzhou.aliyuncs.com/" + strnow + '/' + imei + '/'+ picname + '.jpg'
-
- exist = bucket.object_exists(strnow + '/' + imei + '/'+ picname + '.jpg')
-
- if exist:
- print('object exist')
- if BZYphoto.objects.filter(addr=new_addr).exists():
- pass
- else:
- BZYphoto.objects.create(equip_id=equip_obj,addr=new_addr,upl_time=_time)
- os.remove(pic_addr_1)
- print("图片删除成功")
- print("数据库创建链接成功")
- else:
- print('object not exist')
- bucket_pic = bucket.put_object_from_file(strnow + '/' + imei + '/'+ picname + '.jpg', pic_addr_1)
- if BZYphoto.objects.filter(addr=new_addr).exists():
- pass
- else:
- BZYphoto.objects.create(equip_id=equip_obj,addr=new_addr,upl_time=_time)
- os.remove(pic_addr_1)
- print("图片删除成功")
- # BZYphoto.objects.create(equip_id=imei,addr=new_addr,upl_time=picname)
- print("oss传输成功")
-
-
-
- # if BZYphoto.objects.filter(addr=pic_addr).exists():
- # print("照片存在")
- # print("新的照片路径为",new_addr)
- # # 返回值为true表示文件存在,false表示文件不存在。
- # if exist:
- # if BZYphoto.objects.filter(addr=new_addr).exists():
- # BZYphoto.objects.filter(addr=new_addr).update(equip_id=equip_obj,upl_time=_time)
- # print('object exist')
- # BZYphoto.objects.filter(addr=pic_addr).update(equip_id=equip_obj,addr=new_addr,upl_time=_time)
- # else:
- # BZYphoto.objects.filter(addr=pic_addr).update(equip_id=equip_obj,addr=new_addr,upl_time=_time)
- # bucket_pic = bucket.put_object_from_file(strnow + '/' + imei + '/'+ picname + '.jpg', pic_addr_1)
- # print("oss传输成功")
- # print('object not exist')
- # else:
- # print("照片不存在")
- # # print("图片时间为",get_FileCreateTime(picname))
- # if exist:
- # print('object exist')
- # if BZYphoto.objects.filter(addr=new_addr).exists():
- # BZYphoto.objects.filter(addr=new_addr).update(equip_id=equip_obj,upl_time=_time)
- # else:
- # BZYphoto.objects.create(equip_id=equip_obj,addr=new_addr,upl_time=_time)
- # else:
- # print('object not exist')
- # if BZYphoto.objects.filter(addr=new_addr).exists():
- # BZYphoto.objects.filter(addr=new_addr).update(equip_id=equip_obj,upl_time=_time)
- # else:
- # BZYphoto.objects.create(equip_id=equip_obj,addr=new_addr,upl_time=_time)
- # # BZYphoto.objects.create(equip_id=imei,addr=new_addr,upl_time=picname)
- # bucket_pic = bucket.put_object_from_file(strnow + '/' + imei + '/'+ picname + '.jpg', pic_addr_1)
- # print("oss传输成功")
-
-
- if equip_obj.ftp_addr:
- # bzy_imgs = "http://www.yfzhwlw.com/bzy/" + photo_addr
- data = {
- "Image":new_addr,
- "imei":imei,
- }
- data = json.dumps(data)
- headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
- bzy_photo = requests.post(equip_obj.ftp_addr,data=data,headers=headers,timeout=3)
- print("转发成功")
- print('=================================================')
- # while True:
- # sta1 = BZYstatus.objects.all()
- # sta3 = []
- # photo_list = []
- # for i in sta1:
- # sta3.append(i.equip_id.equip_id)
- # for imei in sta3:
- # equip_obj = Equip.objects.get(equip_id=imei)
- # file_dir = "../pyftp/ftp_file/bzy_photo/%s"%imei
- # for root, dirs, files in os.walk(file_dir):
- # print(root)
- # if root.endswith(imei):
- # pass
- # else:
- # # print("当前目录------->>",root)
- # for roots, dirss, file in os.walk(root):
- # # print(roots) #当前目录路径
- # # print(dirs) #当前路径下所有子目录
- # # print(file) #当前路径下所有非目录子文件
- # if file != []:
- # for i in file:
- # photo_addr = roots + "/" + i
- # photo_addr = photo_addr.replace("../","")
- # # print(photo_addr)
- # if BZYphoto.objects.filter(addr=photo_addr).exists():
- # # print("照片存在")
- # pass
- # else:
- # # addr = photo_addr
- # addr = photo_addr.split("/")[-1]
- # if "192.168" in addr:
- # print("长addr",addr)
- # _time = addr.split("_")[2][0:14]
- # print("------->>",len(_time))
- # try:
- # _time = datetime.datetime.strptime(_time,'%Y%m%d%H%M%S')
- # except:
- # continue
- # print(_time)
- # # if i.upl_time.hour != _time.hour:
- # # i.upl_time = _time
- # # i.save()
- # elif len(addr) == 18:
- # print("短addr",addr)
- # _time = addr.split(".")[0][0:14]
- # print("------->>",len(_time))
- # _time = _time.split("\r")[0]
- # try:
- # _time = datetime.datetime.strptime(_time,'%Y%m%d%H%M%S')
- # except:
- # continue
- # print(_time)
- # # if i.upl_time.hour != _time.hour:
- # # i.upl_time = _time
- # # i.save()
- # print("图片时间为",_time)
- # print("图片链接为",photo_addr)
- # BZYphoto.objects.create(equip_id=equip_obj,addr=photo_addr,upl_time=_time)
-
-
-
-
-
-
- # if equip_obj.ftp_addr:
- # bzy_imgs = "http://www.yfzhwlw.com/bzy/" + photo_addr
- # data = {
- # "Image":bzy_imgs,
- # "imei":imei,
- # }
- # data = json.dumps(data)
- # headers = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
- # bzy_photo = requests.post(equip_obj.ftp_addr,data=data,headers=headers,timeout=3)
- # print("转发成功")
- # print('=================================================')
- # sys.stdout = origin
- # f.close()
- # print("过滤完成")
- # time.sleep(3600)
|