-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.ahk
274 lines (270 loc) · 8.31 KB
/
events.ahk
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
; 用于进行对外通讯,只读本地文件 和发送数据
#include common.ahk
#Include "lib/JSON.ahk"
; 如果无需记录,那么将关闭界面设置功能
if(needRecordKey=1){
Init() ; 判断后端接口,启动相关程序
; 启动可能需要花费一些时间,CheckServer进行重复判断
CheckServer()
}
; 设置检测间隔,单位为毫秒
SetTimer IniMonitor,2000
IniMonitor(){
global lastModified
if not FileExist(IniFile){
return
}
modified := FileGetTime(IniFile)
if (modified != lastModified)
{
; 如果端口发生了变化则需要完全重启
newPort :=IniRead(IniFile,"common","serverPort",9900 )
newRemoteType :=IniRead(IniFile,"common","remoteType",1 )
newShowHttpDebug :=IniRead(IniFile,"common","showHttpDebug",0 )
if newPort != serverPort || newRemoteType != remoteType || newShowHttpDebug != showHttpDebug
{
ExitServer()
Sleep(200) ; 最好等待一会儿
}
; 当文件发生变化后,需要重新载入
Reload()
}
}
; 网络相关函数代码
Init(){
try{
global reqXMLHTTP := ComObject("Msxml2.XMLHTTP") ; Msxml2.XMLHTTP Microsoft.XMLHTTP
}
catch
{
MsgBox msgXML msgNotLaunch
}
}
; 发送数据
StartHttp(task,route,data,timeout:=8000)
{
if reqXMLHTTP = 0
return
; 如果正在运行则退出,同一时间只会有一个
if HttpCtrlObj['state'] = 'wait'
{
; OutputDebug ("AutoHotkey :skip " task)
return 0
}
HttpCtrlObj['task'] := task ; 任务名
HttpCtrlObj['tick'] := A_TickCount ; 任务tick
HttpCtrlObj['timeout'] := timeout ; 任务超时,一般不用设置
sendData(route,data)
; 检测数据变化情况
SetTimer ServerCore, 10
return 1
}
; 是否曾经启动或Cmd
CheckServer(){
; 如果不能连接上则需要启动Node服务
global CheckServerCount
if CheckServerCount < CheckServerMax
{
ShowTxt(msgTry (CheckServerCount+1) msgTimes )
StartHttp('connect','/version','')
}else{
MsgBox msgTry CheckServerCount msgTimes msgNotLaunch
}
CheckServerCount +=1
Sleep 3000
}
; 服务核心处理
ServerCore()
{
state := HttpCtrlObj['state'] ; 此值会随时变化,先记录下
if( (A_TickCount - HttpCtrlObj['tick']) > HttpCtrlObj['timeout'])
{
; 超时,需要取消定时
SetTimer ServerCore, 0
; 此时可以尝试启动服务
ShowTxt 'Task[' HttpCtrlObj['task'] ']timeout:' HttpCtrlObj['timeout']
return
}
if(state ='wait')
{
return
}
; 连接完成,取消定时
SetTimer ServerCore, 0
if HttpCtrlObj['task']='connect' {
if state ='succ' && InStr(HttpCtrlObj['resp'],'showKeyBoardServer Version:') > 0
{
; 成功启动后端服务
global serverState := 1
ShowTxt msgLaunchSucc
; 准备发送一些数据给后端
SendPCInfo(0)
}else{
; 需要重试,超过N次后判断失败
if serverState = -1{
startServer() ; 启动服务
}
CheckServer()
}
}else{
; 常规数据发送处理
global countOfConnectFail
if state !='succ'{
ShowTxt HttpCtrlObj['task'] ',connect fail!' ; 可能后端服务被关闭了
OutputDebug 'connect fail'
countOfConnectFail += 1
if(countOfConnectFail >= maxCountOfConnectFail){
MsgBox msgTry countOfConnectFail msgTimes msgConnectFail
countOfConnectFail := 0 ; 成功一次则复位
}
}Else{
countOfConnectFail := 0 ; 成功一次则复位
}
}
}
; 根据进程id获取命令行
GetCommand(procId)
{
cmdLine := ""
try{
wmi := ComObjGet("winmgmts:")
queryEnum := wmi.ExecQuery(""
. "Select * from Win32_Process where ProcessId=" . procId)
._NewEnum()
; Get first matching process.
if queryEnum(&proc)
cmdLine := proc.CommandLine
}
return cmdLine
}
; 启动服务
startServer()
{
cmdExe := serverExecName ' ' ; Node命令带空格支持后面的参数
cmd := cmdExe '"' httpPath 'server.js"' ; 完整命令行
global serverState :=0 ; 假设启动失败
; 读取 httpPath+ kbserver.pid 进程文件检查是否是node的
; 需要启动服务,要防止路径上有空格
pidFile := httpPath 'kbserver.pid'
if FileExist(pidFile){
strPid := FileRead(pidFile, "`n UTF-8")
iPid := ProcessExist(strPid)
if iPid>0{
try {
path := ProcessGetPath(iPid)
SplitPath Path ,&OutFileName, &OutDir
if OutFileName = trim(cmdExe) {
cmdLine := GetCommand(iPid) ; 尝试获取命令行根据命令行参数排挤
if cmdLine !="" {
canKill := (cmdLine = cmd)
}else{ ; 没有成功获取命令行则直接用node的路径判断
canKill := ((OutDir '\') = httpPath)
}
if canKill {
ProcessClose(iPid) ;如果是 自己的进程则杀进程
}
}
}
FileDelete pidFile ; 删除进程文件
}
}
try {
; Run cmd ,,'Hide' Show
ShowFlag := 'Hide'
if showHttpDebug {
ShowFlag := 'Show'
}
Run cmd,httpPath,ShowFlag, &OutputVarPID
Sleep 1000 ;启动服务需要等待
}catch{
MsgBox msgLaunch "`n" cmd
}
}
; 自动发送 AllKeyRecord 数据
AutoSendData()
{
if serverState = 1 {
StartHttp('data','/data',AllKeyRecord,timeout:=8000)
}
}
; 发送退出数据
ExitServer()
{
if serverState = 1 {
StartHttp('exit','/exit','')
}
}
; 发送数据给后端服务,flag=0 表示第一次发送数据
SendPCInfo(flag)
{
; 获取不同屏幕
MCount := MonitorGetCount()
JSONStr := '{"screen":['
loop MCount {
if A_Index != 1{
JSONStr := JSONStr ','
}
try {
MonitorGet(A_Index, &Left, &Top, &Right, &Bottom)
global minLeft := Min(minLeft,Left)
global minTop := Min(minTop,Top)
global maxRight := Max(maxRight,Right)
global maxBottom := Max(maxBottom,Bottom)
tmp := '{"Left":' Left ',"Top":' Top ',"Right":' Right ',"Bottom":' Bottom '}'
JSONStr := JSONStr tmp
}catch{
; 出现异常无需发送数据
return
}
}
JSONStr := JSONStr '],"flag":' flag '}'
if serverState = 1 {
StartHttp('sendPCInfo','/sendPCInfo',JSONStr,timeout:=8000)
}
}
; 数据回调和核心发送
preReadyState := 0 ; http前一个状态
Ready() {
if reqXMLHTTP = 0 {
return
}
; OutputDebug 'readyState : ' reqXMLHTTP.readyState
if (reqXMLHTTP.readyState !=4 ) ; Not done yet.
{
; HttpCtrlObj['state'] := 'wait'
global preReadyState := reqXMLHTTP.readyState ; 获取前一个状态
return
}
; OutputDebug 'state : ' reqXMLHTTP.status ' preReadyState:' preReadyState
if (reqXMLHTTP.status == 200 && preReadyState == 3) ; 不知道为何 status == 200 不能判断,所以需要增加一个前一个状态为3的判断
{ ; OK.
HttpCtrlObj['state'] := 'succ'
; 可能出现 Error: (0x8000000A) 完成该操作所需的数据还不可使用。屏蔽掉
try{
HttpCtrlObj['resp'] := reqXMLHTTP.responseText
}
}
else{
HttpCtrlObj['state'] := 'error'
}
;MsgBox "Status " reqXMLHTTP.status,, 16
}
sendData(route,data:=''){
HttpCtrlObj['state'] := 'wait'
reqXMLHTTP.open('POST', serverUrl route , true)
reqXMLHTTP.setRequestHeader("Content-Type", "application/json")
; reqXMLHTTP.setRequestHeader("Cache-Control", "no-cache")
; Set our callback function.application/json text/plain"
reqXMLHTTP.onreadystatechange := Ready
; Send the reqXMLHTTPuest. Ready() will be called when it's complete.
;data["time"] := A_Now
str := ''
if(data!=''){
if( Type(data) = 'Map'){
str := JSON.stringify(data)
}else{
str := data ; 普通字符串
}
}
reqXMLHTTP.send( str )
}