Kaynağa Gözat

Added utility to poll temperature from external device.

Spencer Gardner 7 yıl önce
ebeveyn
işleme
d04e304bca
6 değiştirilmiş dosya ile 128 ekleme ve 35 silme
  1. 27 0
      array/array_flags.go
  2. 6 3
      json.go
  3. 80 0
      json_get/json_get.go
  4. 10 2
      measurement.go
  5. 2 24
      temperature_logger/log.go
  6. 3 6
      writer.go

+ 27 - 0
array/array_flags.go

@@ -0,0 +1,27 @@
+package array
+
+import (
+	"encoding/json"
+	"errors"
+	"strconv"
+	"strings"
+)
+
+type ArrayFlags map[int]string
+
+func (i ArrayFlags) String() string {
+	text, _ := json.Marshal(i)
+	return string(text)
+}
+
+func (i ArrayFlags) Set(value string) error {
+
+	values := strings.Split(value, ":")
+	if len(values) != 2 {
+		return errors.New("Invalid location")
+	}
+	id, _ := strconv.ParseInt(values[0], 10, 32)
+
+	i[int(id)] = values[1]
+	return nil
+}

+ 6 - 3
json.go

@@ -51,10 +51,13 @@ func (jp jsonParser) Measure() (*Measurement, error) {
 			return nil, err
 		}
 		if len(jsonPacket.Model) > 0 {
-			batteryGood := strings.Compare(jsonPacket.Battery, "OK") == 0
-			fmt.Printf("Model: %s, ID:%d, Temperature:%f, Battery good: %t\n", jsonPacket.Model, jsonPacket.ID, jsonPacket.Temperature_C, batteryGood)
+			batteryState := BATT_BAD
+			if strings.Compare(jsonPacket.Battery, "OK") == 0 {
+				batteryState = BATT_GOOD
+			}
+			fmt.Printf("Model: %s, ID:%d, Temperature:%f, Battery good: %t\n", jsonPacket.Model, jsonPacket.ID, jsonPacket.Temperature_C, batteryState)
 
-			return &Measurement{ID: jsonPacket.ID, Temperature: jsonPacket.Temperature_C, BatteryGood: batteryGood}, nil
+			return &Measurement{ID: jsonPacket.ID, Temperature: jsonPacket.Temperature_C, BatteryState: batteryState}, nil
 		}
 	}
 	return nil, nil

+ 80 - 0
json_get/json_get.go

@@ -0,0 +1,80 @@
+package main
+
+import (
+	"encoding/json"
+	"flag"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"strings"
+	"time"
+
+	temperature "git.snppla.net/snppla/log_temperature"
+)
+
+var host *string
+var db *string
+var username *string
+var password *string
+var dbHost *string
+var location *string
+var seconds *int
+
+type jsonPacket struct {
+	Temperature_C float64
+	Temperature_F float64
+}
+
+func main() {
+	host = flag.String("host", "http://localhost:80", "Http address (http://localhost:80)")
+	db = flag.String("db", "temperature", "Influxdb database")
+	username = flag.String("user", "", "Username")
+	password = flag.String("password", "", "Password")
+	dbHost = flag.String("dbHost", "http://localhost:8086", "Host to connect to")
+	location = flag.String("location", "unknown", "Location of the device")
+	seconds = flag.Int("delay", 1, "Number of seconds to delay between pollings")
+	flag.Parse()
+	locations := map[int]string{0: *location}
+	writer, err := temperature.NewInfluxWriter(*db, *dbHost, *username, *password, locations)
+	if err != nil {
+
+		fmt.Println(err.Error())
+		return
+	}
+	for {
+		resp, err := http.Get(*host)
+		if err != nil {
+			fmt.Println(err.Error())
+			return
+		}
+		defer resp.Body.Close()
+		contents, err := ioutil.ReadAll(resp.Body)
+		if err != nil {
+			fmt.Println(err.Error())
+			return
+		}
+
+		buffer := []byte(contents)
+		if json.Valid(buffer) {
+			var jsonPacket jsonPacket
+
+			err := json.Unmarshal(buffer, &jsonPacket)
+			if err != nil {
+				fmt.Println(err.Error())
+				return
+			}
+			if strings.Contains(strings.ToLower(string(contents)), "_f") {
+				jsonPacket.Temperature_C = (jsonPacket.Temperature_F - 32) * 5 / 9
+			}
+
+			fmt.Printf("Celcius: %f. Fahrenheit: %f \n", jsonPacket.Temperature_C, jsonPacket.Temperature_F)
+			measurement := temperature.Measurement{Temperature: jsonPacket.Temperature_C, BatteryState: temperature.BATT_NOT_AVAILABLE, ID: 0}
+			err = writer.Write(measurement)
+			if err != nil {
+				fmt.Println(err.Error())
+				return
+			}
+		}
+		time.Sleep(time.Duration(*seconds) * time.Second)
+	}
+}

