From 4e2bab3a871866f6af715b302f760c11a0fccc59 Mon Sep 17 00:00:00 2001 From: Jeff Kletsky Date: Tue, 13 Apr 2021 18:41:01 -0700 Subject: [PATCH 1/3] plugin: web_api: Declarations into main, return_200_* utility By moving the declarations into main, it now "does nothing" if not enabled. Make extension of command set easier with utilties: proc ::wibble::return_200_json {{content "{status: ok}"}} proc ::wibble::return_200_text {{content "ok"}} Signed-Off-By: Jeff Kletsky --- de1plus/plugins/web_api/plugin.tcl | 158 ++++++++++++++++------------- 1 file changed, 87 insertions(+), 71 deletions(-) diff --git a/de1plus/plugins/web_api/plugin.tcl b/de1plus/plugins/web_api/plugin.tcl index a4fcf276..eb4fa17b 100644 --- a/de1plus/plugins/web_api/plugin.tcl +++ b/de1plus/plugins/web_api/plugin.tcl @@ -2,7 +2,6 @@ package require de1_machine set plugin_name "web_api" -namespace eval ::wibble {} namespace eval ::plugins::${plugin_name} { variable author "Johanna Schander" @@ -34,84 +33,101 @@ namespace eval ::plugins::${plugin_name} { after 2000 [list info_page [translate "Please change the Webserver auth key!"] [translate "Ok"]] return } - # Define handlers - ::wibble::handle /on togglePowerOn - ::wibble::handle /off togglePowerOff - ::wibble::handle /status checkStatus - ::wibble::handle / notfound - # Start a server and enter the event loop if not already there. - catch { - ::wibble::listen 8080 - } - } -} -proc ::wibble::check_auth {state} { - set auth [dict getnull $state request query auth] - set auth [lindex $auth 1] + # Auth - if {$auth eq ""} { - return [unauthorized $state] - } + proc ::wibble::check_auth {state} { + set auth [dict getnull $state request query auth] + set auth [lindex $auth 1] - if {$auth != $::plugins::web_api::settings(webserver_authentication_key)} { - return [unauthorized $state] - } + if {$auth eq ""} { + return [unauthorized $state] + } - return true; -} + if {$auth != $::plugins::web_api::settings(webserver_authentication_key)} { + return [unauthorized $state] + } -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; -} + 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; + } -proc ::wibble::togglePowerOn {state} { - if { ![check_auth $state] } { - return; - } + # Utilities - start_idle + proc ::wibble::return_200_json {{content "{status: ok}"}} { - dict set response status 200 - dict set state response header content-type "" {application/json charset utf-8} - dict set response content "{status: \"ok\"}" - sendresponse $response -} + dict set response status 200 + dict set state response header content-type "" {application/json charset utf-8} + dict set response content "$content" + sendresponse $response + } -proc ::wibble::togglePowerOff {state} { - if { ![check_auth $state] } { - return; - } - - start_sleep - - dict set response status 200 - dict set state response header content-type "" {application/json charset utf-8} - dict set response content "{status: \"ok\"}" - sendresponse $response -} + proc ::wibble::return_200_text {{content "ok"}} { + + dict set response status 200 + dict set state response header content-type "" {text/plain charset utf-8} + dict set response content "$content" + sendresponse $response + } + + # Actions + + proc ::wibble::togglePowerOn {state} { + if { ![check_auth $state] } { + return; + } + + start_idle + + ::wibble::return_200_json + } + + proc ::wibble::togglePowerOff {state} { + if { ![check_auth $state] } { + return; + } -proc ::wibble::checkStatus {state} { - if { ![check_auth $state] } { - return; - } - - dict set response status 200 - dict set state response header content-type "" {text/plain charset utf-8} - - # Returning a simple text 1 if the machine is in anything other than an sleep state. Return text 0 if sleep. - # Return values chosen by cribbing from Supereg/homebridge-http-switch - - if { $::de1_num_state($::de1(state)) != "Sleep" } { - dict set response content "1" - } else { - dict set response content "0" - } - - sendresponse $response -} \ No newline at end of file + start_sleep + + ::wibble::return_200_json + } + + proc ::wibble::checkStatus {state} { + if { ![check_auth $state] } { + return; + } + + # Returning a simple text 1 if the machine is in anything other than an sleep state. Return text 0 if sleep. + # Return values chosen by cribbing from Supereg/homebridge-http-switch + + if { $::de1_num_state($::de1(state)) != "Sleep" } { + set not_sleep "1" + } else { + set not_sleep "0" + } + + ::wibble::return_200_text $not_sleep + } + + # Define handlers + + ::wibble::handle /on togglePowerOn + ::wibble::handle /off togglePowerOff + ::wibble::handle /status checkStatus + ::wibble::handle / notfound + + # Start a server and enter the event loop if not already there. + + catch { + ::wibble::listen 8080 + } + + } ;# main +} From 39156589538cb1e148d2057c3bc124cf46d161b9 Mon Sep 17 00:00:00 2001 From: Jeff Kletsky Date: Tue, 13 Apr 2021 18:44:50 -0700 Subject: [PATCH 2/3] plugins: web_api: Respect ::plugins::web_api::settings(webserver_port) Use the settings value instead of being hard-coded to 8080 Signed-Off-By: Jeff Kletsky --- de1plus/plugins/web_api/plugin.tcl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/de1plus/plugins/web_api/plugin.tcl b/de1plus/plugins/web_api/plugin.tcl index eb4fa17b..24b4248a 100644 --- a/de1plus/plugins/web_api/plugin.tcl +++ b/de1plus/plugins/web_api/plugin.tcl @@ -126,7 +126,7 @@ namespace eval ::plugins::${plugin_name} { # Start a server and enter the event loop if not already there. catch { - ::wibble::listen 8080 + ::wibble::listen $::plugins::web_api::settings(webserver_port) } } ;# main From 9c13cd3d19ffb3cfbb601975cc34976d28da014c Mon Sep 17 00:00:00 2001 From: Jeff Kletsky Date: Tue, 13 Apr 2021 19:22:52 -0700 Subject: [PATCH 3/3] plugins: web_api: Add /flush (log) --- de1plus/plugins/web_api/plugin.tcl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/de1plus/plugins/web_api/plugin.tcl b/de1plus/plugins/web_api/plugin.tcl index 24b4248a..d8ff95af 100644 --- a/de1plus/plugins/web_api/plugin.tcl +++ b/de1plus/plugins/web_api/plugin.tcl @@ -99,6 +99,16 @@ namespace eval ::plugins::${plugin_name} { ::wibble::return_200_json } + proc ::wibble::flushLog {state} { + if { ![check_auth $state] } { + return; + } + + ::logging::flush_log + + ::wibble::return_200_json + } + proc ::wibble::checkStatus {state} { if { ![check_auth $state] } { return; @@ -120,7 +130,8 @@ namespace eval ::plugins::${plugin_name} { ::wibble::handle /on togglePowerOn ::wibble::handle /off togglePowerOff - ::wibble::handle /status checkStatus + ::wibble::handle /status checkStatus + ::wibble::handle /flush flushLog ::wibble::handle / notfound # Start a server and enter the event loop if not already there.