Jelajahi Sumber

Merge remote-tracking branch 'origin/master' into HEAD

Spencer Gardner 5 tahun lalu
induk
melakukan
e146065d75
14 mengubah file dengan 513 tambahan dan 306 penghapusan
  1. 2 1
      .gitignore
  2. 29 12
      Dockerfile
  3. 27 0
      array/array_flags.go
  4. 64 0
      json.go
  5. 84 0
      json_get/json_get.go
  6. 184 0
      json_test.go
  7. 0 222
      log.go
  8. 18 0
      measurement.go
  9. 6 0
      parser.go
  10. 0 43
      parser/json.go
  11. 0 14
      parser/parser.go
  12. 10 6
      simple.go
  13. 75 0
      temperature_logger/log.go
  14. 14 8
      writer.go

+ 2 - 1
.gitignore

@@ -1 +1,2 @@
-/debug
+/logger/debug
+/temperature_logger/debug

+ 29 - 12
Dockerfile

@@ -1,5 +1,5 @@
 from debian as build-rtl_433
-RUN apt-get update && apt-get upgrade -y  && apt-get clean
+RUN apt-get update && apt-get upgrade -y && apt-get clean
 RUN apt-get update && apt-get install -y libtool libusb-1.0.0-dev librtlsdr-dev rtl-sdr build-essential autoconf cmake pkg-config git && apt-get clean
 RUN git clone https://github.com/merbanan/rtl_433.git
 WORKDIR /rtl_433
@@ -14,23 +14,40 @@ RUN apt-get update && apt-get upgrade -y
 
 #############################
 
-from debian as build-log
-RUN apt-get update && apt-get upgrade -y && apt-get clean
-RUN apt-get install -y golang git
-RUN mkdir -p /go/src/log-temperature
-ADD log.go /go/src/log-temperature
-WORKDIR /go/src/log-temperature
+from golang as build-log
+RUN mkdir -p /go/src/git.snppla.net/snppla/log_temperature
+ADD ./ /go/src/git.snppla.net/snppla/log_temperature/
+WORKDIR /go/src/git.snppla.net/snppla/log_temperature/temperature_logger
 ENV GOPATH /go
 ENV GOBIN /go/bin
 RUN go get
 RUN go install
 
+WORKDIR /go/src/git.snppla.net/snppla/log_temperature/json_get/
+RUN go get
+RUN go install
+
+#############################
+from debian as json_get
+
+COPY --from=build-log /go/bin/json_get /usr/bin
+
+ENV DB temperature
+ENV USER ""
+ENV PASSWORD ""
+ENV HOST http://localhost:80
+ENV DB_HOST http://localhost:8086
+ENV LOCATION "unknown"
+ENV DELAY 60
+
+CMD json_get -db $DB -host $HOST -dbHost $DB_HOST -location $LOCATION -password $PASSWORD -user $USER -delay $DELAY
+
 ##############################
-from debian as main
-RUN apt-get update && apt-get upgrade -y 
-RUN apt-get install -y librtlsdr0 && apt-get clean
+from debian as log_temperature
+RUN apt-get update && apt-get upgrade -y && apt-get clean
+RUN apt-get update && apt-get install -y librtlsdr0 && apt-get clean
 COPY --from=build-rtl_433 /rtl_433/install/bin/rtl_433 /usr/bin
-COPY --from=build-log /go/bin/log-temperature /usr/bin
+COPY --from=build-log /go/bin/temperature_logger /usr/bin
 
 ENV DB temperature
 ENV USER ""
@@ -39,7 +56,7 @@ ENV HOST http://localhost:8086
 ENV RTL_ARGS ""
 ENV LOCATIONS ""
 
-CMD rtl_433 $RTL_ARGS | log-temperature -host $HOST -db $DB -password $PASSWORD -user $USER $LOCATIONS
+CMD rtl_433 $RTL_ARGS -F json | temperature_logger -host $HOST -db $DB -password $PASSWORD -user $USER $LOCATIONS
 
 ##############################
 

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

+ 64 - 0
json.go

