/
dailyAction.go
147 lines (108 loc) · 2.71 KB
/
dailyAction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// dailyTask
package oneday
import (
"fmt"
"log"
"runtime"
"os/exec"
"github.com/gen2brain/beeep"
//"github.com/go-toast/toast"
"github.com/jakecoffman/cron"
)
/*
*
1. 完整框架:https://github.com/ouqiang/gocron
2. 只有后端服务端的定时器 https://pkg.go.dev/github.com/robfig/cron
3. https://blog.csdn.net/sryan/article/details/50129133
4. 用法:https://github.com/eddycjy/go-gin-example/tree/master/service/article_service
*
*/
type DailyAction struct {
Name string
}
var JobStandUpFun = func() {
sysType := runtime.GOOS
if sysType == "windows" {
NotifyStadnUpWindos()
}
}
var JobSleepNow = func() {
ShutDownPc()
}
var JobPutDownComputerAndPhone = func() {
PutDownComputerAndPhone()
}
var SleepTrip = func() {
EverydaySleep()
}
func (da *DailyAction) StartCron() {
fmt.Println("start StartCron")
c := cron.New()
// job_stand_up := "0 25 * * * *" //25提醒站立一次
// c.AddFunc(job_stand_up, JobStandUpFun, "番茄时间")
// c.AddFunc("0 30 22 * * *", JobSleepNow, "如果自己控制不了,执行22:30 强制关机")
// c.AddFunc("0 0 20 * * *", JobPutDownComputerAndPhone, "20:00 远离手机和电脑")
c.AddFunc("0 30 21 * * *", SleepTrip, "20:00 远离手机和电脑")
c.Start()
}
/*
*
消息通知弹框
https://github.com/gen2brain/beeep
https://github.com/0xAX/notificator
https://github.com/go-toast/toast
https://github.com/ying32/govcl
https://zhuanlan.zhihu.com/p/404062419
*
*/
func NotifyStadnUp() {
err := beeep.Notify("Title", "Message body", "assets/information.png")
if err != nil {
panic(err)
}
}
func NotifyStadnUpWindos() {
/*
notification := toast.Notification{
AppID: "Microsoft.Windows.Shell.RunDialog",
Title: "站立提醒",
Message: "是的到了休息一下",
Icon: "d:\\golang_official_logo_icon_169092.png", // 文件必须存在
Actions: []toast.Action{
{"protocol", "马上去做", ""},
{"protocol", "延迟", ""},
},
}
err := notification.Push()
if err != nil {
log.Fatalln(err)
}*/
}
// ShutDownPc cmd
func ShutDownPc() {
if runtime.GOOS == "windows" {
fmt.Println("ShutDownPc")
//60*5
arg := []string{"-s", "-t", "30"}
cmd := exec.Command("shutdown", arg...)
d, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(string(d))
}
}
// 习惯:不拖堂 20点远离手机和电脑
// -s"正常关机、"-f"强制关机、"-r"重启、"-t"定时关机
// shutdown -h 休眠
func PutDownComputerAndPhone() {
if runtime.GOOS == "windows" {
fmt.Println("习惯:不拖堂 20点远离手机和电脑")
cmd := exec.Command("shutdown", "-h")
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
}
}