def set_timeout(self, _timeout):
     """
     Sets time period between consecutive sends
     @type _timeout: number
     @param _timeout: the timeout value to be set
     """
     if (self._MIN_TIMEOUT <= _timeout and _timeout <= self._MAX_TIMEOUT):
         self.timeout = _timeout
         self.timer = RepeatTimer(self.timeout, self.send_msg)
         self.timer.start()
     else:
         DEBUG('Invalid value!')
Exemple #2
0
class GenericPoll(BaseObject):
    def __init__(self, **kwargs):
        super(GenericPoll, self).__init__(**kwargs)
        self.interval = kwargs.get('interval', 2.0)
        self.polling = False
        self.interval_call = kwargs.get('interval_call')
        self.timer = None
    def start_poll(self, **kwargs):
        self.stop_poll()
        self.timer = RepeatTimer(self.interval, self.on_interval)
        self.polling = True
        self.timer.start()
    def stop_poll(self, **kwargs):
        if self.polling:
            self.polling = False
            self.timer.cancel()
        self.timer = None
    def on_interval(self):
        if self.interval_call is not None:
            self.interval_call()
 def set_timeout(self, _timeout):
     """
     Sets time period between consecutive sends
     @type _timeout: number
     @param _timeout: the timeout value to be set
     """
     if(self._MIN_TIMEOUT <= _timeout and _timeout <= self._MAX_TIMEOUT):
         self.timeout = _timeout
         self.timer = RepeatTimer(self.timeout,self.send_msg)
         self.timer.start()
     else:
         DEBUG('Invalid value!')
Exemple #4
0
    def run_application(self, nb_worker=8, nb_operations=100):
        thread_list = []
        for i in range(nb_worker):
            client = myClient(self.node_id, i, nb_operations, self.key_range,
                              self.send_queue, self.recv_queue,
                              self.performance_queue, self._lock, self.metrics)
            thread_list.append(client)
            client.start()

        RepeatTimer(self.interval, self.monitor_thread).start()

        for thread in thread_list:
            thread.join()
 def runButton(self, event):  # wxGlade: SinFrogMainWindow.<event_handler>
     if shell.IsUserAnAdmin():
         self.SinFrogMainWindow_statusbar.SetStatusText("Running tests.")
         display = DisplayFrame(None)
         display.Show()
         #Wait 900 seconds = 15 minutes
         self.r = RepeatTimer(900.0, runTests, 0, [display, self.userIdentification])
         #self.r = threading.Thread(runTests(display, self.userIdentification))
         self.r.start()
     else:
         administratorFrame = Administrator(None)
         administratorFrame.Show()
         administratorFrame.Raise()
Exemple #6
0
class GenericPoll(BaseObject):
    def __init__(self, **kwargs):
        super(GenericPoll, self).__init__(**kwargs)
        self.interval = kwargs.get('interval', 2.0)
        self.polling = False
        self.interval_call = kwargs.get('interval_call')
        self.timer = None

    def start_poll(self, **kwargs):
        self.stop_poll()
        self.timer = RepeatTimer(self.interval, self.on_interval)
        self.polling = True
        self.timer.start()

    def stop_poll(self, **kwargs):
        if self.polling:
            self.polling = False
            self.timer.cancel()
        self.timer = None

    def on_interval(self):
        if self.interval_call is not None:
            self.interval_call()
Exemple #7
0
 def start_poll(self, **kwargs):
     self.stop_poll()
     self.timer = RepeatTimer(self.interval, self.on_interval)
     self.polling = True
     self.timer.start()
 def run(self):
     LOGMSG("Sensor thread running..")
     self.timer = RepeatTimer(self.timeout,self.send_rcv_msg) # For now, send only temp requests
     self.timer.start()