@@ -0,0 +1,64 @@
+package temperature
+
+import (
+	"bufio"
+	"encoding/json"
+	"errors"
+	"fmt"
+	"strings"
+)
+
+// JSONParser reads the json output
+type jsonParser struct {
+	scanner *bufio.Scanner
+}
+
+// NewJSONParser returns a parser for reading json temperature strings
+func NewJSONParser(reader *bufio.Reader) Parser {
+	return &jsonParser{scanner: bufio.NewScanner(reader)}
+}
+
+// {"time" : "2018-04-13 23:04:50", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
+type jsonPacket struct {
+	Time          string
+	Model         string
+	ID            int
+	Battery       string
+	Temperature_C float64
+}
+
+// Measure returns the temperature. Null if it can't
+func (jp jsonParser) Measure() (*Measurement, error) {
+	if !jp.scanner.Scan() {
+		return nil, errors.New("EOF")
+	}
+	if err := jp.scanner.Err(); err != nil {
+		return nil, err
+	}
+	text := jp.scanner.Text()
+	firstIndex := strings.Index(text, "{")
+	if firstIndex != -1 {
+		text = text[firstIndex:]
+	}
+
+	buffer := []byte(text)
+	if json.Valid(buffer) {
+		var jsonPacket jsonPacket
+
+		err := json.Unmarshal(buffer, &jsonPacket)
+		if err != nil {
+			fmt.Println(err.Error())
+			return nil, err
+		}
+		if len(jsonPacket.Model) > 0 {
+			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, BatteryState: batteryState}, nil
+		}
+	}
+	return nil, nil
+}

+ 84 - 0
json_get/json_get.go

@@ -0,0 +1,84 @@
+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 checkTemperature(writer temperature.Writer) {
+	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
+		}
+	}
+}
+
+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 {
+		checkTemperature(writer)
+		time.Sleep(time.Duration(*seconds) * time.Second)
+	}
+}

+ 184 - 0
json_test.go

