Project

General

Profile

Here you can see how our database class works and how to use it!

Design of the class

The DBMySQLConnection class is used to connect to the database, on the server (main controller) side.

Creating the DBMySQLConnection object

You create an object by typing x = DBMySQLConnection(username, password, host, dbname), this will create and initialize the DBMySQLConnection object.

Connecting to the DB [connectDB()]

The next thing you will have to do is to connect to the database, and that is easily done using the command connectDB(). It will return you a:

1, if the connection was created and successful, or a
4, if the username or password are wrong
2, if wrong host address was given (might be that mysql is not running).
3, if an unknown error happened (set debugMode = 1 to print what error happened)

Fetch all the tasks from the TempTaskTable [anyTaskToDo()]

Now we should check if there is any task to do when the main controller is started. All the current tasks to be done should be stored in the temporary task table called TempTaskTable. They are stored in the TempTaskTable because we don't want to make a look up in the big TaskTable, since if we have a lots of rows inside, it might slow up the look up time (i.e. checking if the task is new or not). The anyTaskToDo() function will select from the database the new tasks and store them in a list, called tasksList. The function anyTaskToDo() will return a :

1, if there are some tasks to do
4, if no tasks were found in the database
2, if the TempTaskTable doesn't exist
3, if an unknown error happened, if debugMode is set to 1 (debugMode = 1), then it will also print what type of error happened.
0, if a connection to the database doesn't exist

If the function returns a 1, that means the tasksList is filled with tasks (i.e. tests to be executed) the main controller should execute. Inside of the tasksList there are lists, each task represents a list of three attributes, taskID, from, to. Later on will be explained how to retrieve the tasks from the tasksList.

How to clean completely the tasksList list [cleanTasksList()]

If you want to completely clean the tasksList then you should execute cleanTasksList() function. It will return you a:

1, if it successfully deleted the tasks from the tasksList.
0, if a connection to the database doesn't exist

How to remove a single task from the tasksList [removeTaskFromList(taskID)]

If you want to remove singles tasks from the tasksList (i.e. you finished one test and then you want to remove it from the table) then you should execute removeTaskFromList(taskID), where taskID is the ID of the task (i.e. task identifier) and it can be of integer or string type. After execution it will return you:

1, if the task was deleted
2, if the task with the given taskID wasn't found
0, if a connection to the database doesn't exist

How to find the IP address of the device/system (i.e. of SIP, GSMBOXes, etc.) [deviceAddress(deviceName)]

If you want to find out the IP address of the device/system (i.e. SIP, GSMBOXes and etc.) you will execute the deviceAddress(deviceName) function, where deviceName is of type string and should be the device name (i.e. GSM1, what you fetched from the database in fields from or to). It will return a:

1, if device address was found which is then stored in the deviceAddr variable
4, if the device wasn't found in the table
2, if the deviceAddressTable table doesn't exist
3, if some unknown error happened (set debugMode = 1 to print what the unknown error was)
0, if a connection to the database doesn't exist

Update the task result [updateTaskResult(taskID, status)]

This will update the task result in the TaskTable. You will have to execute updateTaskTable(taskID, status), where taskID and status are of type integer. It will return you a value of:

1, if the update was successful
2, that taskID doesn't exist
3, if some unknown error happened (set debugMode = 1 to print what the unknown error was)
0, if a connection to the database doesn't exist

Updating the PingResultsTable [updatePingResult(taskNo, sipServer, sipGate, sipLoc, gsmBox1, gsmBox2)]

Before doing any test, each subsystem is pinged first to see if it is alive and can we proceed with the test. Once you execute updatePingResult(taskNo, sipServer, sipGate, sipLoc, gsmBox1, gsmBox2), where all the input parameters are of type integer. The function will return you a:

1, if the ping table was successfully updated
4, if test(s) with taskNo doesn't(don't) exist
3, if some unknown error happened (set debugMode = 1 to print what the unknown error was)
0, if a connection to the database doesn't exist

You finished a task and want to delete the task from the TempTaskTable [deleteTempTask(taskID)]

When you finish with one task/test you want to delete that task/test from the temporary task table. You would do it by executing the deleteTempTask(taskID) function, where taskID is of integer type. After execution of the function, it will return the following values:

1, if the task with the taskID was successfully deleted from the temporary task table (TempTaskTable)
4, if the task with the taskID wasn't found in the temporary task table (e.g. you deleted it already), or some other "unknown error" appeared in the execution
3, if some unknown error happened (set debugMode = 1 to print what the unknown error was)
0, if a connection to the database doesn't exist

Adding the results to the result table (ResultTable) [addResult(taskID, result)]

Once when you are finished with your test you need to store the result in the database. You can easily do it by executing addResult(taskID, result) function, where the taskID and result are of type integer. The function will return you the following integers:

1, if the result with taskID was successfully added
4, if some "unknown error" happens (set debugMode = 1 to print more information about the error)
2, if a duplicate entry with the same taskID already exists in the ResultTable
3, if some unknown error happened (set debugMode = 1 to print what the unknown error was)
0, if a connection to the database doesn't exist

Inserting tasks into the TempTaskTable and TaskTable [insertTaskIn2(fromDevice, toDevice, taskNo)]

In case that we want to insert a new task into our two tables in the database, we would call the insertTaskIn2(fromDevice, toDevice, taskNo) procedure, where fromDevice is a string that represents the device you want to call from, to toDevice which is a string name of the device you want to receive the call and taskNo is an integer that represents the task number of the current test. The tasks will be also inserted into our task list (i.e. in tasksList). We do this in case one test case didn't work, e.g. device1 to device2, then we want to test is it also from device2 to device1. The function will return you:

1, if the task of adding was successful to the database
2, if a duplicate entry with the same key exists (i.e. with taskID)
3, if an unknown error happened, if debugMode is set to 1 (debugMode = 1), then it will also print what type of error happened.
0, if a connection to the database doesn't exist

Search the task list [searchTaskList(fromDevice, toDevice)]

If you want to see is there a task in the list, you can do it by executing the searchTaskList(fromDevice, toDevice) procedure, where fromDevice is a string with the device name, e.g. GSM1 and toDevice is a string containing the name of the other device that you are calling. The procedure will return you the following:

1, if the task was found in the tasksList
2, if the task wasn't found in the tasksList
0, if a connection to the database doesn't exist

Sample code which makes the use of the class

Here is a sample code that utilizes the class.

user = 'root'
passw = 'mysqlpassword'
host = 'localhost'
dbname = 'gsmselftesting'

x = DBMySQLConnection(user, passw, host, dbname)
print "connected to the DB ", x.connectDB()
print "try to delete everything in the tasksList",x.cleanTasksList()
print "do i have anything to do", x.anyTasksToDo()

for item in x.tasksList:
        taskID = item[0]
        callFrom = item[1]
        callTo = item[2]
        print "taskID, call from, to ", taskID , callFrom , callTo

print "remove a task ",x.removeTaskFromList('2')

#print x.cleanTasksList()

for item in x.tasksList:
        device = item[1]
        print "device name", device
        do = x.deviceAddress(device)
        print "device's IP", x.deviceAddr

print "update task result ", x.updateTaskResult(1,3)

print "update ping Table", x.updatePingResult(1,5,4,3,2,1)
print "delete a task from temp table", x.deleteTempTask(2)
print "add a result to the table", x.addResult(2,34)
print "close connection to the DB", x.closeDBConn()

del x #delete

Class code

Here you can see the class code: It is in the repository dbClass.py