forked from randomcoffeesnob/decent-advanced-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.tcl
347 lines (298 loc) · 9.47 KB
/
plugin.tcl
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package require de1_machine 1.2
package require json
package require de1_profile 2.0
set plugin_name "advanced_rest_api"
namespace eval ::plugins::${plugin_name} {
variable author "Yannick Dietler"
variable contact "[email protected]"
variable version 1.1
variable description "API to control the DE1's power state and getting additional Information"
variable name "Advanced REST API"
# based on Johanna Schander's Web API
proc main {} {
package require wibble
# Create settings if non-existant
if {[array size ::plugins::advanced_rest_api::settings] == 0} {
array set ::plugins::advanced_rest_api::settings {
webserver_port 8888
webserver_authentication_key "myFancyAuthenticationKey"
}
save_plugin_settings advanced_rest_api
}
# Auth
proc ::wibble::check_auth {state} {
set auth [dict getnull $state request query auth]
set auth [lindex $auth 1]
if {$auth eq "" && $::plugins::advanced_rest_api::settings(webserver_authentication) == 1} {
return [unauthorized $state]
}
if {$auth != $::plugins::advanced_rest_api::settings(webserver_authentication_key) && $::plugins::advanced_rest_api::settings(webserver_authentication) == 1} {
return [unauthorized $state]
}
return true;
}
proc ::wibble::unauthorized {state} {
dict set response status 403
dict set state response header content-type "" {application/json charset utf-8}
dict set response content "{status: \"unauthorized\"}"
sendresponse $response
return false;
}
# Utilities
proc ::wibble::return_200_json {content} {
dict set response status 200
dict set state response header content-type "" {application/json charset utf-8}
dict set response content "$content"
sendresponse $response
}
# from https://wiki.tcl-lang.org/page/JSON
proc ::wibble::compile_json {spec data} {
while [llength $spec] {
set type [lindex $spec 0]
set spec [lrange $spec 1 end]
switch -- $type {
dict {
lappend spec * string
set json {}
foreach {key val} $data {
foreach {keymatch valtype} $spec {
if {[string match $keymatch $key]} {
lappend json [subst {"$key":[
::wibble::compile_json $valtype $val]}]
break
}
}
}
return "{[join $json ,]}"
}
list {
if {![llength $spec]} {
set spec string
} else {
set spec [lindex $spec 0]
}
set json {}
foreach {val} $data {
lappend json [::wibble::compile_json $spec $val]
}
return "\[[join $json ,]\]"
}
string {
if {[string is double -strict $data]} {
return $data
} else {
return "\"$data\""
}
}
default {error "Invalid type"}
}
}
}
# Index endpoint
proc ::wibble::indexpage {state} {
if { ![check_auth $state] } {
return;
}
set fp [open "[homedir]/[plugin_directory]/advanced_rest_api/index.html" r]
set file_data [read $fp]
close $fp
dict set state response status 200
dict set state response header content-type "" text/html
dict set state response content $file_data
sendresponse [dict get $state response]
}
# Profile endpoints
proc ::wibble::profile {state } {
if { ![check_auth $state] } {
return;
}
set method [dict get $state request method]
if {$method eq "POST"} {
set postdata [dict get $state request rawpost]
set localfilename "[clock seconds].tcl"
set path "[pwd]/profiles/$localfilename"
set fileId [open $path "w"]
puts -nonewline $fileId $postdata
close $fileId
::wibble::return_200_json "$localfilename written"
}
if {$method eq "GET"} {
set path [dict get $state request path]
set profile [lindex [split $path "/"] 3]
if {$profile != ""} {
set fd [open "[pwd]/profiles/$profile" r]
fconfigure $fd -translation binary
set content [read $fd]; close $fd
# ::wibble::return_200_json [::wibble::compile_json {dict} $content]
::wibble::return_200_json $content
} else {
::wibble::return_200_json [::wibble::compile_json {list} [glob -tails -directory [pwd]/profiles/ *.tcl]]
}
}
if {$method eq "PUT"} {
#change the profile to ?
}
}
#history
proc ::wibble::history {state} {
if { ![check_auth $state] } {
return;
}
set path [dict get $state request path]
set shot [lindex [split $path "/"] 3]
if {$shot != ""} {
set fd [open "[pwd]/history/$shot" r]
fconfigure $fd -translation binary
set content [read $fd]; close $fd
::wibble::return_200_json $content
} else {
set shotlist [glob -tails -directory [pwd]/history/ *.shot]
::wibble::return_200_json [::wibble::compile_json "{list}" $shotlist]
}
}
# based on https://github.com/Testsubject1683/de1-mirror/tree/webapi
proc ::wibble::status {} {
# depending on the current state, we supply different type of data
set return [dict create]
set json_structure {dict state string}
dict set return "state" $::de1_num_state($::de1(state))
dict set return "substate" $::de1_substate_types($::de1(substate))
switch -- $::de1_num_state($::de1(state)) {
"Idle" {
dict set return "profile" [::profile::filename_from_title $::settings(profile_title)]
dict set return "espresso_count" $::settings(espresso_count)
dict set return "steaming_count" $::settings(steaming_count)
dict set return "bean_brand" $::settings(bean_brand)
dict set return "bean_type" $::settings(bean_type)
dict set return "bean_notes" $::settings(bean_notes)
dict set return "roast_date" $::settings(roast_date)
dict set return "roast_level" $::settings(roast_level)
dict set return "skin" [::profile::filename_from_title $::settings(skin)]
dict set return "head_temperature" $::de1(head_temperature)
dict set return "mix_temperature" $::de1(mix_temperature)
dict set return "steam_heater_temperature" $::de1(steam_heater_temperature)
}
"Espresso" {
foreach key [list "espresso_elapsed" "espresso_pressure" "espresso_weight" "espresso_flow" "espresso_flow_weight" "espresso_temperature_basket" "espresso_temperature_mix"] {
#dict append ret "$key" [::${key} range 0 end]
append json_structure " ${key} list"
dict set return $key [split [::${key} range 0 end] " "]
}
}
"Sleep" {
}
"GoingToSleep" {
}
"Busy" {
}
"Steam" {
}
"HotWater" {
}
"ShortCal" {
}
"SelfTest" {
}
"LongCal" {
}
"Descale" {
}
"FatalError" {
}
"Init" {
}
"NoRequest" {
}
"SkipToNext" {
}
"HotWaterRinse" {
}
"SteamRinse" {
}
"Refill" {
}
"Clean" {
}
"InBootLoader" {
}
"AirPurge" {
}
}
append json_structure " * string"
::wibble::return_200_json [::wibble::compile_json $json_structure $return]
}
proc ::wibble::docs {state} {
set fp [open "[homedir]/[plugin_directory]/advanced_rest_api/doc.json" r]
set file_data [read $fp]
close $fp
::wibble::return_200_json $file_data
}
proc ::wibble::state {state} {
if { ![check_auth $state] } {
return;
}
set method [dict get $state request method]
set current_state $::de1_num_state($::de1(state))
if {$method eq "GET"} {
set path [dict get $state request path]
switch -- $path {
"/api/status/details" {
::wibble::status
}
"/api/status" {
if { $::de1_num_state($::de1(state)) != "Sleep" } {
dict set state_response is_active true
dict set state_response espresso_count $::settings(espresso_count)
dict set state_response steaming_count $::settings(steaming_count)
} else {
dict set state_response is_active false
}
::wibble::return_200_json [::wibble::compile_json {dict} $state_response]
}
}
}
if {$method eq "POST"} {
set postdata [::json::json2dict [dict get $state request rawpost]]
if {[dict exists $postdata active]} {
set new_state [dict get $postdata active]
switch -- $new_state {
"false" {
if {$current_state != "Sleep"} {
start_sleep
}
dict set state_change_response is_active false
::wibble::return_200_json [::wibble::compile_json {dict} $state_change_response]
}
"true" {
if {$current_state != "Idle"} {
start_idle
}
dict set state_change_response is_active true
::wibble::return_200_json [::wibble::compile_json {dict} $state_change_response]
}
}
} else {
::wibble::return_200_json {}
}
}
}
proc ::wibble::flushLog {state} {
if { ![check_auth $state] } {
return;
}
::logging::flush_log
::wibble::return_200_json
}
# Define handlers
::wibble::handle /api/status state
::wibble::handle /api/flush flushLog
::wibble::handle /api/profile profile
::wibble::handle /api/shot history
::wibble::handle /api/help docs
::wibble::handle / indexpage
# Start a server and enter the event loop if not already there.
catch {
::wibble::listen $::plugins::advanced_rest_api::settings(webserver_port)
}
} ;# main
}