@@ -0,0 +1,184 @@
+package temperature
+
+import (
+	"bufio"
+	"fmt"
+	"strings"
+	"testing"
+)
+
+var validStrings = []string{`{"time" : "2018-04-13 23:04:50", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}`,
+	`{"time" : "2018-04-13 23:04:50", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
+	`,
+}
+
+var invalidStrings = []string{`sfdxcvxvczxcvzxcvzxcvzxsdfasdfasd
+	sdfsdf
+	sd
+	f
+	sdf
+	sdf
+	fasdfzx`, `asdfasdfavxc
+	sfdsdsdf`,
+	`{"timsdfe" : "2018-04-13 23:04:50", "modsdfsel" : "Acurite 606TX Sensor", "idsdf" : -5, "battsdfsdery" : "OK", "tempersdfsdature_C" : 21.800}`,
+}
+
+func TestValidJson(t *testing.T) {
+	for _, string := range validStrings {
+		reader := bufio.NewReader(strings.NewReader(string))
+		parser := NewJSONParser(reader)
+
+		measurement, err := parser.Measure()
+		if err != nil {
+			t.Errorf("Recieved error: %s", err)
+		}
+		if measurement.ID != -5 {
+			t.Errorf("ID was incorrect. Expect %d but got %d", -5, measurement.ID)
+		}
+	}
+}
+
+func TestInvalidJson(t *testing.T) {
+	for _, string := range invalidStrings {
+		reader := bufio.NewReader(strings.NewReader(string))
+		parser := NewJSONParser(reader)
+		measurement, err := parser.Measure()
+		if err != nil {
+			t.Errorf("Invalid strings are not an error")
+		}
+		if measurement != nil {
+			t.Errorf("There should have been no measurement for %s", string)
+		}
+	}
+}
+
+func TestRealString(t *testing.T) {
+	fmt.Println("starting")
+	reader := bufio.NewReader(strings.NewReader(realString))
+	parser := NewJSONParser(reader)
+	var measurements = make([]Measurement, 0, 100)
+
+	for measurement, err := parser.Measure(); err == nil; measurement, err = parser.Measure() {
+		if measurement != nil {
+			measurements = append(measurements, *measurement)
+		}
+	}
+
+	if len(measurements) != 20 {
+		t.Errorf("Expected %d measurements. Got %d", 20, len(measurements))
+	}
+}
+
+var realString = `Registering protocol [1] "Rubicson Temperature Sensor"
+Registering protocol [2] "Prologue Temperature Sensor"
+Registering protocol [3] "Waveman Switch Transmitter"
+Registering protocol [4] "LaCrosse TX Temperature / Humidity Sensor"
+Registering protocol [5] "Acurite 609TXC Temperature and Humidity Sensor"
+Registering protocol [6] "Oregon Scientific Weather Sensor"
+Registering protocol [7] "KlikAanKlikUit Wireless Switch"
+Registering protocol [8] "AlectoV1 Weather Sensor (Alecto WS3500 WS4500 Ventus W155/W044 Oregon)"
+Registering protocol [9] "Cardin S466-TX2"
+Registering protocol [10] "Fine Offset Electronics, WH2 Temperature/Humidity Sensor"
+Registering protocol [11] "Nexus Temperature & Humidity Sensor"
+Registering protocol [12] "Ambient Weather Temperature Sensor"
+Registering protocol [13] "Calibeur RF-104 Sensor"
+Registering protocol [14] "GT-WT-02 Sensor"
+Registering protocol [15] "Danfoss CFR Thermostat"
+Registering protocol [16] "Chuango Security Technology"
+Registering protocol [17] "Generic Remote SC226x EV1527"
+Registering protocol [18] "TFA-Twin-Plus-30.3049 and Ea2 BL999"
+Registering protocol [19] "Fine Offset Electronics WH1080/WH3080 Weather Station"
+Registering protocol [20] "WT450"
+Registering protocol [21] "LaCrosse WS-2310 Weather Station"
+Registering protocol [22] "Esperanza EWS"
+Registering protocol [23] "Efergy e2 classic"
+Registering protocol [24] "Generic temperature sensor 1"
+Registering protocol [25] "WG-PB12V1"
+Registering protocol [26] "HIDEKI TS04 Temperature, Humidity, Wind and Rain Sensor"
+Registering protocol [27] "Watchman Sonic / Apollo Ultrasonic / Beckett Rocket oil tank monitor"
+Registering protocol [28] "CurrentCost Current Sensor"
+Registering protocol [29] "emonTx OpenEnergyMonitor"
+Registering protocol [30] "HT680 Remote control"
+Registering protocol [31] "S3318P Temperature & Humidity Sensor"
+Registering protocol [32] "Akhan 100F14 remote keyless entry"
+Registering protocol [33] "Quhwa"
+Registering protocol [34] "OSv1 Temperature Sensor"
+Registering protocol [35] "Proove"
+Registering protocol [36] "Bresser Thermo-/Hygro-Sensor 3CH"
+Registering protocol [37] "Springfield Temperature and Soil Moisture"
+Registering protocol [38] "Oregon Scientific SL109H Remote Thermal Hygro Sensor"
+Registering protocol [39] "Acurite 606TX Temperature Sensor"
+Registering protocol [40] "TFA pool temperature sensor"
+Registering protocol [41] "Kedsum Temperature & Humidity Sensor"
+Registering protocol [42] "blyss DC5-UK-WH (433.92 MHz)"
+Registering protocol [43] "Steelmate TPMS"
+Registering protocol [44] "Schrader TPMS"
+Registering protocol [45] "Elro DB286A Doorbell"
+Registering protocol [46] "Efergy Optical"
+Registering protocol [47] "Honda Car Key"
+Registering protocol [48] "Fine Offset Electronics, XC0400"
+Registering protocol [49] "Radiohead ASK"
+Registering protocol [50] "Kerui PIR Sensor"
+Registering protocol [51] "Fine Offset WH1050 Weather Station"
+Registering protocol [52] "Honeywell Door/Window Sensor"
+Registering protocol [53] "Maverick ET-732/733 BBQ Sensor"
+Registering protocol [54] "LaCrosse TX141-Bv2/TX141TH-Bv2 sensor"
+Registering protocol [55] "Acurite 00275rm,00276rm Temp/Humidity with optional probe"
+Registering protocol [56] "LaCrosse TX35DTH-IT Temperature sensor"
+Registering protocol [57] "LaCrosse TX29IT Temperature sensor"
+Registering protocol [58] "Vaillant calorMatic 340f Central Heating Control"
+Registering protocol [59] "Fine Offset Electronics, WH25 Temperature/Humidity/Pressure Sensor"
+Registering protocol [60] "Fine Offset Electronics, WH0530 Temperature/Rain Sensor"
+Registering protocol [61] "IBIS beacon"
+Registering protocol [62] "Oil Ultrasonic STANDARD FSK"
+Registering protocol [63] "Citroen TPMS"
+Registering protocol [64] "Oil Ultrasonic STANDARD ASK"
+Registering protocol [65] "Thermopro TP11 Thermometer"
+Registering protocol [66] "Solight TE44"
+Registering protocol [67] "Wireless Smoke and Heat Detector GS 558"
+Registering protocol [68] "Generic wireless motion sensor"
+Registering protocol [69] "Toyota TPMS"
+Registering protocol [70] "Ford TPMS"
+Registering protocol [71] "Renault TPMS"
+Registering protocol [72] "FT-004-B Temperature Sensor"
+Registering protocol [73] "Ford Car Key"
+Registering protocol [74] "Philips outdoor temperature sensor"
+Registering protocol [75] "Schrader TPMS EG53MA4"
+Registering protocol [76] "Nexa"
+Registering protocol [77] "Thermopro TP12 Thermometer"
+Registering protocol [78] "GE Color Effects"
+Registering protocol [79] "X10 Security"
+Registering protocol [80] "Interlogix GE UTC Security Devices"
+Registered 80 out of 101 device decoding protocols
+Found 1 device(s)
+
+trying device  0:  Realtek, RTL2838UHIDIR, SN: 00000001
+Found Rafael Micro R820T tuner
+Using device 0: Generic RTL2832U OEM
+Exact sample rate is: 250000.000414 Hz
+[R82XX] PLL not locked!
+Sample rate set to 250000.
+Bit detection level set to 0 (Auto).
+Tuner gain set to Auto.
+Reading samples in async mode...
+Tuned to 433920000 Hz.
+^[ {"time" : "2018-04-13 23:00:05", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
+{"time" : "2018-04-13 23:00:11", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
+{"time" : "2018-04-13 23:00:36", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
+{"time" : "2018-04-13 23:00:42", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
+{"time" : "2018-04-13 23:01:07", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
+{"time" : "2018-04-13 23:01:13", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
+{"time" : "2018-04-13 23:01:38", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
+{"time" : "2018-04-13 23:01:44", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
+{"time" : "2018-04-13 23:02:09", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
+{"time" : "2018-04-13 23:02:15", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
+{"time" : "2018-04-13 23:02:40", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
+{"time" : "2018-04-13 23:02:46", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
+{"time" : "2018-04-13 23:03:11", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
+{"time" : "2018-04-13 23:03:17", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
+{"time" : "2018-04-13 23:03:42", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
+{"time" : "2018-04-13 23:03:48", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
+{"time" : "2018-04-13 23:04:13", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.700}
+{"time" : "2018-04-13 23:04:19", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
+{"time" : "2018-04-13 23:04:44", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.700}
+{"time" : "2018-04-13 23:04:50", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}`

