From 8d2d8c744787d9991d0fe3986c0da6c4ed0a78b9 Mon Sep 17 00:00:00 2001 From: Vasilis Nicolaou Date: Fri, 26 Mar 2021 11:47:03 +0000 Subject: [PATCH] Enrich system info response with temperature --- service/web/cmd/pi-web-agent.go | 15 +++++++++--- service/web/internal/system_files.go | 12 +++++++++- service/web/pkg/live_info.go | 35 ++++++++++++++++++++++++---- service/web/test/os_info_test.go | 10 +++++++- 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/service/web/cmd/pi-web-agent.go b/service/web/cmd/pi-web-agent.go index 5dcb4099..3fbf4f9e 100644 --- a/service/web/cmd/pi-web-agent.go +++ b/service/web/cmd/pi-web-agent.go @@ -8,9 +8,18 @@ import ( "encoding/json" ) -func os_info_handler(w http.ResponseWriter, req *http.Request) { +type system_info_response struct { + OS_Info api.Os_Info_Response + Temperature api.Temperature_Response +} + +func system_info_handler(w http.ResponseWriter, req *http.Request) { os_info := api.OS_Info() - json.NewEncoder(w).Encode(os_info) + temperature := api.Measure_Temperature() + json.NewEncoder(w).Encode(system_info_response{ + os_info, + temperature, + }) } func main() { @@ -23,7 +32,7 @@ func main() { } // Simple static webserver: http.HandleFunc(api_action_prefix, dummyHandler) - http.HandleFunc(api_info_prefix + "os_info", os_info_handler) + http.HandleFunc(api_info_prefix + "os_info", system_info_handler) http.Handle("/assets/", http.FileServer(http.Dir("assets"))) log.Fatal(http.ListenAndServe(":8080", nil)) diff --git a/service/web/internal/system_files.go b/service/web/internal/system_files.go index 2c41cf24..ddfb8db8 100644 --- a/service/web/internal/system_files.go +++ b/service/web/internal/system_files.go @@ -1,5 +1,8 @@ package shell + import ( + "strings" + "github.com/spf13/viper" ) @@ -10,4 +13,11 @@ func Read_Env_Config(path string, filename string) (*viper.Viper, error) { config.SetConfigName(filename) err := config.ReadInConfig() return config, err -} \ No newline at end of file +} + +func ReadConfigFromString(content string) (*viper.Viper, error) { + config := viper.New() + config.SetConfigType("env") + err := config.ReadConfig(strings.NewReader(content)) + return config, err +} diff --git a/service/web/pkg/live_info.go b/service/web/pkg/live_info.go index 887500a8..1a6bd64a 100644 --- a/service/web/pkg/live_info.go +++ b/service/web/pkg/live_info.go @@ -2,29 +2,54 @@ package api import ( "log" + shell "github.com/vaslabs/pi-web-agent/internal" ) -type os_info_response struct { +type Os_Info_Response struct { Id string "" Version_Id string "" Version_Codename string "" } -func OS_Info() os_info_response { +func OS_Info() Os_Info_Response { return OS_Info_From("/etc", "os-release") } -func OS_Info_From(path string, filename string) os_info_response { +func OS_Info_From(path string, filename string) Os_Info_Response { config, err := shell.Read_Env_Config(path, filename) if err != nil { log.Fatalf("Error %s", err.Error()) - return os_info_response{} + return Os_Info_Response{} } else { - return os_info_response{ + return Os_Info_Response{ config.GetString("ID"), config.GetString("VERSION_ID"), config.GetString("VERSION_CODENAME"), } } } + +type Temperature_Response struct { + Temp string +} + +func Measure_Temperature() Temperature_Response { + return Measure_Temperature_From("/opt/vc/bin/vcgencmd", "measure_temp") +} + +func Measure_Temperature_From(command string, args ...string) Temperature_Response { + output, error := shell.RunSingle(command, args...) + if error != nil { + log.Fatalf("Error while measuring temperature: %s", error.Error()) + return Temperature_Response{"unknown"} + } else { + config, err := shell.ReadConfigFromString(output) + if err != nil { + log.Fatalf("Error reading temperature output %s", err.Error()) + return Temperature_Response{"unknown"} + } else { + return Temperature_Response{config.GetString("temp")} + } + } +} diff --git a/service/web/test/os_info_test.go b/service/web/test/os_info_test.go index cb1ddee1..16a54cec 100644 --- a/service/web/test/os_info_test.go +++ b/service/web/test/os_info_test.go @@ -15,6 +15,14 @@ func TestOsReleaseParsing(t *testing.T) { } } +func TestTempMeasure(t *testing.T) { + expectedTemp := "49.2'C" + temperature := api.Measure_Temperature_From("echo", "temp=49.2'C") + if (temperature.Temp != expectedTemp) { + expectationFailure(expectedTemp, temperature.Temp, t) + } +} + func expectationFailure(expected string, got string, t *testing.T) { t.Errorf("Expected %s but got %s", expected, got) -} +} \ No newline at end of file