class Sensor(Thread):
    """
    The Data Retrieval Module (DRM). 
    
    Its role is to request sensor and statistical data from connected devices, and store that information in the database
    
        - Uses a timeout to periodically request data from Base Station
    """
    DEBUG_MODE = 0
    """
    Operational mode. 
    
    If debug mode is switched, the program will skip all procedures and will execute the custom code located after the control statement:
            
    if(not DEBUG_MODE):
    ...
    else:
    """
    #Thresholds
    maxtemp = 0
    mintemp = 0


    # Tuple containing board addresses
    boards = {}
    closed = 0
    paused = 0
    text = ''

    #polling intervals
    sleep = 2
    stat_interval = 6 # multiply with timeout to get time, set to 0 to disable
    reset_interval = 0#12
    timeout = 4*sleep
    _MIN_TIMEOUT = 0
    _MAX_TIMEOUT = 120
    _DEFAULT_TIMEOUT = 3

    _BCAST = b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'
    """
    Hex address of the broadcast address
    """
    _SLIP_ADDR = 'SLIPCC'
    """
    Default device address. It should be followed by the device's hardware address, repeated twice.
    """

    def __init__(self, serial):
        self.closed = 0
        self.serial = serial
        self.stat_counter = 0
        self.reset_counter = 0
        LOGMSG("Sensor thread started..")
        time.sleep(1)
        Thread.__init__ ( self )

    def run(self):
        LOGMSG("Sensor thread running..")
        self.timer = RepeatTimer(self.timeout,self.send_rcv_msg) # For now, send only temp requests
        self.timer.start()

    def close(self):
        LOGMSG("Sensor thread closing...")
        self.timer.cancel()
        time.sleep(3)
        self.send_reset()
        try:
           sys.exit(0)
        except SystemExit:
            pass
        
    def send_reset(self):
        msg = pack('8sBBB',self._BCAST,kelvin.DISPATCH,kelvin.CMD | kelvin.RESET, 0)
        self.serial.write(msg)

    def sht_temp_scale( self , data ):
        """
        Calibrate temperature data.
        @type data: number
        @param data: the temperature reading
        @return: the calibrated temperature reading
        """
        #(value,) = unpack( '>h' , data )
        value = data[0] << 8 | data[1]
        #Formula for calculating the temperature from the SHT at 3.3v
        scaled = (value/100.0 - 39.65)
        DEBUG( str(value) + ' (raw) -> Scaled!! ' + str(scaled) )
        return scaled

    def sht_hum_scale(self, data):
        """
        Calibrate humidity data.
        @type data: number
        @param data: the humidity reading
        @return: the calibrated humidity reading
        """
        value = data[0] << 8 | data[1]
        scaled = (value*0.0367) -2.0468 + ((value*value)*(-0.000001595))
        DEBUG(str(value) + '(raw) -> scaled!! ' + str(scaled))
        return scaled

    def light_scale( self , data ):
        """
        Calibrate light data.
        @type data: number
        @param data: the light reading
        @return: the calibrated light reading
        """
        #Equation to convert to LUX is ADC val * 5
        return data*5

    def pressure_scale( self , data ):
        """
        Calibrate pressure data.
        @type data: number
        @param data: the pressure reading
        @return: the calibrated pressurereading
        """
        #Equation to convert to kPa
        return (((data*11)/2125)+0.095)/0.009

    def read_data(self) :
        """
        Read incoming packages and process them.
        
        Uses base64 encoding and decoding to read data.
            - Checks for address validity and tries to amend corrupted addresses.
            - If unable to amend address, discards packet
            - Reads and stores data in database
            - If the data comes from a temperature sensors, sends a LED colour change command to the device
        """
        self.text = ''
        for y in self.serial:
            if( len(y) < 11 ):
                continue
            
            length = y[10]
            address = ''
            dispatch = ''
            cmd = 0
            datalen = 0
            if(length == 0):
                fmt = ('8sBBB')
                try:
                    (address , dispatch , cmd , datalen) = unpack( fmt  , y )
                except:
                    LOGMSG(y)
                    continue
            
            elif(length == 5):
                try:
                    (address, dispatch, cmd, datalen, rtt, hops) = unpack('>8sBBBLB',y)
                except:
                    LOGMSG(y)
                    continue
            else:
                fmt = ('8sBBB%ds' % ( length ));
                try:
                    (address , dispatch , cmd , datalen , data ) = unpack( fmt  , y )
                except:
                    LOGMSG(y)
                    continue
                        
            similar = self.sim(address[:6].decode('utf-8'),'SLIPCC')
            if(similar == 6 and address[6] == address[7]):
                addr = self.format_address(address)
            elif((similar < 6 and similar > 3) and address[6] == address[7]):
                addr = self.format_address(b'SLIPCC'+address[6:8])                    
            else:
                addr = ''

            if(addr != ''):
                tm = round(time.time())                                
                if( cmd == kelvin.TEMP ):
                    scaled = self.sht_temp_scale(data)
                    LOGMSG("Node %02X gives temp %d\n" % (address[7] , scaled) )
                    self.add_to_list(addr,tm)
                    if(scaled <self.maxtemp and scaled > self.mintemp):
                        LOGMSG("Measured temperature is within norms")
                        msg = pack('8sBBBB',address,kelvin.DISPATCH,kelvin.CMD | kelvin.LED,1,5)
                        self.serial.write(msg)
                    elif(scaled < self.mintemp):
                        LOGMSG("Measured temperature is below norms")
                        msg = pack('8sBBBB',address,kelvin.DISPATCH, kelvin.CMD | kelvin.LED, 1,3)
                        self.serial.write(msg)
                    elif(scaled > self.maxtemp):
                        LOGMSG("Measured temperature is above norms")
                        msg = pack('8sBBBB',address,kelvin.DISPATCH, kelvin.CMD | kelvin.LED, 1,6)
                        self.serial.write(msg)
                    self.req_queue.put((1,self.db.set_measurements,('temp',addr,self.sht_temp_scale(data),tm)))
                elif( cmd == kelvin.HUM):
                    scaled = self.sht_hum_scale(data)
                    LOGMSG("Node %02X gives humidity %02X\n" % (address[7] , scaled) )
                    self.add_to_list(addr,tm)
                    self.req_queue.put((1,self.db.set_measurements,('humidity',addr,self.sht_hum_scale(data),tm)))
                elif(cmd == kelvin.PRSR):
                    LOGMSG("Node %02X gives pressure %02X\n" % (address[7],data[0]))
                    self.req_queue.put((1,self.db.set_measurements,('pressure',addr,self.pressure_scale(data[0]),tm)))
                elif(cmd == kelvin.LGHT):
                    LOGMSG("Node %02X gives light %02X\n" % (address[7],data[0]))
                    self.req_queue.put((1,self.db.set_measurements,('light',addr,self.light_scale(data[0]),tm)))
                elif(cmd == kelvin.PING):
                    #if(rtt < 10000):
                    stat_string = "%d: Got PING stats for node %02X: RTT is: %d; HOPS is: %d" % (time.time(), address[7],rtt,hops)
                    LOGMSG(stat_string)
                    try:
                        file = open('network_stats.log', 'a')
                        file.write(stat_string + "\n")
                        file.close()
                    except:
                        pass
                    #self.req_queue.put((1,self.db.set_stat_data,(addr, rtt,hops,tm)))
    
    def set_timeout(self, _timeout):
        """
        Sets time period between consecutive sends
        @type _timeout: number
        @param _timeout: the timeout value to be set
        """
        if(self._MIN_TIMEOUT <= _timeout and _timeout <= self._MAX_TIMEOUT):
            self.timeout = _timeout
            self.timer = RepeatTimer(self.timeout,self.send_msg)
            self.timer.start()
        else:
            DEBUG('Invalid value!')

    def reset_timeout(self):
        """
        Resets the timeout value to its default value
        """
        self.timeout = self._DEFAULT_TIMEOUT

    def send_rcv_msg(self):
        """
        Sends requests and reads responses
        
            - Uses a small sleep interval between each transmission to prevent race conditions
            - checks for active devices        
        """
        if (not self.DEBUG_MODE):
            try:
                if self.stat_interval != 0:
                    if self.stat_counter > self.stat_interval:
                        time.sleep(self.sleep)
                        msg = pack('8sBBB',self._BCAST,kelvin.DISPATCH, kelvin.CMD | kelvin.PING,0)
                        self.serial.write(msg)
                    
                        self.stat_counter = 0

                        return # reading sensor data should not interfere with RTT!
                    else:
                        self.stat_counter += 1
                        
                msg = pack('8sBBB',self._BCAST,kelvin.DISPATCH,kelvin.CMD | kelvin.TEMP, 0)
                self.serial.write(msg)
                time.sleep(self.sleep)
                msg = pack('8sBBB',self._BCAST,kelvin.DISPATCH,kelvin.CMD | kelvin.HUM, 0)
                self.serial.write(msg)
                time.sleep(self.sleep)
                msg = pack('8sBBB',self._BCAST,kelvin.DISPATCH,kelvin.CMD | kelvin.PRSR, 0)
                self.serial.write(msg)
                time.sleep(self.sleep)
                msg = pack('8sBBB',self._BCAST,kelvin.DISPATCH,kelvin.CMD | kelvin.LGHT, 0)
                self.serial.write(msg)
            except ValueError:
                self.close()
                
            self.read_data()
            #self.print_buffer()
            self.check_board_status()

            if self.reset_interval != 0:
                if self.reset_counter > self.reset_interval:
                    time.sleep(self.sleep)
                    self.send_reset()
                    time.sleep(self.sleep)
                    self.reset_counter = 0
                else:
                    self.reset_counter += 1
            
        else:
            # Add new code to be debugged here
            msg = pack('8sBBB',self._BCAST,kelvin.DISPATCH, kelvin.CMD | kelvin.PING,0)
            self.serial.write(msg)
            self.read_data()
            #self.send_reset()
            #time.sleep(self.sleep)

    def set_db_data(self,_req_queue,_db):
        self.req_queue = _req_queue
        self.db = _db

    def format_address(self,addr):
        """
        Converts the address into more readible form. Used when storing data in database
        """
        _str = ''
        for x in addr:
            if( x <= 0xF ):
                _str += '0'
            _str += hex(x) + ':'

        return _str[:-1].replace("0x","")


    def add_to_list(self,addr,tm):
        self.boards[addr] = tm

    def remove_from_list(self,addr):
        del self.boards[addr]

    def check_board_status(self):
        """
        Performs a check on all devices in the active list. 
            - If a device has not responded for the past 4*timeout iterations, it is deemed inactive and is removed from the inactive list
        """
        inactive = []
        for x in self.boards:
            tm = time.time()
            if(tm - self.boards.get(x) > 4*self.timeout):
                self.req_queue.put((2,self.db.set_status,(x,0)))
                LOGMSG('Board with address '+x +' has become inactive!')
                inactive.append(x)
                
            else:
                self.req_queue.put((2,self.db.set_status,(x,1)))
        for x in inactive:
            self.remove_from_list(x)

    def sim(self,str1,str2):
        """
        Checks for similarity between two strings. 
        @return: similarity measure
        """
        buff1 = str.lower(str1)
        buff2 = str.lower(str2)
        if(len(str1) != len(str2)):
            return 0
        count = 0
        i = 0
        while(i<len(str1)):
            if(buff1[i] == buff2[i]):
                count += 1
            i+=1
        return count

    def set_user_settings(self,_maxtemp,_mintemp):
        if(_mintemp != ''):
            self.mintemp = _mintemp
        if(_max_temp != ''):
            self.maxtemp = _maxtemp
        self.req_queue.put((1,self.db.set_user_settings,(_maxtemp,_mintemp)))

    def get_user_settings(self):
        xml = XMLGenerator()
        xml.add_user_setting('mintemp',self.mintemp)
        xml.add_user_setting('maxtemp',self.maxtemp)
        return xml.print_xml()
                                               
    ################ DEBUG #######################

    def print_buffer( self ):
        """
        Print serial buffer. Used for debugging purposes
        """
        for y in self.serial:
            DEBUG( y )
