|
|
@@ -6,7 +6,10 @@ import os
|
|
|
from utils.utils_func import get_folder, check_path
|
|
|
import datetime
|
|
|
from utils.SingletonType import SingletonType
|
|
|
-from databases import CRUD,SqlQuery,PhotoRecord
|
|
|
+from databases import CRUD, SqlQuery, PhotoRecord
|
|
|
+import asyncio
|
|
|
+
|
|
|
+
|
|
|
def updateImageRaw(time_str, image_path, goods_art_no, image_index):
|
|
|
session = SqlQuery()
|
|
|
crud = CRUD(PhotoRecord)
|
|
|
@@ -52,7 +55,7 @@ class FileEventHandler(FileSystemEventHandler, metaclass=SingletonType):
|
|
|
self.observer = None
|
|
|
self.last_create_time = datetime.datetime.now()
|
|
|
|
|
|
- def start_observer(self,path):
|
|
|
+ def start_observer(self, path):
|
|
|
if self.observer != None:
|
|
|
return
|
|
|
print("图片保存目录:", path)
|
|
|
@@ -62,7 +65,9 @@ class FileEventHandler(FileSystemEventHandler, metaclass=SingletonType):
|
|
|
|
|
|
watch_path = self.check_and_get_real_dir(path)
|
|
|
if watch_path:
|
|
|
- self.observer.schedule(self, watch_path, recursive=True) # recursive 遍历目录
|
|
|
+ self.observer.schedule(
|
|
|
+ self, watch_path, recursive=True
|
|
|
+ ) # recursive 遍历目录
|
|
|
self.observer.start()
|
|
|
print("开启开门狗目录监听")
|
|
|
else:
|
|
|
@@ -73,7 +78,7 @@ class FileEventHandler(FileSystemEventHandler, metaclass=SingletonType):
|
|
|
逻辑:
|
|
|
1、检查当前路径如包含Originals 则定位到对应Originals的父级目录
|
|
|
2、如没有Originals,则认定为一个普通目录(且子文件夹没有Originals),否则定位到父级目录
|
|
|
-
|
|
|
+
|
|
|
"""
|
|
|
if not os.path.exists(watch_path):
|
|
|
return None
|
|
|
@@ -91,9 +96,11 @@ class FileEventHandler(FileSystemEventHandler, metaclass=SingletonType):
|
|
|
year = now.year
|
|
|
month = now.month
|
|
|
day = now.day
|
|
|
- path = r"{root_path}\Originals\{year}\{month}\{day}".format(root_path=root_path, year=year, month=month, day=day)
|
|
|
+ path = r"{root_path}\Originals\{year}\{month}\{day}".format(
|
|
|
+ root_path=root_path, year=year, month=month, day=day
|
|
|
+ )
|
|
|
check_path(path)
|
|
|
- print("watch_path:",path)
|
|
|
+ print("watch_path:", path)
|
|
|
return path
|
|
|
|
|
|
def on_moved(self, event):
|
|
|
@@ -104,10 +111,24 @@ class FileEventHandler(FileSystemEventHandler, metaclass=SingletonType):
|
|
|
# if os.path.split(event.dest_path)[0] == settings.PhotoOutputDir:
|
|
|
# print("1111111")
|
|
|
pass
|
|
|
+
|
|
|
def updatePhotoRecord(self):
|
|
|
session = SqlQuery()
|
|
|
crud = CRUD(PhotoRecord)
|
|
|
crud.read()
|
|
|
+
|
|
|
+ async def sendCallback(self, file_path):
|
|
|
+ await asyncio.sleep(0.01)
|
|
|
+ self.mcu.msg_type = "run_mcu_single"
|
|
|
+ print("货号不存在,监听不写入", self.mcu.msg_type)
|
|
|
+ self.mcu.sendSocketMessage(
|
|
|
+ code=0,
|
|
|
+ msg="主图测试拍摄完成",
|
|
|
+ device_status=2,
|
|
|
+ data={"file_path": file_path.replace("\\", "/")},
|
|
|
+ )
|
|
|
+ self.msg_type = "mcu"
|
|
|
+
|
|
|
def on_created(self, event):
|
|
|
if not event.is_directory:
|
|
|
file_path = event.src_path
|
|
|
@@ -119,25 +140,27 @@ class FileEventHandler(FileSystemEventHandler, metaclass=SingletonType):
|
|
|
try:
|
|
|
take_time = time.time()
|
|
|
self.send_log("获取文件file_path:{}".format(file_path))
|
|
|
- create_time = datetime.datetime.fromtimestamp(os.path.getctime(file_path))
|
|
|
+ create_time = datetime.datetime.fromtimestamp(
|
|
|
+ os.path.getctime(file_path)
|
|
|
+ )
|
|
|
# print("获取文件create_time:{}".format(create_time))
|
|
|
- self.get_photo_info(raw_path=file_path, create_time=create_time, take_time=take_time)
|
|
|
+ self.get_photo_info(
|
|
|
+ raw_path=file_path, create_time=create_time, take_time=take_time
|
|
|
+ )
|
|
|
self.send_log("获取文件create_time:{}".format(create_time))
|
|
|
if file_path == None:
|
|
|
print("file_path不存在,监听不写入")
|
|
|
return
|
|
|
if self.goods_art_no == None:
|
|
|
# print("货号不存在,监听不写入")
|
|
|
- self.mcu.msg_type = "run_mcu_single"
|
|
|
- self.mcu.sendSocketMessage(
|
|
|
- code=0,
|
|
|
- msg="主图测试拍摄完成",
|
|
|
- device_status=2,
|
|
|
- data={"file_path": file_path},
|
|
|
- )
|
|
|
- self.msg_type = "mcu"
|
|
|
+ loop = asyncio.new_event_loop()
|
|
|
+ asyncio.set_event_loop(loop)
|
|
|
+ loop.run_until_complete(self.sendCallback(file_path))
|
|
|
+ loop.close()
|
|
|
return
|
|
|
- updateImageRaw(create_time, file_path, self.goods_art_no,self.image_index)
|
|
|
+ updateImageRaw(
|
|
|
+ create_time, file_path, self.goods_art_no, self.image_index
|
|
|
+ )
|
|
|
except BaseException as e:
|
|
|
print("获取文件create_time失败", e)
|
|
|
self.send_log("获取文件处理失败{}".format(e))
|
|
|
@@ -163,7 +186,9 @@ class FileEventHandler(FileSystemEventHandler, metaclass=SingletonType):
|
|
|
# 判断是否为文件
|
|
|
if os.path.isfile(file_path):
|
|
|
# 获取文件创建时间
|
|
|
- create_time = datetime.datetime.fromtimestamp(os.path.getctime(file_path))
|
|
|
+ create_time = datetime.datetime.fromtimestamp(
|
|
|
+ os.path.getctime(file_path)
|
|
|
+ )
|
|
|
# 更新最早时间
|
|
|
if create_time > self.last_create_time:
|
|
|
self.last_create_time = create_time
|