update current project state
This commit is contained in:
@@ -27,9 +27,10 @@ import (
|
||||
)
|
||||
|
||||
type deviceAlarmState struct {
|
||||
InCondition bool
|
||||
StartTime int64
|
||||
AlarmPauseUntil int64
|
||||
InCondition bool
|
||||
StartTime int64
|
||||
AlarmPauseUntil int64
|
||||
FireLeaveForbidUntil int64
|
||||
// fire_check火焰检测状态
|
||||
FireCheckInCondition bool
|
||||
FireCheckStartTime int64
|
||||
@@ -42,6 +43,9 @@ var mergingAlarms sync.Map
|
||||
// 全局上报管理器
|
||||
var GlobalReporter = reporter.NewReporter()
|
||||
|
||||
const defaultFireLeaveForbidDuration = 15 * time.Minute
|
||||
const buildVersion = "2026-05-13-logger-init-fix"
|
||||
|
||||
// DeviceManager 设备管理器
|
||||
type DeviceManager struct {
|
||||
mu sync.Mutex
|
||||
@@ -63,6 +67,71 @@ func getDeviceState(deviceUUID string) *deviceAlarmState {
|
||||
return s
|
||||
}
|
||||
|
||||
func applyFireLeaveForbidWindow(duration time.Duration) {
|
||||
if duration <= 0 {
|
||||
return
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
until := now + int64(duration.Seconds())
|
||||
applyFireLeaveForbidWindowUntil(now, until)
|
||||
}
|
||||
|
||||
func applyFireLeaveForbidWindowUntil(startTime, endTime int64) {
|
||||
if endTime <= time.Now().Unix() {
|
||||
return
|
||||
}
|
||||
count := 0
|
||||
GlobalDeviceManager.RangeDevices(func(uuid string, device *connect.DeviceData) bool {
|
||||
state := getDeviceState(uuid)
|
||||
state.FireLeaveForbidUntil = endTime
|
||||
state.InCondition = false
|
||||
count++
|
||||
return true
|
||||
})
|
||||
logger.Logger.Printf("fire_leave reporting paused: start=%d, end=%d, devices=%d", startTime, endTime, count)
|
||||
}
|
||||
|
||||
func clearFireLeaveForbidWindow() {
|
||||
count := 0
|
||||
GlobalDeviceManager.RangeDevices(func(uuid string, device *connect.DeviceData) bool {
|
||||
state := getDeviceState(uuid)
|
||||
state.FireLeaveForbidUntil = 0
|
||||
state.InCondition = false
|
||||
count++
|
||||
return true
|
||||
})
|
||||
logger.Logger.Printf("fire_leave reporting pause cleared, devices=%d", count)
|
||||
}
|
||||
|
||||
func persistAndApplyFireLeaveForbidWindow(minute string, duration time.Duration) {
|
||||
if duration <= 0 {
|
||||
if err := reporter.SaveForbidTimeWindow(minute, 0, 0); err != nil {
|
||||
logger.Logger.Printf("Failed to clear forbid time config: %v", err)
|
||||
}
|
||||
clearFireLeaveForbidWindow()
|
||||
return
|
||||
}
|
||||
startTime := time.Now().Unix()
|
||||
endTime := startTime + int64(duration.Seconds())
|
||||
if err := reporter.SaveForbidTimeWindow(minute, startTime, endTime); err != nil {
|
||||
logger.Logger.Printf("Failed to save forbid time config: %v", err)
|
||||
} else {
|
||||
logger.Logger.Printf("Forbid time config saved successfully: Minute=%s, Start=%d, End=%d", minute, startTime, endTime)
|
||||
}
|
||||
applyFireLeaveForbidWindowUntil(startTime, endTime)
|
||||
}
|
||||
|
||||
func restoreFireLeaveForbidWindow(deviceUUID string) {
|
||||
config, err := reporter.LoadForbidTimeConfig()
|
||||
if err != nil || config.EndTime <= time.Now().Unix() {
|
||||
return
|
||||
}
|
||||
state := getDeviceState(deviceUUID)
|
||||
state.FireLeaveForbidUntil = config.EndTime
|
||||
state.InCondition = false
|
||||
logger.Logger.Printf("Restored fire_leave forbid window: DeviceUUID=%s, Start=%d, End=%d", deviceUUID, config.StartTime, config.EndTime)
|
||||
}
|
||||
|
||||
// AddDevice 添加设备
|
||||
func (dm *DeviceManager) AddDevice(deviceData connect.DeviceData) {
|
||||
dm.mu.Lock()
|
||||
@@ -196,6 +265,7 @@ func processDevice(ctx context.Context, deviceData connect.DeviceData) {
|
||||
|
||||
// 首先将设备添加到设备管理器
|
||||
GlobalDeviceManager.AddDevice(deviceData)
|
||||
restoreFireLeaveForbidWindow(deviceData.DeviceUUID)
|
||||
defer GlobalDeviceManager.StopDevice(deviceData.DeviceUUID)
|
||||
|
||||
// 创建带取消的子上下文
|
||||
@@ -588,6 +658,15 @@ func processDevice(ctx context.Context, deviceData connect.DeviceData) {
|
||||
|
||||
conditionMet := noPerson && tempStableHigh
|
||||
|
||||
if now < state.FireLeaveForbidUntil {
|
||||
if state.InCondition {
|
||||
state.InCondition = false
|
||||
}
|
||||
logger.Logger.Printf("fire_leave report paused: %ds remaining (DeviceUUID=%s)",
|
||||
state.FireLeaveForbidUntil-now, deviceData.DeviceUUID)
|
||||
continue
|
||||
}
|
||||
|
||||
// 1. 冷却期检查
|
||||
if now < state.AlarmPauseUntil {
|
||||
if state.InCondition {
|
||||
@@ -775,6 +854,7 @@ func (dm *DeviceManager) RangeDevices(fn func(uuid string, device *connect.Devic
|
||||
}
|
||||
}
|
||||
func main() {
|
||||
fmt.Printf("FireLeave Tool build: %s\n", buildVersion)
|
||||
|
||||
var rtspURL string
|
||||
var deviceUUID string
|
||||
@@ -842,6 +922,11 @@ func main() {
|
||||
go func() {
|
||||
// 创建GPIO按钮监视器
|
||||
monitor := gpio_monitor.NewGPIOButtonMonitor(gpio_monitor.ButtonGPIO)
|
||||
monitor.SetOnPress(func() {
|
||||
minute := reporter.GetForbidTimeConfig()
|
||||
duration := reporter.ParseForbidDuration(minute, defaultFireLeaveForbidDuration)
|
||||
persistAndApplyFireLeaveForbidWindow(minute, duration)
|
||||
})
|
||||
// 启动GPIO监控
|
||||
if err := monitor.Start(ctx); err != nil {
|
||||
logger.Logger.Printf("Failed to start GPIO button monitor: %v", err)
|
||||
@@ -908,8 +993,7 @@ func printGlobalDeviceStatus() {
|
||||
i := 1
|
||||
for uuid, data := range activeDevices {
|
||||
|
||||
stateInf, _ := deviceStates.Load(uuid)
|
||||
state := stateInf.(*deviceAlarmState)
|
||||
state := getDeviceState(uuid)
|
||||
cooldownLeft := int64(0)
|
||||
if now < state.AlarmPauseUntil {
|
||||
cooldownLeft = state.AlarmPauseUntil - now
|
||||
@@ -998,13 +1082,7 @@ func handleForbidTimeMessage(message []byte) {
|
||||
// 处理禁止时间配置
|
||||
if content.Minute != "" {
|
||||
logger.Logger.Printf("Forbid time config received: Minute=%s", content.Minute)
|
||||
|
||||
// 保存到缓存文件
|
||||
if err := reporter.SaveForbidTimeConfig(content.Minute); err != nil {
|
||||
logger.Logger.Printf("Failed to save forbid time config: %v", err)
|
||||
} else {
|
||||
logger.Logger.Printf("Forbid time config saved successfully: Minute=%s", content.Minute)
|
||||
}
|
||||
persistAndApplyFireLeaveForbidWindow(content.Minute, reporter.ParseForbidDuration(content.Minute, 0))
|
||||
}
|
||||
|
||||
// 处理事件周期时限配置
|
||||
|
||||
Reference in New Issue
Block a user