summaryrefslogtreecommitdiffstats
path: root/Code/Server-Code/GSMHandler.py
diff options
context:
space:
mode:
Diffstat (limited to 'Code/Server-Code/GSMHandler.py')
-rw-r--r--Code/Server-Code/GSMHandler.py103
1 files changed, 69 insertions, 34 deletions
diff --git a/Code/Server-Code/GSMHandler.py b/Code/Server-Code/GSMHandler.py
index cf9a2b4..1379fe9 100644
--- a/Code/Server-Code/GSMHandler.py
+++ b/Code/Server-Code/GSMHandler.py
@@ -4,34 +4,55 @@ import LogFileClass
from time import sleep
import sys
import setproctitle
-
+import csv
+global sipServer, username, password, handler
global portListen
global portAddress
-
-
-if len(sys.argv) <> 4:
- print "Error given command"
- sys.exit(2)
-
-
-handler = sys.argv[3]
-portAddress = sys.argv[2]
-portListen = int(sys.argv[1])
+global nameOfLogFile
def initLogfile():
global nameOfLogFile
nameOfLogFile = str(handler)+' handler.log'
+def writeToFile():
+ try:
+ string = '--'
+ with open('handler.txt', 'w') as F:
+ writer = csv.writer(F)
+ writer.writerow([string])
+ F.close()
+ except ValueError:
+ print "can't write to file"
+
+def openFile():
+ global sipServer, username, password, handler
+ global portListen
+ global portAddress
+
+ #String format: 'Account:username:password:server:handler:PortName:portCaller
+ File = open('handler.txt')
+ reader = csv.reader(File, delimiter=':')
+ for row in reader:
+ if str(row[0]) == 'Account':
+ handler = str(row[4])
+ portAddress = str(row[5])
+ portListen = int(row[6])
+ File.close()
+ writeToFile()
+
baudRate = 19200
+#Taking account information
+openFile()
+
+#start Logging
initLogfile()
logger = LogFileClass.Logging(nameOfLogFile)
+logger.logEvent('')
errorCount = 0
-logger.logEvent('')
-
whileCounter =0
#define global varibales
@@ -42,8 +63,10 @@ lastState = 0
resetState = 0
deviceError = 0
+
+
def initSystem():
- print 'init system'
+
global handlerSocket
global gsmDevice
@@ -58,20 +81,23 @@ def initSystem():
lastState = 0 #variable used to know
numberToCall = '000' #number to call
resetState = 0
-
+
handlerSocket = ServerClass.ServerHandler(portListen)
logger.logEvent('LISTEN ON PORT: '+str(portListen))
anyConnection = handlerSocket.openSocket()
- #add this if you need it
- gsmDevice = GSMClass.serialPort(portAddress, baudRate, 15)
- initDevice = gsmDevice.portInit(5)
+
+ try :
+ logger.logEvent('Trying connect to the device')
+ gsmDevice = GSMClass.serialPort(portAddress, baudRate, 15)
+ initDevice = gsmDevice.portInit(5)
+ logger.logEvent('init device ' + str(initDevice))
+
+ except ValueError:
+ logger.logEvent('Failure when trying connect to device')
+
########################################################
- #add nice formating to the log file :)
- print 'any connection ' + str(anyConnection)
- print 'initDevice ' + str(initDevice)
-
if initDevice!= 1:
deviceError = 1
else:
@@ -82,7 +108,7 @@ def initSystem():
logger.logEvent('CONNECTION ESTABLISHED AND DEVICE WORKING: ' + str(handlerSocket.connectedTo()))
return 1
- elif anyConnection == 1 and initDevice != 0:
+ elif anyConnection == 1 and initDevice != 1:
logger.logEvent('$connection established but device not working: ' + str(handlerSocket.connectedTo()))
resetState = 1
return 2
@@ -90,7 +116,10 @@ def initSystem():
logger.logEvent('$no connection')
logger.logEvent('$else case in init system' + str(anyConnection) + ' ' + str(initDevice) + ' ')
resetState = 1
- return 0
+
+ return 0
+
+
def receiveMessage(timeout):
@@ -221,12 +250,10 @@ def receiveStart():
callSuccess = 'CALL OK'
else:
callSuccess = 'CALL NOT OK'
-
-
-
- sendMessage = handlerSocket.sendData(callSuccess)
if deviceError==0:
- tryHangUp = gsmDevice.hangUp()
+ tryHangUp = gsmDevice.hangUp()
+ sendMessage = handlerSocket.sendData(callSuccess)
+
return sendMessage
############################
@@ -280,10 +307,12 @@ def callStart(numberToCall):
else:
callSuccess = 'CALL NOT OK'
- handResponse = handlerSocket.sendData(callSuccess)
-
if deviceError==0:
- tryHangUp = gsmDevice.hangUp()
+ try:
+ tryHangUp = gsmDevice.hangUp()
+ except ValueError:
+ print 'Error when try hangup the call'
+ handResponse = handlerSocket.sendData(callSuccess)
return handResponse
#########################
@@ -313,7 +342,7 @@ def other():
setproctitle.setproctitle('GSM Handler')
-while 1:
+try:
test = initSystem()
if test == 1:
@@ -328,10 +357,16 @@ while 1:
print 'initialized system but device not working'
logger.logEvent('initialized system but device not working')
handlerSocket.sendData('DEVICE NOT READY')
+
elif test ==0:
print 'nobody can connect, reboot board, restart cellphone'
logger.logEvent('nobody can connect, reboot board, restart cellphone')
-
+
+ logger.closeLogging()
del handlerSocket
del gsmDevice
+except ValueError:
+ logger.logEvent('Could not start the handler')
+ logger.closeLogging()
+