+ 0 - 222
log.go

@@ -1,222 +0,0 @@
-package main
-
-import (
-	"bufio"
-	"encoding/json"
-	"errors"
-	"flag"
-	"fmt"
-	"os"
-	"strconv"
-	"strings"
-
-	"git.snppla.net/snppla/log-temperature/parser"
-	"git.snppla.net/snppla/log-temperature/writer"
-)
-
-type arrayFlags map[int]string
-
-var db *string
-var username *string
-var password *string
-var host *string
-var test *bool
-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
-}
-
-func main() {
-	db = flag.String("db", "temperature", "Influxdb database")
-	username = flag.String("user", "", "Username")
-	password = flag.String("password", "", "Password")
-	host = flag.String("host", "http://localhost", "Host to connect to")
-	test = flag.Bool("test", false, "Use a test input string")
-	flag.Var(&locations, "location", "Id and location of the sensor (5:conference-room")
-
-	var reader *bufio.Reader
-
-	flag.Parse()
-
-	if *test {
-		reader = bufio.NewReader(strings.NewReader(jasonTestString))
-	} else {
-		reader = bufio.NewReader(os.Stdin)
-	}
-
-	parser := parser.JSONParser{Reader: reader}
-	writer, err := writer.NewInfluxWriter(*db, *host, *username, *password, locations)
-	if err != nil {
-		fmt.Println(err.Error())
-		return
-	}
-	for {
-		measurement, err := parser.Measure()
-		if measurement != nil {
-			writer.Write(*measurement)
-		}
-		if err != nil {
-			fmt.Println(err.Error())
-			return
-		}
-	}
-}
-
-const (
-	simpleTestString = `
-	2018-04-13 22:57:36 :   Acurite 606TX Sensor    :       -5
-        Battery:         OK
-        Temperature:     21.8 C
-2018-04-13 22:58:01 :   Acurite 606TX Sensor    :       103
-        Battery:         OK
-        Temperature:     24.3 C
-2018-04-13 22:58:07 :   Acurite 606TX Sensor    :       -5
-        Battery:         OK
-        Temperature:     21.8 C
-2018-04-13 22:58:32 :   Acurite 606TX Sensor    :       103
-        Battery:         OK
-        Temperature:     24.6 C
-2018-04-13 22:58:38 :   Acurite 606TX Sensor    :       -5
-        Battery:         OK
-        Temperature:     21.8 C
-2018-04-13 22:59:03 :   Acurite 606TX Sensor    :       103
-        Battery:         OK
-        Temperature:     24.8 C
-2018-04-13 22:59:09 :   Acurite 606TX Sensor    :       -5
-        Battery:         OK
-        Temperature:     21.8 C
-
-	`
-	jasonTestString = `
-	Registering protocol [1] "Rubicson Temperature Sensor"
-	Registering protocol [2] "Prologue Temperature Sensor"
-	Registering protocol [3] "Waveman Switch Transmitter"
-	Registering protocol [4] "LaCrosse TX Temperature / Humidity Sensor"
-	Registering protocol [5] "Acurite 609TXC Temperature and Humidity Sensor"
-	Registering protocol [6] "Oregon Scientific Weather Sensor"
-	Registering protocol [7] "KlikAanKlikUit Wireless Switch"
-	Registering protocol [8] "AlectoV1 Weather Sensor (Alecto WS3500 WS4500 Ventus W155/W044 Oregon)"
-	Registering protocol [9] "Cardin S466-TX2"
-	Registering protocol [10] "Fine Offset Electronics, WH2 Temperature/Humidity Sensor"
-	Registering protocol [11] "Nexus Temperature & Humidity Sensor"
-	Registering protocol [12] "Ambient Weather Temperature Sensor"
-	Registering protocol [13] "Calibeur RF-104 Sensor"
-	Registering protocol [14] "GT-WT-02 Sensor"
-	Registering protocol [15] "Danfoss CFR Thermostat"
-	Registering protocol [16] "Chuango Security Technology"
-	Registering protocol [17] "Generic Remote SC226x EV1527"
-	Registering protocol [18] "TFA-Twin-Plus-30.3049 and Ea2 BL999"
-	Registering protocol [19] "Fine Offset Electronics WH1080/WH3080 Weather Station"
-	Registering protocol [20] "WT450"
-	Registering protocol [21] "LaCrosse WS-2310 Weather Station"
-	Registering protocol [22] "Esperanza EWS"
-	Registering protocol [23] "Efergy e2 classic"
-	Registering protocol [24] "Generic temperature sensor 1"
-	Registering protocol [25] "WG-PB12V1"
-	Registering protocol [26] "HIDEKI TS04 Temperature, Humidity, Wind and Rain Sensor"
-	Registering protocol [27] "Watchman Sonic / Apollo Ultrasonic / Beckett Rocket oil tank monitor"
-	Registering protocol [28] "CurrentCost Current Sensor"
-	Registering protocol [29] "emonTx OpenEnergyMonitor"
-	Registering protocol [30] "HT680 Remote control"
-	Registering protocol [31] "S3318P Temperature & Humidity Sensor"
-	Registering protocol [32] "Akhan 100F14 remote keyless entry"
-	Registering protocol [33] "Quhwa"
-	Registering protocol [34] "OSv1 Temperature Sensor"
-	Registering protocol [35] "Proove"
-	Registering protocol [36] "Bresser Thermo-/Hygro-Sensor 3CH"
-	Registering protocol [37] "Springfield Temperature and Soil Moisture"
-	Registering protocol [38] "Oregon Scientific SL109H Remote Thermal Hygro Sensor"
-	Registering protocol [39] "Acurite 606TX Temperature Sensor"
-	Registering protocol [40] "TFA pool temperature sensor"
-	Registering protocol [41] "Kedsum Temperature & Humidity Sensor"
-	Registering protocol [42] "blyss DC5-UK-WH (433.92 MHz)"
-	Registering protocol [43] "Steelmate TPMS"
-	Registering protocol [44] "Schrader TPMS"
-	Registering protocol [45] "Elro DB286A Doorbell"
-	Registering protocol [46] "Efergy Optical"
-	Registering protocol [47] "Honda Car Key"
-	Registering protocol [48] "Fine Offset Electronics, XC0400"
-	Registering protocol [49] "Radiohead ASK"
-	Registering protocol [50] "Kerui PIR Sensor"
-	Registering protocol [51] "Fine Offset WH1050 Weather Station"
-	Registering protocol [52] "Honeywell Door/Window Sensor"
-	Registering protocol [53] "Maverick ET-732/733 BBQ Sensor"
-	Registering protocol [54] "LaCrosse TX141-Bv2/TX141TH-Bv2 sensor"
-	Registering protocol [55] "Acurite 00275rm,00276rm Temp/Humidity with optional probe"
-	Registering protocol [56] "LaCrosse TX35DTH-IT Temperature sensor"
-	Registering protocol [57] "LaCrosse TX29IT Temperature sensor"
-	Registering protocol [58] "Vaillant calorMatic 340f Central Heating Control"
-	Registering protocol [59] "Fine Offset Electronics, WH25 Temperature/Humidity/Pressure Sensor"
-	Registering protocol [60] "Fine Offset Electronics, WH0530 Temperature/Rain Sensor"
-	Registering protocol [61] "IBIS beacon"
-	Registering protocol [62] "Oil Ultrasonic STANDARD FSK"
-	Registering protocol [63] "Citroen TPMS"
-	Registering protocol [64] "Oil Ultrasonic STANDARD ASK"
-	Registering protocol [65] "Thermopro TP11 Thermometer"
-	Registering protocol [66] "Solight TE44"
-	Registering protocol [67] "Wireless Smoke and Heat Detector GS 558"
-	Registering protocol [68] "Generic wireless motion sensor"
-	Registering protocol [69] "Toyota TPMS"
-	Registering protocol [70] "Ford TPMS"
-	Registering protocol [71] "Renault TPMS"
-	Registering protocol [72] "FT-004-B Temperature Sensor"
-	Registering protocol [73] "Ford Car Key"
-	Registering protocol [74] "Philips outdoor temperature sensor"
-	Registering protocol [75] "Schrader TPMS EG53MA4"
-	Registering protocol [76] "Nexa"
-	Registering protocol [77] "Thermopro TP12 Thermometer"
-	Registering protocol [78] "GE Color Effects"
-	Registering protocol [79] "X10 Security"
-	Registering protocol [80] "Interlogix GE UTC Security Devices"
-	Registered 80 out of 101 device decoding protocols
-	Found 1 device(s)
-	
-	trying device  0:  Realtek, RTL2838UHIDIR, SN: 00000001
-	Found Rafael Micro R820T tuner
-	Using device 0: Generic RTL2832U OEM
-	Exact sample rate is: 250000.000414 Hz
-	[R82XX] PLL not locked!
-	Sample rate set to 250000.
-	Bit detection level set to 0 (Auto).
-	Tuner gain set to Auto.
-	Reading samples in async mode...
-	Tuned to 433920000 Hz.
-	^[ {"time" : "2018-04-13 23:00:05", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
-	{"time" : "2018-04-13 23:00:11", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
-	{"time" : "2018-04-13 23:00:36", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
-	{"time" : "2018-04-13 23:00:42", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
-	{"time" : "2018-04-13 23:01:07", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
-	{"time" : "2018-04-13 23:01:13", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
-	{"time" : "2018-04-13 23:01:38", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
-	{"time" : "2018-04-13 23:01:44", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
-	{"time" : "2018-04-13 23:02:09", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
-	{"time" : "2018-04-13 23:02:15", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
-	{"time" : "2018-04-13 23:02:40", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
-	{"time" : "2018-04-13 23:02:46", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
-	{"time" : "2018-04-13 23:03:11", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
-	{"time" : "2018-04-13 23:03:17", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
-	{"time" : "2018-04-13 23:03:42", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
-	{"time" : "2018-04-13 23:03:48", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
-	{"time" : "2018-04-13 23:04:13", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.700}
-	{"time" : "2018-04-13 23:04:19", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
-	{"time" : "2018-04-13 23:04:44", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.700}
-	{"time" : "2018-04-13 23:04:50", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
-	
-	
-
-`
-)

