log.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package main
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "errors"
  6. "flag"
  7. "fmt"
  8. "os"
  9. "strconv"
  10. "strings"
  11. temperature "git.snppla.net/snppla/log_temperature"
  12. )
  13. type arrayFlags map[int]string
  14. var db *string
  15. var username *string
  16. var password *string
  17. var host *string
  18. var locations = arrayFlags{}
  19. func (i arrayFlags) String() string {
  20. text, _ := json.Marshal(i)
  21. return string(text)
  22. }
  23. func (i arrayFlags) Set(value string) error {
  24. values := strings.Split(value, ":")
  25. if len(values) != 2 {
  26. return errors.New("Invalid location")
  27. }
  28. id, _ := strconv.ParseInt(values[0], 10, 32)
  29. i[int(id)] = values[1]
  30. return nil
  31. }
  32. func main() {
  33. db = flag.String("db", "temperature", "Influxdb database")
  34. username = flag.String("user", "", "Username")
  35. password = flag.String("password", "", "Password")
  36. host = flag.String("host", "http://localhost", "Host to connect to")
  37. flag.Var(&locations, "location", "Id and location of the sensor (5:conference-room")
  38. var reader *bufio.Reader
  39. flag.Parse()
  40. reader = bufio.NewReader(os.Stdin)
  41. parser := temperature.NewJSONParser(reader)
  42. writer, err := temperature.NewInfluxWriter(*db, *host, *username, *password, locations)
  43. if err != nil {
  44. fmt.Println(err.Error())
  45. return
  46. }
  47. for {
  48. measurement, err := parser.Measure()
  49. if measurement != nil {
  50. writer.Write(*measurement)
  51. }
  52. if err != nil {
  53. fmt.Println(err.Error())
  54. return
  55. }
  56. }
  57. }
  58. const (
  59. simpleTestString = `
  60. 2018-04-13 22:57:36 : Acurite 606TX Sensor : -5
  61. Battery: OK
  62. Temperature: 21.8 C
  63. 2018-04-13 22:58:01 : Acurite 606TX Sensor : 103
  64. Battery: OK
  65. Temperature: 24.3 C
  66. 2018-04-13 22:58:07 : Acurite 606TX Sensor : -5
  67. Battery: OK
  68. Temperature: 21.8 C
  69. 2018-04-13 22:58:32 : Acurite 606TX Sensor : 103
  70. Battery: OK
  71. Temperature: 24.6 C
  72. 2018-04-13 22:58:38 : Acurite 606TX Sensor : -5
  73. Battery: OK
  74. Temperature: 21.8 C
  75. 2018-04-13 22:59:03 : Acurite 606TX Sensor : 103
  76. Battery: OK
  77. Temperature: 24.8 C
  78. 2018-04-13 22:59:09 : Acurite 606TX Sensor : -5
  79. Battery: OK
  80. Temperature: 21.8 C
  81. `
  82. )