@@ -16,87 +16,42 @@ class SensorData(object):
16
16
battery ('Normal')
17
17
"""
18
18
19
-
20
- def parse (live_data_html ):
21
- """
22
- Extract sensor's data from html (LiveData.html from your ObserverIP)
23
- Returns touple with (sensor1, sensor2 -> SensorData)
24
- """
25
-
26
- tree = html .fromstring (live_data_html )
27
- title = tree .xpath ('//title/text()' )
28
- if title [0 ] != TITLE :
29
- raise Exception ('Wrong html page. Good one have to have title {}' .format (TITLE ))
30
-
31
- inSensor = SensorData ()
32
- time_str = tree .xpath ('//input[@name="CurrTime"]/@value' )[0 ]
33
- inSensor .time = datetime .strptime (time_str , '%H:%M %m/%d/%Y' )
34
- inSensor .temp = float (tree .xpath ('//input[@name="inTemp"]/@value' )[0 ])
35
- inSensor .humidity = float (tree .xpath ('//input[@name="inHumi"]/@value' )[0 ])
36
- inSensor .abs_press = float (tree .xpath ('//input[@name="AbsPress"]/@value' )[0 ])
37
- inSensor .rel_press = float (tree .xpath ('//input[@name="RelPress"]/@value' )[0 ])
38
- inSensor .battery = tree .xpath ('//input[@name="inBattSta"]/@value' )[0 ]
39
-
40
- outSensor = SensorData ()
41
- outSensor .time = inSensor .time
42
- outSensor .temp = float (tree .xpath ('//input[@name="outTemp"]/@value' )[0 ])
43
- outSensor .humidity = float (tree .xpath ('//input[@name="outHumi"]/@value' )[0 ])
44
- outSensor .abs_press = inSensor .abs_press
45
- outSensor .rel_press = inSensor .rel_press
46
- outSensor .battery = tree .xpath ('//input[@name="outBattSta2"]/@value' )[0 ]
47
-
48
- return inSensor , outSensor
49
-
50
-
51
- def get (url ):
52
- """
53
- Load ObserverIP live data page from the URL and parse it
54
-
55
- Returns touple with (sensor1, sensor2 -> SensorData)
56
- """
57
-
58
- page = requests .get (url ).content
59
- return parse (page )
60
-
61
-
62
- def test ():
63
- with open ('LiveData.html' , 'r' ) as f :
64
- page = f .read ()
65
-
66
- inSensor , outSensor = parse (page )
67
-
68
- print ('Time: {}\n ' .format (inSensor .time ))
69
- print ('''Indoor\n {delimiter}\n Temperature: {temp}\n Humidity: {humidity}
70
- Absolute preassure: {abs_press}\n Relative preassure: {rel_press}\n Battery status: {battery}\n ''' .format (
71
- delimiter = '=' * 20 ,
72
- temp = inSensor .temp ,
73
- humidity = inSensor .humidity ,
74
- abs_press = inSensor .abs_press ,
75
- rel_press = inSensor .rel_press ,
76
- battery = inSensor .battery
77
- ))
78
- print ('''Outdoor\n {delimiter}\n Temperature: {temp}\n Humidity: {humidity}
79
- Battery status: {battery}\n ''' .format (
80
- delimiter = '=' * 20 ,
81
- temp = inSensor .temp ,
82
- humidity = inSensor .humidity ,
83
- battery = inSensor .battery
84
- ))
85
-
86
- assert inSensor .time == datetime .strptime ('2017/10/06 13:55:00' , '%Y/%m/%d %H:%M:%S' )
87
- assert inSensor .temp == 21.9
88
- assert inSensor .humidity == 50.0
89
- assert inSensor .abs_press == 744.29
90
- assert inSensor .rel_press == 728.31
91
- assert inSensor .battery == 'Normal'
92
- assert outSensor .temp == 10.1
93
- assert outSensor .humidity == 71.0
94
- assert outSensor .battery == 'Normal'
95
-
96
-
97
- if __name__ == '__main__' :
98
- test ()
99
-
100
-
101
-
102
-
19
+ def parse (self , live_data_html ):
20
+ """
21
+ Extract sensor's data from html (LiveData.html from your ObserverIP)
22
+ Returns touple with (sensor1, sensor2 -> SensorData)
23
+ """
24
+
25
+ tree = html .fromstring (live_data_html )
26
+ title = tree .xpath ('//title/text()' )
27
+ if title [0 ] != TITLE :
28
+ raise Exception ('Wrong html page. Good one have to have title {}' .format (TITLE ))
29
+
30
+ inSensor = SensorData ()
31
+ time_str = tree .xpath ('//input[@name="CurrTime"]/@value' )[0 ]
32
+ inSensor .time = datetime .strptime (time_str , '%H:%M %m/%d/%Y' )
33
+ inSensor .temp = float (tree .xpath ('//input[@name="inTemp"]/@value' )[0 ])
34
+ inSensor .humidity = float (tree .xpath ('//input[@name="inHumi"]/@value' )[0 ])
35
+ inSensor .abs_press = float (tree .xpath ('//input[@name="AbsPress"]/@value' )[0 ])
36
+ inSensor .rel_press = float (tree .xpath ('//input[@name="RelPress"]/@value' )[0 ])
37
+ inSensor .battery = tree .xpath ('//input[@name="inBattSta"]/@value' )[0 ]
38
+
39
+ outSensor = SensorData ()
40
+ outSensor .time = inSensor .time
41
+ outSensor .temp = float (tree .xpath ('//input[@name="outTemp"]/@value' )[0 ])
42
+ outSensor .humidity = float (tree .xpath ('//input[@name="outHumi"]/@value' )[0 ])
43
+ outSensor .abs_press = inSensor .abs_press
44
+ outSensor .rel_press = inSensor .rel_press
45
+ outSensor .battery = tree .xpath ('//input[@name="outBattSta2"]/@value' )[0 ]
46
+
47
+ return inSensor , outSensor
48
+
49
+ def get (self , url ):
50
+ """
51
+ Load ObserverIP live data page from the URL and parse it
52
+
53
+ Returns touple with (sensor1, sensor2 -> SensorData)
54
+ """
55
+
56
+ page = requests .get (url ).content
57
+ return self .parse (page )
0 commit comments