/* robojam 2014-07-21 */

var RJ = RJ || {};
(function(){
    var PORT = 8080;
    var SERIAL_PORT = "/dev/ttyACM0";
    var CLIENT_PAGE = "client.html";
    var SERIAL_PARAMS = {
        baudRate: 115200,
    	dataBits: 8,
    	parity: "none",
    	stopBits: 1,
    	flowControl: false
    };
    var WebSocketServer = require('websocket').server;
    var http = require('http');
    var fs = require('fs');
    var SerialPort = require("serialport").SerialPort;
    var port;
    var plainHttpServer;
    var wsServer;

    var log;
    var onMessage;
    var onSerialData;

    /* open serial port */
    port = new SerialPort(SERIAL_PORT, SERIAL_PARAMS, function(error) {
        log("serial port open error:" + error);
    });

    /* logging function */
    log = function(msg) {
        console.log(new Date() + " " + msg);
    };

    /* WebSocket meessage handler */
    onMessage = function(message) {
        var data = JSON.parse(message.utf8Data);
        log(data.direction);
        port.write(data.direction, function(error, results) {
            log("serial error:" + error);
            log("serial result:" + results);
        });
    };

    /* Serial port handler */
    onSerialData = function(input) {
        if(new String(input).trim().length > 0) {
	    log("receive from serial:" + input);
        }
    };

    /* set up serial port */
    port.on("open", function() {
        /* startup server after opened serial port */
        log("open serialport!");
    });
    port.on("data", onSerialData);

    /* HTTP server for static files. */
    plainHttpServer = http.createServer( function(req, res) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(fs.readFileSync(CLIENT_PAGE));
    });

    plainHttpServer.listen(PORT, function() {
        log("Server is listening on port " + PORT);
    });

    wsServer = new WebSocketServer({httpServer: plainHttpServer});
    wsServer.on("request", function(request) {
        var connection;
        log("request " + request.origin);
        connection = request.accept(null, request.origin);
        log("Connection accepted. Peer " + connection.remoteAddress);

        connection.on("message", onMessage); 
        connection.on("close", function(reasonCode, description) {
            log("Peer " + connection.remoteAddress + " disconnected.");
        });
    });

})();


@@ -147,16 +147,17 @@
 .html%22;%0A
+%0A
     var 
@@ -288,16 +288,204 @@
 %0A    %7D;%0A
+%0A    var VIDEO_PARAMS = %5B%0A        %22-i%22, %22/dev/video0%22,%0A        %22-s%22, %22320x240%22,%0A        %22-f%22, %22video4linux2%22,%0A        %22-f%22, %22mpeg1video%22,%0A        %22-b%22, %22800k%22,%0A        %22-r%22, %2230%22,%0A    %5D;%0A%0A
     var 
@@ -1250,91 +1250,121 @@
     
-log(%22serial error:%22 + error);%0A            log(%22serial result:%22 + results);%0A        
+if(error) %7B%0A                log(%22write error:%22 + error);%0A            %7D%0A        %7D);%0A        port.drain(function()%7B
 %7D);%0A

fix serialport bug