def ready_internet(self):
     self.logger.info("Attempting to connect to the internet.")
     ## open a pppd instance
     OSTools.pppd_launch()
     
     ## wait for pppd to settle
     time.sleep(5)
     
     retrycount = 5
     while retrycount > 0:
         ## to see if ppp is up
         if OSTools.pppd_status():
             ## ppp is up, try ping
             x = self._try_network()
             if x is not None:
                 self.disp.control_led('net', True)
                 self.disp.control_led('neterr', False)
                 self.logger.info("Ping success.")
                 return True
             else:
                 self.disp.control_led('neterr', True)
                 self.disp.control_led('net', False)
             self.logger.info("Sleeping for 10s.")
             time.sleep(10)
         else:
             # ppp is not up
             ## check if process is running
             ppp_pid = OSTools.pppd_pid()
             if ppp_pid == 0:
                 ## ppp has quit
                 raise InternetConnectionError('pppd unexpectedly quit!')
             else:
                 ## wait 10 seconds, and retry
                 time.sleep(10)
         retrycount -= 1
         self.logger.info("Rechecking network, remaining attempts %d." % retrycount)
     OSTools.pppd_terminate(OSTools.pppd_pid())
     return False
Ejemplo n.º 2
0
def main():
    UPTIME = 0
    START = datetime.utcnow()
    
    while True:
        UPTIME = datetime.utcnow() - START
        uptime = UPTIME.seconds
        # check if network is up
        pppdstatus = OSTools.pppd_status()
        if pppdstatus:
            heartbeat_int = 30.0
        else:
            heartbeat_int = 60.0
        if pppdstatus:
            try:
                rwip = OSTools.net_get_ip_address('ppp0')
            except socket.error:
                logger.exception("Error getting local IP")
                rwip = '0.0.0.0'
            try:
                send_heartbeat(Constants.URLJsonAPI, uptime, rwip)
            except:
                logger.exception("Unknown Error")
        time.sleep(heartbeat_int)