1
0

power_shutdown.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // ============================================================================
  2. //go:build !service
  3. // power_shutdown.go — 延时关机(工具页)
  4. //
  5. // 用户在工具页输入「多少分钟后关机」,调用 ShutdownDelay 下发系统关机倒计时。
  6. // 复用 pccontrol.go 的系统命令构造(shutdown /s /t N /f),交给 Windows 系统计时,
  7. // 即使本程序退出,倒计时仍由系统执行。返回计划执行时间供前端展示。
  8. // ============================================================================
  9. package main
  10. import (
  11. "fmt"
  12. "strings"
  13. "time"
  14. )
  15. // ShutdownDelay 在指定分钟后执行关机。
  16. // minutes 必须 > 0;返回计划执行时间(HH:MM:SS)与提示信息。
  17. func (s *SettingsService) ShutdownDelay(minutes int) (string, error) {
  18. if minutes <= 0 {
  19. return "", fmt.Errorf("关机分钟数必须大于 0")
  20. }
  21. if minutes > 24*60 {
  22. return "", fmt.Errorf("关机分钟数过大(最多 24 小时)")
  23. }
  24. delaySec := minutes * 60
  25. cmd := pcSystemCommand(pcPoweroff, delaySec)
  26. if cmd == nil {
  27. return "", fmt.Errorf("当前系统不支持 shutdown 命令")
  28. }
  29. // 标记为待执行(与 API 路由共用状态,便于「主机」页取消)
  30. pcMu.Lock()
  31. if pcPending {
  32. remaining := time.Until(pcExecuteAt).Round(time.Second)
  33. pcMu.Unlock()
  34. return "", fmt.Errorf("已有待执行的电源命令,剩余 %v", remaining)
  35. }
  36. pcPending = true
  37. pcPendingCmd = pcPoweroff
  38. pcExecuteAt = time.Now().Add(time.Duration(delaySec) * time.Second)
  39. pcSystemTimer = true
  40. pcLastCmd = pcPoweroff
  41. pcLastTime = time.Now()
  42. pcRequested = true
  43. pcMu.Unlock()
  44. if output, err := cmd.CombinedOutput(); err != nil {
  45. pcMu.Lock()
  46. pcPending = false
  47. pcPendingCmd = ""
  48. pcExecuteAt = time.Time{}
  49. pcSystemTimer = false
  50. pcMu.Unlock()
  51. return "", fmt.Errorf("关机命令下发失败: %v | %s", err, strings.TrimSpace(string(output)))
  52. }
  53. execAt := pcExecuteAt.Format("15:04:05")
  54. fmt.Fprintf(apiLogRing, "[SYS] 工具页延时关机 | %d 分钟后(%s)\n", minutes, execAt)
  55. return execAt, nil
  56. }
  57. // PoweroffNow 立即关闭计算机(托盘菜单「关机」确认后调用)。
  58. // 与工具页延时关机共用 pccontrol 的 pending 状态,避免与倒计时冲突。
  59. func (s *SettingsService) PoweroffNow() error {
  60. cmd := pcSystemCommand(pcPoweroff, 0)
  61. if cmd == nil {
  62. return fmt.Errorf("当前系统不支持 shutdown 命令")
  63. }
  64. pcMu.Lock()
  65. if pcPending {
  66. remaining := time.Until(pcExecuteAt).Round(time.Second)
  67. pcMu.Unlock()
  68. return fmt.Errorf("已有待执行的电源命令,剩余 %v", remaining)
  69. }
  70. // 立即关机无需系统计时,但记录状态供「主机」页展示与取消(取消仅影响系统倒计时,这里直接执行)
  71. pcPending = true
  72. pcPendingCmd = pcPoweroff
  73. pcExecuteAt = time.Now()
  74. pcSystemTimer = false
  75. pcLastCmd = pcPoweroff
  76. pcLastTime = time.Now()
  77. pcRequested = true
  78. pcMu.Unlock()
  79. if output, err := cmd.CombinedOutput(); err != nil {
  80. pcMu.Lock()
  81. pcPending = false
  82. pcPendingCmd = ""
  83. pcExecuteAt = time.Time{}
  84. pcMu.Unlock()
  85. return fmt.Errorf("关机命令下发失败: %v | %s", err, strings.TrimSpace(string(output)))
  86. }
  87. fmt.Fprintln(apiLogRing, "[SYS] 托盘菜单立即关机")
  88. return nil
  89. }
  90. // SleepNow 立即休眠(一键功能按钮调用)。
  91. // 复用 pccontrol 的立即执行路径(rundll32 SetSuspendState),异步执行避免阻塞绑定调用。
  92. func (s *SettingsService) SleepNow() (string, error) {
  93. pcMu.Lock()
  94. pcLastCmd = pcSleep
  95. pcLastTime = time.Now()
  96. pcRequested = true
  97. pcMu.Unlock()
  98. go pcExecuteNow(pcSleep)
  99. return "已触发休眠", nil
  100. }
  101. // ToggleDesktopIcons 隐藏/显示桌面图标(一键功能按钮调用)。
  102. // hide=true 隐藏,hide=false 显示;返回操作结果描述。
  103. func (s *SettingsService) ToggleDesktopIcons(hide bool) (string, error) {
  104. return setDesktopIconsVisible(hide)
  105. }
  106. // ToggleDesktopIconOnly 只隐藏/显示桌面图标(壁纸保留;一键功能「隐藏图标」按钮调用)。
  107. // hide=true 只隐藏图标(壁纸不动),hide=false 恢复显示;返回操作结果描述。
  108. func (s *SettingsService) ToggleDesktopIconOnly(hide bool) (string, error) {
  109. return setDesktopIconOnlyVisible(hide)
  110. }
  111. // CancelShutdown 取消延时关机(工具页「取消」按钮调用)。
  112. func (s *SettingsService) CancelShutdown() error {
  113. pcMu.Lock()
  114. if !pcPending || pcPendingCmd != pcPoweroff {
  115. pcMu.Unlock()
  116. return fmt.Errorf("当前没有待执行的关机命令")
  117. }
  118. pcPending = false
  119. pcPendingCmd = ""
  120. pcExecuteAt = time.Time{}
  121. pcSystemTimer = false
  122. pcMu.Unlock()
  123. pcAbortShutdown()
  124. fmt.Fprintln(apiLogRing, "[SYS] 工具页取消延时关机")
  125. return nil
  126. }