class SinFrogMainWindow(wx.Frame):
    
    def __init__(self, *args, **kwds):
        # begin wxGlade: SinFrogMainWindow.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        
        # Menu Bar
        self.SinFrogMainWindow_menubar = wx.MenuBar()
        self.File = wx.Menu()
        self.SaveName = wx.MenuItem(self.File, wx.NewId(), "Save", "Save preferences", wx.ITEM_NORMAL)
        self.File.AppendItem(self.SaveName)
        self.File.AppendSeparator()
        self.QuitName = wx.MenuItem(self.File, wx.NewId(), "Quit", "Quit application", wx.ITEM_NORMAL)
        self.File.AppendItem(self.QuitName)
        self.SinFrogMainWindow_menubar.Append(self.File, "File")
        wxglade_tmp_menu = wx.Menu()
        self.AboutName = wx.MenuItem(wxglade_tmp_menu, wx.NewId(), "About", "About application", wx.ITEM_NORMAL)
        wxglade_tmp_menu.AppendItem(self.AboutName)
        self.SinFrogMainWindow_menubar.Append(wxglade_tmp_menu, "Help")
        self.SetMenuBar(self.SinFrogMainWindow_menubar)
        # Menu Bar end
        self.SinFrogMainWindow_statusbar = self.CreateStatusBar(1, 0)
        self.DistrictsLabel = wx.StaticText(self, -1, "District:")
        self.DistrictsCombobox = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN | wx.CB_DROPDOWN)
        self.IspLabel = wx.StaticText(self, -1, "ISP name:")
        self.IspCombobox = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN | wx.CB_DROPDOWN)
        self.PlanLabel = wx.StaticText(self, -1, "Plan name:")
        self.PlanCombobox = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN | wx.CB_DROPDOWN)
        self.UserIdentificationLabel = wx.StaticText(self, -1, "Identification number")
        self.UserIdentificationValue = wx.StaticText(self, -1, "")
        self.SaveButton = wx.Button(self, wx.ID_SAVE, "")
        self.QuitButton = wx.Button(self, wx.ID_EXIT, "")
        self.RunButton = wx.Button(self, -1, "Run")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_MENU, self.saveMenu, self.SaveName)
        self.Bind(wx.EVT_MENU, self.quitMenu, self.QuitName)
        self.Bind(wx.EVT_MENU, self.aboutMenu, self.AboutName)
        self.Bind(wx.EVT_COMBOBOX, self.IspChanged, self.IspCombobox)
        self.Bind(wx.EVT_BUTTON, self.saveMenu, self.SaveButton)
        self.Bind(wx.EVT_BUTTON, self.quitMenu, self.QuitButton)
        self.Bind(wx.EVT_BUTTON, self.runButton, self.RunButton)
        # end wxGlade
        
        #Loading Combobox content
        self.DistrictsCombobox.AppendItems(["01 = Raffles Place, Cecil, Marina, People's Park", "02 = Anson, Tanjong Pagar", "03 = Queenstown, Tiong Bahru", "04 = Telok Blangah, Harbourfront", "05 = Pasir Panjang, Hong Leong Garden, Clementi New Town", "06 = High Street, Beach Road (part)", "07 = Middle Road, Golden Mile", "08 = Little India", "09 = Orchard, Cairnhill, River Valley", "10 = Ardmore, Bukit Timah, Holland Road, Tanglin", "11 = Watten Estate, Novena, Thomson", "12 = Balestier, Toa Payoh, Serangoon", "13 = Macpherson, Braddell", "14 = Geylang, Eunos", "15 = Katong, Joo Chiat,,Amber Road", "16 = Bedok, Upper East Coast, Eastwood, Kew Drive", "17 = Loyang, Changi", "18 = Simei, Tampines, Pasir Ris", "19 = Serangoon,Garden, Hougang, Ponggol", "20 = Bishan, Ang Mo Kio", "21 = Upper Bukit Timah, Clementi Park, Ulu Pandan", "22 = Jurong", "23 = Hillview, Dairy Farm, Bukit Panjang, Choa Chu Kang", "24 = Lim Chu Kang, Tengah", "25 = Kranji, Woodgrove, Woodlands", "26 = Upper Thomson, Springleaf", "27 = Yishun, Sembawang", "28 = Seletar"])
        self.IspCombobox.AppendItems(["ViewQwest", "Singtel", "StarHub", "M1", "MyRepublic", "SuperInternet"])

    def __set_properties(self):
        # begin wxGlade: SinFrogMainWindow.__set_properties
        self.SetTitle("SinFrog")
        self.SinFrogMainWindow_statusbar.SetStatusWidths([-1])
        # statusbar fields
        SinFrogMainWindow_statusbar_fields = ["SinFrog"]
        for i in range(len(SinFrogMainWindow_statusbar_fields)):
            self.SinFrogMainWindow_statusbar.SetStatusText(SinFrogMainWindow_statusbar_fields[i], i)
        self.DistrictsCombobox.SetMinSize((300, 21))
        self.UserIdentificationLabel.Hide()
        self.UserIdentificationValue.Hide()
        self.RunButton.Hide()
        # end wxGlade
        
        #Checking config.ini existence
        if os.path.exists(r'config.ini'):
            config = configobj.ConfigObj("config.ini")
            district = config['District']
            isp = config['ISP']
            plan = config['Plan']
            user = config['User']
            self.DistrictsCombobox.SetValue(district['name'])
            self.IspCombobox.SetValue(isp['name'])
            self.PlanCombobox.SetValue(plan['name'])
            self.userIdentification = user['identification']
            self.UserIdentificationValue.SetLabel(self.userIdentification)
            self.UserIdentificationValue.Show()
            self.UserIdentificationLabel.Show()
            self.SaveButton.Hide()
            self.RunButton.Show()
        else:
            self.DistrictsCombobox.SetValue("Select your district of residence please.")
            self.IspCombobox.SetValue("Select your optical fibre ISP please.")
            self.PlanCombobox.SetValue("Will be updated accordingly to ISP.")            

    def __do_layout(self):
        # begin wxGlade: SinFrogMainWindow.__do_layout
        SinFrogSizer = wx.BoxSizer(wx.VERTICAL)
        ButtonSizer = wx.BoxSizer(wx.HORIZONTAL)
        PreferencesSizer = wx.GridSizer(3, 2, 0, 0)
        PreferencesSizer.Add(self.DistrictsLabel, 0, wx.EXPAND, 0)
        PreferencesSizer.Add(self.DistrictsCombobox, 0, wx.EXPAND, 0)
        PreferencesSizer.Add(self.IspLabel, 0, wx.EXPAND, 0)
        PreferencesSizer.Add(self.IspCombobox, 0, wx.EXPAND, 0)
        PreferencesSizer.Add(self.PlanLabel, 0, wx.EXPAND, 0)
        PreferencesSizer.Add(self.PlanCombobox, 0, wx.EXPAND, 0)
        PreferencesSizer.Add(self.UserIdentificationLabel, 0, wx.EXPAND, 0)
        PreferencesSizer.Add(self.UserIdentificationValue, 0, wx.EXPAND, 0)
        SinFrogSizer.Add(PreferencesSizer, 1, wx.EXPAND, 0)
        ButtonSizer.Add(self.SaveButton, 0, wx.EXPAND, 0)
        ButtonSizer.Add(self.QuitButton, 0, wx.EXPAND, 0)
        ButtonSizer.Add(self.RunButton, 0, wx.EXPAND, 0)
        SinFrogSizer.Add(ButtonSizer, 1, wx.EXPAND, 0)
        self.SetSizer(SinFrogSizer)
        SinFrogSizer.Fit(self)
        self.Layout()
        # end wxGlade

    #Click on a "Save" button
    def saveMenu(self, event):  # wxGlade: SinFrogMainWindow.<event_handler>
        if (self.DistrictsCombobox.GetCurrentSelection() > -1) and (self.IspCombobox.GetCurrentSelection() > -1) and (self.PlanCombobox.GetCurrentSelection() > -1):
            config = configobj.ConfigObj()
            config.filename = "config.ini"
            config["District"] = {}
            config["District"]["name"] = self.DistrictsCombobox.GetValue()
            config["ISP"] = {}
            config["ISP"]["name"] = self.IspCombobox.GetValue()
            config["Plan"] = {}
            config["Plan"]["name"] = self.PlanCombobox.GetValue()
            
            #New user so generating a userID
            url = ('http://sinfrog.metabaron.net/userID.php')
            data = json.dumps(config)
            clen = len(data)
            req = urllib2.Request(url, data, {'Content-Type': 'application/json', 'Content-Length': clen})
            f= urllib2.urlopen(req)
            self.userIdentification = f.read()
            f.close()
            
            config["User"] = {}
            config["User"]["identification"] = self.userIdentification
            config.write()
            self.UserIdentificationValue.SetLabel(self.userIdentification)
            self.UserIdentificationValue.Show()
            self.UserIdentificationLabel.Show()
            self.SinFrogMainWindow_statusbar.SetStatusText("Saved.")
            self.RunButton.Show()
            self.Layout()
        else:
            self.SinFrogMainWindow_statusbar.SetStatusText("ERROR: Please select both distric, IPS and plan name first.")

    def quitMenu(self, event):  # wxGlade: SinFrogMainWindow.<event_handler>
        if hasattr(self, 'r'):
            self.r.cancel()
        self.Destroy()

    def aboutMenu(self, event):  # wxGlade: SinFrogMainWindow.<event_handler>
        about = AboutFrame(None)
        about.Show()

    #Trigger when you change the ISP
    def IspChanged(self, event):  # wxGlade: SinFrogMainWindow.<event_handler>
        if event.GetString() == "Singtel":
            self.PlanCombobox.Clear()
            self.PlanCombobox.SetValue("Select your Singtel plan please.")
            self.PlanCombobox.AppendItems(["exPress 50/25", "exPress 100/50", "exPress 150/50", "exPlore Home 50/25", "exPlore Home Sports 50/25", "exPlore Home 100/50", "exPlore Home Sports 100/50", "exPlore Home 200/100", "exPlore Home Sports 200/100", "exCiteHome 200/100"])
        elif event.GetString() == "ViewQwest":
            self.PlanCombobox.Clear()
            self.PlanCombobox.SetValue("Select your ViewQwest plan please.")
            self.PlanCombobox.AppendItems(["ViewQwest 60/50", "ViewQwest 100/50", "ViewQwest 150/75", "ViewQwest 200/100", "ViewQwest 500/200", "ViewQwest 1000/500", "ViewQwest 200/100 Static"])
        elif event.GetString() == "StarHub":
            self.PlanCombobox.Clear()
            self.PlanCombobox.SetValue("Select your StarHub plan please.")
            self.PlanCombobox.AppendItems(["MaxInfinity Premium Plus 50/25", "MaxInfinity Ultimate Plus 100/50", "MaxInfinity Elite Plus 150/75", "MaxInfinity Platinum Plus 200/100"])
        elif event.GetString() == "M1":
            self.PlanCombobox.Clear()
            self.PlanCombobox.SetValue("Select your M1 plan please.")
            self.PlanCombobox.AppendItems(["HomePac 25/25", "HomePac 50/50", "HomePac 100/50", "HomePac 150/75", "HomePac 200/100", "HomePac 1000/500"])
        elif event.GetString() == "MyRepublic":
            self.PlanCombobox.Clear()
            self.PlanCombobox.SetValue("Select your MyRepublic plan please.")
            self.PlanCombobox.AppendItems(["Pure 100", "Gamer 100", "Tutor 100"])
        elif event.GetString() == "SuperInternet":
            self.PlanCombobox.Clear()
            self.PlanCombobox.SetValue("Select your SuperInternet plan please.")
            self.PlanCombobox.AppendItems(["100Mbps Basic Residential Service / 12 months", "100Mbps Basic Residential Service / 24 months", "100Mbps Home Premium Service / 12 months", "100Mbps Home Premium Service / 24 months"])

    #Start ping and download tests
    def runButton(self, event):  # wxGlade: SinFrogMainWindow.<event_handler>
        if shell.IsUserAnAdmin():
            self.SinFrogMainWindow_statusbar.SetStatusText("Running tests.")
            display = DisplayFrame(None)
            display.Show()
            #Wait 900 seconds = 15 minutes
            self.r = RepeatTimer(900.0, runTests, 0, [display, self.userIdentification])
            #self.r = threading.Thread(runTests(display, self.userIdentification))
            self.r.start()
        else:
            administratorFrame = Administrator(None)
            administratorFrame.Show()
            administratorFrame.Raise()
