<s> import pexpect <EOL> import subprocess <EOL> import thread <EOL> from multiprocessing . pool import ThreadPool <EOL> import time <EOL> from settings import Settings <EOL> import requests <EOL> import json <EOL> from logger import Logger <EOL> class Scanner ( ) : <EOL> def __init__ ( self , settings , logger ) : <EOL> self . _logger = logger <EOL> self . _bleScanProc = None <EOL> self . _scanProc = None <EOL> self . _addrToRssi = { } <EOL> self . _keepScanning = True <EOL> self . _settings = settings <EOL> self . _lastFetch = None <EOL> self . _nonDiscKeepScanning = True <EOL> self . _addresses = None <EOL> def log ( self , data ) : <EOL> self . _logger . log ( data ) <EOL> def getAddresses ( self , type = None ) : <EOL> fetchNewAssets = False <EOL> if ( self . _lastFetch is not None ) : <EOL> if ( ( time . time ( ) - self . _lastFetch ) > int ( self . _settings . baseServer_Recache ) ) : <EOL> fetchNewAssets = True <EOL> else : <EOL> fetchNewAssets = True <EOL> if ( fetchNewAssets ) : <EOL> try : <EOL> self . _lastFetch = time . time ( ) <EOL> self . log ( "<STR_LIT>" ) <EOL> resp = requests . get ( self . _settings . baseServer_URL + "<STR_LIT>" + self . _settings . baseServer_Reader , timeout = int ( self . _settings . baseServer_Timeout ) ) <EOL> self . _addresses = resp . json ( ) <EOL> self . log ( "<STR_LIT>" ) <EOL> self . _nonDiscKeepScanning = False <EOL> except Exception , error : <EOL> self . log ( "<STR_LIT>" + str ( error ) + "<STR_LIT>" ) <EOL> if ( type is None ) : <EOL> return self . _addresses <EOL> elif ( type is "<STR_LIT:all>" ) : <EOL> return dict ( self . _addresses [ "<STR_LIT>" ] . items ( ) + self . _addresses [ "<STR_LIT>" ] . items ( ) + self . _addresses [ "<STR_LIT>" ] . items ( ) ) <EOL> else : <EOL> return self . _addresses [ type ] <EOL> def send_payload ( self , addr , rssi ) : <EOL> if ( addr in self . getAddresses ( "<STR_LIT:all>" ) ) : <EOL> self . _addrToRssi [ addr ] = rssi ; <EOL> if ( addr in self . getAddresses ( "<STR_LIT>" ) ) : <EOL> type = "<STR_LIT>" <EOL> elif ( addr in self . getAddresses ( "<STR_LIT>" ) ) : <EOL> type = "<STR_LIT>" <EOL> else : <EOL> type = "<STR_LIT>" <EOL> if ( ( type == "<STR_LIT>" and self . _settings . scanMode_LE ) or ( type == "<STR_LIT>" and self . _settings . scanMode_Disc ) or ( type == "<STR_LIT>" and self . _settings . scanMode_NonDisc ) ) : <EOL> try : <EOL> payload = { "<STR_LIT>" : self . _settings . baseServer_Reader , "<STR_LIT:name>" : self . getAddresses ( "<STR_LIT:all>" ) [ addr ] [ "<STR_LIT:name>" ] , "<STR_LIT:address>" : addr , "<STR_LIT>" : rssi , "<STR_LIT:type>" : type } <EOL> self . log ( "<STR_LIT>" + self . _settings . baseServer_URL ) <EOL> self . log ( "<STR_LIT>" ) <EOL> self . log ( json . dumps ( payload , indent = <NUM_LIT:4> ) ) <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> resp = requests . post ( self . _settings . baseServer_URL , data = json . dumps ( payload ) , headers = headers , timeout = int ( self . _settings . baseServer_Timeout ) ) <EOL> self . log ( "<STR_LIT>" + str ( resp . status_code ) ) <EOL> except Exception , error : <EOL> self . log ( "<STR_LIT>" + str ( error ) ) <EOL> def scanProcessSpawner ( self ) : <EOL> while self . _keepScanning : <EOL> self . _scanProc = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' ] , cwd = '<STR_LIT>' , stderr = subprocess . STDOUT , stdout = subprocess . PIPE ) <EOL> self . _scanProc . wait ( ) <EOL> if ( self . _settings . readTimeout_Disc > <NUM_LIT:0> ) : <EOL> time . sleep ( self . _settings . readTimeout_Disc ) <EOL> def nonDiscScanProcessSpawner ( self , addr ) : <EOL> while ( self . _nonDiscKeepScanning ) : <EOL> result = pexpect . run ( "<STR_LIT>" + addr , timeout = None ) <EOL> if "<STR_LIT>" not in result : <EOL> pexpect . run ( "<STR_LIT>" + addr , timeout = None ) <EOL> else : <EOL> rssi = result . rstrip ( ) . split ( "<STR_LIT>" ) [ <NUM_LIT:1> ] <EOL> if ( addr in self . _addrToRssi ) : <EOL> if ( rssi != self . _addrToRssi [ addr ] ) : <EOL> self . send_payload ( addr , rssi ) <EOL> else : <EOL> self . send_payload ( addr , rssi ) <EOL> if ( self . _settings . readTimeout_NonDisc > <NUM_LIT:0> ) : <EOL> time . sleep ( self . _settings . readTimeout_NonDisc ) <EOL> def nonDiscScanPoolInitiate ( self ) : <EOL> pool = ThreadPool ( processes = <NUM_LIT:10> ) <EOL> while self . _keepScanning : <EOL> self . log ( "<STR_LIT>" ) <EOL> self . _nonDiscKeepScanning = True <EOL> pool . map ( self . nonDiscScanProcessSpawner , self . getAddresses ( "<STR_LIT>" ) . keys ( ) ) <EOL> def bleScanProccessSpawnerAsync ( self ) : <EOL> self . _bleScanProc = subprocess . Popen ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , cwd = '<STR_LIT>' , stderr = subprocess . STDOUT , stdout = subprocess . PIPE ) <EOL> def parseHcidump ( self ) : <EOL> self . log ( "<STR_LIT>" ) <EOL> child = pexpect . spawn ( "<STR_LIT>" , timeout = None ) <EOL> while self . _keepScanning : <EOL> child . expect ( "<STR_LIT>" , timeout = None ) <EOL> addr = child . after <EOL> if ( addr in self . getAddresses ( "<STR_LIT>" ) ) : <EOL> continue <EOL> child . expect ( "<STR_LIT>" , timeout = None ) <EOL> rssi = child . after <EOL> if ( addr in self . _addrToRssi ) : <EOL> if ( rssi != self . _addrToRssi [ addr ] ) : <EOL> self . send_payload ( addr , rssi ) <EOL> else : <EOL> self . send_payload ( addr , rssi ) <EOL> def StartScanning ( self ) : <EOL> self . log ( json . dumps ( self . getAddresses ( ) , indent = <NUM_LIT:4> ) ) <EOL> if ( self . _settings . scanMode_LE ) : <EOL> self . log ( "<STR_LIT>" ) <EOL> self . bleScanProccessSpawnerAsync ( ) <EOL> if ( self . _settings . scanMode_Disc ) : <EOL> self . log ( "<STR_LIT>" ) <EOL> thread . start_new_thread ( self . scanProcessSpawner , ( ) ) <EOL> if ( self . _settings . scanMode_NonDisc ) : <EOL> self . log ( "<STR_LIT>" ) <EOL> thread . start_new_thread ( self . nonDiscScanPoolInitiate , ( ) ) <EOL> self . parseHcidump ( ) <EOL> def StopScanning ( self ) : <EOL> self . _keepScanning = False <EOL> if ( self . _settings . scanMode_LE ) : <EOL> self . log ( "<STR_LIT>" ) <EOL> self . log ( self . _bleScanProc . pid ) <EOL> try : self . _bleScanProc . terminate ( ) <EOL> except ( OSError ) : pass <EOL> self . log ( "<STR_LIT>" ) <EOL> if ( self . _settings . scanMode_Disc ) : <EOL> self . log ( "<STR_LIT>" ) <EOL> self . log ( self . _scanProc . pid ) <EOL> try : self . _scanProc . terminate ( ) <EOL> except ( OSError ) : pass <EOL> self . log ( "<STR_LIT>" ) </s>
