BikeTalker : 2 talker.py - 17chuchu/AutomaticBicycleInterface GitHub Wiki

This is python file will handle the communication between Django and ROS and the bike's camera.

Table of Contents

ClientSocket

The most important class would be ClientSocket. It connects the bike to the Django server.

Bike ID

Bike ID is the id of this bike you can customise it here. The id can be as long as you want but it has to start with BK-. In addition, it must be unique (within the domain of bike id).

bikeid = 'BK-1234'

Port and IP

These are a port and an IP of the Django server.

port = "7010"  // line 145
ip = "localhost"

Additional Information

The ClientSocket works like a normal Socket connection class. To clarify, look at this function.

@staticmethod
def initialize():
    websocket.enableTrace(True);
    ClientSocket.ws = websocket.WebSocketApp('ws://' + ClientSocket.ip + ':' + ClientSocket.port + '/',
                                            on_message=ClientSocket.on_message,
                                            on_close=ClientSocket.on_close,
                                            on_open=ClientSocket.on_open,
                                            on_error=ClientSocket.on_error)

If you want to modify or create a socket connection with websocket class, don't forget to add on_message,on_close,on_open,on_error to your constructor parameters.

Run Socket Forever

To run this class, execute this function. This function will run forever and you don't have to touch this file again.

def runSocketServer():
    ClientSocket.initialize()
    while(True):   // If some error happens, this function will run again. (Errors most likely happen when this class cannot connect to the Django Server.) 
        if(not ClientSocket.isWSConnect):
            ClientSocket.ws.run_forever()
        time.sleep(1)

CameraManager

This is a WebSocket class, designed to communicate with the bike's camera.

Port

You can customize the port which this application will run on here.

port = "7010"  // line 83

ContinueSending

This function is very important as it will keep sending the room id to the camera every 1 seconds ( So the camera can continue ). I have it dose this so in case you want to relaunch the camera, then you don't have to relaunch this python code alone with it.

@staticmethod  # line 102
def continueSending():
    print("Start feeding roomid to camera.")
    while(True):
        if(CameraManager.currentcamera != None):
            CameraManager.currentcamera.sendMessage(CameraManager.roomid)  // Keep sending the room id.
        time.sleep(1)

Comment

The Comment class is nearly identical to BikeComment.py of the Django Server. In addition to that Django class, this class also has a method to convert this class to JSON string and convert a JSON string to this object.

@staticmethod
def strToComment(request):
    data = json.loads(request)

    command = (data['command'] if ('command' in data) else 'undefined')
    topic = (data['topic'] if ('topic' in data) else 'undefined')
    comment = (data['comment'] if ('comment' in data) else 'undefined')
    
    return Comment(id,topic,comment)

@staticmethod
def commentToJsonStr(comment):
    data = dict()
    data['command'] = (comment.command if (type(comment.command) == str) else 'undefined')
    data['topic'] = comment.topic
    data['comment'] = comment.comment
    return json.dumps(data)