Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve web_api plugin #119

Merged
merged 3 commits into from
Apr 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 98 additions & 71 deletions de1plus/plugins/web_api/plugin.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -34,84 +33,112 @@ 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}"}} {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something seems a bit off with your whitespaces here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New code: Single tab indentation, consistent with much of the de1app Tcl code I've worked with.
start_idle: 4 spaces

We probably should come up with a standard (my preference including no trailing whitespace and each file ends with a newline with no blank lines)

I can see arguments for or against spaces/tabs and 4/8-column indentation


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;
}

start_sleep

::wibble::return_200_json
}

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
}
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;
}

# 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 /flush flushLog
::wibble::handle / notfound

# Start a server and enter the event loop if not already there.

catch {
::wibble::listen $::plugins::web_api::settings(webserver_port)
}

} ;# main
}