feat: implementation of dht11 sensor
This commit is contained in:
		
							
								
								
									
										149
									
								
								pkg/types/gpio.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										149
									
								
								pkg/types/gpio.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,149 @@ | ||||
| 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 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,7 +1,9 @@ | ||||
| package types | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| @@ -11,7 +13,7 @@ type Sensor struct { | ||||
| 	SensorName        string      `json:"sensor_name"` | ||||
| 	SensorLocation    string      `json:"sensor_location"` | ||||
| 	WireID            *string     `json:"wire_id"` | ||||
| 	GPIONumber        *string     `json:"gpio_number"` | ||||
| 	GPIONumber        *GPIO       `json:"gpio_number"` | ||||
| 	SensorModel       SensorModel `json:"sensor_model"` | ||||
| 	SensorEnabled     bool        `json:"sensor_enabled"` | ||||
| 	SensorLastContact time.Time   `json:"sensor_last_contact"` | ||||
| @@ -19,6 +21,26 @@ type Sensor struct { | ||||
| 	CreationDate      time.Time   `json:"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) Name() string { | ||||
| 	if s.SensorName != "" { | ||||
| 		return s.SensorName | ||||
| @@ -27,25 +49,3 @@ func (s *Sensor) Name() string { | ||||
| 	} | ||||
| 	return s.SensorID | ||||
| } | ||||
|  | ||||
| type SensorModel string | ||||
|  | ||||
| const ( | ||||
| 	DHT11   SensorModel = "DHT11" | ||||
| 	DHT22               = "DHT22" | ||||
| 	DS18B20             = "DS18B20" | ||||
| ) | ||||
|  | ||||
| // SelectSensorModel converts a string into a constant | ||||
| func SelectSensorModel(model string) (SensorModel, error) { | ||||
| 	switch model { | ||||
| 	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) | ||||
| 	} | ||||
| } | ||||
|   | ||||
							
								
								
									
										25
									
								
								pkg/types/sensor_model.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								pkg/types/sensor_model.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| package types | ||||
|  | ||||
| import "fmt" | ||||
|  | ||||
| type SensorModel string | ||||
|  | ||||
| const ( | ||||
| 	DHT11   SensorModel = "DHT11" | ||||
| 	DHT22               = "DHT22" | ||||
| 	DS18B20             = "DS18B20" | ||||
| ) | ||||
|  | ||||
| // SelectSensorModel converts a string into a constant | ||||
| func SelectSensorModel(model string) (SensorModel, error) { | ||||
| 	switch model { | ||||
| 	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