Exemple #11
0
                print "Forward Full: %s" % reqdom
                return relay(data)
            else:
                print "DROP"
                return None
    return None


if __name__ == "__main__":
    #initial config parsing
    parseconfig()

    if len(sys.argv) > 1:
        if sys.argv[1] == "-auto":
            print "Automatic Config Loading Activated"
            cfgthread = RepeatTimer(float(RELOAD_TIME), parseconfig)
            cfgthread.start()

    #append needed library
    sys.path.append("libs/dpkt-1.7/dpkt")
    #try to import the library
    try:
        import dpkt
    except:
        print "Could not locate dpkt library"
        sys.exit()
    print "Starting fw-dns"
    #bind on localhost
    blis(IF_LISTEN)
    print "Listening on localhost %s" % IF_LISTEN
    #connect to remote server
Exemple #12
0
def start_timer(action, interval, delay):
    RepeatTimer(interval, action.do, delay, daemon=True).start()
 def run(self):
     LOGMSG("Sensor thread running..")
     self.timer = RepeatTimer(
         self.timeout,
         self.send_rcv_msg)  # For now, send only temp requests
     self.timer.start()
class Sensor(Thread):
    """
    The Data Retrieval Module (DRM). 
    
    Its role is to request sensor and statistical data from connected devices, and store that information in the database
    
        - Uses a timeout to periodically request data from Base Station
    """
    DEBUG_MODE = 0
    """
    Operational mode. 
    
    If debug mode is switched, the program will skip all procedures and will execute the custom code located after the control statement:
            
    if(not DEBUG_MODE):
    ...
    else:
    """
    #Thresholds
    maxtemp = 0
    mintemp = 0

    # Tuple containing board addresses
    boards = {}
    closed = 0
    paused = 0
    text = ''

    #polling intervals
    sleep = 2
    stat_interval = 6  # multiply with timeout to get time, set to 0 to disable
    reset_interval = 0  #12
    timeout = 4 * sleep
    _MIN_TIMEOUT = 0
    _MAX_TIMEOUT = 120
    _DEFAULT_TIMEOUT = 3

    _BCAST = b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'
    """
    Hex address of the broadcast address
    """
    _SLIP_ADDR = 'SLIPCC'
    """
    Default device address. It should be followed by the device's hardware address, repeated twice.
    """
    def __init__(self, serial):
        self.closed = 0
        self.serial = serial
        self.stat_counter = 0
        self.reset_counter = 0
        LOGMSG("Sensor thread started..")
        time.sleep(1)
        Thread.__init__(self)

    def run(self):
        LOGMSG("Sensor thread running..")
        self.timer = RepeatTimer(
            self.timeout,
            self.send_rcv_msg)  # For now, send only temp requests
        self.timer.start()

    def close(self):
        LOGMSG("Sensor thread closing...")
        self.timer.cancel()
        time.sleep(3)
        self.send_reset()
        try:
            sys.exit(0)
        except SystemExit:
            pass

    def send_reset(self):
        msg = pack('8sBBB', self._BCAST, kelvin.DISPATCH,
                   kelvin.CMD | kelvin.RESET, 0)
        self.serial.write(msg)

    def sht_temp_scale(self, data):
        """
        Calibrate temperature data.
        @type data: number
        @param data: the temperature reading
        @return: the calibrated temperature reading
        """
        #(value,) = unpack( '>h' , data )
        value = data[0] << 8 | data[1]
        #Formula for calculating the temperature from the SHT at 3.3v
        scaled = (value / 100.0 - 39.65)
        DEBUG(str(value) + ' (raw) -> Scaled!! ' + str(scaled))
        return scaled

    def sht_hum_scale(self, data):
        """
        Calibrate humidity data.
        @type data: number
        @param data: the humidity reading
        @return: the calibrated humidity reading
        """
        value = data[0] << 8 | data[1]
        scaled = (value * 0.0367) - 2.0468 + ((value * value) * (-0.000001595))
        DEBUG(str(value) + '(raw) -> scaled!! ' + str(scaled))
        return scaled

    def light_scale(self, data):
        """
        Calibrate light data.
        @type data: number
        @param data: the light reading
        @return: the calibrated light reading
        """
        #Equation to convert to LUX is ADC val * 5
        return data * 5

    def pressure_scale(self, data):
        """
        Calibrate pressure data.
        @type data: number
        @param data: the pressure reading
        @return: the calibrated pressurereading
        """
        #Equation to convert to kPa
        return (((data * 11) / 2125) + 0.095) / 0.009

    def read_data(self):
        """
        Read incoming packages and process them.
        
        Uses base64 encoding and decoding to read data.
            - Checks for address validity and tries to amend corrupted addresses.
            - If unable to amend address, discards packet
            - Reads and stores data in database
            - If the data comes from a temperature sensors, sends a LED colour change command to the device
        """
        self.text = ''
        for y in self.serial:
            if (len(y) < 11):
                continue

            length = y[10]
            address = ''
            dispatch = ''
            cmd = 0
            datalen = 0
            if (length == 0):
                fmt = ('8sBBB')
                try:
                    (address, dispatch, cmd, datalen) = unpack(fmt, y)
                except:
                    LOGMSG(y)
                    continue

            elif (length == 5):
                try:
                    (address, dispatch, cmd, datalen, rtt,
                     hops) = unpack('>8sBBBLB', y)
                except:
                    LOGMSG(y)
                    continue
            else:
                fmt = ('8sBBB%ds' % (length))
                try:
                    (address, dispatch, cmd, datalen, data) = unpack(fmt, y)
                except:
                    LOGMSG(y)
                    continue

            similar = self.sim(address[:6].decode('utf-8'), 'SLIPCC')
            if (similar == 6 and address[6] == address[7]):
                addr = self.format_address(address)
            elif ((similar < 6 and similar > 3) and address[6] == address[7]):
                addr = self.format_address(b'SLIPCC' + address[6:8])
            else:
                addr = ''

            if (addr != ''):
                tm = round(time.time())
                if (cmd == kelvin.TEMP):
                    scaled = self.sht_temp_scale(data)
                    LOGMSG("Node %02X gives temp %d\n" % (address[7], scaled))
                    self.add_to_list(addr, tm)
                    if (scaled < self.maxtemp and scaled > self.mintemp):
                        LOGMSG("Measured temperature is within norms")
                        msg = pack('8sBBBB', address, kelvin.DISPATCH,
                                   kelvin.CMD | kelvin.LED, 1, 5)
                        self.serial.write(msg)
                    elif (scaled < self.mintemp):
                        LOGMSG("Measured temperature is below norms")
                        msg = pack('8sBBBB', address, kelvin.DISPATCH,
                                   kelvin.CMD | kelvin.LED, 1, 3)
                        self.serial.write(msg)
                    elif (scaled > self.maxtemp):
                        LOGMSG("Measured temperature is above norms")
                        msg = pack('8sBBBB', address, kelvin.DISPATCH,
                                   kelvin.CMD | kelvin.LED, 1, 6)
                        self.serial.write(msg)
                    self.req_queue.put(
                        (1, self.db.set_measurements,
                         ('temp', addr, self.sht_temp_scale(data), tm)))
                elif (cmd == kelvin.HUM):
                    scaled = self.sht_hum_scale(data)
                    LOGMSG("Node %02X gives humidity %02X\n" %
                           (address[7], scaled))
                    self.add_to_list(addr, tm)
                    self.req_queue.put(
                        (1, self.db.set_measurements,
                         ('humidity', addr, self.sht_hum_scale(data), tm)))
                elif (cmd == kelvin.PRSR):
                    LOGMSG("Node %02X gives pressure %02X\n" %
                           (address[7], data[0]))
                    self.req_queue.put(
                        (1, self.db.set_measurements,
                         ('pressure', addr, self.pressure_scale(data[0]), tm)))
                elif (cmd == kelvin.LGHT):
                    LOGMSG("Node %02X gives light %02X\n" %
                           (address[7], data[0]))
                    self.req_queue.put(
                        (1, self.db.set_measurements,
                         ('light', addr, self.light_scale(data[0]), tm)))
                elif (cmd == kelvin.PING):
                    #if(rtt < 10000):
                    stat_string = "%d: Got PING stats for node %02X: RTT is: %d; HOPS is: %d" % (
                        time.time(), address[7], rtt, hops)
                    LOGMSG(stat_string)
                    try:
                        file = open('network_stats.log', 'a')
                        file.write(stat_string + "\n")
                        file.close()
                    except:
                        pass
                    #self.req_queue.put((1,self.db.set_stat_data,(addr, rtt,hops,tm)))

    def set_timeout(self, _timeout):
        """
        Sets time period between consecutive sends
        @type _timeout: number
        @param _timeout: the timeout value to be set
        """
        if (self._MIN_TIMEOUT <= _timeout and _timeout <= self._MAX_TIMEOUT):
            self.timeout = _timeout
            self.timer = RepeatTimer(self.timeout, self.send_msg)
            self.timer.start()
        else:
            DEBUG('Invalid value!')

    def reset_timeout(self):
        """
        Resets the timeout value to its default value
        """
        self.timeout = self._DEFAULT_TIMEOUT

    def send_rcv_msg(self):
        """
        Sends requests and reads responses
        
            - Uses a small sleep interval between each transmission to prevent race conditions
            - checks for active devices        
        """
        if (not self.DEBUG_MODE):
            try:
                if self.stat_interval != 0:
                    if self.stat_counter > self.stat_interval:
                        time.sleep(self.sleep)
                        msg = pack('8sBBB', self._BCAST, kelvin.DISPATCH,
                                   kelvin.CMD | kelvin.PING, 0)
                        self.serial.write(msg)

                        self.stat_counter = 0

                        return  # reading sensor data should not interfere with RTT!
                    else:
                        self.stat_counter += 1

                msg = pack('8sBBB', self._BCAST, kelvin.DISPATCH,
                           kelvin.CMD | kelvin.TEMP, 0)
                self.serial.write(msg)
                time.sleep(self.sleep)
                msg = pack('8sBBB', self._BCAST, kelvin.DISPATCH,
                           kelvin.CMD | kelvin.HUM, 0)
                self.serial.write(msg)
                time.sleep(self.sleep)
                msg = pack('8sBBB', self._BCAST, kelvin.DISPATCH,
                           kelvin.CMD | kelvin.PRSR, 0)
                self.serial.write(msg)
                time.sleep(self.sleep)
                msg = pack('8sBBB', self._BCAST, kelvin.DISPATCH,
                           kelvin.CMD | kelvin.LGHT, 0)
                self.serial.write(msg)
            except ValueError:
                self.close()

            self.read_data()
            #self.print_buffer()
            self.check_board_status()

            if self.reset_interval != 0:
                if self.reset_counter > self.reset_interval:
                    time.sleep(self.sleep)
                    self.send_reset()
                    time.sleep(self.sleep)
                    self.reset_counter = 0
                else:
                    self.reset_counter += 1

        else:
            # Add new code to be debugged here
            msg = pack('8sBBB', self._BCAST, kelvin.DISPATCH,
                       kelvin.CMD | kelvin.PING, 0)
            self.serial.write(msg)
            self.read_data()
            #self.send_reset()
            #time.sleep(self.sleep)

    def set_db_data(self, _req_queue, _db):
        self.req_queue = _req_queue
        self.db = _db

    def format_address(self, addr):
        """
        Converts the address into more readible form. Used when storing data in database
        """
        _str = ''
        for x in addr:
            if (x <= 0xF):
                _str += '0'
            _str += hex(x) + ':'

        return _str[:-1].replace("0x", "")

    def add_to_list(self, addr, tm):
        self.boards[addr] = tm

    def remove_from_list(self, addr):
        del self.boards[addr]

    def check_board_status(self):
        """
        Performs a check on all devices in the active list. 
            - If a device has not responded for the past 4*timeout iterations, it is deemed inactive and is removed from the inactive list
        """
        inactive = []
        for x in self.boards:
            tm = time.time()
            if (tm - self.boards.get(x) > 4 * self.timeout):
                self.req_queue.put((2, self.db.set_status, (x, 0)))
                LOGMSG('Board with address ' + x + ' has become inactive!')
                inactive.append(x)

            else:
                self.req_queue.put((2, self.db.set_status, (x, 1)))
        for x in inactive:
            self.remove_from_list(x)

    def sim(self, str1, str2):
        """
        Checks for similarity between two strings. 
        @return: similarity measure
        """
        buff1 = str.lower(str1)
        buff2 = str.lower(str2)
        if (len(str1) != len(str2)):
            return 0
        count = 0
        i = 0
        while (i < len(str1)):
            if (buff1[i] == buff2[i]):
                count += 1
            i += 1
        return count

    def set_user_settings(self, _maxtemp, _mintemp):
        if (_mintemp != ''):
            self.mintemp = _mintemp
        if (_max_temp != ''):
            self.maxtemp = _maxtemp
        self.req_queue.put(
            (1, self.db.set_user_settings, (_maxtemp, _mintemp)))

    def get_user_settings(self):
        xml = XMLGenerator()
        xml.add_user_setting('mintemp', self.mintemp)
        xml.add_user_setting('maxtemp', self.maxtemp)
        return xml.print_xml()

    ################ DEBUG #######################

    def print_buffer(self):
        """
        Print serial buffer. Used for debugging purposes
        """
        for y in self.serial:
            DEBUG(y)
