forked from RitterHou/time-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
221 lines (200 loc) · 4.6 KB
/
common.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"bytes"
"encoding/base64"
"github.com/faiface/beep"
"github.com/faiface/beep/speaker"
"github.com/faiface/beep/wav"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
"io/ioutil"
"log"
"os"
"os/user"
"path"
"time"
)
var (
icon []byte
blackIcon []byte
current []byte
clock []byte
hour []byte
minute []byte
numbers = make([][]byte, 11)
app string
link string
rootDir = ""
logName = "TimeAlert.log"
)
func init() {
var base64ToByteArray = func(value string) []byte {
data, err := base64.StdEncoding.DecodeString(value)
if err != nil {
log.Fatalln(err)
}
return data
}
icon = base64ToByteArray(Icon)
blackIcon = base64ToByteArray(BlackIcon)
current = base64ToByteArray(CurrentBase64)
clock = base64ToByteArray(Clock)
hour = base64ToByteArray(HourBase64)
minute = base64ToByteArray(MinuteBase64)
numbers[0] = base64ToByteArray(Num0Base64)
numbers[1] = base64ToByteArray(Num1Base64)
numbers[2] = base64ToByteArray(Num2Base64)
numbers[3] = base64ToByteArray(Num3Base64)
numbers[4] = base64ToByteArray(Num4Base64)
numbers[5] = base64ToByteArray(Num5Base64)
numbers[6] = base64ToByteArray(Num6Base64)
numbers[7] = base64ToByteArray(Num7Base64)
numbers[8] = base64ToByteArray(Num8Base64)
numbers[9] = base64ToByteArray(Num9Base64)
numbers[10] = base64ToByteArray(Num10Base64)
app = os.Args[0] // 可执行文件的路径
userInfo, err := user.Current()
if nil != err {
log.Fatalln(err)
}
// 快捷方式的路径
link = path.Join(userInfo.HomeDir, LinkSuffix)
rootDir = path.Join(userInfo.HomeDir, ".TimeAlert")
if _, err := os.Stat(rootDir); os.IsNotExist(err) {
err = os.Mkdir(rootDir, os.ModeDir)
if err != nil {
log.Fatal(err)
}
}
}
// 把数字格式化为对应的声音
func format(num int) [][]byte {
if num <= 10 {
return [][]byte{numbers[num]}
} else if num < 20 {
return [][]byte{numbers[10], numbers[num%10]}
} else {
ten := num / 10
one := num % 10
if one == 0 {
return [][]byte{numbers[ten], numbers[10]}
} else {
return [][]byte{numbers[ten], numbers[10], numbers[one]}
}
}
}
// 播放声音
func play(data []byte) {
var err error
s, format, err := wav.Decode(ioutil.NopCloser(bytes.NewReader(data)))
if err != nil {
log.Fatalln(err)
}
err = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
if err != nil {
log.Fatalln(err)
}
playing := make(chan struct{})
speaker.Play(beep.Seq(s, beep.Callback(func() {
close(playing)
})))
<-playing // 阻塞直到声音播放结束
}
// int是否包含在slice中
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// 创建快捷方式
func createShortcut(source string, target string) error {
var err error
err = ole.CoInitializeEx(0, ole.COINIT_APARTMENTTHREADED|ole.COINIT_SPEED_OVER_MEMORY)
if err != nil {
return err
}
oleShellObject, err := oleutil.CreateObject("WScript.Shell")
if err != nil {
return err
}
defer oleShellObject.Release()
wShell, err := oleShellObject.QueryInterface(ole.IID_IDispatch)
if err != nil {
return err
}
defer wShell.Release()
cs, err := oleutil.CallMethod(wShell, "CreateShortcut", target)
if err != nil {
return err
}
iDispatch := cs.ToIDispatch()
_, err = oleutil.PutProperty(iDispatch, "TargetPath", source)
if err != nil {
return err
}
_, err = oleutil.CallMethod(iDispatch, "Save")
if err != nil {
return err
}
return nil
}
// 创建快捷方式
func makeShortcut() {
err := createShortcut(app, link)
if err != nil {
log.Fatalln(err)
}
}
// 删除快捷方式
func removeShortcut() {
err := os.Remove(link)
if err != nil {
log.Fatalln(err)
}
}
// 更新快捷方式
func updateShortcut() {
removeShortcut()
makeShortcut()
}
// 设置log的相关属性
func initLog() {
logFile, _ := os.OpenFile(path.Join(rootDir, logName), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
log.SetOutput(logFile)
log.SetFlags(log.LstdFlags | log.Lshortfile)
os.Stdout = logFile
os.Stderr = logFile
}
// 检查激活状态
func checkActive() bool {
inactiveFile := path.Join(rootDir, ".inactive")
if _, err := os.Stat(inactiveFile); os.IsNotExist(err) {
// 不存在,则激活
return true
} else {
return false
}
}
// 设置为未激活
func setInactive() {
inactiveFile := path.Join(rootDir, ".inactive")
f, err := os.OpenFile(inactiveFile, os.O_CREATE, 0644)
if err != nil {
log.Fatal(err)
}
err = f.Close()
if err != nil {
log.Fatal(err)
}
}
// 设置为激活
func setActive() {
inactiveFile := path.Join(rootDir, ".inactive")
err := os.Remove(inactiveFile)
if err != nil {
log.Fatal(err)
}
}