12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package main
- import (
- "bufio"
- "encoding/json"
- "errors"
- "flag"
- "fmt"
- "os"
- "strconv"
- "strings"
- temperature "git.snppla.net/snppla/log_temperature"
- )
- 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
- }
- 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
- `
- )
|