-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
174 lines (148 loc) · 2.9 KB
/
Copy pathmain.go
File metadata and controls
174 lines (148 loc) · 2.9 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"time"
"runtime"
"gopkg.in/yaml.v3"
)
type HM_t struct {
Hours int `yaml:"hours"`
Minutes int `yaml:"minutes"`
}
type Setting_t struct {
Rules []struct {
If struct {
Weeks []string `yaml:"weeks"`
} `yaml:"if"`
Apply struct {
Allow_times []struct {
Start HM_t `yaml:"start"`
End HM_t `yaml:"end"`
} `yaml:"allowtimes"`
} `yaml:"apply"`
} `yaml:"rules"`
}
var start_time1 int
var end_time1 int
var start_time2 int
var end_time2 int
var setting Setting_t
var weeks_map = map[string]time.Weekday{
"Mon": time.Monday,
"Tue": time.Tuesday,
"Wed": time.Wednesday,
"Thu": time.Thursday,
"Fri": time.Friday,
"Sat": time.Saturday,
"Sun": time.Sunday,
}
func get_HOME_path()string{
switch runtime.GOOS {
case "windows":
home := os.Getenv("USERPROFILE")
if home != "" {
return home
}
return os.Getenv("HOME")
default:
// Linux,macOS
home := os.Getenv("HOME")
if home != "" {
return home
}
return os.Getenv("USERPROFILE")
}
}
func generate_setting_path1()string{
home_path:=get_HOME_path()
return filepath.Join(home_path, ".Do-not-use-PC.yaml")
}
func generate_setting_path2()string{
RunPath, err := os.Executable()
if err != nil {
panic(err)
}
RunDirPath := filepath.Dir(RunPath)
return filepath.Join(RunDirPath, "setting.yaml")
}
func poweroff(){
switch runtime.GOOS {
case "windows":
err:=exec.Command("shutdown", "/s", "/f").Run()
if err!=nil{
panic(err)
}
case "linux":
err:=exec.Command("systemctl", "poweroff").Run()
if err!=nil{
panic(err)
}
default:
panic("Not supporting OS")
}
}
func load_setting() {
path1:=generate_setting_path1()
path2:=generate_setting_path2()
path:=""
_, err := os.Stat(path1)
if err == nil {
path=path1
}else{
_, err := os.Stat(path2)
if err == nil {
path=path2
}else{
panic("Noting setting file.")
}
}
setting_str, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalln(err)
}
err = yaml.Unmarshal([]byte(setting_str), &setting)
if err != nil {
log.Fatalf("error: %v", err)
}
}
func main() {
shutdown_running := false
start_time1 = 60*21 + 0
end_time1 = 60*6 + 0
start_time2 = 60*22 + 45
end_time2 = 60*6 + 0
load_setting()
for {
now := time.Now()
now_temp := 60*now.Hour() + now.Minute()
if !shutdown_running {
(func() {
for _, e := range setting.Rules {
dofunc := func() bool {
flag := true
for _, e2 := range e.Apply.Allow_times {
if 60*e2.Start.Hours+e2.Start.Minutes <= now_temp && now_temp <= 60*e2.End.Hours+e2.End.Minutes {
flag = false
}
}
return flag
}
for _, e2 := range e.If.Weeks {
if e2 == "All" || now.Weekday() == weeks_map[e2] {
if dofunc() {
poweroff()
shutdown_running = true
return
}
}
}
}
})()
}
time.Sleep(time.Millisecond * 500)
}
}