@@ -121,6 +121,9 @@ def discover_cube(self):
121121            logging .log (logging .ERROR , 'Could not send UDP discover brodcast, socket error is: {}' .format (e ))
122122            sys .exit ()
123123
124+         cube_data  =  None 
125+         cube_ip  =  None 
126+ 
124127        while  True :
125128            try :
126129                recv_data , recvaddr  =  udp_recv_socket .recvfrom (4096 )
@@ -152,8 +155,8 @@ def _get_cube_data(self):
152155            tcp_socket .connect ((self .cube_ip , self .cube_port ))
153156        except  (socket .timeout , socket .error ) as  e :
154157            logging .log (logging .ERROR , 'Could not open TCP connection to MAX Cube, socket error is: {}' .format (e ))
155-             return  None 
156158            tcp_socket .close ()
159+             return  None 
157160
158161        received_data  =  b'' 
159162        logging .log (logging .INFO , 'connecting to MAX Cube to retrieve data' )
@@ -193,21 +196,16 @@ def _decode_m_line(m_line):
193196
194197        data ['rooms' ] =  {}
195198        for  i  in  range (data ['room_count' ]):
196-             room  =  {}
197-             room ['id' ] =  ord (decoded .read (1 ))
198-             room ['name_len' ] =  ord (decoded .read (1 ))
199+             room  =  {'id' : ord (decoded .read (1 )), 'name_len' : ord (decoded .read (1 ))}
199200            room ['name' ] =  decoded .read (room ['name_len' ])
200201            room ['rf_address' ] =  binascii .b2a_hex (decoded .read (3 ))
201202            data ['rooms' ][room ['id' ]] =  room 
202203
203204        data ['devices_count' ] =  ord (decoded .read (1 ))
204205        data ['devices' ] =  []
205206        for  i  in  range (data ['devices_count' ]):
206-             device  =  {}
207-             device ['type' ] =  ord (decoded .read (1 ))
208-             device ['rf_address' ] =  binascii .b2a_hex (decoded .read (3 ))
209-             device ['serial' ] =  decoded .read (10 )
210-             device ['name_len' ] =  ord (decoded .read (1 ))
207+             device  =  {'type' : ord (decoded .read (1 )), 'rf_address' : binascii .b2a_hex (decoded .read (3 )),
208+                       'serial' : decoded .read (10 ), 'name_len' : ord (decoded .read (1 ))}
211209            device ['name' ] =  decoded .read (device ['name_len' ])
212210            device ['room_id' ] =  ord (decoded .read (1 ))
213211            data ['devices' ].append (device )
@@ -304,57 +302,7 @@ def get_current_temperature(self, city, units='metric'):
304302            return  None 
305303
306304
307- def  main (args , loglevel ):
308-     last_window_status  =  None 
309-     logging .basicConfig (format = "%(asctime)-15s %(levelname)s: %(message)s" , level = loglevel )
310-     notifier_log_http  =  False 
311-     if  loglevel  ==  logging .DEBUG :
312-         notifier_log_http  =  True 
313-     if  args .user  and  args .token :
314-         notify  =  Notifier (user = args .user , token = args .token , debug = notifier_log_http )
315-     else :
316-         notify  =  Notifier ()
317- 
318-     temperature  =  OpenWeatherMap (args .owmappid )
319- 
320-     logging .log (logging .INFO , 'searching for MAX Cube in the network' )
321-     max_cube  =  MaxConnection (discover_ip_subnet = args .network )
322- 
323-     while  True :
324-         skip_run  =  False 
325-         window_status  =  max_cube .window_switch_status (args .simulation )
326-         logging .log (logging .INFO , 'current window data: {}' .format (window_status ))
327-         outside_temperature  =  temperature .get_current_temperature (args .city )
328-         logging .log (logging .INFO , 'current temperature in {}: {}' .format (args .city , outside_temperature ))
329- 
330-         if  not  window_status :
331-             skip_run  =  True 
332-             logging .log (logging .INFO , 'did not receive any data from MAX Cube, skipping this cycle' )
333-         elif  not  outside_temperature :
334-             skip_run  =  True 
335-             logging .log (logging .INFO , 'did not receive any temperature data, skipping this cycle' )
336-         elif  not  outside_temperature  <=  args .threshold :
337-             skip_run  =  True 
338-             logging .log (logging .INFO , 'current outside temperature above threshold of {}, skipping this ' 
339-                                       'cycle' .format (args .threshold ))
340- 
341-         if  not  last_window_status  and  window_status :
342-             last_window_status  =  window_status 
343- 
344-         if  not  skip_run :
345-             for  rf_addr  in  window_status :
346-                 if  window_status [rf_addr ]['status' ] ==  'open' :
347-                     if  window_status [rf_addr ]['status' ] ==  last_window_status [rf_addr ]['status' ]:
348-                         logging .log (logging .INFO , 'sending notify because of open window' )
349-                         notify .send_msg ('{} was open for more than {} minutes, and the temperature ' 
350-                                         'in {} is {}' .format (window_status [rf_addr ]['name' ],
351-                                                              args .interval , args .city , outside_temperature ))
352-         last_window_status  =  window_status 
353-         logging .log (logging .INFO , 'sleeping for {} minutes' .format (args .interval ))
354-         time .sleep (int (args .interval )* 60 )
355- 
356- 
357- if  __name__  ==  '__main__' :
305+ def  main ():
358306    parser  =  argparse .ArgumentParser (description = "This deamon polls the MAX Cube for all window status. " 
359307                                                 "If a window is open longer than twice the poll interval a " 
360308                                                 "notification will be sent using the notifier plugin" ,
@@ -405,4 +353,54 @@ def main(args, loglevel):
405353    else :
406354        loglevel  =  logging .WARNING 
407355
408-     main (args , loglevel )
356+     last_window_status  =  None 
357+     logging .basicConfig (format = "%(asctime)-15s %(levelname)s: %(message)s" , level = loglevel )
358+     notifier_log_http  =  False 
359+     if  loglevel  ==  logging .DEBUG :
360+         notifier_log_http  =  True 
361+     if  args .user  and  args .token :
362+         notify  =  Notifier (user = args .user , token = args .token , debug = notifier_log_http )
363+     else :
364+         notify  =  Notifier ()
365+ 
366+     temperature  =  OpenWeatherMap (args .owmappid )
367+ 
368+     logging .log (logging .INFO , 'searching for MAX Cube in the network' )
369+     max_cube  =  MaxConnection (discover_ip_subnet = args .network )
370+ 
371+     while  True :
372+         skip_run  =  False 
373+         window_status  =  max_cube .window_switch_status (args .simulation )
374+         logging .log (logging .INFO , 'current window data: {}' .format (window_status ))
375+         outside_temperature  =  temperature .get_current_temperature (args .city )
376+         logging .log (logging .INFO , 'current temperature in {}: {}' .format (args .city , outside_temperature ))
377+ 
378+         if  not  window_status :
379+             skip_run  =  True 
380+             logging .log (logging .INFO , 'did not receive any data from MAX Cube, skipping this cycle' )
381+         elif  not  outside_temperature :
382+             skip_run  =  True 
383+             logging .log (logging .INFO , 'did not receive any temperature data, skipping this cycle' )
384+         elif  not  outside_temperature  <=  args .threshold :
385+             skip_run  =  True 
386+             logging .log (logging .INFO , 'current outside temperature above threshold of {}, skipping this ' 
387+                                       'cycle' .format (args .threshold ))
388+ 
389+         if  not  last_window_status  and  window_status :
390+             last_window_status  =  window_status 
391+ 
392+         if  not  skip_run :
393+             for  rf_addr  in  window_status :
394+                 if  window_status [rf_addr ]['status' ] ==  'open' :
395+                     if  window_status [rf_addr ]['status' ] ==  last_window_status [rf_addr ]['status' ]:
396+                         logging .log (logging .INFO , 'sending notify because of open window' )
397+                         notify .send_msg ('{} was open for more than {} minutes, and the temperature ' 
398+                                         'in {} is {}' .format (window_status [rf_addr ]['name' ],
399+                                                              args .interval , args .city , outside_temperature ))
400+         last_window_status  =  window_status 
401+         logging .log (logging .INFO , 'sleeping for {} minutes' .format (args .interval ))
402+         time .sleep (int (args .interval )* 60 )
403+ 
404+ 
405+ if  __name__  ==  '__main__' :
406+     main ()
0 commit comments