Back to Parent

<!DOCTYPE HTML>
<html>
<head>
    <!--used jquery to send AJAX requests-->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
    <script type="text/javascript">
    //chair's name : accessToken;
    
    var chairsLogInfo = {
            "53ff6e066667574822262067": "15916cf78641fbabdec9c7766212ad846a9799de", //fan sai
            "53ff6f066667574826352167": "f36986b13669d9499004aed7de5af9c96a871fac", //jeremy
            "53ff68066667574851512367": "e31a1e42deb194ee4cfdfde6bfec16c417cfb935", //vanessa
            "53ff6d066667574815382067": "b142ca27e40c939294795da923ee4a98376ed7be"  //raj

        },
        chairStatus = {},
        waitingChairs = [],
        connectedChairs = [],
        chair,
        loc = {
            "53ff6e066667574822262067": "San Francisco", 
            "53ff6f066667574826352167": "Chicago", 
            "53ff68066667574851512367": "Pittsburgh", 
            "53ff6d066667574815382067": "New York"  
        };

    for (chair in chairsLogInfo ){
        if(chairsLogInfo.hasOwnProperty(chair)){

            start(chair, chairsLogInfo[chair]);
            chairStatus[chair] = {
                "status": "unconnected",
                "partner": undefined,
                "init": function(){
                    this.status = "unconnected";
                    this.partner = undefined;
                }
            }
        }
        console.log("Watch chair: ", loc[chair]);
        
    }


    //build connections with spark cores
    function start(deviceID, accessToken) {
        var eventSource = new EventSource("https://api.spark.io/v1/devices/" + deviceID + "/events/?access_token=" + accessToken);

        eventSource.addEventListener('error', function(e) {
                console.log("Errored!"); 
            },false);

        eventSource.addEventListener('sdnt/sitdown', sitDownHandler, false);
        eventSource.addEventListener('sdnt/leave', leaveHandler, false);
    };


    function sitDownHandler(e){
        
        var id = JSON.parse(e.data).coreid;

        if(waitingChairs.indexOf(id) === -1 && chairStatus[id].status === "unconnected"){
            chairStatus[id].status = "waiting";
            waitingChairs.push(id);
            console.log("Chair ", loc[id], " added to waiting list");
        }

        matcher();
    }


    function leaveHandler(e){
        
        var id = JSON.parse(e.data).coreid;

        console.log("Chair ", id, " leaves");

        //if is a waiting chair, remove from the waiting list
        if(chairStatus[id].status === "waiting"){
            waitingChairs = waitingChairs.filter(function(v){
                return v !== id;
            })
            chairStatus[id].init();
        //if is a connected chair, disconect it
        }else if(chairStatus[id].status === "connected"){
            disconnect(id);
        }
    }

    //match waiting chairs in pair
    function matcher(){
        console.log('Matcher executed, waiting chairs:', waitingChairs);
        if(waitingChairs.length < 2) return;
        connect(waitingChairs.shift(), waitingChairs.shift());
        if(waitingChairs.length >= 2) matcher();
    }


    //connect two chairs
    function connect(chair1, chair2){
        console.log("Connect ", loc[chair1], " and ", loc[chair2])

        //connect chair1
        chairStatus[chair1].status = "connected";
        chairStatus[chair1].partner = chair2;
        callFn(chair1, "connect", 1);

        //connect chair2
        chairStatus[chair2].status = "connected";
        chairStatus[chair2].partner = chair1;
        callFn(chair2, "connect", 1);
    }

    //disconnect a chair, put its partner into waiting list
    function disconnect(chair){
        var partner = chairStatus[chair].partner;
        console.log("Disconnected ", loc[chair]);

        //put its partner to "waiting" status,
        if(partner){
            console.log("Disconnected ", loc[chair], "'s partner ", loc[partner]);
            waitingChairs.push(partner);
            chairStatus[partner].status = "waiting";
            callFn(partner, "disconnect", 1);
        }

        callFn(chair, "disconnect", 1);
        chairStatus[chair].init();
        matcher();
    }

    //a short hand function for send POST request
    function callFn(chairId, fnName, arg) {
        $.post( "https://api.spark.io/v1/devices/" + chairId + "/" + fnName + 
                        "?access_token=" + chairsLogInfo[chairId], 
                        { "args": arg });
    }
    </script>
</body>
</html>
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0