feat: v1.1.0 - 图片按日期分文件夹保存、设置面板、序号显示模式、自动刷新
This commit is contained in:
49
scheduler.py
49
scheduler.py
@@ -7,7 +7,7 @@ import datetime
|
||||
from camera import CameraCapture
|
||||
from analyzer import ImageAnalyzer
|
||||
from database import db
|
||||
from config import CAPTURE_INTERVAL
|
||||
from config import config_mgr
|
||||
|
||||
|
||||
class VisionScheduler:
|
||||
@@ -17,9 +17,7 @@ class VisionScheduler:
|
||||
self.camera = CameraCapture()
|
||||
self.analyzer = ImageAnalyzer()
|
||||
self.running = False
|
||||
self.interval = CAPTURE_INTERVAL
|
||||
self.timer = None
|
||||
self.auto_analyze = True # 自动分析
|
||||
|
||||
# 统计
|
||||
self.capture_count = 0
|
||||
@@ -27,28 +25,19 @@ class VisionScheduler:
|
||||
self.last_analyze_time = None
|
||||
self.errors = []
|
||||
|
||||
def start(self, interval=None, auto_analyze=None):
|
||||
"""启动定时拍照
|
||||
|
||||
Args:
|
||||
interval: 拍照间隔(秒)
|
||||
auto_analyze: 是否自动分析
|
||||
"""
|
||||
def start(self):
|
||||
"""启动定时拍照"""
|
||||
if self.running:
|
||||
return {'success': False, 'error': '已在运行中'}
|
||||
|
||||
if interval:
|
||||
self.interval = interval
|
||||
if auto_analyze is not None:
|
||||
self.auto_analyze = auto_analyze
|
||||
|
||||
self.running = True
|
||||
interval = config_mgr.get('capture_interval', 60)
|
||||
self._schedule_next()
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'interval': self.interval,
|
||||
'auto_analyze': self.auto_analyze
|
||||
'interval': interval,
|
||||
'auto_analyze': config_mgr.get('auto_analyze', True)
|
||||
}
|
||||
|
||||
def stop(self):
|
||||
@@ -64,7 +53,8 @@ class VisionScheduler:
|
||||
"""安排下一次拍照"""
|
||||
if not self.running:
|
||||
return
|
||||
self.timer = threading.Timer(self.interval, self._capture_task)
|
||||
interval = config_mgr.get('capture_interval', 60)
|
||||
self.timer = threading.Timer(interval, self._capture_task)
|
||||
self.timer.start()
|
||||
|
||||
def _capture_task(self):
|
||||
@@ -78,12 +68,15 @@ class VisionScheduler:
|
||||
|
||||
if result['success']:
|
||||
# 记录到数据库
|
||||
image_id = db.add_image(result['path'])
|
||||
image_id = db.add_image(
|
||||
result['path'],
|
||||
date_folder=result.get('date_folder')
|
||||
)
|
||||
self.capture_count += 1
|
||||
self.last_capture_time = datetime.datetime.now().isoformat()
|
||||
|
||||
# 自动分析
|
||||
if self.auto_analyze:
|
||||
if config_mgr.get('auto_analyze', True):
|
||||
self._analyze_task(image_id, result['path'])
|
||||
else:
|
||||
self.errors.append({
|
||||
@@ -133,12 +126,15 @@ class VisionScheduler:
|
||||
result = self.camera.capture()
|
||||
|
||||
if result['success']:
|
||||
image_id = db.add_image(result['path'])
|
||||
image_id = db.add_image(
|
||||
result['path'],
|
||||
date_folder=result.get('date_folder')
|
||||
)
|
||||
self.capture_count += 1
|
||||
self.last_capture_time = datetime.datetime.now().isoformat()
|
||||
|
||||
# 如果自动分析开启,立即分析
|
||||
if self.auto_analyze:
|
||||
if config_mgr.get('auto_analyze', True):
|
||||
threading.Thread(
|
||||
target=self._analyze_task,
|
||||
args=(image_id, result['path'])
|
||||
@@ -148,7 +144,8 @@ class VisionScheduler:
|
||||
'success': True,
|
||||
'image_id': image_id,
|
||||
'path': result['path'],
|
||||
'timestamp': result['timestamp']
|
||||
'timestamp': result['timestamp'],
|
||||
'date_folder': result.get('date_folder')
|
||||
}
|
||||
|
||||
return result
|
||||
@@ -201,8 +198,8 @@ class VisionScheduler:
|
||||
"""获取调度器状态"""
|
||||
return {
|
||||
'running': self.running,
|
||||
'interval': self.interval,
|
||||
'auto_analyze': self.auto_analyze,
|
||||
'interval': config_mgr.get('capture_interval', 60),
|
||||
'auto_analyze': config_mgr.get('auto_analyze', True),
|
||||
'capture_count': self.capture_count,
|
||||
'last_capture_time': self.last_capture_time,
|
||||
'last_analyze_time': self.last_analyze_time,
|
||||
@@ -211,7 +208,7 @@ class VisionScheduler:
|
||||
|
||||
def set_interval(self, interval):
|
||||
"""设置拍照间隔"""
|
||||
self.interval = interval
|
||||
config_mgr.set('capture_interval', interval)
|
||||
if self.running:
|
||||
# 重启定时器
|
||||
self.stop()
|
||||
|
||||
Reference in New Issue
Block a user