-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathruntime.lua
More file actions
106 lines (89 loc) · 2.56 KB
/
runtime.lua
File metadata and controls
106 lines (89 loc) · 2.56 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
-- Control aliases
Status = Controls.Status
-- Variables and flags
DebugTx=false
DebugRx=false
DebugFunction=false
DebugPrint=Properties["Debug Print"].Value
-- Sockets and services
-- Timers, tables, and constants
StatusState = { OK = 0, COMPROMISED = 1, FAULT = 2, NOTPRESENT = 3, MISSING = 4, INITIALIZING = 5 }
PollTimer = Timer.New()
PollTime = 3
-- Helper functions
-- A function to determine common print statement scenarios for troubleshooting
function SetupDebugPrint()
if DebugPrint=="Tx/Rx" then
DebugTx,DebugRx=true,true
elseif DebugPrint=="Tx" then
DebugTx=true
elseif DebugPrint=="Rx" then
DebugRx=true
elseif DebugPrint=="Function Calls" then
DebugFunction=true
elseif DebugPrint=="All" then
DebugTx,DebugRx,DebugFunction=true,true,true
end
end
-- Update the Status control
function ReportStatus(state,msg)
if DebugFunction then print("ReportStatus() Called") end
local msg=msg or ""
Status.Value=StatusState[state]
Status.String=msg
end
-- Send data to device
function Send(cmd)
if DebugFunction then print("Send() Called") end
if DebugTx then print("Tx: "..cmd) end
end
-- A function to clear controls/flags/variables and clears tables
function ClearVariables()
if DebugFunction then print("ClearVariables() Called") end
PollTimer:Stop()
end
-- A function to trigger other functions or set flags if disconnected
function Disconnected()
if DebugFunction then print("Disconnect() Called") end
ClearVariables()
end
-- A function to handle connection
function Connect()
if DebugFunction then print("Connect() Called") end
ClearVariables()
Device:Connect(IPAddress.String,Port.Value)
end
-- A function to trigger other functions once connected
function Connected()
if DebugFunction then print("Connected() Called") end
ReportStatus("OK")
GetDeviceInfo()
PollTimer:Start(PollTime)
end
-- Initial data grab from device
function GetDeviceInfo()
if DebugFunction then print("GetDeviceInfo() Called") end
end
-- Poll function for updates and state changes
function PollDevice()
if DebugFunction then print("PollDevice() Called") end
end
-- EventHandlers
PollTimer.EventHandler = PollDevice
-- Buffer Management
function ParseResponse()
if DebugFunction then print("ParseResponse() Called") end
-- Entire buffer is set to newrx here
rx = Device:Read(Device.BufferLength)
while rx do
if DebugRx then print("Rx: "..rx) end
end
end
-- Socket Management
-- Initialization Function
function Initialization()
if DebugFunction then print("Initialization() Called") end
SetupDebugPrint()
Connect()
end
Initialization()