log.go 3.3 KB

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