Skip to content

Commit

Permalink
Merge pull request #154 from CameronRP/speed-up-rename
Browse files Browse the repository at this point in the history
Speed up changing device name
  • Loading branch information
CameronRP authored Sep 30, 2024
2 parents b2566a9 + e611e61 commit ca9eada
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 25 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ jobs:
- name: Run Go Tests
run: go test ./...

- name: Install Staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Run Staticcheck
run: staticcheck --checks="all,-ST1000,-ST1022,-ST1020,-ST1003" ./...

- name: Check GoReleaser
uses: goreleaser/goreleaser-action@v6
with:
Expand Down
1 change: 1 addition & 0 deletions _release/managementd.service
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[Unit]
Description=Cacophonator management interface
After=network.target
ConditionPathExists=/etc/salt/minion_id

[Service]
ExecStart=/usr/bin/managementd
Expand Down
23 changes: 16 additions & 7 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (

goapi "github.com/TheCacophonyProject/go-api"
goconfig "github.com/TheCacophonyProject/go-config"
"github.com/TheCacophonyProject/go-utils/logging"
"github.com/TheCacophonyProject/go-utils/saltutil"
signalstrength "github.com/TheCacophonyProject/management-interface/signal-strength"
saltrequester "github.com/TheCacophonyProject/salt-updater"
"github.com/godbus/dbus"
Expand All @@ -48,10 +50,9 @@ import (
"github.com/TheCacophonyProject/trap-controller/trapdbusclient"

netmanagerclient "github.com/TheCacophonyProject/rpi-net-manager/netmanagerclient"
"github.com/sirupsen/logrus"
)

var log *logrus.Logger
var log *logging.Logger

const (
cptvGlob = "*.cptv"
Expand All @@ -69,7 +70,7 @@ type ManagementAPI struct {
appVersion string
}

func NewAPI(router *mux.Router, config *goconfig.Config, appVersion string, l *logrus.Logger) (*ManagementAPI, error) {
func NewAPI(router *mux.Router, config *goconfig.Config, appVersion string, l *logging.Logger) (*ManagementAPI, error) {
log = l
thermalRecorder := goconfig.DefaultThermalRecorder()
if err := config.Unmarshal(goconfig.ThermalRecorderKey, &thermalRecorder); err != nil {
Expand All @@ -84,9 +85,9 @@ func NewAPI(router *mux.Router, config *goconfig.Config, appVersion string, l *l
}, nil
}

func (s *ManagementAPI) StopHotspotTimer() {
if s.hotspotTimer != nil {
s.hotspotTimer.Stop()
func (api *ManagementAPI) StopHotspotTimer() {
if api.hotspotTimer != nil {
api.hotspotTimer.Stop()
}
}

Expand All @@ -104,7 +105,7 @@ func checkIsConnectedToNetworkWithRetries() (string, error) {
return ssid, err
}

func (server *ManagementAPI) ManageHotspot() {
func (api *ManagementAPI) ManageHotspot() {
// Check if we are connected to a network
ssid, err := checkIsConnectedToNetworkWithRetries()
if err != nil {
Expand Down Expand Up @@ -1259,6 +1260,10 @@ func (api *ManagementAPI) SetSaltGrains(w http.ResponseWriter, r *http.Request)
}
}

if !saltutil.IsSaltIdSet() {
http.Error(w, "Salt is not yet ready to set grains", http.StatusInternalServerError)
return
}
cmd := exec.Command("salt-call", "grains.setval", key, value)
if output, err := cmd.CombinedOutput(); err != nil {
http.Error(w, fmt.Sprintf("failed to set grain: %s, output: %s", err, output), http.StatusInternalServerError)
Expand Down Expand Up @@ -1642,6 +1647,10 @@ func (api *ManagementAPI) UploadLogs(w http.ResponseWriter, r *http.Request) {
return
}

if !saltutil.IsSaltIdSet() {
http.Error(w, "Salt is not yet ready to upload logs", http.StatusInternalServerError)
return
}
if err := exec.Command("salt-call", "cp.push", logFileName+".gz").Run(); err != nil {
log.Printf("Error pushing log file with salt: %v", err)
serverError(&w, err)
Expand Down
9 changes: 4 additions & 5 deletions cmd/managementd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import (
netmanagerclient "github.com/TheCacophonyProject/rpi-net-manager/netmanagerclient"
"github.com/TheCacophonyProject/thermal-recorder/headers"
"github.com/alexflint/go-arg"
"github.com/sirupsen/logrus"
)

const (
Expand All @@ -64,7 +63,7 @@ var (
headerInfo *headers.HeaderInfo
frameCh = make(chan *FrameData, 4)
connected atomic.Bool
log *logrus.Logger
log = logging.NewLogger("info")
)

type Args struct {
Expand Down Expand Up @@ -274,15 +273,15 @@ func handleConn(conn net.Conn) error {

log.Printf("connection from %s %s (%dx%d@%dfps) frame size %d", headerInfo.Brand(), headerInfo.Model(), headerInfo.ResX(), headerInfo.ResY(), headerInfo.FPS(), headerInfo.FrameSize())

var clearB []byte = make([]byte, 5)
clearB := make([]byte, 5)
_, err = io.ReadFull(reader, clearB)
if err != nil {
return err
}

rawFrame := make([]byte, headerInfo.FrameSize())
var frame *cptvframe.Frame = cptvframe.NewFrame(headerInfo)
var frames int = 0
frame := cptvframe.NewFrame(headerInfo)
frames := 0
var lastFrame *FrameData
connected.Store(true)
for {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.3
require (
github.com/TheCacophonyProject/audiobait/v3 v3.0.1
github.com/TheCacophonyProject/event-reporter v1.3.2-0.20200210010421-ca3fcb76a231
github.com/TheCacophonyProject/go-api v1.2.1
github.com/TheCacophonyProject/go-api v1.2.2
github.com/TheCacophonyProject/go-config v1.22.0
github.com/TheCacophonyProject/go-cptv v0.0.0-20211109233846-8c32a5d161f7
github.com/TheCacophonyProject/lepton3 v0.0.0-20211005194419-22311c15d6ee
Expand All @@ -19,12 +19,11 @@ require (
)

require (
github.com/TheCacophonyProject/go-utils v0.1.1
github.com/TheCacophonyProject/go-utils v0.1.3
github.com/TheCacophonyProject/rpi-net-manager v0.4.0-deb12
github.com/TheCacophonyProject/thermal-recorder v1.22.1-0.20230627011240-89964c0511f7
github.com/TheCacophonyProject/trap-controller v0.0.0-20230227002937-262a1adfaa47
github.com/alexflint/go-arg v1.4.3
github.com/sirupsen/logrus v1.9.3
golang.org/x/text v0.16.0

)
Expand All @@ -48,6 +47,7 @@ require (
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ github.com/TheCacophonyProject/event-reporter/v3 v3.8.0 h1:FYNR+MX5ypyG7UWpogDnD
github.com/TheCacophonyProject/event-reporter/v3 v3.8.0/go.mod h1:WTppJtTBxduasM1Or5SAh4Mm0YrTDnprOChjnGYgyEI=
github.com/TheCacophonyProject/go-api v0.0.0-20190923033957-174cea2ac81c/go.mod h1:FfMpa4cFhNXQ9tuKG18HO6yLExezcJhzjUjBOFocrQw=
github.com/TheCacophonyProject/go-api v1.0.4/go.mod h1:F7UUNgsLhbw7hsiNBMRB9kQz9uXXosVmNToqImz7EA8=
github.com/TheCacophonyProject/go-api v1.2.1 h1:QS8mzStQHzb3oAqqwjoLqCvXsRM8fx7F9I7hpgKND5c=
github.com/TheCacophonyProject/go-api v1.2.1/go.mod h1:F7UUNgsLhbw7hsiNBMRB9kQz9uXXosVmNToqImz7EA8=
github.com/TheCacophonyProject/go-api v1.2.2 h1:lXV2/upgA6lgLWOuTMRJoTjMXxlSveN9v8Dr3mvYqPM=
github.com/TheCacophonyProject/go-api v1.2.2/go.mod h1:innR3kf5xnua2wbnLvOudI13j2TU1sGY1dxJQJLRZkI=
github.com/TheCacophonyProject/go-config v0.0.0-20190922224052-7c2a21bc6b88/go.mod h1:gPUJLVu408NRz9/P3BrsxzOzLc+KJLrv+jVdDw3RI0Y=
github.com/TheCacophonyProject/go-config v0.0.0-20190927054511-c93578ae648a/go.mod h1:QCgT+KCrz1CmLVpeeOMl5L8/X1QvWwpsLzR7afTmEJc=
github.com/TheCacophonyProject/go-config v1.4.0/go.mod h1:oARW/N3eJbcewCqB+Jc7TBwuODawwYgpo56UO6yBdKU=
Expand All @@ -71,8 +71,8 @@ github.com/TheCacophonyProject/go-cptv v0.0.0-20200616224711-fc633122087a/go.mod
github.com/TheCacophonyProject/go-cptv v0.0.0-20200818214604-bd5d4aa36043/go.mod h1:wG4/P/TsGtk33uBClYPjRlSbcdQrIASXutOUV8LMn2o=
github.com/TheCacophonyProject/go-cptv v0.0.0-20211109233846-8c32a5d161f7 h1:sf9KTj7u3mFMx5NsLpQtf8FtP3BDZAgtHutanDygQgk=
github.com/TheCacophonyProject/go-cptv v0.0.0-20211109233846-8c32a5d161f7/go.mod h1:T74NuMjo2YrLoyhAd0+9hj2pgVt8F7DWWzTMlU8aH6k=
github.com/TheCacophonyProject/go-utils v0.1.1 h1:VOt9EphEqRUYMqKJlJeliIarIMlCVKYGb1fdqM6b4YM=
github.com/TheCacophonyProject/go-utils v0.1.1/go.mod h1:jZPUZ4GtYVxnlTtqiYKMFWDT//kmxdbwjLW3HCyCmCE=
github.com/TheCacophonyProject/go-utils v0.1.3 h1:DSuDeJz7ZM00yQRLsoukWH0fnC+8X8+ziYxOl6l3wEY=
github.com/TheCacophonyProject/go-utils v0.1.3/go.mod h1:jZPUZ4GtYVxnlTtqiYKMFWDT//kmxdbwjLW3HCyCmCE=
github.com/TheCacophonyProject/lepton3 v0.0.0-20200121020734-2ae28662e1bc/go.mod h1:xzPAWtvVCbJdJC2Gn1cG0Ovs/VP7XGGiQpUU8wU4HME=
github.com/TheCacophonyProject/lepton3 v0.0.0-20200213011619-1934a9300bd3/go.mod h1:xzPAWtvVCbJdJC2Gn1cG0Ovs/VP7XGGiQpUU8wU4HME=
github.com/TheCacophonyProject/lepton3 v0.0.0-20200909032119-e2b2b778a8ee/go.mod h1:+FTQKx63hdEbuTe/nxNv9TQ2EWqdlzMZx7UNLGCX9SE=
Expand Down
14 changes: 10 additions & 4 deletions management-interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/json"
"html/template"
"io"
"io/ioutil"
"net"
"net/http"
"os"
Expand All @@ -36,6 +35,8 @@ import (
"github.com/TheCacophonyProject/audiobait/v3/playlist"
goconfig "github.com/TheCacophonyProject/go-config"
"github.com/TheCacophonyProject/rpi-net-manager/netmanagerclient"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/TheCacophonyProject/go-utils/logging"
"github.com/gobuffalo/packr"
Expand All @@ -59,7 +60,11 @@ func init() {
tmpl.Funcs(template.FuncMap{"DeviceName": func() string { return deviceName }})
for _, name := range templateBox.List() {
t := tmpl.New(name)
template.Must(t.Parse(templateBox.String(name)))
s, err := templateBox.FindString(name)
if err != nil {
log.Fatal(err)
}
template.Must(t.Parse(s))
}
}

Expand Down Expand Up @@ -139,7 +144,7 @@ func readFile(file string) string {
}

// The /etc/salt/minion_id file contains the ID.
out, err := ioutil.ReadFile(file)
out, err := os.ReadFile(file)
if err != nil {
return ""
}
Expand Down Expand Up @@ -216,7 +221,8 @@ func DiskMemoryHandler(w http.ResponseWriter, r *http.Request) {
words[0] = words[0] + " K"
words[1] = words[1][2:]
words[0], words[1] = words[1], words[0] // This reverses the 2 columns
words[0] = strings.Title(words[0])
titleCaser := cases.Title(language.Und)
words[0] = titleCaser.String(words[0])
}
outputStrings2 = append(outputStrings2, words)
if words[0] == "Free Swap" {
Expand Down
4 changes: 2 additions & 2 deletions signal-strength/signal-strength.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package signalstrength

import (
"encoding/xml"
"io/ioutil"
"io"
"net/http"
"net/http/cookiejar"
"time"
Expand Down Expand Up @@ -57,7 +57,7 @@ func Run() (int, error) {
if err != nil {
return 0, err
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit ca9eada

Please sign in to comment.