From 8af4c3117b5addbe13d8213dc6b40a3705ab0398 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Thu, 11 Oct 2018 14:09:12 -0600 Subject: [PATCH] Add ability to configure sensors --- cli.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ install.sh | 13 +++++++-- 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/cli.go b/cli.go index e1c3f69..8598b6f 100644 --- a/cli.go +++ b/cli.go @@ -6,6 +6,8 @@ import ( "os" "strconv" "strings" + + "github.com/yryz/ds18b20" ) // ReadInput reads the next line from a Reader. It will return 'def' if nothing @@ -72,6 +74,90 @@ func PromptForConfiguration() Config { panic(err) } + // Configure sensors + sensors, err := ds18b20.Sensors() + if err != nil { + fmt.Println("Couldn't find any sensors. Did you enable the 1-wire bus?") + fmt.Printf("The error was: %s\n", err) + os.Exit(1) + } + + for _, sensor := range sensors { + fmt.Printf("Configure sensor w/ ID: %s\n", sensor) + fmt.Print("[Y/n]: ") + choice := ReadInput(reader, "y") + if strings.ToLower(choice)[0] != 'y' { + continue + } + + var s Sensor + s.ID = sensor + + fmt.Print("Sensor alias: ") + s.Alias = ReadInput(reader, "") + if s.Alias == "" { + panic("Alias cannot be blank") + } + + fmt.Print("High temperature: ") + s.HighTemp, err = strconv.ParseFloat(ReadInput(reader, ""), 64) + if err != nil { + panic(err) + } + + fmt.Print("Cooling minutes: ") + s.CoolMinutes, err = strconv.ParseFloat(ReadInput(reader, ""), 64) + if err != nil { + panic(err) + } + + fmt.Print("Cooling GPIO: ") + resp, err := strconv.ParseInt(ReadInput(reader, ""), 10, 32) + s.CoolGPIO = int32(resp) + if err != nil { + panic(err) + } + + fmt.Print("Invert cooling switch [false]: ") + s.CoolInvert, err = strconv.ParseBool(ReadInput(reader, "false")) + if err != nil { + panic(err) + } + + fmt.Print("Low temperature: ") + s.LowTemp, err = strconv.ParseFloat(ReadInput(reader, ""), 64) + if err != nil { + panic(err) + } + + fmt.Print("Heating minutes: ") + s.HeatMinutes, err = strconv.ParseFloat(ReadInput(reader, ""), 64) + if err != nil { + panic(err) + } + + fmt.Print("Heating GPIO: ") + resp, err = strconv.ParseInt(ReadInput(reader, ""), 10, 32) + s.HeatGPIO = int32(resp) + if err != nil { + panic(err) + } + + fmt.Print("Invert heating switch [false]: ") + s.HeatInvert, err = strconv.ParseBool(ReadInput(reader, "false")) + if err != nil { + panic(err) + } + + fmt.Print("Enable verbose logging [false]: ") + s.Verbose, err = strconv.ParseBool(ReadInput(reader, "false")) + if err != nil { + panic(err) + } + + config.Sensors = append(config.Sensors, s) + } + return config } diff --git a/install.sh b/install.sh index a1690f9..767fb4b 100644 --- a/install.sh +++ b/install.sh @@ -6,12 +6,18 @@ INSTALLUSER=pi BINURL='https://gitlab.com/shouptech/tempgopher/-/jobs/artifacts/master/raw/tempgopher?job=build' CONFIGFILE=$INSTALLDIR/config.yml +# Load w1_therm module +sudo /sbin/modprobe w1_therm + # Download binary sudo mkdir -p $INSTALLDIR sudo curl -L $BINURL -o $INSTALLBIN sudo chmod +x $INSTALLBIN sudo chown -R $INSTALLUSER: $INSTALLDIR +# Generate a configuration file +sudo -u $INSTALLUSER $INSTALLBIN -c $CONFIGFILE config + # Create unit file sudo sh -c "cat > /etc/systemd/system/tempgopher.service" << EOM [Unit] @@ -21,15 +27,18 @@ After=network.target [Service] Type=simple WorkingDirectory=$INSTALLDIR -ExecStart=$INSTALLBIN -c $CONFIGFILE run -ExecReload=/bin/kill -HUP \$MAINPID +PermissionsStartOnly=true User=$INSTALLUSER Group=$INSTALLUSER +ExecStartPre=/sbin/modprobe w1_therm +ExecStart=$INSTALLBIN -c $CONFIGFILE run +ExecReload=/bin/kill -HUP \$MAINPID [Install] WantedBy=multi-user.target EOM +# Enable and start the service sudo systemctl daemon-reload sudo systemctl enable tempgopher.service sudo systemctl start tempgopher.service