WIP
This commit is contained in:
parent
3532c771ec
commit
617454f66b
4
Gopkg.lock
generated
4
Gopkg.lock
generated
@ -3,11 +3,11 @@
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:fc0785e9dcf11c34eba33d5b308e5cdc47ef03a5fea6bb9e6bc55f9cbcaa70a6"
|
||||
digest = "1:7753db8f0bb46d1196ca6d01c52397b96de630975857bcdca0af62a5f107f588"
|
||||
name = "git.cryptic.systems/fh-trier/go-flucky-server"
|
||||
packages = ["pkg/types"]
|
||||
pruneopts = "UT"
|
||||
revision = "6ee773aeec13f439a8d963bf37d67999cbfcd85c"
|
||||
revision = "75118080be43f6e26d2701094ca86c23d9364a92"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:870d441fe217b8e689d7949fef6e43efbc787e50f200cb1e70dbca9204a1d6be"
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"git.cryptic.systems/fh-trier/go-flucky/cmd/config"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/cmd/remote"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/cmd/sensor"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/cmd/temperature"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -24,7 +23,7 @@ func Execute(version string) {
|
||||
config.InitCmd(rootCmd, configDir)
|
||||
remote.InitCmd(rootCmd, configDir)
|
||||
sensor.InitCmd(rootCmd, configDir)
|
||||
temperature.InitCmd(rootCmd, configDir)
|
||||
//temperature.InitCmd(rootCmd, configDir)
|
||||
|
||||
rootCmd.Execute()
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ var listRemoteCmd = &cobra.Command{
|
||||
Short: "List Remove Servers",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if err := remote.List(os.Stdout, configDir); err != nil {
|
||||
if err := remote.Print(os.Stdout, configDir); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
|
30
cmd/sensor/add.go
Normal file
30
cmd/sensor/add.go
Normal file
@ -0,0 +1,30 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var enabled bool
|
||||
var sensorLocation, wireID, wirePath string
|
||||
|
||||
var addSensorCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add Sensor",
|
||||
Args: cobra.ExactArgs(3),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if err := sensor.Add(&args[0], &sensorLocation, &args[1], &wireID, &args[2], wirePath, configDir, enabled); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(addSensorCmd)
|
||||
addSensorCmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "Enable Sensor")
|
||||
addSensorCmd.Flags().StringVarP(&sensorLocation, "sensor-location", "l", "", "Sensor location")
|
||||
addSensorCmd.Flags().StringVarP(&wireID, "wire-id", "i", "", "Wire-ID")
|
||||
addSensorCmd.Flags().StringVarP(&wirePath, "wire-path", "w", "/sys/bus/w1/devices", "Wire device path")
|
||||
}
|
24
cmd/sensor/disable.go
Normal file
24
cmd/sensor/disable.go
Normal file
@ -0,0 +1,24 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var disableSensorCmd = &cobra.Command{
|
||||
Use: "disable",
|
||||
Short: "Disable Sensor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if err := sensor.Disable(args[0], configDir); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(disableSensorCmd)
|
||||
}
|
24
cmd/sensor/enable.go
Normal file
24
cmd/sensor/enable.go
Normal file
@ -0,0 +1,24 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var enableSensorCmd = &cobra.Command{
|
||||
Use: "enable",
|
||||
Short: "Enable Sensor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if err := sensor.Enable(args[0], configDir); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(enableSensorCmd)
|
||||
}
|
28
cmd/sensor/list.go
Normal file
28
cmd/sensor/list.go
Normal file
@ -0,0 +1,28 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var quiet bool
|
||||
|
||||
var listSensorCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List Sensors",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if err := sensor.Print(os.Stdout, configDir, quiet); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
//28-01143277168e
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(listSensorCmd)
|
||||
|
||||
listSensorCmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "List only sensor id's")
|
||||
}
|
23
cmd/sensor/rm.go
Normal file
23
cmd/sensor/rm.go
Normal file
@ -0,0 +1,23 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rmSensorCmd = &cobra.Command{
|
||||
Use: "rm",
|
||||
Short: "Remove Sensor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if err := sensor.Remove(args[0], configDir); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(rmSensorCmd)
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"git.cryptic.systems/fh-trier/go-flucky/cmd/sensor/temperature"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -18,6 +17,4 @@ func InitCmd(cmd *cobra.Command, cnf string) {
|
||||
|
||||
cmd.AddCommand(sensorCmd)
|
||||
|
||||
temperature.InitCmd(sensorCmd, cnf)
|
||||
|
||||
}
|
||||
|
@ -1,49 +0,0 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
temperatureSensor "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor/temperature"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var sensorType string
|
||||
|
||||
var addTemperatureSensorCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add Temperature Sensor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
t, err := matchSensorTyp(sensorType)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// DS18B20
|
||||
if t == types.SENSOR_DS18B20 {
|
||||
if err := temperatureSensor.AddDS18B20(args[0], sensorName, configDir, wirePath); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
temperatureSensorCmd.AddCommand(addTemperatureSensorCmd)
|
||||
addTemperatureSensorCmd.Flags().StringVarP(&sensorType, "type", "t", "DS18B20", "Sensor Types: DS18B20, DHT11")
|
||||
}
|
||||
|
||||
func matchSensorTyp(sensorType string) (types.SensorType, error) {
|
||||
|
||||
switch sensorType {
|
||||
case "DS18B20":
|
||||
return types.SENSOR_DS18B20, nil
|
||||
case "DHT11":
|
||||
return types.SENSOR_DHT11, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("Can not match %v with a sensor type", sensorType)
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
temperatureSensor "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor/temperature"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var quiet bool
|
||||
|
||||
var listTemperatureSensorCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List Temperature Sensors",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if err := temperatureSensor.Print(os.Stdout, configDir, quiet); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
temperatureSensorCmd.AddCommand(listTemperatureSensorCmd)
|
||||
|
||||
listTemperatureSensorCmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "List only sensor id's")
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
temperatureSensor "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor/temperature"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var rmTemperatureSensorCmd = &cobra.Command{
|
||||
Use: "rm",
|
||||
Short: "Remove Temperature Sensor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if err := temperatureSensor.Remove(args[0], configDir); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
temperatureSensorCmd.AddCommand(rmTemperatureSensorCmd)
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var configDir, sensorName, wirePath string
|
||||
|
||||
var temperatureSensorCmd = &cobra.Command{
|
||||
Use: "temperature",
|
||||
Short: "Manage Temperature Sensors",
|
||||
}
|
||||
|
||||
// Execute a
|
||||
func InitCmd(cmd *cobra.Command, cnf string) {
|
||||
configDir = cnf
|
||||
|
||||
cmd.AddCommand(temperatureSensorCmd)
|
||||
temperatureSensorCmd.PersistentFlags().StringVarP(&sensorName, "name", "n", "", "Define an name for the sensor")
|
||||
temperatureSensorCmd.PersistentFlags().StringVarP(&wirePath, "wire-path", "w", "/sys/bus/w1/devices", "Default path for wire devices")
|
||||
|
||||
}
|
@ -8,20 +8,28 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var push bool
|
||||
var follow, writeLogfiles, push bool
|
||||
|
||||
var getTemperatureCmd = &cobra.Command{
|
||||
Use: "get",
|
||||
Short: "get temperature from sensor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if err := temperature.Get(os.Stdout, args[0], configDir); err != nil {
|
||||
if follow {
|
||||
if err := temperature.GetFollow(args, writeLogfiles, push, configDir, os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
if err := temperature.Get(args, writeLogfiles, push, configDir, os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
temperatureCmd.AddCommand(getTemperatureCmd)
|
||||
getTemperatureCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow output")
|
||||
getTemperatureCmd.Flags().BoolVarP(&push, "push", "p", false, "Push to remote server")
|
||||
getTemperatureCmd.Flags().BoolVarP(&writeLogfiles, "log", "l", true, "Write to logfiles")
|
||||
}
|
||||
|
@ -1,27 +0,0 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/temperature"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
//var seconds int32
|
||||
|
||||
var logTemperatureCmd = &cobra.Command{
|
||||
Use: "logs",
|
||||
Short: "logs print all temperatures from all sensors",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if err := temperature.Logs(os.Stdout, configDir); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
temperatureCmd.AddCommand(logTemperatureCmd)
|
||||
//logTemperatureCmd.Flags().Int32VarP(&seconds, "seconds", "s", 1, "Interval to print new temperatures from sensors")
|
||||
}
|
23
cmd/temperature/print.go
Normal file
23
cmd/temperature/print.go
Normal file
@ -0,0 +1,23 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var writeLog bool
|
||||
|
||||
var printTemperatureCmd = &cobra.Command{
|
||||
Use: "print",
|
||||
Short: "print all temperatures from all sensors secondly",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// if err := temperature.Print(os.Stdout, configDir, writeLog); err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
temperatureCmd.AddCommand(printTemperatureCmd)
|
||||
printTemperatureCmd.Flags().BoolVarP(&writeLog, "log", "l", true, "Append to logfiles")
|
||||
}
|
1
pkg/logs/logs.go
Normal file
1
pkg/logs/logs.go
Normal file
@ -0,0 +1 @@
|
||||
package logs
|
44
pkg/logs/temperature.go
Normal file
44
pkg/logs/temperature.go
Normal file
@ -0,0 +1,44 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
||||
)
|
||||
|
||||
// Temperature write a given array of temperatures to a logfile
|
||||
func Temperature(temperatures []*stypes.Temperature, config *types.Config) error {
|
||||
|
||||
logPath := filepath.Dir(config.TemperatureLogfile)
|
||||
|
||||
// create log dir if not exist
|
||||
if _, err := os.Stat(logPath); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(logPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not create directory: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(config.TemperatureLogfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
w := bufio.NewWriter(f)
|
||||
|
||||
for _, temperature := range temperatures {
|
||||
fmt.Fprintf(w, "%v\t%v\t%v\t%v\n", temperature.TemperatureID, temperature.TemperatureValue, temperature.TemperatureDate, temperature.SensorID)
|
||||
}
|
||||
|
||||
err = w.Flush()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not flush writer: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -88,14 +88,14 @@ func Disable(remoteName string, configDir string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func List(w io.Writer, configDir string) error {
|
||||
func Print(w io.Writer, configDir string) error {
|
||||
|
||||
configuration, err := config.Read(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
||||
|
||||
fmt.Fprint(tw, "name\taddress\tenabled\tregistered\n")
|
||||
|
||||
|
@ -1,14 +1,194 @@
|
||||
package sensor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
"github.com/satori/go.uuid"
|
||||
|
||||
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
||||
)
|
||||
|
||||
// Exists returns a boolean if the sensor exists
|
||||
func Exists(wirePath, sensorID string) bool {
|
||||
// Add ...
|
||||
func Add(sensorName, sensorLocation, sensorType, wireID, gpioNumber *string, wirePath, configDir string, enabled bool) error {
|
||||
|
||||
// read cnf file
|
||||
cnf, err := config.Read(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s := &types.Sensor{
|
||||
Enabled: enabled,
|
||||
RemoteSensor: &stypes.Sensor{
|
||||
SensorID: uuid.NewV4().String(),
|
||||
SensorName: sensorName,
|
||||
SensorLocation: sensorLocation,
|
||||
GPIONumber: gpioNumber,
|
||||
SensorType: sensorType,
|
||||
DeviceID: cnf.DeviceID,
|
||||
CreationDate: time.Now(),
|
||||
},
|
||||
}
|
||||
|
||||
// If the new sensor is a wire sensor
|
||||
if wireID != nil {
|
||||
|
||||
// check if sensor exists
|
||||
if !exists(wirePath, *wireID) {
|
||||
return fmt.Errorf("Can not find sensor: %v", filepath.Join(wirePath, *wireID))
|
||||
}
|
||||
|
||||
// check if sensor is redundant
|
||||
if isWireIDRedundant(*wireID, cnf.Sensors) {
|
||||
return fmt.Errorf("Sensor %v already exists", *wireID)
|
||||
}
|
||||
|
||||
s.RemoteSensor.WireID = wireID
|
||||
}
|
||||
|
||||
// check if sensor is redundant
|
||||
if isSensorNameRedundant(*sensorName, cnf.Sensors) {
|
||||
return fmt.Errorf("Sensor %v already exists", *sensorName)
|
||||
}
|
||||
|
||||
// append sensor to list
|
||||
cnf.Sensors = append(cnf.Sensors, s)
|
||||
|
||||
// write cnf file
|
||||
if err := config.Write(cnf, configDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Enable a remote link
|
||||
func Enable(sensorName string, configDir string) error {
|
||||
|
||||
cnf, err := config.Read(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// search after duplicate remote_names
|
||||
var found bool
|
||||
for _, sensor := range cnf.Sensors {
|
||||
if *sensor.RemoteSensor.SensorName == sensorName ||
|
||||
sensor.RemoteSensor.SensorID == sensorName {
|
||||
sensor.Enabled = true
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not find sensor %v", sensorName)
|
||||
}
|
||||
|
||||
if err := config.Write(cnf, configDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Disable a remote link
|
||||
func Disable(sensorName string, configDir string) error {
|
||||
|
||||
cnf, err := config.Read(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// search after duplicate remote_names
|
||||
var found bool
|
||||
for _, sensor := range cnf.Sensors {
|
||||
if *sensor.RemoteSensor.SensorName == sensorName ||
|
||||
sensor.RemoteSensor.SensorID == sensorName {
|
||||
sensor.Enabled = false
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("Can not find sensor %v", sensorName)
|
||||
}
|
||||
|
||||
if err := config.Write(cnf, configDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Print a list with the given writer w over all temperature sensors
|
||||
func Print(w io.Writer, configDir string, quiet bool) error {
|
||||
|
||||
// read cnf file
|
||||
cnf, err := config.Read(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// declar tabwriter
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 3, ' ', 0)
|
||||
|
||||
// headline
|
||||
if !quiet {
|
||||
fmt.Fprint(tw, "id\tname\tlocation\ttype\twire-id\tgpio\tenabled\n")
|
||||
}
|
||||
|
||||
for _, sensor := range cnf.Sensors {
|
||||
if quiet {
|
||||
fmt.Fprintf(tw, "%v\n", sensor.RemoteSensor.SensorID)
|
||||
} else {
|
||||
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\t%v\t%v\t%v\n", sensor.RemoteSensor.SensorID, *sensor.RemoteSensor.SensorName, *sensor.RemoteSensor.SensorLocation, *sensor.RemoteSensor.SensorType, *sensor.RemoteSensor.WireID, *sensor.RemoteSensor.GPIONumber, sensor.Enabled)
|
||||
}
|
||||
}
|
||||
|
||||
tw.Flush()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove a sensor
|
||||
func Remove(sensorName, configDir string) error {
|
||||
|
||||
cnf, err := config.Read(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var j int
|
||||
for _, sensor := range cnf.Sensors {
|
||||
if *sensor.RemoteSensor.SensorName == sensorName ||
|
||||
sensor.RemoteSensor.SensorID == sensorName {
|
||||
cnf.Sensors = append(cnf.Sensors[:j], cnf.Sensors[j+1:]...)
|
||||
|
||||
if j > 0 {
|
||||
j = j - 1
|
||||
}
|
||||
continue
|
||||
}
|
||||
j++
|
||||
}
|
||||
|
||||
if err := config.Write(cnf, configDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func exists(wirePath, sensorID string) bool {
|
||||
sensorPath := filepath.Join(wirePath, sensorID)
|
||||
if _, err := os.Stat(sensorPath); os.IsNotExist(err) {
|
||||
return false
|
||||
@ -16,10 +196,21 @@ func Exists(wirePath, sensorID string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// IsRedundant returns a boolean if the sensorID is in the array
|
||||
func IsRedundant(sensorID string, sensors []*types.WireSensor) bool {
|
||||
// isWireIDRedundant returns a boolean if the sensor id redundant
|
||||
func isWireIDRedundant(wireID string, sensors []*types.Sensor) bool {
|
||||
for _, sensor := range sensors {
|
||||
if sensor.ID == sensorID {
|
||||
if *sensor.RemoteSensor.WireID == wireID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// isSensorNameRedundant returns a boolean if the sensor name redundant
|
||||
func isSensorNameRedundant(sensorName string, sensors []*types.Sensor) bool {
|
||||
for _, sensor := range sensors {
|
||||
if *sensor.RemoteSensor.SensorName == sensorName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -1,109 +0,0 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"text/tabwriter"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
||||
)
|
||||
|
||||
// AddDS18B20 a new temperature sensor from type ds18b20
|
||||
func AddDS18B20(sensorID, sensorName, configDir, wirePath string) error {
|
||||
|
||||
// read cnf file
|
||||
cnf, err := config.Read(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// check if sensor exists
|
||||
if !sensor.Exists(wirePath, sensorID) {
|
||||
return fmt.Errorf("Can not find sensor: %v", filepath.Join(wirePath, sensorID))
|
||||
}
|
||||
|
||||
// check if sensor is redundant
|
||||
if sensor.IsRedundant(sensorID, cnf.TemperatureSensors) {
|
||||
return fmt.Errorf("Sensor %v already exists as temperature sensor", sensorID)
|
||||
}
|
||||
|
||||
temperatureSensor := &types.WireSensor{
|
||||
ID: sensorID,
|
||||
Name: sensorName,
|
||||
Typ: types.SENSOR_DS18B20,
|
||||
WirePath: wirePath,
|
||||
}
|
||||
|
||||
// append sensor to list
|
||||
cnf.TemperatureSensors = append(cnf.TemperatureSensors, temperatureSensor)
|
||||
|
||||
// write cnf file
|
||||
if err := config.Write(cnf, configDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Print a list with the given writer w over all temperature sensors
|
||||
func Print(w io.Writer, configDir string, quiet bool) error {
|
||||
|
||||
// read cnf file
|
||||
cnf, err := config.Read(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// declar tabwriter
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
|
||||
|
||||
// headline
|
||||
if !quiet {
|
||||
fmt.Fprint(tw, "id\tname\ttype\tpath\n")
|
||||
}
|
||||
|
||||
for _, sensor := range cnf.TemperatureSensors {
|
||||
if quiet {
|
||||
fmt.Fprintf(tw, "%v\n", sensor.ID)
|
||||
} else {
|
||||
fmt.Fprintf(tw, "%v\t%v\t%v\t%v\n", sensor.ID, sensor.Name, sensor.Typ, sensor.WirePath)
|
||||
}
|
||||
}
|
||||
|
||||
tw.Flush()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove a temperature sensor over the given sensor id
|
||||
func Remove(sensorID, configDir string) error {
|
||||
|
||||
// read cnf file
|
||||
cnf, err := config.Read(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// remove matching sensor ids
|
||||
var j int
|
||||
for _, sensor := range cnf.TemperatureSensors {
|
||||
if sensor.ID == sensorID {
|
||||
cnf.TemperatureSensors = append(cnf.TemperatureSensors[:j], cnf.TemperatureSensors[j+1:]...)
|
||||
if j > 0 {
|
||||
j = j - 1
|
||||
}
|
||||
continue
|
||||
}
|
||||
j++
|
||||
}
|
||||
|
||||
// write cnf file
|
||||
if err := config.Write(cnf, configDir); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -3,15 +3,13 @@ package temperature
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/httpcall"
|
||||
|
||||
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/logs"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||
@ -20,21 +18,9 @@ import (
|
||||
"github.com/yryz/ds18b20"
|
||||
)
|
||||
|
||||
// Get ...
|
||||
func Get(w io.Writer, sensorName string, configDir string) error {
|
||||
var temperatureLog = "temperature.log"
|
||||
|
||||
temperature, err := getTemperature(sensorName, configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "%3.3f\n", temperature.TemperatureValue)
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// Logs ...
|
||||
func Logs(w io.Writer, configDir string) error {
|
||||
func Get(argSensorNames []string, writeLogfiles, push bool, configDir string, w io.Writer) error {
|
||||
|
||||
// get cnf
|
||||
cnf, err := config.Read(configDir)
|
||||
@ -42,103 +28,212 @@ func Logs(w io.Writer, configDir string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// declar tabwriter
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
|
||||
|
||||
// headlines
|
||||
for _, sensor := range cnf.TemperatureSensors {
|
||||
if sensor.Name != "" {
|
||||
fmt.Fprintf(tw, "%v\t", sensor.Name)
|
||||
} else {
|
||||
fmt.Fprintf(tw, "%v\t", sensor.ID)
|
||||
}
|
||||
var sensorNames []string
|
||||
|
||||
// headlines
|
||||
if len(argSensorNames) > 1 {
|
||||
for _, argSensorName := range argSensorNames {
|
||||
if len(argSensorNames) > 1 {
|
||||
fmt.Fprintf(tw, "%v\t", argSensorName)
|
||||
}
|
||||
sensorNames = append(sensorNames, argSensorName)
|
||||
}
|
||||
fmt.Fprint(tw, "\n")
|
||||
} else {
|
||||
numOfSensors := len(cnf.TemperatureSensors)
|
||||
|
||||
// body
|
||||
ticker := time.NewTicker(1 * time.Second)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case _, more := <-ticker.C:
|
||||
if !more {
|
||||
return
|
||||
for _, temperatureSensor := range cnf.TemperatureSensors {
|
||||
switch {
|
||||
case temperatureSensor.Name != "" && numOfSensors <= 1:
|
||||
sensorNames = append(sensorNames, temperatureSensor.Name)
|
||||
case temperatureSensor.Name == "" && numOfSensors <= 1:
|
||||
sensorNames = append(sensorNames, temperatureSensor.ID)
|
||||
case temperatureSensor.Name != "" && numOfSensors > 1:
|
||||
fmt.Fprintf(tw, "%v\t", temperatureSensor.Name)
|
||||
sensorNames = append(sensorNames, temperatureSensor.Name)
|
||||
break
|
||||
case temperatureSensor.Name == "" && numOfSensors > 1:
|
||||
sensorNames = append(sensorNames, temperatureSensor.ID)
|
||||
fmt.Fprintf(tw, "%v\t", temperatureSensor.ID)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for _, sensor := range cnf.TemperatureSensors {
|
||||
temperature, err := ds18b20.Temperature(sensor.ID)
|
||||
if numOfSensors > 1 {
|
||||
fmt.Fprint(tw, "\n")
|
||||
}
|
||||
}
|
||||
|
||||
temperatures, err := getTemperatures(sensorNames, configDir)
|
||||
if err != nil {
|
||||
return
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(tw, "%3.3f°\t", temperature)
|
||||
|
||||
for _, temperature := range temperatures {
|
||||
fmt.Fprintf(tw, "%v\t", temperature.TemperatureValue)
|
||||
}
|
||||
|
||||
fmt.Fprint(tw, "\n")
|
||||
|
||||
tw.Flush()
|
||||
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
signalChannel := make(chan os.Signal)
|
||||
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
sig := <-signalChannel
|
||||
log.Printf("Got signal %s, initiating shutdown\n", sig)
|
||||
ticker.Stop()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Push ...
|
||||
func Push(sensorName, configDir string) error {
|
||||
|
||||
var temperatures []*stypes.Temperature
|
||||
|
||||
temperature, err := getTemperature(sensorName, configDir)
|
||||
if err != nil {
|
||||
if writeLogfiles {
|
||||
if err := logs.Temperature(temperatures, cnf); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
temperatures = append(temperatures, &temperature)
|
||||
|
||||
if push {
|
||||
if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func getTemperature(sensorName, configDir string) (stypes.Temperature, error) {
|
||||
func GetFollow(sensorNames []string, writeLogfiles, push bool, configDir string, w io.Writer) error {
|
||||
|
||||
// // get cnf
|
||||
// cnf, err := config.Read(configDir)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// var temperatures []*stypes.Temperature
|
||||
|
||||
// // tabwriter
|
||||
// tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
|
||||
|
||||
// // headlines
|
||||
// var sensorNames []string
|
||||
// cnt := len(cnf.TemperatureSensors)
|
||||
// for _, sensor := range cnf.TemperatureSensors {
|
||||
|
||||
// switch {
|
||||
// case sensor.Name == "" && cnt == 1:
|
||||
// sensorNames = append(sensorNames, sensor.ID)
|
||||
// break
|
||||
|
||||
// case sensor.Name != "" && cnt == 1:
|
||||
// sensorNames = append(sensorNames, sensor.Name)
|
||||
// break
|
||||
|
||||
// case sensor.Name == "" && cnt > 1:
|
||||
// sensorNames = append(sensorNames, sensor.ID)
|
||||
// fmt.Fprintf(tw, "%v\t", sensor.ID)
|
||||
// break
|
||||
|
||||
// case sensor.Name != "" && cnt > 1:
|
||||
// sensorNames = append(sensorNames, sensor.Name)
|
||||
// fmt.Fprintf(tw, "%v\t", sensor.Name)
|
||||
// break
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
// if cnt > 1 {
|
||||
// fmt.Fprint(tw, "\n")
|
||||
// }
|
||||
|
||||
// // body
|
||||
// ticker := time.NewTicker(1 * time.Second)
|
||||
|
||||
// go func() {
|
||||
// for {
|
||||
// select {
|
||||
// case _, more := <-ticker.C:
|
||||
// if !more {
|
||||
// return
|
||||
// }
|
||||
|
||||
// // get temperatures from sensors and write them into writer
|
||||
// temperatures, err = getTemperatures(sensorNames, configDir)
|
||||
// for _, temperature := range temperatures {
|
||||
// fmt.Fprintf(tw, "%v\t", temperature.TemperatureValue)
|
||||
// }
|
||||
|
||||
// fmt.Fprint(tw, "\n")
|
||||
|
||||
// // flush writer
|
||||
// tw.Flush()
|
||||
|
||||
// }
|
||||
// }
|
||||
// }()
|
||||
|
||||
// signalChannel := make(chan os.Signal)
|
||||
// signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// sig := <-signalChannel
|
||||
// fmt.Printf("Got signal %s, initiating shutdown\n", sig)
|
||||
// ticker.Stop()
|
||||
|
||||
// // write logfiles
|
||||
// if writeLogfiles {
|
||||
// if err := logs.Temperature(temperatures, cnf); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Push(sensorName, configDir string) error {
|
||||
|
||||
// var temperatures []*stypes.Temperature
|
||||
|
||||
// temperature, err := getTemperature(sensorName, configDir)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// temperatures = append(temperatures, &temperature)
|
||||
|
||||
// if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func getTemperatures(sensorNames []string, configDir string) ([]*stypes.Temperature, error) {
|
||||
|
||||
temperatures := []*stypes.Temperature{}
|
||||
|
||||
// read cnf
|
||||
cnf, err := config.Read(configDir)
|
||||
if err != nil {
|
||||
return stypes.Temperature{}, err
|
||||
return []*stypes.Temperature{}, err
|
||||
}
|
||||
|
||||
var foundSensor types.WireSensor
|
||||
// search after sensor
|
||||
var foundSensors []*types.WireSensor
|
||||
var sensorFound bool
|
||||
for _, sensor := range cnf.TemperatureSensors {
|
||||
if sensor.ID == sensorName || sensor.Name == sensorName {
|
||||
foundSensor = *sensor
|
||||
for _, configSensor := range cnf.TemperatureSensors {
|
||||
for _, sensorName := range sensorNames {
|
||||
if configSensor.ID == sensorName || configSensor.Name == sensorName {
|
||||
foundSensors = append(foundSensors, configSensor)
|
||||
sensorFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !sensorFound {
|
||||
return stypes.Temperature{}, fmt.Errorf("Sensor not found sensor %v in config", sensorName)
|
||||
return []*stypes.Temperature{}, fmt.Errorf("Sensor not found any sensor in the list")
|
||||
}
|
||||
|
||||
for _, foundSensor := range foundSensors {
|
||||
t, err := ds18b20.Temperature(foundSensor.ID)
|
||||
if err != nil {
|
||||
return stypes.Temperature{}, fmt.Errorf("Can not read temperature from sensor %v: %v", foundSensor.ID, err)
|
||||
return []*stypes.Temperature{}, fmt.Errorf("Can not read temperature from sensor %v: %v", foundSensor.ID, err)
|
||||
}
|
||||
|
||||
temperature := stypes.Temperature{
|
||||
temperature := &stypes.Temperature{
|
||||
TemperatureID: uuid.NewV4().String(),
|
||||
TemperatureValue: t,
|
||||
SensorID: foundSensor.ID,
|
||||
@ -146,5 +241,8 @@ func getTemperature(sensorName, configDir string) (stypes.Temperature, error) {
|
||||
DeviceID: cnf.DeviceID,
|
||||
}
|
||||
|
||||
return temperature, nil
|
||||
temperatures = append(temperatures, temperature)
|
||||
}
|
||||
|
||||
return temperatures, nil
|
||||
}
|
||||
|
@ -5,12 +5,18 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DeviceID string `json:"device_id"`
|
||||
DeviceName string `json:"device_name"`
|
||||
DeviceLocation string `json:"device_location"`
|
||||
TemperatureLogfile string `json:"temperature_logfile"`
|
||||
HumidityLogfile string `json:"humiditiy_logfile"`
|
||||
Remotes []*Remote `json:"remotes"`
|
||||
TemperatureSensors []*WireSensor `json:"temperature_sensors"`
|
||||
Sensors []*Sensor `json:"sensors"`
|
||||
}
|
||||
|
||||
func (c *Config) ToJSON() (string, error) {
|
||||
@ -43,23 +49,15 @@ func (c *Config) JSONDecoder(r io.Reader) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remote ...
|
||||
type Remote struct {
|
||||
Name string `json:"remote_name"`
|
||||
Address string `json:"remote_address"`
|
||||
Registered bool `json:"remote_registered"`
|
||||
Enabled bool `json:"remote_enabled"`
|
||||
}
|
||||
|
||||
type WireSensor struct {
|
||||
ID string `json:"sensor_id"`
|
||||
Name string `json:"sensor_name"`
|
||||
Typ SensorType `json:"sensor_typ"`
|
||||
GPIO int `json:"gpio_number"`
|
||||
WirePath string `json:"wire_path"`
|
||||
type Sensor struct {
|
||||
RemoteSensor *stypes.Sensor `json:"sensor"`
|
||||
Enabled bool `json:"sensor_enabled"`
|
||||
}
|
||||
|
||||
type SensorType string
|
||||
|
||||
const (
|
||||
SENSOR_DS18B20 SensorType = "DS18B20"
|
||||
SENSOR_DHT11 = "DHT11"
|
||||
)
|
||||
|
@ -29,4 +29,10 @@ Type=simple
|
||||
# %P Unescaped prefix name Same as "%p", but with escaping undone.
|
||||
# %u User name This is the name of the user running the service manager instance. In case of the system manager this resolves to "root".
|
||||
# %U User UID This is the numeric UID of the user running the service manager instance. In case of the system manager this resolves to "0".
|
||||
ExecStart=/usr/bin/flucky temperature push %i
|
||||
|
||||
ExecStart=/usr/local/bin/flucky temperature push %i
|
||||
|
||||
Environment=USER=root
|
||||
|
||||
User=root
|
||||
Group=root
|
||||
|
@ -16,10 +16,12 @@ type TypeObject interface {
|
||||
// Device ...
|
||||
type Device struct {
|
||||
DeviceID string `json:"device_id"`
|
||||
DeviceName *string `json:"device_name"`
|
||||
DeviceLocation *string `json:"device_location"`
|
||||
CreationDate time.Time `json:"creation_date"`
|
||||
}
|
||||
|
||||
// EncodeToJson needs a writer to write the struct into json string
|
||||
// EncodeToJSON needs a writer to write the struct into json string
|
||||
func (d *Device) EncodeToJSON(w io.Writer) error {
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", " ")
|
||||
@ -30,7 +32,7 @@ func (d *Device) EncodeToJSON(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodeFromJson decode a json string from a reader into a struct
|
||||
// DecodeFromJSON decode a json string from a reader into a struct
|
||||
func (d *Device) DecodeFromJSON(r io.Reader) error {
|
||||
decoder := json.NewDecoder(r)
|
||||
if err := decoder.Decode(&d); err != nil {
|
||||
@ -42,14 +44,13 @@ func (d *Device) DecodeFromJSON(r io.Reader) error {
|
||||
// Humidity ...
|
||||
type Humidity struct {
|
||||
HumidityID string `json:"humidity_id"`
|
||||
HumidityValue json.Number `json:"humidity_value"`
|
||||
HumidityValue float64 `json:"humidity_value,string"`
|
||||
HumidityDate time.Time `json:"humidity_date"`
|
||||
SensorID string `json:"sensor_id"`
|
||||
DeviceID string `json:"device_id"`
|
||||
CreationDate time.Time `json:"creation_date"`
|
||||
}
|
||||
|
||||
// EncodeToJson needs a writer to write the struct into json string
|
||||
// EncodeToJSON needs a writer to write the struct into json string
|
||||
func (h *Humidity) EncodeToJSON(w io.Writer) error {
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", " ")
|
||||
@ -60,7 +61,7 @@ func (h *Humidity) EncodeToJSON(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodeFromJson decode a json string from a reader into a struct
|
||||
// DecodeFromJSON decode a json string from a reader into a struct
|
||||
func (h *Humidity) DecodeFromJSON(r io.Reader) error {
|
||||
decoder := json.NewDecoder(r)
|
||||
if err := decoder.Decode(&h); err != nil {
|
||||
@ -69,17 +70,48 @@ func (h *Humidity) DecodeFromJSON(r io.Reader) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Sensor ...
|
||||
type Sensor struct {
|
||||
SensorID string `json:"sensor_id"`
|
||||
SensorName *string `json:"sensor_name"`
|
||||
SensorLocation *string `json:"sensor_location"`
|
||||
WireID *string `json:"wire_id"`
|
||||
GPIONumber *string `json:"gpio_number"`
|
||||
SensorType *string `json:"sensor_type"`
|
||||
DeviceID string `json:"device_id"`
|
||||
CreationDate time.Time `json:"creation_date"`
|
||||
}
|
||||
|
||||
// EncodeToJSON needs a writer to write the struct into json string
|
||||
func (s *Sensor) EncodeToJSON(w io.Writer) error {
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", " ")
|
||||
err := encoder.Encode(&s)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error in encoding struct to json: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodeFromJSON decode a json string from a reader into a struct
|
||||
func (s *Sensor) DecodeFromJSON(r io.Reader) error {
|
||||
decoder := json.NewDecoder(r)
|
||||
if err := decoder.Decode(&s); err != nil {
|
||||
return fmt.Errorf("Can not unmarshal JSON: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Temperature ...
|
||||
type Temperature struct {
|
||||
TemperatureID string `json:"temperature_id"`
|
||||
TemperatureValue float64 `json:"temperature_value,string"`
|
||||
TemperatureDate time.Time `json:"temperature_date"`
|
||||
SensorID string `json:"sensor_id"`
|
||||
DeviceID string `json:"device_id"`
|
||||
CreationDate time.Time `json:"creation_date"`
|
||||
}
|
||||
|
||||
// EncodeToJson needs a writer to write the struct into json string
|
||||
// EncodeToJSON needs a writer to write the struct into json string
|
||||
func (t *Temperature) EncodeToJSON(w io.Writer) error {
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", " ")
|
||||
@ -90,7 +122,7 @@ func (t *Temperature) EncodeToJSON(w io.Writer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodeFromJson decode a json string from a reader into a struct
|
||||
// DecodeFromJSON decode a json string from a reader into a struct
|
||||
func (t *Temperature) DecodeFromJSON(r io.Reader) error {
|
||||
decoder := json.NewDecoder(r)
|
||||
if err := decoder.Decode(&t); err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user