-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdriver.lua
192 lines (161 loc) · 4.56 KB
/
driver.lua
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
require "json"
JSON=(loadstring(json.JSON_LIBRARY_CHUNK))()
function GetState()
getPlugState()
UpdateState()
end
function SetState()
UpdateState()
end
function UpdateState()
C4:SendToProxy(5001, "ICON_CHANGED", { icon = strState, icon_description = strState })
C4:UpdateProperty("Current State", strState)
end
function ReceivedFromProxy(idBinding, strCommand, tParams)
if strCommand == "SELECT" then
if strState == "On" then
turnPlugOff()
else
if strState == "Off" then
turnPlugOn()
end
end
end
end
function OnPropertyChanged(strProperty)
strIP = Properties["IP Address"]
end
function OnDriverInit()
strState = "Off"
strIP = ""
for k,v in pairs(Properties) do
OnPropertyChanged(k)
end
GetState()
C4:UpdateProperty ('Driver Version', C4:GetDriverConfigInfo ("version"))
C4:SetTimer(5000, function(timer, skips) getPlugState() end, true)
end
-- Turn the plug on
function turnPlugOn()
strCommand = '{"system":{"set_relay_state":{"state":1}}}'
sendData(strCommand)
end
-- Turn the plug off
function turnPlugOff()
strCommand = '{"system":{"set_relay_state":{"state":0}}}'
sendData(strCommand)
end
-- Get the Plug status
function getPlugState()
strCommand = '{"system":{"get_sysinfo":{}}}'
szData = tpEndcodeData(strCommand)
sendOverSocket(szData, 3000, function(info, err)
if (info ~= nil) then
strResult = tpDecodeData(info)
jsonData = JSON:decode(strResult)
iState = jsonData['system']['get_sysinfo']['relay_state']
if (iState == 1) then
strState = "On"
else
strState = "Off"
end
UpdateState()
else
strState = "Error"
UpdateState()
end
end)
end
-- Send the encoded data to the plug and get a response, or an error
function sendData(strCommand)
szData = tpEndcodeData(strCommand)
sendOverSocket(szData, 3000, function(info, err)
if (info ~= nil) then
strResult = tpDecodeData(info)
jsonData = JSON:decode(strResult)
getPlugState()
else
strState = "Error"
UpdateState()
end
end)
end
-- Send data over a socket with a timeout
function sendOverSocket(szData, timeout, done)
local timer
local completed = false
local complete = function(data, errMsg)
if (not completed) then
completed = true
if (timer ~= nil) then
timer:Cancel()
end
done(data, errMsg)
end
end
local cli = C4:CreateTCPClient()
:OnConnect(function(client)
local remote = client:GetRemoteAddress()
client:Write(szData):ReadUpTo(1024)
end)
:OnDisconnect(function(client, errCode, errMsg)
if (errCode ~= 0) then
complete(nil, "Disconnected with error " .. errCode .. ": " .. errMsg)
else
complete(nil, "Disconnected and no response received")
end
end)
:OnRead(function(client, data)
client:Close()
complete(data)
end)
:OnError(function(client, errCode, errMsg)
strState = "Error"
complete(nil, "Error " .. errCode .. ": " .. errMsg)
end)
:Connect(strIP, 9999)
if (timeout > 0) then
timer = C4:SetTimer(timeout, function()
cli:Close()
complete(nil, "Timed out!")
end)
end
end
-- Encode the data for the plug using a autokey cipher
-- Use an inital vector of -85 (171)
function tpEndcodeData(strData)
local key = 171
result = "\0\0\0\0"
for i = 1, #strData do
local ord = string.byte( strData, i )
local val = bit.bxor(key, ord)
key = val
result = result .. string.char(val)
end
return result
end
-- Decode the data from the plug using a autokey cipher
-- Use an inital vector of -85 (171)
function tpDecodeData(szData)
local key = 171
result = ""
for i = 5, #szData do
local char = string.byte( szData, i )
val = bit.bxor(char,key)
key = char
result = result .. string.char(val)
end
return result
end
-- Helper function to convert string to hex
function string.fromhex(str)
return (str:gsub('..', function (cc)
return string.char(tonumber(cc, 16))
end))
end
-- Helper function to convert hex to string
function string.tohex(str)
return (str:gsub('.', function (c)
return string.format('%02X', string.byte(c))
end))
end