Example #1
0
 def Start(self):
     #debug('Start')
     if self.connected:
         ret = False
         error('Start already connected')
     else:
         info('Connecting to: {}:{}'.format(self.HOST, self.PORT))
         count = 0
         with self.mutex:
             count += 1
             while self.connected == False and count < 30:
                 try:
                     self.sock = None
                     self.wrappedSocket = None
                     ##debug('Start wrap_socket')
                     self.sock = socket(AF_INET, SOCK_STREAM)
                     #self.wrappedSocket = wrap_socket(self.sock, ca_certs="/etc/myDevices/ca.crt", cert_reqs=CERT_REQUIRED)
                     self.wrappedSocket = wrap_socket(self.sock)
                     self.wrappedSocket.connect((self.HOST, self.PORT))
                     info('myDevices cloud connected')
                     self.connected = True
                 except socket_error as serr:
                     Daemon.OnFailure('cloud', serr.errno)
                     error('Start failed: ' + str(self.HOST) + ':' +
                           str(self.PORT) + ' Error:' + str(serr))
                     self.connected = False
                     sleep(30 - count)
     return self.connected
Example #2
0
 def ReadMessage(self):
     ret = True
     if self.connected == False:
         ret = False
     else:
         try:
             self.count = 4096
             timeout_in_seconds = 10
             ready = select([self.wrappedSocket], [], [],
                            timeout_in_seconds)
             if ready[0]:
                 message = self.wrappedSocket.recv(self.count).decode()
                 buffering = len(message) == 4096
                 while buffering and message:
                     if self.CheckJson(message):
                         buffering = False
                     else:
                         more = self.wrappedSocket.recv(self.count).decode()
                         if not more:
                             buffering = False
                         else:
                             message += more
                 try:
                     if message:
                         messageObject = loads(message)
                         del message
                         self.readQueue.put(messageObject)
                     else:
                         error('ReadMessage received empty message string')
                 except:
                     exception('ReadMessage error: ' + str(message))
                     return False
                 Daemon.Reset('cloud')
         except IOError as ioerr:
             debug('IOError: ' + str(ioerr))
             self.Restart()
             #Daemon.OnFailure('cloud', ioerr.errno)
         except socket_error as serr:
             Daemon.OnFailure('cloud', serr.errno)
         except:
             exception('ReadMessage error')
             ret = False
             sleep(1)
             Daemon.OnFailure('cloud')
     return ret
Example #3
0
def writePidToFile(pidfile):
    if path.isfile(pidfile):
        info(pidfile + " already exists, exiting")
        with open(pidfile, 'r') as file:
            pid = int(file.read())
            if ProcessInfo.IsRunning(pid) and pid != getpid():
                Daemon.Exit()
                return
    pid = str(getpid())
    with open(pidfile, 'w') as file:
        file.write(pid)
Example #4
0
 def SendMessage(self, message):
     logJson(message, 'SendMessage')
     ret = True
     if self.connected == False:
         error('SendMessage fail')
         ret = False
     else:
         try:
             data = bytes(message, 'UTF-8')
             max_size = 16383
             if len(data) > max_size:
                 start = 0
                 current = max_size
                 end = len(data)
                 self.wrappedSocket.send(data[start:current])
                 while current < end:
                     start = current
                     current = start + max_size if start + max_size < end else end
                     self.wrappedSocket.send(data[start:current])
             else:
                 self.wrappedSocket.send(data)
             if self.onMessageSent:
                 self.onMessageSent(message)
             message = None
         except socket_error as serr:
             error('SendMessage:' + str(serr))
             ret = False
             Daemon.OnFailure('cloud', serr.errno)
             sleep(1)
         except IOError as ioerr:
             debug('IOError: ' + str(ioerr))
             self.Restart()
             #Daemon.OnFailure('cloud', ioerr.errno)
         except socket_error as serr:
             Daemon.OnFailure('cloud', serr.errno)
         except:
             exception('SendMessage error')
     return ret
Example #5
0
 def CheckSubscription(self):
     inviteCode = self.config.get('Agent', 'InviteCode')
     cayenneApiClient = CayenneApiClient(self.CayenneApiHost)
     authId = cayenneApiClient.loginDevice(inviteCode)
     if authId == None:
         error(
             'Registration failed for invite code {}, closing the process'.
             format(inviteCode))
         Daemon.Exit()
     else:
         info('Registration succeeded for invite code {}, auth id = {}'.
              format(inviteCode, authId))
         self.config.set('Agent', 'Initialized', 'true')
         self.MachineId = authId
Example #6
0
 def Stop(self):
     #debug('Stop started')
     Daemon.Reset('cloud')
     ret = True
     if self.connected == False:
         ret = False
         error('Stop not connected')
     else:
         with self.mutex:
             try:
                 self.wrappedSocket.shutdown(SHUT_RDWR)
                 self.wrappedSocket.close()
                 info('myDevices cloud disconnected')
             except socket_error as serr:
                 debug(str(serr))
                 error('myDevices cloud disconnected error:' + str(serr))
                 ret = False
             self.connected = False
     #debug('Stop finished')
     return ret
Example #7
0
 def TestDownload(self):
     try:
         a = datetime.now()
         info('Excuting regular download test for network speed')
         url = self.config.cloudConfig.DownloadSpeedTestUrl if 'DownloadSpeedTestUrl' in self.config.cloudConfig else defaultUrl
         debug(url + ' ' + download_path)
         request.urlretrieve(url, download_path)
         request.urlcleanup()
         b = datetime.now()
         c = b - a
         if path.exists(download_path):
             size = path.getsize(download_path) / mb
             self.downloadSpeed = size / c.total_seconds()
             remove(download_path)
             return True
     except socket_error as serr:
         error('TestDownload:' + str(serr))
         ret = False
         Daemon.OnFailure('cloud', serr.errno)
         return
     except:
         exception('TestDownload Failed')
     return False