Exemple #15
0
whitelist = tuple()
banned = defaultdict(int)


def analyze(line):
    for w in whitelist:
        if w in line:
            return
    ip = line.split('-', 1)[0]
    banned[ip] += 1
    if banned[ip] >= 20:
        system('ipfw table 2 add %s' % ip)
        system(
            '''netstat -an | grep %s | awk '{print $4"."$5}' | awk -F '\.' '{print $1"."$2"."$3"."$4" "$5" "$6"."$7"."$8"."$9" "$10}' | while read line; do tcpdrop $line; done'''
            % ip)
        with open('banned.log', 'a') as f:
            f.write(time.ctime() + ' - %s\n' % ip)


if __name__ == '__main__':
    with open('whitelist.txt') as w:
        whitelist = w.read().splitlines()

RepeatTimer(5.0, lambda: banned.clear()).start()

log = argv[1]
print 'Now parsing %s' % log
p_tail = Popen('tail -f %s' % log, shell=True, stdout=PIPE)
while True:
    analyze(p_tail.stdout.readline())
Exemple #16
0
    # Add the urls to the queue to be processed
    #
    for item in results:
        task_queue.put(item.url)

    #
    # Start up out workers to process the queue
    #
    for i in range(NUMBER_OF_PROCESSES):
        Thread(target=worker, args=(task_queue, done_queue)).start()

    #
    # We're done, so tell all the workers to stop
    #
    for i in range(NUMBER_OF_PROCESSES):
        task_queue.put('STOP')