+ 10 - 2
measurement.go

@@ -1,10 +1,18 @@
 package temperature
 
+type BatteryState int
+
+const (
+	BATT_NOT_AVAILABLE BatteryState = -1
+	BATT_BAD           BatteryState = 0
+	BATT_GOOD          BatteryState = 1
+)
+
 // Measurement provides an interface for each reading
 type Measurement struct {
 	// ID of the sensor
 	ID int
 	// Temperature in celcius
-	Temperature float64
-	BatteryGood bool
+	Temperature  float64
+	BatteryState BatteryState
 }

+ 2 - 24
temperature_logger/log.go

@@ -2,41 +2,19 @@ package main
 
 import (
 	"bufio"
-	"encoding/json"
-	"errors"
 	"flag"
 	"fmt"
 	"os"
-	"strconv"
-	"strings"
 
 	temperature "git.snppla.net/snppla/log_temperature"
+	"git.snppla.net/snppla/log_temperature/array"
 )
 
-type arrayFlags map[int]string
-
 var db *string
 var username *string
 var password *string
 var host *string
-var locations = arrayFlags{}
-
-func (i arrayFlags) String() string {
-	text, _ := json.Marshal(i)
-	return string(text)
-}
-
-func (i arrayFlags) Set(value string) error {
-
-	values := strings.Split(value, ":")
-	if len(values) != 2 {
-		return errors.New("Invalid location")
-	}
-	id, _ := strconv.ParseInt(values[0], 10, 32)
-
-	i[int(id)] = values[1]
-	return nil
-}
+var locations = array.ArrayFlags{}
 
 func main() {
 	db = flag.String("db", "temperature", "Influxdb database")

+ 3 - 6
writer.go

@@ -37,11 +37,8 @@ func (i influxWriter) Write(measurement Measurement) error {
 		tags["location"] = "id:" + strconv.FormatInt(int64(measurement.ID), 10)
 	}
 	var batteryState int
-	if measurement.BatteryGood {
-		batteryState = 1
-	} else {
-		batteryState = 0
-	}
+	batteryState = int(measurement.BatteryState)
+
 	fields := map[string]interface{}{
 		"temperature":  measurement.Temperature,
 		"battery_good": batteryState,
@@ -51,7 +48,6 @@ func (i influxWriter) Write(measurement Measurement) error {
 
 	if err != nil {
 		fmt.Println("Could not log temperature")
-		fmt.Println(err)
 		return err
 	}
 	bp.AddPoint(pt)
@@ -69,6 +65,7 @@ func (i influxWriter) Write(measurement Measurement) error {
 
 // NewInfluxWriter creates an influxdb writer
 func NewInfluxWriter(db string, host string, username string, password string, locations map[int]string) (Writer, error) {
+
 	client, err := influx.NewHTTPClient(influx.HTTPConfig{
 		Addr:     host,
 		Username: username,