Skip to content

Commit

Permalink
Merge pull request #81 from CameronRP/udpate-config
Browse files Browse the repository at this point in the history
Update config
  • Loading branch information
CameronRP authored Oct 13, 2019
2 parents f57cb82 + c69ed13 commit 8d4b00e
Show file tree
Hide file tree
Showing 13 changed files with 480 additions and 337 deletions.
2 changes: 0 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ nfpms:
files:
"_release/managementd-avahi.service": "/etc/avahi/services/managementd.service"
"_release/managementd.service": "/etc/systemd/system/managementd.service"
config_files:
"_release/managementd.yaml": "/etc/cacophony/managementd.yaml"

checksum:
name_template: '{{ .ProjectName }}_{{ .Version }}_checksums.txt'
Expand Down
2 changes: 0 additions & 2 deletions _release/managementd.yaml

This file was deleted.

23 changes: 23 additions & 0 deletions a_managementinterface-packr.go

Large diffs are not rendered by default.

166 changes: 150 additions & 16 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"time"

goapi "github.com/TheCacophonyProject/go-api"
goconfig "github.com/TheCacophonyProject/go-config"
signalstrength "github.com/TheCacophonyProject/management-interface/signal-strength"
"github.com/godbus/dbus"
"github.com/gorilla/mux"
Expand All @@ -45,38 +46,43 @@ const (

type ManagementAPI struct {
cptvDir string
config *goconfig.Config
}

func NewAPI(cptvDir string) *ManagementAPI {
return &ManagementAPI{
cptvDir: cptvDir,
func NewAPI(config *goconfig.Config) (*ManagementAPI, error) {
thermalRecorder := goconfig.DefaultThermalRecorder()
if err := config.Unmarshal(goconfig.ThermalRecorderKey, &thermalRecorder); err != nil {
return nil, err
}

return &ManagementAPI{
cptvDir: thermalRecorder.OutputDir,
config: config,
}, nil
}

// GetDeviceInfo returns information about this device
func (api *ManagementAPI) GetDeviceInfo(w http.ResponseWriter, r *http.Request) {
config, err := goapi.LoadConfig()

if err != nil {
var device goconfig.Device
if err := api.config.Unmarshal(goconfig.DeviceKey, &device); err != nil {
log.Printf("/device-info failed: %v", err)
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, "failed to read device config\n")
return
}

type deviceInfo struct {
*goapi.Config
DeviceID int `json:"deviceID"`
ServerURL string `json:"serverURL"`
Groupname string `json:"groupname"`
Devicename string `json:"devicename"`
DeviceID int `json:"deviceID"`
}
info := deviceInfo{Config: config}

privConfig, err := goapi.LoadPrivateConfig()
if err != nil {
log.Printf("/device-info error loading private config: %v", err)
} else {
info.DeviceID = privConfig.DeviceID
info := deviceInfo{
ServerURL: device.Server,
Groupname: device.Group,
Devicename: device.Name,
DeviceID: device.ID,
}

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(info)
}
Expand Down Expand Up @@ -207,6 +213,134 @@ func (api *ManagementAPI) Reboot(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

// GetConfig will return the config settings and the defaults
func (api *ManagementAPI) GetConfig(w http.ResponseWriter, r *http.Request) {
if err := api.config.Update(); err != nil {
serverError(&w, err)
return
}

configSections := []string{
goconfig.AudioKey,
goconfig.BatteryKey,
goconfig.DeviceKey,
goconfig.GPIOKey,
goconfig.LeptonKey,
goconfig.LocationKey,
goconfig.ModemdKey,
goconfig.PortsKey,
goconfig.TestHostsKey,
goconfig.ThermalMotionKey,
goconfig.ThermalRecorderKey,
goconfig.ThermalThrottlerKey,
goconfig.WindowsKey,
}

configDefaults := map[string]interface{}{
goconfig.AudioKey: goconfig.DefaultAudio(),
goconfig.GPIOKey: goconfig.DefaultGPIO(),
goconfig.LeptonKey: goconfig.DefaultLepton(),
goconfig.ModemdKey: goconfig.DefaultModemd(),
goconfig.PortsKey: goconfig.DefaultPorts(),
goconfig.TestHostsKey: goconfig.DefaultTestHosts(),
goconfig.ThermalMotionKey: goconfig.DefaultThermalMotion(),
goconfig.ThermalRecorderKey: goconfig.DefaultThermalRecorder(),
goconfig.ThermalThrottlerKey: goconfig.DefaultThermalThrottler(),
goconfig.WindowsKey: goconfig.DefaultWindows(),
}

configMap := map[string]interface{}{}

for _, section := range configSections {
configMap[section] = api.config.Get(section)
}

valuesAndDefaults := map[string]interface{}{
"values": configMap,
"defaults": configDefaults,
}

jsonString, err := json.Marshal(valuesAndDefaults)
if err != nil {
serverError(&w, err)
return
}
w.Write(jsonString)
}

// ClearConfigSection will delete the config from a section so the default values will be used.
func (api *ManagementAPI) ClearConfigSection(w http.ResponseWriter, r *http.Request) {
section := r.FormValue("section")
log.Printf("clearing config section %s", section)

if err := api.config.Unset(section); err != nil {
serverError(&w, err)
}
}

// SetLocation is for specifically writing to location setting.
func (api *ManagementAPI) SetLocation(w http.ResponseWriter, r *http.Request) {
log.Println("update location")
latitude, err := strconv.ParseFloat(r.FormValue("latitude"), 32)
if err != nil {
badRequest(&w, err)
return
}
longitude, err := strconv.ParseFloat(r.FormValue("longitude"), 32)
if err != nil {
badRequest(&w, err)
return
}
altitude, err := strconv.ParseFloat(r.FormValue("altitude"), 32)
if err != nil {
badRequest(&w, err)
return
}
accuracy, err := strconv.ParseFloat(r.FormValue("accuracy"), 32)
if err != nil {
badRequest(&w, err)
return
}

timeMillis, err := strconv.ParseInt(r.FormValue("timestamp"), 10, 64)
if err != nil {
badRequest(&w, err)
return
}

location := goconfig.Location{
Latitude: float32(latitude),
Longitude: float32(longitude),
Accuracy: float32(accuracy),
Altitude: float32(altitude),
Timestamp: time.Unix(timeMillis/1000, 0),
}

if err := api.config.Set(goconfig.LocationKey, &location); err != nil {
serverError(&w, err)
}
}

func badRequest(w *http.ResponseWriter, err error) {
(*w).WriteHeader(http.StatusBadRequest)
io.WriteString(*w, err.Error())
}

func serverError(w *http.ResponseWriter, err error) {
log.Printf("server error: %v", err)
(*w).WriteHeader(http.StatusInternalServerError)
}

func (api *ManagementAPI) writeConfig(newConfig map[string]interface{}) error {
log.Printf("writing to config: %s", newConfig)
for k, v := range newConfig {
if err := api.config.Set(k, v); err != nil {
return err
}
}
return nil
}

func getCptvNames(dir string) []string {
matches, _ := filepath.Glob(filepath.Join(dir, cptvGlob))
failedUploadMatches, _ := filepath.Glob(filepath.Join(dir, failedUploadsFolder, cptvGlob))
Expand Down
36 changes: 36 additions & 0 deletions cmd/managementd/a_main-packr.go

Large diffs are not rendered by default.

31 changes: 21 additions & 10 deletions cmd/managementd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,41 @@ package main

import (
"fmt"
"io/ioutil"

yaml "gopkg.in/yaml.v2"
goconfig "github.com/TheCacophonyProject/go-config"
)

// Config for management interface
type Config struct {
Port int `yaml:"port"`
CPTVDir string `yaml:"cptv-dir"`
Port int
CPTVDir string
config *goconfig.Config
}

func (c Config) String() string {
return fmt.Sprintf("{ Port: %d, CPTVDir: %s }", c.Port, c.CPTVDir)
}

// ParseConfigFile parses the given file
func ParseConfigFile(filepath string) (*Config, error) {
buf, err := ioutil.ReadFile(filepath)
// ParseConfig parses the config
func ParseConfig(configDir string) (*Config, error) {
config, err := goconfig.New(configDir)
if err != nil {
return nil, err
}
config := Config{}
if err := yaml.Unmarshal(buf, &config); err != nil {

ports := goconfig.DefaultPorts()
if err := config.Unmarshal(goconfig.PortsKey, &ports); err != nil {
return nil, err
}
return &config, nil

thermalRecorder := goconfig.DefaultThermalRecorder()
if err := config.Unmarshal(goconfig.ThermalRecorderKey, &thermalRecorder); err != nil {
return nil, err
}

return &Config{
Port: ports.Managementd,
CPTVDir: thermalRecorder.OutputDir,
config: config,
}, nil
}
24 changes: 15 additions & 9 deletions cmd/managementd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ import (
"github.com/gobuffalo/packr"
"github.com/gorilla/mux"

goconfig "github.com/TheCacophonyProject/go-config"
managementinterface "github.com/TheCacophonyProject/management-interface"
"github.com/TheCacophonyProject/management-interface/api"
)

const (
configFile = "/etc/cacophony/managementd.yaml"
configDir = goconfig.DefaultConfigDir
)

var version = "<not set>"
Expand All @@ -41,7 +42,7 @@ func main() {
log.SetFlags(0) // Removes timestamp output
log.Printf("running version: %s", version)

config, err := ParseConfigFile(configFile)
config, err := ParseConfig(configDir)
if err != nil {
log.Fatal(err)
return
Expand All @@ -62,21 +63,24 @@ func main() {
router.HandleFunc("/wifi-networks", managementinterface.WifiNetworkHandler).Methods("GET", "POST")
router.HandleFunc("/network", managementinterface.NetworkHandler).Methods("GET")
router.HandleFunc("/interface-status/{name:[a-zA-Z0-9-* ]+}", managementinterface.CheckInterfaceHandler).Methods("GET")
router.HandleFunc("/online-state", managementinterface.ToggleOnlineState).Methods("POST")
router.HandleFunc("/speaker", managementinterface.SpeakerTestHandler).Methods("GET")
router.HandleFunc("/speaker/status", managementinterface.SpeakerStatusHandler).Methods("GET")
router.HandleFunc("/disk-memory", managementinterface.DiskMemoryHandler).Methods("GET")
router.HandleFunc("/location", managementinterface.LocationHandler).Methods("GET", "POST") // Form to view and/or set location manually.
router.HandleFunc("/clock", managementinterface.TimeHandler).Methods("GET", "POST") // Form to view and/or adjust time settings.
router.HandleFunc("/about", managementinterface.AboutHandler).Methods("GET")
router.HandleFunc("/location", managementinterface.GenLocationHandler(config.config)).Methods("GET") // Form to view and/or set location manually.
router.HandleFunc("/clock", managementinterface.TimeHandler).Methods("GET", "POST") // Form to view and/or adjust time settings.
router.HandleFunc("/about", managementinterface.AboutHandlerGen(config.config)).Methods("GET")

router.HandleFunc("/advanced", managementinterface.AdvancedMenuHandler).Methods("GET")
router.HandleFunc("/camera", managementinterface.CameraHandler).Methods("GET")
router.HandleFunc("/camera/snapshot", managementinterface.CameraSnapshot).Methods("GET")
router.HandleFunc("/rename", managementinterface.Rename).Methods("GET")

// API
apiObj := api.NewAPI(config.CPTVDir)
apiObj, err := api.NewAPI(config.config)
if err != nil {
log.Fatal(err)
return
}
apiRouter := router.PathPrefix("/api").Subrouter()
apiRouter.HandleFunc("/device-info", apiObj.GetDeviceInfo).Methods("GET")
apiRouter.HandleFunc("/recordings", apiObj.GetRecordings).Methods("GET")
Expand All @@ -86,8 +90,10 @@ func main() {
apiRouter.HandleFunc("/signal-strength", apiObj.GetSignalStrength).Methods("GET")
apiRouter.HandleFunc("/reregister", apiObj.Reregister).Methods("POST")
apiRouter.HandleFunc("/reboot", apiObj.Reboot).Methods("POST")
apiRouter.HandleFunc("/location", managementinterface.APILocationHandler).Methods("POST") // Set location via a POST request.
apiRouter.HandleFunc("/clock", managementinterface.APITimeHandler).Methods("POST") // Set times via a POST request.
apiRouter.HandleFunc("/config", apiObj.GetConfig).Methods("GET")
apiRouter.HandleFunc("/clear-config-section", apiObj.ClearConfigSection).Methods("POST")
apiRouter.HandleFunc("/location", apiObj.SetLocation).Methods("POST") // Set location via a POST request.
apiRouter.HandleFunc("/clock", managementinterface.APITimeHandler).Methods("POST") // Set times via a POST request.
apiRouter.Use(basicAuth)

listenAddr := fmt.Sprintf(":%d", config.Port)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/TheCacophonyProject/management-interface
go 1.12

require (
github.com/TheCacophonyProject/go-api v0.0.0-20190901221455-8bdb00df1b38
github.com/TheCacophonyProject/go-api v0.0.0-20190923033957-174cea2ac81c
github.com/TheCacophonyProject/go-config v0.0.0-20191003001220-bff132a03491
github.com/gobuffalo/packr v1.30.1
github.com/godbus/dbus v4.1.0+incompatible
github.com/gorilla/context v1.1.1 // indirect
Expand Down
Loading

0 comments on commit 8d4b00e

Please sign in to comment.