Skip to content

Commit

Permalink
Enrich system info response with temperature
Browse files Browse the repository at this point in the history
  • Loading branch information
vaslabs committed Mar 26, 2021
1 parent 96a4866 commit 8d2d8c7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
15 changes: 12 additions & 3 deletions service/web/cmd/pi-web-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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))
Expand Down
12 changes: 11 additions & 1 deletion service/web/internal/system_files.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package shell

import (
"strings"

"github.com/spf13/viper"
)

Expand All @@ -10,4 +13,11 @@ func Read_Env_Config(path string, filename string) (*viper.Viper, error) {
config.SetConfigName(filename)
err := config.ReadInConfig()
return config, err
}
}

func ReadConfigFromString(content string) (*viper.Viper, error) {
config := viper.New()
config.SetConfigType("env")
err := config.ReadConfig(strings.NewReader(content))
return config, err
}
35 changes: 30 additions & 5 deletions service/web/pkg/live_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")}
}
}
}
10 changes: 9 additions & 1 deletion service/web/test/os_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 8d2d8c7

Please sign in to comment.