log.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "time"
  10. influx "github.com/influxdata/influxdb/client/v2"
  11. )
  12. var db *string
  13. var username *string
  14. var password *string
  15. var host *string
  16. func main() {
  17. db = flag.String("db", "temperature", "Influxdb database")
  18. username = flag.String("user", "", "Username")
  19. password = flag.String("password", "", "Password")
  20. host = flag.String("host", "http://localhost", "Host to connect to")
  21. flag.Parse()
  22. reader := bufio.NewReader(os.Stdin)
  23. //reader := bufio.NewReader(strings.NewReader(testString))
  24. client, err := influx.NewHTTPClient(influx.HTTPConfig{
  25. Addr: *host,
  26. Username: *username,
  27. Password: *password,
  28. })
  29. if err != nil {
  30. fmt.Println("Could not connect")
  31. fmt.Println(err)
  32. return
  33. }
  34. _ = reader
  35. for {
  36. text, err := reader.ReadString('\n')
  37. if err != nil {
  38. return
  39. }
  40. fields := strings.Fields(text)
  41. for i := 0; i < len(fields); i++ {
  42. fields[i] = strings.ToLower(fields[i])
  43. }
  44. if len(fields) == 3 {
  45. if strings.Compare(fields[0], "temperature:") == 0 {
  46. temp, _ := strconv.ParseFloat(fields[1], 64)
  47. unit := fields[2]
  48. if strings.Compare(unit, "c") == 0 {
  49. temp = (temp - 32) * (5.0 / 9.0)
  50. }
  51. fmt.Println("temp is " + strconv.FormatFloat(temp, 'f', 6, 64))
  52. logTemp(temp, client)
  53. }
  54. }
  55. }
  56. }
  57. func logTemp(temp float64, client influx.Client) {
  58. bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{
  59. Database: *db,
  60. Precision: "s",
  61. })
  62. if err != nil {
  63. fmt.Println("Could not log temperature")
  64. fmt.Println(err)
  65. return
  66. }
  67. tags := map[string]string{"temperature": "conference-room"}
  68. fields := map[string]interface{}{
  69. "temperature": temp,
  70. }
  71. pt, err := influx.NewPoint("temperature", tags, fields, time.Now())
  72. if err != nil {
  73. fmt.Println("Could not log temperature")
  74. fmt.Println(err)
  75. return
  76. }
  77. bp.AddPoint(pt)
  78. err = client.Write(bp)
  79. if err != nil {
  80. fmt.Println("Could not log temperature")
  81. fmt.Println(err)
  82. return
  83. }
  84. }
  85. const (
  86. testString = `
  87. Registering protocol [1] "Acurite 606TX Temperature Sensor"
  88. Registered 1 out of 101 device decoding protocols
  89. Found 1 device(s)
  90. trying device 0: Realtek, RTL2838UHIDIR, SN: 00000001
  91. Detached kernel driver
  92. Found Rafael Micro R820T tuner
  93. Using device 0: Generic RTL2832U OEM
  94. Exact sample rate is: 250000.000414 Hz
  95. [R82XX] PLL not locked!
  96. Sample rate set to 250000.
  97. Bit detection level set to 0 (Auto).
  98. Tuner gain set to Auto.
  99. Reading samples in async mode...
  100. Tuned to 433920000 Hz.
  101. 2018-04-07 21:51:13 : Acurite 606TX Sensor : -5
  102. Battery: OK
  103. Temperature: 25.3 C
  104. 2018-04-07 21:51:18 : Acurite 606TX Sensor : -5
  105. Battery: OK
  106. Temperature: 25.3 C
  107. 2018-04-07 21:51:20 : Acurite 606TX Sensor : -5
  108. Battery: OK
  109. Temperature: 25.3 C
  110. 2018-04-07 21:51:21 : Acurite 606TX Sensor : -5
  111. Battery: OK
  112. Temperature: 25.3 C
  113. 2018-04-07 21:51:51 : Acurite 606TX Sensor : -5
  114. Battery: OK
  115. Temperature: 25.7 C
  116. 2018-04-07 21:52:21 : Acurite 606TX Sensor : -5
  117. Battery: OK
  118. Temperature: 26.2 C
  119. 2018-04-07 21:52:53 : Acurite 606TX Sensor : -5
  120. Battery: OK
  121. Temperature: 26.3 C
  122. 2018-04-07 21:53:24 : Acurite 606TX Sensor : -5
  123. Battery: OK
  124. Temperature: 26.4 C
  125. `
  126. )