+ 18 - 0
measurement.go

@@ -0,0 +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
+	BatteryState BatteryState
+}

+ 6 - 0
parser.go

@@ -0,0 +1,6 @@
+package temperature
+
+// Parser provides an interface for parsing temperature strings
+type Parser interface {
+	Measure() (*Measurement, error)
+}

+ 0 - 43
parser/json.go

@@ -1,43 +0,0 @@
-package parser
-
-import (
-	"bufio"
-	"encoding/json"
-	"fmt"
-	"strconv"
-)
-
-// JSONParser reads the json output
-type JSONParser struct {
-	Reader *bufio.Reader
-}
-
-// {"time" : "2018-04-13 23:04:50", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
-type jsonPacket struct {
-	Time          string
-	Model         string
-	ID            int
-	Battery       string
-	Temperature_C float64
-}
-
-// Measure returns the temperature. Null if it can't
-func (jp JSONParser) Measure() (*Measurement, error) {
-	text, err := jp.Reader.ReadString('\n')
-	if err != nil {
-		return nil, err
-	}
-	buffer := []byte(text)
-	if json.Valid(buffer) {
-		var jsonPacket jsonPacket
-
-		err = json.Unmarshal(buffer, &jsonPacket)
-		if err != nil {
-			fmt.Println(err.Error())
-			return nil, err
-		}
-		fmt.Println("temp is " + strconv.FormatFloat(jsonPacket.Temperature_C, 'f', 6, 64))
-		return &Measurement{ID: jsonPacket.ID, Temperature: jsonPacket.Temperature_C}, nil
-	}
-	return nil, nil
-}

