log.go 9.8 KB

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