1
0
Fork 0
mirror of https://github.com/shouptech/tempgopher.git synced 2026-02-03 08:39:43 +00:00

Add ability to configure sensors

This commit is contained in:
Emma 2018-10-11 14:09:12 -06:00
parent 0153e453dc
commit 8af4c3117b
2 changed files with 97 additions and 2 deletions

86
cli.go
View file

@ -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
}

View file

@ -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