-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageSender.py
75 lines (62 loc) · 2.01 KB
/
MessageSender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
##
# @package MessageSender Module contains the logic for sending the constructed payload to the server
#
import logging
import PeriodicTimer
import threading
import time
from PayloadQueue import PayloadQueue
from HttpSender import HttpSender
from MqttSender import MqttSender
import Status
import LedStatus
import GsmUtility
stop = False
class MessageSender ( threading.Thread ):
LOG = logging.getLogger( __name__ )
def __init__( self ):
threading.Thread.__init__( self )
##
#
def run( self ):
global stop
while True:
Status.messageSenderWorking = True
if stop:
self.LOG.info( 'Got stop signal from the Main module. Stopping thread' )
break
try:
self.LOG.info('Sleeping for: %s', 30)
time.sleep( 30 )
if GsmUtility.internetPresent():
self.sendPayload()
try:
subprocess.call( 'sync', shell=True )
except Exception as e:
self.LOG.error( 'Error syncing filesystem changes: %s', e )
self.sendPayload()
except Exception as e:
self.LOG.error( 'Error in Message Sender module: %s', e)
##
#
def sendPayload( self ):
self.LOG.info( 'Sending Payload' )
pq = PayloadQueue()
print(pq.isBacklogPresent())
while pq.isBacklogPresent():
print('Backlog is present')
payload = pq.getPayload()
retries = 3
sender = HttpSender()
success = False
while retries > 0:
retries -= 1
if sender.sendPayload( payload ):
pq.deleteLastPayload()
success = True
self.LOG.info( 'Payload sent' )
break
if not success:
self.LOG.warning( 'Payload sending failed' )
break