refactor code

much better handling of commands, but still needs some improvement
This commit is contained in:
David Daily 2021-01-20 08:18:23 -06:00
parent c11053c3c3
commit 2fcdf6fd3b

133
flag.ahk
View File

@ -1,28 +1,13 @@
; AutoHotkey.exe C:\Users\Leand.000\Documents\D3K\flag.ahk [Script Parameters]
#SingleInstance force ; Only one instance at a time #SingleInstance force ; Only one instance at a time
FileEncoding, UTF-8 ; Makes sure special characters dont break stuff FileEncoding, UTF-8 ; Makes sure special characters dont break stuff
global whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
global userID := "<user ID from the control software>" global userID := "user ID from control software"
func := A_Args[1] If (A_Args.Length() < 3)
col := A_Args[2]
Switch A_Args[3]
{ {
Case "hold": If (A_Args[1] = "off")
length := 10000
Case A_Args[3] is number:
length := A_Args[3]
Default:
length := 0
}
If (A_Args.Length() < 2)
{
If (func = "off")
{ {
off() off()
} Else { } Else {
@ -31,19 +16,30 @@ If (A_Args.Length() < 2)
ExitApp ExitApp
} }
Switch func Switch A_Args[3]
{
Case A_Args[3] is number:
length := A_Args[3]
Case "hold":
length := 100000
Default:
length := 0
}
Switch A_Args[1]
{ {
Case "s": Case "s":
solid(col, length) solid(A_Args[2], length)
Case "b": Case "b":
blink(col, length) blink(A_Args[2], length)
Case "p": Case "p":
patt(col, length) patt(A_Args[2], length)
Default: Default:
MsgBox,, Flag, % "Incorrect parameters passed to script: " func ", " col ", " length MsgBox,, Flag, % "Incorrect parameters passed to script: " A_Args[1] ", " A_Args[2] ", " length
} }
solid(input, length := 0)
solid(input, length)
{ {
Switch input Switch input
{ {
@ -69,7 +65,7 @@ solid(input, length := 0)
} }
) )
} }
req("https://api.luxafor.com/webhook/v1/actions/solid_color", json_str, length) translate("https://api.luxafor.com/webhook/v1/actions/solid_color", json_str, length)
} }
blink(input, length) blink(input, length)
@ -77,69 +73,81 @@ blink(input, length)
Switch input Switch input
{ {
Case "red", "green", "yellow", "blue", "white", "cyan", "magenta": Case "red", "green", "yellow", "blue", "white", "cyan", "magenta":
color := input
Default:
MsgBox % input " is not an accepted input"
ExitApp
}
json_str = json_str =
( (
{ {
"userId": "%userID%", "userId": "%userID%",
"actionFields":{ "actionFields":{
"color": "%input%" "color": "%color%"
} }
} }
) )
req("https://api.luxafor.com/webhook/v1/actions/blink", json_str, length) translate("https://api.luxafor.com/webhook/v1/actions/blink", json_str, length)
Default:
MsgBox % input " is not an accepted input"
}
} }
patt(input, length) patt(input, length)
{ {
Switch input Switch input
{ {
Case "police", "traffic_lights", "random1", "random2", "random3", "random4", "random5": Case "police", "rainbow", "sea", "synthetic":
pattern := input
Case "traffic_lights":
pattern := "traffic lights"
Case "white_wave":
pattern := "white wave"
Case "random":
Random, rando, 1, 5
pattern := "random %rando%"
Case "random1":
pattern := "random 1"
Case "random2":
pattern := "random 2"
Case "random3":
pattern := "random 3"
Case "random4":
pattern := "random 4"
Case "random5":
pattern := "random 5"
Default:
MsgBox % input " is not an accepted input"
ExitApp
}
json_str = json_str =
( (
{ {
"userId": "%userID%", "userId": "%userID%",
"actionFields":{ "actionFields":{
"pattern": "%input%" "pattern": "%pattern%"
} }
} }
) )
req("https://api.luxafor.com/webhook/v1/actions/pattern", json_str, length) translate("https://api.luxafor.com/webhook/v1/actions/pattern", json_str, length)
Default:
MsgBox % input " is not an accepted input"
}
} }
req(url, data, len := 0) translate(url, data, length)
{ {
If (InStr(url, "solid")) If (length = 0)
{ {
If (len = 0) req(url, data)
{
whr.Open("POST", url, false)
whr.SetRequestHeader("Content-Type", "application/json")
whr.Send(data)
} Else {
whr.Open("POST", url, false)
whr.SetRequestHeader("Content-Type", "application/json")
whr.Send(data)
Sleep, (len * 1000)
off()
}
} Else { } Else {
start_time := A_TickCount start_time := A_TickCount
time_to_run := len * 1000 time_to_run := length * 1000
end_time := start_time + time_to_run end_time := start_time + time_to_run
while (A_tickcount < end_time) { while (A_tickcount < end_time) {
whr.Open("POST", url, false) req(url, data)
whr.SetRequestHeader("Content-Type", "application/json")
whr.Send(data)
whr.WaitForResponse()
} }
} }
If (InStr(url, "solid"))
{
Sleep, (length * 1000)
off()
}
} }
off() off()
@ -154,9 +162,18 @@ off()
} }
} }
) )
whr.Open("POST", "https://api.luxafor.com/webhook/v1/actions/solid_color", false) url := "https://api.luxafor.com/webhook/v1/actions/solid_color"
whr.SetRequestHeader("Content-Type", "application/json") req(url, data)
whr.Send(data)
} }
req(url, data)
{
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("POST", url, false)
whr.SetRequestHeader("Content-Type", "application/json")
whr.Send(data)
whr.WaitForResponse()
}
ExitApp ExitApp