log.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package main
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "errors"
  6. "flag"
  7. "fmt"
  8. parser "log-temperature/parser"
  9. "strconv"
  10. "strings"
  11. "time"
  12. influx "github.com/influxdata/influxdb/client/v2"
  13. )
  14. type arrayFlags map[int]string
  15. var db *string
  16. var username *string
  17. var password *string
  18. var host *string
  19. var locations = arrayFlags{}
  20. func (i arrayFlags) String() string {
  21. text, _ := json.Marshal(i)
  22. return string(text)
  23. }
  24. func (i arrayFlags) Set(value string) error {
  25. values := strings.Split(value, ":")
  26. if len(values) != 2 {
  27. return errors.New("Invalid location")
  28. }
  29. id, _ := strconv.ParseInt(values[0], 10, 32)
  30. i[int(id)] = values[1]
  31. return nil
  32. }
  33. func main() {
  34. db = flag.String("db", "temperature", "Influxdb database")
  35. username = flag.String("user", "", "Username")
  36. password = flag.String("password", "", "Password")
  37. host = flag.String("host", "http://localhost", "Host to connect to")
  38. flag.Var(&locations, "location", "Id and location of the sensor (5:conference-room")
  39. flag.Parse()
  40. //reader := bufio.NewReader(os.Stdin)
  41. reader := bufio.NewReader(strings.NewReader(jasonTestString))
  42. client, err := influx.NewHTTPClient(influx.HTTPConfig{
  43. Addr: *host,
  44. Username: *username,
  45. Password: *password,
  46. })
  47. if err != nil {
  48. fmt.Println("Could not connect")
  49. fmt.Println(err)
  50. return
  51. }
  52. _ = reader
  53. parser := parser.JSONParser{reader}
  54. for {
  55. measurement, err := parser.Measure()
  56. if measurement != nil {
  57. logTemp(*measurement, client)
  58. _ = client
  59. }
  60. if err != nil {
  61. fmt.Println(err.Error())
  62. return
  63. }
  64. }
  65. }
  66. func logTemp(measurement parser.Measurement, client influx.Client) {
  67. bp, err := influx.NewBatchPoints(influx.BatchPointsConfig{
  68. Database: *db,
  69. Precision: "s",
  70. })
  71. if err != nil {
  72. fmt.Println("Could not log temperature")
  73. fmt.Println(err)
  74. return
  75. }
  76. var tags = map[string]string{}
  77. if val, ok := locations[measurement.ID]; ok {
  78. tags["location"] = val
  79. } else {
  80. tags["location"] = "unknown"
  81. }
  82. fields := map[string]interface{}{
  83. "temperature": measurement.Temperature,
  84. }
  85. pt, err := influx.NewPoint("temperature", tags, fields, time.Now())
  86. if err != nil {
  87. fmt.Println("Could not log temperature")
  88. fmt.Println(err)
  89. return
  90. }
  91. bp.AddPoint(pt)
  92. err = client.Write(bp)
  93. if err != nil {
  94. fmt.Println("Could not log temperature")
  95. fmt.Println(err)
  96. return
  97. }
  98. }
  99. const (
  100. simpleTestString = `
  101. 2018-04-13 22:57:36 : Acurite 606TX Sensor : -5
  102. Battery: OK
  103. Temperature: 21.8 C
  104. 2018-04-13 22:58:01 : Acurite 606TX Sensor : 103
  105. Battery: OK
  106. Temperature: 24.3 C
  107. 2018-04-13 22:58:07 : Acurite 606TX Sensor : -5
  108. Battery: OK
  109. Temperature: 21.8 C
  110. 2018-04-13 22:58:32 : Acurite 606TX Sensor : 103
  111. Battery: OK
  112. Temperature: 24.6 C
  113. 2018-04-13 22:58:38 : Acurite 606TX Sensor : -5
  114. Battery: OK
  115. Temperature: 21.8 C
  116. 2018-04-13 22:59:03 : Acurite 606TX Sensor : 103
  117. Battery: OK
  118. Temperature: 24.8 C
  119. 2018-04-13 22:59:09 : Acurite 606TX Sensor : -5
  120. Battery: OK
  121. Temperature: 21.8 C
  122. `
  123. jasonTestString = `
  124. Registering protocol [1] "Rubicson Temperature Sensor"
  125. Registering protocol [2] "Prologue Temperature Sensor"
  126. Registering protocol [3] "Waveman Switch Transmitter"
  127. Registering protocol [4] "LaCrosse TX Temperature / Humidity Sensor"
  128. Registering protocol [5] "Acurite 609TXC Temperature and Humidity Sensor"
  129. Registering protocol [6] "Oregon Scientific Weather Sensor"
  130. Registering protocol [7] "KlikAanKlikUit Wireless Switch"
  131. Registering protocol [8] "AlectoV1 Weather Sensor (Alecto WS3500 WS4500 Ventus W155/W044 Oregon)"
  132. Registering protocol [9] "Cardin S466-TX2"
  133. Registering protocol [10] "Fine Offset Electronics, WH2 Temperature/Humidity Sensor"
  134. Registering protocol [11] "Nexus Temperature & Humidity Sensor"
  135. Registering protocol [12] "Ambient Weather Temperature Sensor"
  136. Registering protocol [13] "Calibeur RF-104 Sensor"
  137. Registering protocol [14] "GT-WT-02 Sensor"
  138. Registering protocol [15] "Danfoss CFR Thermostat"
  139. Registering protocol [16] "Chuango Security Technology"
  140. Registering protocol [17] "Generic Remote SC226x EV1527"
  141. Registering protocol [18] "TFA-Twin-Plus-30.3049 and Ea2 BL999"
  142. Registering protocol [19] "Fine Offset Electronics WH1080/WH3080 Weather Station"
  143. Registering protocol [20] "WT450"
  144. Registering protocol [21] "LaCrosse WS-2310 Weather Station"
  145. Registering protocol [22] "Esperanza EWS"
  146. Registering protocol [23] "Efergy e2 classic"
  147. Registering protocol [24] "Generic temperature sensor 1"
  148. Registering protocol [25] "WG-PB12V1"
  149. Registering protocol [26] "HIDEKI TS04 Temperature, Humidity, Wind and Rain Sensor"
  150. Registering protocol [27] "Watchman Sonic / Apollo Ultrasonic / Beckett Rocket oil tank monitor"
  151. Registering protocol [28] "CurrentCost Current Sensor"
  152. Registering protocol [29] "emonTx OpenEnergyMonitor"
  153. Registering protocol [30] "HT680 Remote control"
  154. Registering protocol [31] "S3318P Temperature & Humidity Sensor"
  155. Registering protocol [32] "Akhan 100F14 remote keyless entry"
  156. Registering protocol [33] "Quhwa"
  157. Registering protocol [34] "OSv1 Temperature Sensor"
  158. Registering protocol [35] "Proove"
  159. Registering protocol [36] "Bresser Thermo-/Hygro-Sensor 3CH"
  160. Registering protocol [37] "Springfield Temperature and Soil Moisture"
  161. Registering protocol [38] "Oregon Scientific SL109H Remote Thermal Hygro Sensor"
  162. Registering protocol [39] "Acurite 606TX Temperature Sensor"
  163. Registering protocol [40] "TFA pool temperature sensor"
  164. Registering protocol [41] "Kedsum Temperature & Humidity Sensor"
  165. Registering protocol [42] "blyss DC5-UK-WH (433.92 MHz)"
  166. Registering protocol [43] "Steelmate TPMS"
  167. Registering protocol [44] "Schrader TPMS"
  168. Registering protocol [45] "Elro DB286A Doorbell"
  169. Registering protocol [46] "Efergy Optical"
  170. Registering protocol [47] "Honda Car Key"
  171. Registering protocol [48] "Fine Offset Electronics, XC0400"
  172. Registering protocol [49] "Radiohead ASK"
  173. Registering protocol [50] "Kerui PIR Sensor"
  174. Registering protocol [51] "Fine Offset WH1050 Weather Station"
  175. Registering protocol [52] "Honeywell Door/Window Sensor"
  176. Registering protocol [53] "Maverick ET-732/733 BBQ Sensor"
  177. Registering protocol [54] "LaCrosse TX141-Bv2/TX141TH-Bv2 sensor"
  178. Registering protocol [55] "Acurite 00275rm,00276rm Temp/Humidity with optional probe"
  179. Registering protocol [56] "LaCrosse TX35DTH-IT Temperature sensor"
  180. Registering protocol [57] "LaCrosse TX29IT Temperature sensor"
  181. Registering protocol [58] "Vaillant calorMatic 340f Central Heating Control"
  182. Registering protocol [59] "Fine Offset Electronics, WH25 Temperature/Humidity/Pressure Sensor"
  183. Registering protocol [60] "Fine Offset Electronics, WH0530 Temperature/Rain Sensor"
  184. Registering protocol [61] "IBIS beacon"
  185. Registering protocol [62] "Oil Ultrasonic STANDARD FSK"
  186. Registering protocol [63] "Citroen TPMS"
  187. Registering protocol [64] "Oil Ultrasonic STANDARD ASK"
  188. Registering protocol [65] "Thermopro TP11 Thermometer"
  189. Registering protocol [66] "Solight TE44"
  190. Registering protocol [67] "Wireless Smoke and Heat Detector GS 558"
  191. Registering protocol [68] "Generic wireless motion sensor"
  192. Registering protocol [69] "Toyota TPMS"
  193. Registering protocol [70] "Ford TPMS"
  194. Registering protocol [71] "Renault TPMS"
  195. Registering protocol [72] "FT-004-B Temperature Sensor"
  196. Registering protocol [73] "Ford Car Key"
  197. Registering protocol [74] "Philips outdoor temperature sensor"
  198. Registering protocol [75] "Schrader TPMS EG53MA4"
  199. Registering protocol [76] "Nexa"
  200. Registering protocol [77] "Thermopro TP12 Thermometer"
  201. Registering protocol [78] "GE Color Effects"
  202. Registering protocol [79] "X10 Security"
  203. Registering protocol [80] "Interlogix GE UTC Security Devices"
  204. Registered 80 out of 101 device decoding protocols
  205. Found 1 device(s)
  206. trying device 0: Realtek, RTL2838UHIDIR, SN: 00000001
  207. Found Rafael Micro R820T tuner
  208. Using device 0: Generic RTL2832U OEM
  209. Exact sample rate is: 250000.000414 Hz
  210. [R82XX] PLL not locked!
  211. Sample rate set to 250000.
  212. Bit detection level set to 0 (Auto).
  213. Tuner gain set to Auto.
  214. Reading samples in async mode...
  215. Tuned to 433920000 Hz.
  216. ^[ {"time" : "2018-04-13 23:00:05", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
  217. {"time" : "2018-04-13 23:00:11", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  218. {"time" : "2018-04-13 23:00:36", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
  219. {"time" : "2018-04-13 23:00:42", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  220. {"time" : "2018-04-13 23:01:07", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
  221. {"time" : "2018-04-13 23:01:13", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  222. {"time" : "2018-04-13 23:01:38", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
  223. {"time" : "2018-04-13 23:01:44", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  224. {"time" : "2018-04-13 23:02:09", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
  225. {"time" : "2018-04-13 23:02:15", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  226. {"time" : "2018-04-13 23:02:40", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
  227. {"time" : "2018-04-13 23:02:46", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  228. {"time" : "2018-04-13 23:03:11", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
  229. {"time" : "2018-04-13 23:03:17", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  230. {"time" : "2018-04-13 23:03:42", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
  231. {"time" : "2018-04-13 23:03:48", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  232. {"time" : "2018-04-13 23:04:13", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.700}
  233. {"time" : "2018-04-13 23:04:19", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  234. {"time" : "2018-04-13 23:04:44", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.700}
  235. {"time" : "2018-04-13 23:04:50", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  236. `
  237. )