-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeviceInfoPlugin.brs
More file actions
269 lines (199 loc) · 7.5 KB
/
deviceInfoPlugin.brs
File metadata and controls
269 lines (199 loc) · 7.5 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
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
REM
REM @title Device Information Uploader
REM @author Sabin Maharjan
REM @company Port Of Portland
REM @date-created 04/21/2017
REM @date-last-modified 05/05/2017
REM
REM @description Uploads Device Information Periodically Given the User Variable time value
REM
Function deviceInfoPlugin_Initialize(msgPort As Object, userVariables As Object, bsp as Object)
deviceInfoPlugin = {}
deviceInfoPlugin.msgPort = msgPort
deviceInfoPlugin.userVariables = userVariables
deviceInfoPlugin.bsp = bsp
deviceInfoPlugin.ProcessEvent = deviceInfoPlugin_ProcessEvent
deviceInfoPlugin.timer = CreateObject("roTimer")
deviceInfoPlugin.tokenTimer = CreateObject("roTimer")
deviceInfoPlugin.reg = CreateObject("roRegistrySection", "networking")
deviceInfoPlugin.uploadTimerInSeconds = 60
deviceInfoPlugin.tokenExpire = 1
deviceInfoPlugin.token = "bearer "
'----- Get user Variable for debug (if any)
if userVariables["Enable_Telnet"] <> invalid
enable$ = userVariables["Enable_Telnet"].currentValue$
if LCase(enable$) = "yes"
deviceInfoPlugin.reg.write("telnet", "23")
print "@deviceInfoPlugin TELNET Enabled."
else
deviceInfoPlugin.reg.delete("telnet", "23")
print "@deviceInfoPlugin TELNET Disabled."
end if
end if
'----- Get user Variable for uplaod Time (if any)
if userVariables["DeviceInfo_Upload_Timer_Value"] <> invalid
userVarelapsedTimeInSeconds$ = userVariables["DeviceInfo_Upload_Timer_Value"].currentValue$
deviceInfoPlugin.uploadTimerInSeconds = userVarelapsedTimeInSeconds$.toint()
print "@deviceInfoPlugin Upload Timer Set To "; deviceInfoPlugin.uploadTimerInSeconds; " Seconds"
end if
'----- Get Token
deviceInfoPlugin.tokenTimer.SetPort(deviceInfoPlugin.msgPort)
deviceInfoPlugin.tokenTimer.SetUserData("GET_TOKEN")
deviceInfoPlugin.tokenTimer.SetElapsed(deviceInfoPlugin.tokenExpire , 0)
deviceInfoPlugin.tokenTimer.Start()
'----- Create Message Port and Set Timer
deviceInfoPlugin.timer.SetPort(deviceInfoPlugin.msgPort)
deviceInfoPlugin.timer.SetUserData("SEND_DEVICEINFO")
deviceInfoPlugin.timer.SetElapsed(deviceInfoPlugin.uploadTimerInSeconds, 0)
deviceInfoPlugin.timer.Start()
return deviceInfoPlugin
End Function
Function deviceInfoPlugin_ProcessEvent(event as Object)
retval = false
if type(event) = "roTimerEvent" then
if event.GetUserData() <> invalid then
if event.GetUserData() = "GET_TOKEN" then
print "@deviceInfoPlugin Getting Token..."
GetToken(m)
retval = true
end if
if event.GetUserData() = "SEND_DEVICEINFO" then
print "@deviceInfoPlugin Sending Device Info..."
success = SendDeviceInfo(m)
retval = success
end if
end if
end if
m.timer.Start()
m.tokenTimer.Start()
return retval
End Function
Function newDeviceInfo(userVariables As Object)
player = CreateObject("roDeviceInfo")
registrySection = CreateObject("roRegistrySection", "networking")
net = CreateObject("roNetworkConfiguration", 1)
deviceInfo = {}
deviceInfo.UniqueId = player.GetDeviceUniqueId()
deviceInfo.Model = player.GetModel()
deviceInfo.UpTime = player.GetDeviceUptime()
deviceInfo.Firmware = player.GetVersion()
deviceInfo.BootVersion = player.GetBootVersion()
deviceInfo.UnitName = registrySection.Read("un")
deviceInfo.Ip = net.GetCurrentConfig().ip4_address
deviceInfo.Link = net.GetCurrentConfig().link
deviceInfo.Channel = ""
if (userVariables.Channel <> invalid) then
deviceInfo.Channel = userVariables.Channel.currentValue$
end if
return deviceInfo
End Function
Function GetToken(h as Object)
tokenUrl=""
username = ""
password = ""
if h.userVariables["token_url"]<>invalid
tokenUrl = h.userVariables["token_url"].currentValue$
end if
if h.userVariables["token_user"]<>invalid
username = h.userVariables["token_user"].currentValue$
end if
if h.userVariables["token_password"]<>invalid
password = h.userVariables["token_password"].currentValue$
end if
xfer = CreateObject("roUrlTransfer")
msgPort = CreateObject("roMessagePort")
xfer.SetPort(msgPort)
xfer.SetUserData("TOKEN_REQUESTED")
xfer.SetURL(tokenUrl)
xfer.AddHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
aa = {}
aa.method = "POST"
aa.request_body_string = "grant_type=password&username=" + username + "&password=" + password
aa.response_body_string = true
ok = xfer.AsyncMethod(aa)
gotResult = false
reason = "Unknown"
responseCode = 0
responseBody = ""
if ok then
while gotResult = false
msg = wait(0, msgPort)
if type(msg) = "roUrlEvent" then
if msg.GetUserData() = "TOKEN_REQUESTED"
gotResult = true
reason = msg.GetFailureReason()
responseCode = msg.GetResponseCode()
responseBody = msg
end if
end if
end while
end if
if responseCode = 200 then
print "@deviceInfoPlugin Token Granted Successfully"
jsonObj = ParseJson(responseBody)
h.tokenExpire = jsonObj.expires_in
h.tokenTimer.SetElapsed(jsonObj.expires_in, 0)
h.token = jsonObj.token_type + " " + jsonObj.access_token
else
print "@deviceInfoPlugin Token Not Granted! Response : "; reason
end if
End Function
Function SendDeviceInfo(h as Object) as Object
retval = false
info = CreateObject("roAssociativeArray")
deviceinfo = newDeviceInfo(h.userVariables)
info.AddReplace("SerialNumber", deviceinfo.UniqueId)
info.AddReplace("Model", deviceinfo.Model)
info.AddReplace("UpTime", deviceinfo.UpTime)
info.AddReplace("Firmware", deviceinfo.Firmware)
info.AddReplace("BootVersion", deviceinfo.BootVersion)
info.AddReplace("Name", deviceinfo.UnitName)
info.AddReplace("Ip", deviceinfo.Ip)
info.AddReplace("Link", deviceinfo.Link)
info.AddReplace("Channel", deviceinfo.Channel)
DeviceInfo_url=""
if h.userVariables["DeviceInfo_url"]<>invalid
DeviceInfo_url = h.userVariables["DeviceInfo_url"].currentValue$
end if
if DeviceInfo_url <> ""
print "@deviceInfoPlugin POST Url :"; DeviceInfo_url
print "@deviceInfoPlugin POST-ING Device Info..."
xfer = CreateObject("roUrlTransfer")
msgPort = CreateObject("roMessagePort")
xfer.SetPort(msgPort)
xfer.SetUserData("DEVICEINFO_UPLOADED")
xfer.SetURL(DeviceInfo_url)
xfer.AddHeader("Content-Type", "application/json")
xfer.AddHeader("Authorization", h.token)
dataInfo = FormatJson(info)
print dataInfo
ok = xfer.AsyncPostFromString(dataInfo)
if ok = false then
return false
end if
gotResult = false
reason = "Unknown"
responseCode = 0
while gotResult = false
msg = wait(0, msgPort)
if type(msg) = "roUrlEvent" then
if msg.GetUserData() = "DEVICEINFO_UPLOADED"
gotResult = true
reason = msg.GetFailureReason()
responseCode = msg.GetResponseCode()
end if
end if
end while
print "@deviceInfoPlugin Response Code: "; responseCode
if responseCode >= 200 OR responseCode <= 204 then
print "@deviceInfoPlugin Successfully POSTed Device Info!"
retval = true
else
print "@deviceInfoPlugin Cannot POST Device Info!"
print reason
endif
else
print "@deviceInfoPlugin No DeviceInfo_url user variable is defined."
endif
return retval
End Function