fix: new implementation
changes: - Remove cli Some cli commands are not complete tested and are deprecated. - Daemon - Old version has a very bad implementation of how to verify, if the device or the sensors are in the database insert. The current implementation can be improved but this one is betten then the old one. - Remove complete the cache store implementation. Use a normal array and query the length and capacity to determine how the array cache must be cleaned. - Type Remove unused types and functions
This commit is contained in:
@ -2,7 +2,7 @@ package types
|
||||
|
||||
import "time"
|
||||
|
||||
// Device ...
|
||||
// Device represent a device with all his settings.
|
||||
type Device struct {
|
||||
ID string `json:"id" xml:"id"`
|
||||
Name string `json:"name" xml:"name"`
|
||||
|
@ -1,206 +0,0 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type GPIO string
|
||||
|
||||
const (
|
||||
GPIO02 GPIO = "GPIO02"
|
||||
GPIO03 = "GPIO03"
|
||||
GPIO04 = "GPIO04"
|
||||
GPIO05 = "GPIO05"
|
||||
GPIO06 = "GPIO06"
|
||||
GPIO07 = "GPIO07"
|
||||
GPIO08 = "GPIO08"
|
||||
GPIO10 = "GPIO10"
|
||||
GPIO11 = "GPIO11"
|
||||
GPIO12 = "GPIO12"
|
||||
GPIO13 = "GPIO13"
|
||||
GPIO14 = "GPIO14"
|
||||
GPIO15 = "GPIO15"
|
||||
GPIO16 = "GPIO16"
|
||||
GPIO17 = "GPIO17"
|
||||
GPIO18 = "GPIO18"
|
||||
GPIO19 = "GPIO19"
|
||||
GPIO20 = "GPIO20"
|
||||
GPIO21 = "GPIO21"
|
||||
GPIO22 = "GPIO22"
|
||||
GPIO23 = "GPIO23"
|
||||
GPIO24 = "GPIO24"
|
||||
GPIO25 = "GPIO25"
|
||||
GPIO26 = "GPIO26"
|
||||
GPIO27 = "GPIO27"
|
||||
)
|
||||
|
||||
func GPIOToString(gpio GPIO) (string, error) {
|
||||
switch gpio {
|
||||
case GPIO02:
|
||||
return "GPIO02", nil
|
||||
case GPIO03:
|
||||
return "GPIO03", nil
|
||||
case GPIO04:
|
||||
return "GPIO04", nil
|
||||
case GPIO05:
|
||||
return "GPIO05", nil
|
||||
case GPIO06:
|
||||
return "GPIO06", nil
|
||||
case GPIO07:
|
||||
return "GPIO07", nil
|
||||
case GPIO08:
|
||||
return "GPIO08", nil
|
||||
case GPIO10:
|
||||
return "GPIO10", nil
|
||||
case GPIO11:
|
||||
return "GPIO11", nil
|
||||
case GPIO12:
|
||||
return "GPIO12", nil
|
||||
case GPIO13:
|
||||
return "GPIO13", nil
|
||||
case GPIO14:
|
||||
return "GPIO14", nil
|
||||
case GPIO15:
|
||||
return "GPIO15", nil
|
||||
case GPIO16:
|
||||
return "GPIO16", nil
|
||||
case GPIO17:
|
||||
return "GPIO17", nil
|
||||
case GPIO18:
|
||||
return "GPIO18", nil
|
||||
case GPIO19:
|
||||
return "GPIO19", nil
|
||||
case GPIO20:
|
||||
return "GPIO20", nil
|
||||
case GPIO21:
|
||||
return "GPIO21", nil
|
||||
case GPIO22:
|
||||
return "GPIO22", nil
|
||||
case GPIO23:
|
||||
return "GPIO23", nil
|
||||
case GPIO24:
|
||||
return "GPIO24", nil
|
||||
case GPIO25:
|
||||
return "GPIO25", nil
|
||||
case GPIO26:
|
||||
return "GPIO26", nil
|
||||
case GPIO27:
|
||||
return "GPIO27", nil
|
||||
default:
|
||||
return "", fmt.Errorf("Can not determine gpio %v", gpio)
|
||||
}
|
||||
}
|
||||
|
||||
func GPIOToInt(gpio GPIO) (int, error) {
|
||||
switch gpio {
|
||||
case GPIO02:
|
||||
return 2, nil
|
||||
case GPIO03:
|
||||
return 3, nil
|
||||
case GPIO04:
|
||||
return 4, nil
|
||||
case GPIO05:
|
||||
return 5, nil
|
||||
case GPIO06:
|
||||
return 6, nil
|
||||
case GPIO07:
|
||||
return 7, nil
|
||||
case GPIO08:
|
||||
return 8, nil
|
||||
case GPIO10:
|
||||
return 10, nil
|
||||
case GPIO11:
|
||||
return 11, nil
|
||||
case GPIO12:
|
||||
return 12, nil
|
||||
case GPIO13:
|
||||
return 13, nil
|
||||
case GPIO14:
|
||||
return 14, nil
|
||||
case GPIO15:
|
||||
return 15, nil
|
||||
case GPIO16:
|
||||
return 16, nil
|
||||
case GPIO17:
|
||||
return 17, nil
|
||||
case GPIO18:
|
||||
return 18, nil
|
||||
case GPIO19:
|
||||
return 19, nil
|
||||
case GPIO20:
|
||||
return 20, nil
|
||||
case GPIO21:
|
||||
return 21, nil
|
||||
case GPIO22:
|
||||
return 22, nil
|
||||
case GPIO23:
|
||||
return 23, nil
|
||||
case GPIO24:
|
||||
return 24, nil
|
||||
case GPIO25:
|
||||
return 25, nil
|
||||
case GPIO26:
|
||||
return 26, nil
|
||||
case GPIO27:
|
||||
return 27, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("Can not determine gpio %v", gpio)
|
||||
}
|
||||
}
|
||||
|
||||
func StringToGPIO(gpio string) (GPIO, error) {
|
||||
switch gpio {
|
||||
case "GPIO02":
|
||||
return GPIO02, nil
|
||||
case "GPIO03":
|
||||
return GPIO03, nil
|
||||
case "GPIO04":
|
||||
return GPIO04, nil
|
||||
case "GPIO05":
|
||||
return GPIO05, nil
|
||||
case "GPIO06":
|
||||
return GPIO06, nil
|
||||
case "GPIO07":
|
||||
return GPIO07, nil
|
||||
case "GPIO08":
|
||||
return GPIO08, nil
|
||||
case "GPIO10":
|
||||
return GPIO10, nil
|
||||
case "GPIO11":
|
||||
return GPIO11, nil
|
||||
case "GPIO12":
|
||||
return GPIO12, nil
|
||||
case "GPIO13":
|
||||
return GPIO13, nil
|
||||
case "GPIO14":
|
||||
return GPIO14, nil
|
||||
case "GPIO15":
|
||||
return GPIO15, nil
|
||||
case "GPIO16":
|
||||
return GPIO16, nil
|
||||
case "GPIO17":
|
||||
return GPIO17, nil
|
||||
case "GPIO18":
|
||||
return GPIO18, nil
|
||||
case "GPIO19":
|
||||
return GPIO19, nil
|
||||
case "GPIO20":
|
||||
return GPIO20, nil
|
||||
case "GPIO21":
|
||||
return GPIO21, nil
|
||||
case "GPIO22":
|
||||
return GPIO22, nil
|
||||
case "GPIO23":
|
||||
return GPIO23, nil
|
||||
case "GPIO24":
|
||||
return GPIO24, nil
|
||||
case "GPIO25":
|
||||
return GPIO25, nil
|
||||
case "GPIO26":
|
||||
return GPIO26, nil
|
||||
case "GPIO27":
|
||||
return GPIO27, nil
|
||||
default:
|
||||
return "", fmt.Errorf("Can not determine gpio %v", gpio)
|
||||
}
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RGBLED struct {
|
||||
RGBLEDID string `json:"rgbled_id" xml:"rgbled_id"`
|
||||
RGBLEDName string `json:"rgbled_name" xml:"rgbled_name"`
|
||||
RGBLEDLocation string `json:"rgbled_location" xml:"rgb_location"`
|
||||
BaseColorsToGPIO map[BaseColor]*GPIO `json:"base_colors_to_gpio" xml:"base_colors_to_gpio"`
|
||||
ActionMapping map[LEDAction]LEDColor `json:"action_mapping" xml:"action_mapping"`
|
||||
RGBLEDEnabled bool `json:"rgbled_enabled" xml:"rgb_enabled"`
|
||||
DeviceID string `json:"device_id" xml:"device_id"`
|
||||
CreationDate time.Time `json:"creation_date" xml:"creation_date"`
|
||||
}
|
||||
|
||||
type BaseColor string
|
||||
|
||||
const (
|
||||
BaseColorBlue BaseColor = "blue"
|
||||
BaseColorRed BaseColor = "red"
|
||||
BaseColorGreen BaseColor = "green"
|
||||
)
|
||||
|
||||
type LEDColor string
|
||||
|
||||
const (
|
||||
LEDColorBlue LEDColor = "blue"
|
||||
LEDColorGreen LEDColor = "green"
|
||||
LEDColorNone LEDColor = "none"
|
||||
LEDColorPurple LEDColor = "purple"
|
||||
LEDColorRed LEDColor = "red"
|
||||
LEDColorTurquoise LEDColor = "turquoise"
|
||||
LEDColorYellow LEDColor = "yellow"
|
||||
LEDColorWhite LEDColor = "white"
|
||||
)
|
||||
|
||||
func StringToLEDColor(color string) (LEDColor, error) {
|
||||
switch color {
|
||||
case "blue":
|
||||
return LEDColorBlue, nil
|
||||
case "red":
|
||||
return LEDColorRed, nil
|
||||
case "green":
|
||||
return LEDColorGreen, nil
|
||||
case "none":
|
||||
return LEDColorNone, nil
|
||||
case "purple":
|
||||
return LEDColorPurple, nil
|
||||
case "turquoise":
|
||||
return LEDColorTurquoise, nil
|
||||
case "yellow":
|
||||
return LEDColorYellow, nil
|
||||
case "white":
|
||||
return LEDColorWhite, nil
|
||||
default:
|
||||
return LEDColorNone, fmt.Errorf("Can not convert color to const")
|
||||
}
|
||||
}
|
||||
|
||||
type LEDAction string
|
||||
|
||||
const (
|
||||
LEDActionError LEDAction = "error"
|
||||
LEDActionWarn = "warn"
|
||||
LEDActionRun = "run"
|
||||
LEDActionSync = "sync"
|
||||
LEDActionLogfile = "logfile"
|
||||
)
|
||||
|
||||
var DefaultActionMapping = map[LEDAction]LEDColor{
|
||||
LEDActionError: LEDColorRed,
|
||||
LEDActionWarn: LEDColorYellow,
|
||||
LEDActionRun: LEDColorGreen,
|
||||
LEDActionSync: LEDColorTurquoise,
|
||||
LEDActionLogfile: LEDColorBlue,
|
||||
}
|
@ -1,51 +1,19 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// MeasuredValue represent a value provided by a measuring instrument. For
|
||||
// example from a sensor. It can contains different types, for example humidity
|
||||
// or temperature.
|
||||
type MeasuredValue struct {
|
||||
ID string `json:"id" xml:"id"`
|
||||
Value float64 `json:"value,string" xml:"value,string"`
|
||||
ValueType MeasuredValueType `json:"value_type" xml:"value_type"`
|
||||
FromDate time.Time `json:"from_date" xml:"from_date"`
|
||||
TillDate time.Time `json:"till_date" xml:"till_date"`
|
||||
SensorID string `json:"sensor_id" xml:"sensor_id"`
|
||||
CreationDate *time.Time `json:"creation_date" xml:"creation_date"`
|
||||
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
|
||||
}
|
||||
|
||||
type MeasuredValueType string
|
||||
|
||||
const (
|
||||
MeasuredValueTypeHumidity MeasuredValueType = "humidity"
|
||||
MeasuredValueTypePressure MeasuredValueType = "pressure"
|
||||
MeasuredValueTypeTemperature MeasuredValueType = "temperature"
|
||||
)
|
||||
|
||||
var MeasuredValueTypes = []MeasuredValueType{
|
||||
MeasuredValueTypeHumidity,
|
||||
MeasuredValueTypePressure,
|
||||
MeasuredValueTypeTemperature,
|
||||
}
|
||||
|
||||
func SelectMeasuredValues(measuredValueType MeasuredValueType, measuredValues []*MeasuredValue) []*MeasuredValue {
|
||||
cachedMeasuredValues := make([]*MeasuredValue, 0)
|
||||
for _, measuredValue := range measuredValues {
|
||||
if measuredValue.ValueType == measuredValueType {
|
||||
cachedMeasuredValues = append(cachedMeasuredValues, measuredValue)
|
||||
}
|
||||
}
|
||||
return cachedMeasuredValues
|
||||
}
|
||||
|
||||
func SelectMeasuredValueType(valueType string) (*MeasuredValueType, error) {
|
||||
for _, measuredValueType := range MeasuredValueTypes {
|
||||
if fmt.Sprint(measuredValueType) == valueType {
|
||||
return &measuredValueType, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Can not determine value type: %v", valueType)
|
||||
ID string `json:"id" xml:"id"`
|
||||
Value float64 `json:"value,string" xml:"value,string"`
|
||||
ValueType string `json:"value_type" xml:"value_type"`
|
||||
FromDate time.Time `json:"from_date" xml:"from_date"`
|
||||
TillDate time.Time `json:"till_date" xml:"till_date"`
|
||||
SensorID string `json:"sensor_id" xml:"sensor_id"`
|
||||
CreationDate *time.Time `json:"creation_date" xml:"creation_date"`
|
||||
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
|
||||
}
|
||||
|
@ -1,57 +1,48 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Sensor ...
|
||||
// Sensor represents a sensor with all his settings. The struct does not
|
||||
// contains any read method.
|
||||
type Sensor struct {
|
||||
ID string `json:"id" xml:"id"`
|
||||
Name string `json:"name" xml:"name"`
|
||||
Location string `json:"location" xml:"location"`
|
||||
WireID *string `json:"wire_id" xml:"wire_id"`
|
||||
I2CBus *int `json:"i2c_bus" xml:"i2c_bus"`
|
||||
I2CAddress *uint8 `json:"i2c_address" xml:"i2c_address"`
|
||||
GPIONumber *GPIO `json:"gpio_number" xml:"gpio_number"`
|
||||
Model SensorModel `json:"model" xml:"model"`
|
||||
Enabled bool `json:"enabled" xml:"enabled"`
|
||||
LastContact *time.Time `json:"last_contact" xml:"last_contact"`
|
||||
TickDuration string `json:"tick_duration" xml:"tick_duration"`
|
||||
DeviceID string `json:"device_id" xml:"device_id"`
|
||||
CreationDate time.Time `json:"creation_date" xml:"creation_date"`
|
||||
ID string `json:"id" xml:"id"`
|
||||
Name string `json:"name" xml:"name"`
|
||||
Location string `json:"location" xml:"location"`
|
||||
WireID *string `json:"wire_id" xml:"wire_id"`
|
||||
I2CBus *int `json:"i2c_bus" xml:"i2c_bus"`
|
||||
I2CAddress *uint8 `json:"i2c_address" xml:"i2c_address"`
|
||||
GPIONumber string `json:"gpio_number" xml:"gpio_number"`
|
||||
Model string `json:"model" xml:"model"`
|
||||
Enabled bool `json:"enabled" xml:"enabled"`
|
||||
LastContact *time.Time `json:"last_contact" xml:"last_contact"`
|
||||
TickDuration string `json:"tick_duration" xml:"tick_duration"`
|
||||
DeviceID string `json:"device_id" xml:"device_id"`
|
||||
CreationDate time.Time `json:"creation_date" xml:"creation_date"`
|
||||
}
|
||||
|
||||
// JSONDecoder decodes a json into a sensor
|
||||
func (s *Sensor) JSONDecoder(r io.Reader) error {
|
||||
decoder := json.NewDecoder(r)
|
||||
err := decoder.Decode(s)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not decode sensor from json: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Sensor) JSONEncoder(w io.Writer) error {
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", " ")
|
||||
err := encoder.Encode(s)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not encode sensor to json: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Sensor) FullName() string {
|
||||
if s.Name != "" {
|
||||
return s.Name
|
||||
} else if s.WireID != nil {
|
||||
return *s.WireID
|
||||
} else if s.I2CAddress != nil &&
|
||||
s.I2CBus != nil {
|
||||
return fmt.Sprintf("%v/%v", *s.I2CBus, *s.I2CAddress)
|
||||
}
|
||||
// GetID returns the UUID of the sensor.
|
||||
func (s *Sensor) GetID() string {
|
||||
return s.ID
|
||||
}
|
||||
|
||||
// GetDeviceID returns the UUID of the configured device.
|
||||
func (s *Sensor) GetDeviceID() string {
|
||||
return s.DeviceID
|
||||
}
|
||||
|
||||
// GetName returns the name of the sensor.
|
||||
func (s *Sensor) GetName() string {
|
||||
return s.Name
|
||||
}
|
||||
|
||||
// GetTicker returns a new ticker, which tick every when the sensor should be
|
||||
// read
|
||||
func (s *Sensor) GetTicker() *time.Ticker {
|
||||
duration, err := time.ParseDuration(s.TickDuration)
|
||||
if err != nil {
|
||||
duration = time.Minute
|
||||
}
|
||||
return time.NewTicker(duration)
|
||||
}
|
||||
|
@ -1,28 +0,0 @@
|
||||
package types
|
||||
|
||||
import "fmt"
|
||||
|
||||
type SensorModel string
|
||||
|
||||
const (
|
||||
BME280 SensorModel = "BME280"
|
||||
DHT11 = "DHT11"
|
||||
DHT22 = "DHT22"
|
||||
DS18B20 = "DS18B20"
|
||||
)
|
||||
|
||||
// SelectSensorModel converts a string into a constant
|
||||
func SelectSensorModel(model string) (SensorModel, error) {
|
||||
switch model {
|
||||
case "BME280":
|
||||
return BME280, nil
|
||||
case "DHT11":
|
||||
return DHT11, nil
|
||||
case "DHT22":
|
||||
return DHT22, nil
|
||||
case "DS18B20":
|
||||
return DS18B20, nil
|
||||
default:
|
||||
return "", fmt.Errorf("Sensor Model %v currently not supported", model)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user