if __name__ == '__main__':

    #
    # Run once to update any feeds that need updating, then
    # run test every 5 minutes to check for scheduled feed updates
    #
    try:
        run()
        r = RepeatTimer(60.0, run)
        r.start()
    except (EOFError, KeyboardInterrupt):
        sys.exit()
Exemple #17
0
 def start_poll(self, **kwargs):
     self.stop_poll()
     self.timer = RepeatTimer(self.interval, self.on_interval)
     self.polling = True
     self.timer.start()
Exemple #18
0
            if reqdom.endswith(domain):
                print "Forward Full: %s" % reqdom
                return relay(data)
            else:
                print "DROP"
                return None
    return None
    
if __name__ == "__main__":
    #initial config parsing
    parseconfig()
    
    if len(sys.argv) > 1:
        if sys.argv[1] == "-auto":
            print "Automatic Config Loading Activated"
            cfgthread = RepeatTimer(float(RELOAD_TIME), parseconfig)
            cfgthread.start()    

    #append needed library
    sys.path.append("libs/dpkt-1.7/dpkt") 
    #try to import the library
    try:
        import dpkt
    except:
        print "Could not locate dpkt library"
        sys.exit()
    print "Starting fw-dns"
    #bind on localhost        
    blis(IF_LISTEN)
    print "Listening on localhost %s" % IF_LISTEN
    #connect to remote server