-
Notifications
You must be signed in to change notification settings - Fork 12
/
plugin.tcl
463 lines (408 loc) · 13.9 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package require de1_machine 1.2
package require json
package require de1_profile 2.0
package require de1_vars 1.0
package require de1_utils 1.1
set plugin_name "advanced_rest_api"
namespace eval ::plugins::${plugin_name} {
variable author "Yannick Dietler"
variable contact "ydt@ydt.ch"
variable version 1.2
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 response header content-type {} application/json
dict set response content "$content\n"
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 rawheaders [dict get $state request rawheader]
set filenameIndex [lsearch $rawheaders "filename:*"]
if {$filenameIndex == -1 } {
set localfilename "[clock seconds].tcl"
} else {
set localfilename [lindex [split [lindex $rawheaders $filenameIndex] ": "] end]
}
set postdata [dict get $state request rawpost]
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]
#Load all saved profiles as a list
set savedprofiles [glob -tails -directory [pwd]/profiles/ *.tcl]
if {$profile != ""} {
#Only return profile information if the profile exists
if {$profile in $savedprofiles} {
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 {
#If a profile is specified but does not exist, return all profiles
::wibble::return_200_json [::wibble::compile_json {list} $savedprofiles]
}
} else {
#If no profile was specified, return all profiles
::wibble::return_200_json [::wibble::compile_json {list} $savedprofiles]
}
}
#Set a profile
if {$method eq "PUT"} {
set path [dict get $state request path]
set profile [lindex [split $path "/"] 3]
#Load all saved profiles as a list
set savedprofiles [glob -tails -directory [pwd]/profiles/ *.tcl]
#Only set the profile if it exists
if {$profile in $savedprofiles} {
#The select_profile procedure accepts the profile name without file extension (.tcl)
set rootname [file rootname [file tail $profile]]
select_profile $rootname
::wibble::return_200_json "$profile"
} else {
::wibble::return_200_json [::wibble::compile_json {list} $savedprofiles]
}
}
}
#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]
}
}
#history v2
proc ::wibble::history_v2 {state} {
if { ![check_auth $state] } {
return;
}
set method [dict get $state request method]
if { $method eq "PUT" } {
return [set_next_shot_in_dye $state]
}
set path [dict get $state request path]
set shot [lindex [split $path "/"] 4]
append shotName [lindex [split $shot "."] 0] ".json"
if {$shotName != ""} {
set fd [open "[pwd]/history_v2/$shotName" r]
fconfigure $fd -translation binary
set content [read $fd]; close $fd
::wibble::return_200_json $content
} else {
::wibble::return_200_json
}
}
proc ::wibble::set_next_shot_in_dye { state } {
set path [dict get $state request path]
set shot [lindex [split $path "/"] 4]
msg -INFO "shot name is ${shot}"
set fields {
workflow_settings
shot_profile
ratio
drink_weight
grinder_dose_weight
grinder_setting
grinder_model
workflow
bean_brand
bean_type
roast_date
roast_level
bean_notes
}
::plugins::DYE::shots::source_next_from $shot {} $fields
::wibble::return_200_json ""
}
proc ::wibble::history_sdb { state } {
if { ![check_auth $state] } {
return;
}
# TODO: check SDB plugin exists and is loaded
array set loadedShots [::plugins::SDB::shots *]
set jsonArray {}
set listLength [llength $loadedShots(grinder_setting)]
for {set i 0} {$i < $listLength} {incr i} {
# Create a dictionary for each index
set shotDict [dict create \
clock [lindex $loadedShots(clock) $i] \
grinder_setting [lindex $loadedShots(grinder_setting) $i] \
grinder_model [lindex $loadedShots(grinder_model) $i] \
profile_title [lindex $loadedShots(profile_title) $i] \
bean_desc [lindex $loadedShots(bean_desc) $i] \
bean_brand [lindex $loadedShots(bean_brand) $i] \
bean_type [lindex $loadedShots(bean_type) $i] \
bean_notes [string map {\n \\n} [lindex $loadedShots(bean_notes) $i]] \
drink_weight [lindex $loadedShots(drink_weight) $i] \
grinder_dose_weight [lindex $loadedShots(grinder_dose_weight) $i] \
target_drink_weight [lindex $loadedShots(target_drink_weight) $i] \
extraction_time [lindex $loadedShots(extraction_time) $i] \
filename [lindex $loadedShots(filename) $i] \
espresso_enjoyment [lindex $loadedShots(espresso_enjoyment) $i] \
espresso_notes [string map {\n \\n} [lindex $loadedShots(espresso_notes) $i]] \
shot_desc [lindex $loadedShots(shot_desc) $i]]
# Append the dictionary to the jsonArray list
lappend jsonArray $shotDict
}
::wibble::return_200_json [::wibble::compile_json {list dict} $jsonArray]
}
# 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))
dict set return "battery_percent" [battery_percent]
dict set return "charger_on" $::de1(usb_charger_on)
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" [expr [expr {floor([expr $::de1(head_temperature) * 100])} / 100]]
dict set return "mix_temperature" [expr [expr {floor([expr $::de1(mix_temperature) * 100])} / 100]]
dict set return "steam_heater_temperature" [expr [expr {floor([expr $::de1(steam_heater_temperature) * 100])} / 100]]
dict set return "water_level_ml" [water_tank_level_to_milliliters $::de1(water_level)]
}
"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 /api/v2/shot history_v2
::wibble::handle /api/v2/shots history_sdb
::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
}