workstate.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Author: NiuJiuRu
  2. // Email: niujiuru@qq.com
  3. // Date: 2025-11-20
  4. package mcu_ctrl_board
  5. import "sync"
  6. // 定义_RTU_数据板的工作状态
  7. type WorkState uint32
  8. const (
  9. Idle WorkState = 0 // 空闲(默认)
  10. RtuAppUpgrading WorkState = 1 << 0 // 数据板的固件-升级中
  11. McuAppUpgrading WorkState = 1 << 1 // 控制板的固件-升级中
  12. PhotoCapturing WorkState = 1 << 2 // 当前相机图像-拍照中
  13. PhotoUploading WorkState = 1 << 3 // 相机图像文件-上传中
  14. SensorDataReceiving WorkState = 1 << 4 // 当前环境数据-收集中
  15. SensorDataUploading WorkState = 1 << 5 // 当前环境数据-上传中
  16. SensorHistUploading WorkState = 1 << 6 // 历史环境数据-上传中
  17. )
  18. // RTU数据板的工作状态管理器
  19. type WorkStateMgr struct {
  20. mu sync.RWMutex
  21. state WorkState
  22. }
  23. var GlobalWorkState = &WorkStateMgr{state: Idle}
  24. // 工作状态管理-添加一个状态
  25. func (mgr *WorkStateMgr) Add(s WorkState) {
  26. mgr.mu.Lock()
  27. defer mgr.mu.Unlock()
  28. mgr.state |= s
  29. }
  30. // 工作状态管理-清除一个状态
  31. func (mgr *WorkStateMgr) Remove(s WorkState) {
  32. mgr.mu.Lock()
  33. defer mgr.mu.Unlock()
  34. mgr.state &^= s
  35. }
  36. // 工作状态管理-获取当前状态
  37. // 判断某个状态是否存在
  38. func (mgr *WorkStateMgr) Get() WorkState {
  39. mgr.mu.RLock()
  40. defer mgr.mu.RUnlock()
  41. return mgr.state
  42. }
  43. // 工作状态管理-状态是否存在
  44. func (mgr *WorkStateMgr) Has(s WorkState) bool {
  45. mgr.mu.RLock()
  46. defer mgr.mu.RUnlock()
  47. return mgr.state&s != 0
  48. }