json_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package temperature
  2. import (
  3. "bufio"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. )
  8. var validStrings = []string{`{"time" : "2018-04-13 23:04:50", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}`,
  9. `{"time" : "2018-04-13 23:04:50", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  10. `,
  11. }
  12. var invalidStrings = []string{`sfdxcvxvczxcvzxcvzxcvzxsdfasdfasd
  13. sdfsdf
  14. sd
  15. f
  16. sdf
  17. sdf
  18. fasdfzx`, `asdfasdfavxc
  19. sfdsdsdf`,
  20. `{"timsdfe" : "2018-04-13 23:04:50", "modsdfsel" : "Acurite 606TX Sensor", "idsdf" : -5, "battsdfsdery" : "OK", "tempersdfsdature_C" : 21.800}`,
  21. }
  22. func TestValidJson(t *testing.T) {
  23. for _, string := range validStrings {
  24. reader := bufio.NewReader(strings.NewReader(string))
  25. parser := NewJSONParser(reader)
  26. measurement, err := parser.Measure()
  27. if err != nil {
  28. t.Errorf("Recieved error: %s", err)
  29. }
  30. if measurement.ID != -5 {
  31. t.Errorf("ID was incorrect. Expect %d but got %d", -5, measurement.ID)
  32. }
  33. }
  34. }
  35. func TestInvalidJson(t *testing.T) {
  36. for _, string := range invalidStrings {
  37. reader := bufio.NewReader(strings.NewReader(string))
  38. parser := NewJSONParser(reader)
  39. measurement, err := parser.Measure()
  40. if err != nil {
  41. t.Errorf("Invalid strings are not an error")
  42. }
  43. if measurement != nil {
  44. t.Errorf("There should have been no measurement for %s", string)
  45. }
  46. }
  47. }
  48. func TestRealString(t *testing.T) {
  49. fmt.Println("starting")
  50. reader := bufio.NewReader(strings.NewReader(realString))
  51. parser := NewJSONParser(reader)
  52. var measurements = make([]Measurement, 0, 100)
  53. for measurement, err := parser.Measure(); err == nil; measurement, err = parser.Measure() {
  54. if measurement != nil {
  55. measurements = append(measurements, *measurement)
  56. }
  57. }
  58. if len(measurements) != 20 {
  59. t.Errorf("Expected %d measurements. Got %d", 20, len(measurements))
  60. }
  61. }
  62. var realString = `Registering protocol [1] "Rubicson Temperature Sensor"
  63. Registering protocol [2] "Prologue Temperature Sensor"
  64. Registering protocol [3] "Waveman Switch Transmitter"
  65. Registering protocol [4] "LaCrosse TX Temperature / Humidity Sensor"
  66. Registering protocol [5] "Acurite 609TXC Temperature and Humidity Sensor"
  67. Registering protocol [6] "Oregon Scientific Weather Sensor"
  68. Registering protocol [7] "KlikAanKlikUit Wireless Switch"
  69. Registering protocol [8] "AlectoV1 Weather Sensor (Alecto WS3500 WS4500 Ventus W155/W044 Oregon)"
  70. Registering protocol [9] "Cardin S466-TX2"
  71. Registering protocol [10] "Fine Offset Electronics, WH2 Temperature/Humidity Sensor"
  72. Registering protocol [11] "Nexus Temperature & Humidity Sensor"
  73. Registering protocol [12] "Ambient Weather Temperature Sensor"
  74. Registering protocol [13] "Calibeur RF-104 Sensor"
  75. Registering protocol [14] "GT-WT-02 Sensor"
  76. Registering protocol [15] "Danfoss CFR Thermostat"
  77. Registering protocol [16] "Chuango Security Technology"
  78. Registering protocol [17] "Generic Remote SC226x EV1527"
  79. Registering protocol [18] "TFA-Twin-Plus-30.3049 and Ea2 BL999"
  80. Registering protocol [19] "Fine Offset Electronics WH1080/WH3080 Weather Station"
  81. Registering protocol [20] "WT450"
  82. Registering protocol [21] "LaCrosse WS-2310 Weather Station"
  83. Registering protocol [22] "Esperanza EWS"
  84. Registering protocol [23] "Efergy e2 classic"
  85. Registering protocol [24] "Generic temperature sensor 1"
  86. Registering protocol [25] "WG-PB12V1"
  87. Registering protocol [26] "HIDEKI TS04 Temperature, Humidity, Wind and Rain Sensor"
  88. Registering protocol [27] "Watchman Sonic / Apollo Ultrasonic / Beckett Rocket oil tank monitor"
  89. Registering protocol [28] "CurrentCost Current Sensor"
  90. Registering protocol [29] "emonTx OpenEnergyMonitor"
  91. Registering protocol [30] "HT680 Remote control"
  92. Registering protocol [31] "S3318P Temperature & Humidity Sensor"
  93. Registering protocol [32] "Akhan 100F14 remote keyless entry"
  94. Registering protocol [33] "Quhwa"
  95. Registering protocol [34] "OSv1 Temperature Sensor"
  96. Registering protocol [35] "Proove"
  97. Registering protocol [36] "Bresser Thermo-/Hygro-Sensor 3CH"
  98. Registering protocol [37] "Springfield Temperature and Soil Moisture"
  99. Registering protocol [38] "Oregon Scientific SL109H Remote Thermal Hygro Sensor"
  100. Registering protocol [39] "Acurite 606TX Temperature Sensor"
  101. Registering protocol [40] "TFA pool temperature sensor"
  102. Registering protocol [41] "Kedsum Temperature & Humidity Sensor"
  103. Registering protocol [42] "blyss DC5-UK-WH (433.92 MHz)"
  104. Registering protocol [43] "Steelmate TPMS"
  105. Registering protocol [44] "Schrader TPMS"
  106. Registering protocol [45] "Elro DB286A Doorbell"
  107. Registering protocol [46] "Efergy Optical"
  108. Registering protocol [47] "Honda Car Key"
  109. Registering protocol [48] "Fine Offset Electronics, XC0400"
  110. Registering protocol [49] "Radiohead ASK"
  111. Registering protocol [50] "Kerui PIR Sensor"
  112. Registering protocol [51] "Fine Offset WH1050 Weather Station"
  113. Registering protocol [52] "Honeywell Door/Window Sensor"
  114. Registering protocol [53] "Maverick ET-732/733 BBQ Sensor"
  115. Registering protocol [54] "LaCrosse TX141-Bv2/TX141TH-Bv2 sensor"
  116. Registering protocol [55] "Acurite 00275rm,00276rm Temp/Humidity with optional probe"
  117. Registering protocol [56] "LaCrosse TX35DTH-IT Temperature sensor"
  118. Registering protocol [57] "LaCrosse TX29IT Temperature sensor"
  119. Registering protocol [58] "Vaillant calorMatic 340f Central Heating Control"
  120. Registering protocol [59] "Fine Offset Electronics, WH25 Temperature/Humidity/Pressure Sensor"
  121. Registering protocol [60] "Fine Offset Electronics, WH0530 Temperature/Rain Sensor"
  122. Registering protocol [61] "IBIS beacon"
  123. Registering protocol [62] "Oil Ultrasonic STANDARD FSK"
  124. Registering protocol [63] "Citroen TPMS"
  125. Registering protocol [64] "Oil Ultrasonic STANDARD ASK"
  126. Registering protocol [65] "Thermopro TP11 Thermometer"
  127. Registering protocol [66] "Solight TE44"
  128. Registering protocol [67] "Wireless Smoke and Heat Detector GS 558"
  129. Registering protocol [68] "Generic wireless motion sensor"
  130. Registering protocol [69] "Toyota TPMS"
  131. Registering protocol [70] "Ford TPMS"
  132. Registering protocol [71] "Renault TPMS"
  133. Registering protocol [72] "FT-004-B Temperature Sensor"
  134. Registering protocol [73] "Ford Car Key"
  135. Registering protocol [74] "Philips outdoor temperature sensor"
  136. Registering protocol [75] "Schrader TPMS EG53MA4"
  137. Registering protocol [76] "Nexa"
  138. Registering protocol [77] "Thermopro TP12 Thermometer"
  139. Registering protocol [78] "GE Color Effects"
  140. Registering protocol [79] "X10 Security"
  141. Registering protocol [80] "Interlogix GE UTC Security Devices"
  142. Registered 80 out of 101 device decoding protocols
  143. Found 1 device(s)
  144. trying device 0: Realtek, RTL2838UHIDIR, SN: 00000001
  145. Found Rafael Micro R820T tuner
  146. Using device 0: Generic RTL2832U OEM
  147. Exact sample rate is: 250000.000414 Hz
  148. [R82XX] PLL not locked!
  149. Sample rate set to 250000.
  150. Bit detection level set to 0 (Auto).
  151. Tuner gain set to Auto.
  152. Reading samples in async mode...
  153. Tuned to 433920000 Hz.
  154. ^[ {"time" : "2018-04-13 23:00:05", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
  155. {"time" : "2018-04-13 23:00:11", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  156. {"time" : "2018-04-13 23:00:36", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
  157. {"time" : "2018-04-13 23:00:42", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  158. {"time" : "2018-04-13 23:01:07", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
  159. {"time" : "2018-04-13 23:01:13", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  160. {"time" : "2018-04-13 23:01:38", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
  161. {"time" : "2018-04-13 23:01:44", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  162. {"time" : "2018-04-13 23:02:09", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.900}
  163. {"time" : "2018-04-13 23:02:15", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  164. {"time" : "2018-04-13 23:02:40", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
  165. {"time" : "2018-04-13 23:02:46", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  166. {"time" : "2018-04-13 23:03:11", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
  167. {"time" : "2018-04-13 23:03:17", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  168. {"time" : "2018-04-13 23:03:42", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.800}
  169. {"time" : "2018-04-13 23:03:48", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  170. {"time" : "2018-04-13 23:04:13", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.700}
  171. {"time" : "2018-04-13 23:04:19", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}
  172. {"time" : "2018-04-13 23:04:44", "model" : "Acurite 606TX Sensor", "id" : 103, "battery" : "OK", "temperature_C" : 24.700}
  173. {"time" : "2018-04-13 23:04:50", "model" : "Acurite 606TX Sensor", "id" : -5, "battery" : "OK", "temperature_C" : 21.800}`