Alex Glow
Published

Coffee Status Sign

See whether there is fresh coffee available, and be the first to grab it!

Full instructions provided805
Coffee Status Sign

Story

Read more

Code

file_3648.txt

JavaScript
See the entire code base here: http://jsfiddle.net/brockleej/zbfjp4bc/
var api = pinoccioAPI();

//Usage:
//1. Put your main api.token in the approprate spot.
//2. Put the appropriate troopId, scoutId, pinType 'digital' or 'analog' an pinNum
//3. put the name of the command you wish your troop to run when the button is pushed.
// to make a toggle command in HQ: function toggle {d2=!d2;} //toggles pin d2 the pin. 
api.token = "YourTokenHere";
var troopId = 2;
var scoutId = 1;
var pinType = 'digital';
var pinNum = 0;// d2=0, d3=1, d4=2 etc.
var command = "toggle";

// when a button is clicked run the funtcion tog() 
$("button").click(function () {
    tog();
})

 
// change the pin state in HQ and check to see if it is updated on this page! 
// when the button is clicked the scout is given instructions to run the "toggle" command.
function tog() {

    //toggle command on scout--- function toggle {d2=!d2;} //toggles pin "d2"
    api.rest({
        url: "/v1/" + troopId + "/" + scoutId + "/command",
        data: {
            command: command
        }
    }, function (error, result) {
        // this is where you find out of the command was successful if(!error)
        // and read the result. for commands that print results.
        if (error) return $("#out").text('ERROR: ' + (error.message || error + ''));
        // setting the led does not print a result.
        $("#out").html("success!");
    })

    $("#out").text("sending command " + command + " to troop " + troopId + " and scout " + scoutId);
}



// make a sync stream so we know the real state of pin.
api.sync({stale: 1}).on('data', function (data) {
    // is this is an event for the troop and scout im interested in?
    // the sync returns a lot of data... you could choose nearly any variable to look at.
    if (data.type == pinType && data.troop == troopId && data.scout == scoutId) {
        // read the pin values for the particular scout. 
        var pins = data.value.state;
        var status;
        //The following checks the status on a pin 
        //in the "pinType"  data.type / data.value.state. d2=0, d3=1, d4=2, etc
        //then runs logical test and changes status based on pin state.
        if (pins[pinNum] == 1) {
            //change the status message
            status = "AWESOME";
            //or change the status image
            document.getElementById("sign").src = "http://brockclan.com/on.jpg";
            } else {
                status = "BUMMER";
                document.getElementById("sign").src = "http://brockclan.com/off.jpg";
            }
            // set text based on "stale" events are stale when they have occured before the last online time.
            // its the last recorded value but it may not be the current state.
            var text = status + "!";
            if (data.stale) text = "last known" + status + "!";
            $("#coffee").text(text);
            $(signPic).slideToggle( "slow", function(){
 
  });


        }
    }).on('error', function (error) {
    $("#error").text('sync error. ' + (error.message || error + ''));
})

Credits

Lee Brock

Posted by Alex Glow

Comments

Add projectSign up / Login