+ 0 - 14
parser/parser.go

@@ -1,14 +0,0 @@
-package parser
-
-// Measurement provides an interface for each reading
-type Measurement struct {
-	// ID of the sensor
-	ID int
-	// Temperature in celcius
-	Temperature float64
-}
-
-// Parser provides an interface for parsing temperature strings
-type Parser interface {
-	Measure() (*Measurement, error)
-}

+ 10 - 6
parser/simple.go → simple.go

@@ -1,4 +1,4 @@
-package parser
+package temperature
 
 import (
 	"bufio"
@@ -7,14 +7,18 @@ import (
 	"strings"
 )
 
-// SimpleParser reads the default output and just scans for temperature
-type SimpleParser struct {
-	Reader *bufio.Reader
+type simpleParser struct {
+	reader *bufio.Reader
+}
+
+// NewSimpleParser returns a parser that scans for temperature: 53 C
+func NewSimpleParser(reader *bufio.Reader) Parser {
+	return &simpleParser{reader: reader}
 }
 
 // Measure returns the temperature. Null if it can't
-func (sp SimpleParser) Measure() (*Measurement, error) {
-	text, err := sp.Reader.ReadString('\n')
+func (sp simpleParser) Measure() (*Measurement, error) {
+	text, err := sp.reader.ReadString('\n')
 	if err != nil {
 		return nil, err
 	}

+ 75 - 0
temperature_logger/log.go

@@ -0,0 +1,75 @@
+package main
+
+import (
+	"bufio"
+	"flag"
+	"fmt"
+	"os"
+
+	temperature "git.snppla.net/snppla/log_temperature"
+	"git.snppla.net/snppla/log_temperature/array"
+)
+
+var db *string
+var username *string
+var password *string
+var host *string
+var locations = array.ArrayFlags{}
+
+func main() {
+	db = flag.String("db", "temperature", "Influxdb database")
+	username = flag.String("user", "", "Username")
+	password = flag.String("password", "", "Password")
+	host = flag.String("host", "http://localhost", "Host to connect to")
+	flag.Var(&locations, "location", "Id and location of the sensor (5:conference-room")
+
+	var reader *bufio.Reader
+
+	flag.Parse()
+
+	reader = bufio.NewReader(os.Stdin)
+
+	parser := temperature.NewJSONParser(reader)
+	writer, err := temperature.NewInfluxWriter(*db, *host, *username, *password, locations)
+	if err != nil {
+		fmt.Println(err.Error())
+		return
+	}
+	for {
+		measurement, err := parser.Measure()
+		if measurement != nil {
+			writer.Write(*measurement)
+		}
+		if err != nil {
+			fmt.Println(err.Error())
+			return
+		}
+	}
+}
+
+const (
+	simpleTestString = `
+	2018-04-13 22:57:36 :   Acurite 606TX Sensor    :       -5
+        Battery:         OK
+        Temperature:     21.8 C
+2018-04-13 22:58:01 :   Acurite 606TX Sensor    :       103
+        Battery:         OK
+        Temperature:     24.3 C
+2018-04-13 22:58:07 :   Acurite 606TX Sensor    :       -5
+        Battery:         OK
+        Temperature:     21.8 C
+2018-04-13 22:58:32 :   Acurite 606TX Sensor    :       103
+        Battery:         OK
+        Temperature:     24.6 C
+2018-04-13 22:58:38 :   Acurite 606TX Sensor    :       -5
+        Battery:         OK
+        Temperature:     21.8 C
+2018-04-13 22:59:03 :   Acurite 606TX Sensor    :       103
+        Battery:         OK
+        Temperature:     24.8 C
+2018-04-13 22:59:09 :   Acurite 606TX Sensor    :       -5
+        Battery:         OK
+        Temperature:     21.8 C
+
+	`
+)

+ 14 - 8
writer/writer.go → writer.go

@@ -1,16 +1,16 @@
-package writer
+package temperature
 
 import (
 	"fmt"
+	"strconv"
 	"time"
 
-	"git.snppla.net/snppla/log-temperature/parser"
-	influx "github.com/influxdata/influxdb/client/v2"
+	influx "github.com/influxdata/influxdb1-client/v2"
 )
 
 // Writer is used to write out measurements
 type Writer interface {
-	Write(parser.Measurement) error
+	Write(Measurement) error
 }
 
 type influxWriter struct {
@@ -19,7 +19,7 @@ type influxWriter struct {
 	client    influx.Client
 }
 
-func (i influxWriter) Write(measurement parser.Measurement) error {
+func (i influxWriter) Write(measurement Measurement) error {
 	bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{
 		Database:  i.db,
 		Precision: "s",
@@ -34,20 +34,24 @@ func (i influxWriter) Write(measurement parser.Measurement) error {
 		tags["location"] = val
 
 	} else {
-		tags["location"] = "id:" + string(measurement.ID)
+		tags["location"] = "id:" + strconv.FormatInt(int64(measurement.ID), 10)
 	}
+	var batteryState int
+	batteryState = int(measurement.BatteryState)
+
 	fields := map[string]interface{}{
-		"temperature": measurement.Temperature,
+		"temperature":  measurement.Temperature,
+		"battery_good": batteryState,
 	}
 
 	pt, err := influx.NewPoint("temperature", tags, fields, time.Now())
 
 	if err != nil {
 		fmt.Println("Could not log temperature")
-		fmt.Println(err)
 		return err
 	}
 	bp.AddPoint(pt)
+
 	err = i.client.Write(bp)
 
 	if err != nil {
@@ -55,11 +59,13 @@ func (i influxWriter) Write(measurement parser.Measurement) error {
 		fmt.Println(err)
 		return err
 	}
+	fmt.Println("Wrote measurement successfully")
 	return nil
 }
 
 // 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,