Code for PhotonDoor

var UI = require(‘ui’);
var ajax = require(‘ajax’);
var vibe = require(‘ui/vibe’);
var Vector2 = require(‘vector2’);

var device_name = “garage1”;

var DEVICE_ID = “XXXXXXXXXXXXXXXXXXX”;//FROM SPARKCORE IDE
var ACCESS_TOKEN = “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;//FROM SPARKCORE IDE

console.log(device_name + ” app started”);
var main_window = new UI.Window();
//static text for buttons
var txtOpenLabel = new UI.Text({
position: new Vector2(0, 20),
size: new Vector2(144, 30),
font: ‘Gothic 24’,
text: ‘Door 1-‘,
textAlign: ‘right’,
color: ‘white’
});

var txtCloseLabel = new UI.Text({
position: new Vector2(0, 105),
size: new Vector2(144, 30),
font: ‘Gothic 24’,
text: ‘Door 2-‘,
textAlign: ‘right’,
color: ‘white’
});

var txtUpdateLabel = new UI.Text({
position: new Vector2(0, 62),
size: new Vector2(144, 30),
font: ‘Gothic 24’,
text: ‘-‘,
textAlign: ‘right’,
color: ‘white’
});

var txtHeaderLabel = new UI.Text({
position: new Vector2(0, 0),
size: new Vector2(144, 30),
font: ‘Gothic 18’,
text: ‘PhotonDoor’,
textAlign: ‘center’,
color: ‘white’
});
//initialization
UpdateMain(‘started’);
DoGet(‘status’);
//***************************
//*** FUNCTIONS *************

function UpdateMain(message){
main_window.each(function(element) {
main_window.remove(element);
});
main_window.each(function(element) {
main_window.remove(element);
});

main_window.backgroundColor(‘black’);

var txtStatus = new UI.Text({
position: new Vector2(5, 60),
size: new Vector2(144, 10),
font: ‘Gothic 28 Bold’,
text: message,
textAlign: ‘left’,
color: ‘white’
});

main_window.add(txtHeaderLabel);
main_window.add(txtOpenLabel);
main_window.add(txtStatus);
main_window.add(txtUpdateLabel);
main_window.add(txtCloseLabel);

main_window.show();
}

function DoPost(function_name,function_value){
console.log(“DoPost(): ” + new Date().getTime());

//display card
UpdateMain(“Attempt”);

//make url based on function being called and device tokens
var URL = ‘https://api.spark.io/v1/devices/’ + DEVICE_ID + ‘/’ + function_name +’?access_token=’ + ACCESS_TOKEN; //identify which sparkcore and function

//log data being used
console.log(“function_name: ” + function_name);
console.log(“function_value: ” + function_value);
console.log(“URL: ” + URL);

ajax(
{
url: URL,
method: ‘post’,
type: ‘json’,
data: { “args”: function_value} //string to send to the sparkcore function, can be named whatever
},
function(data) {
// Success
console.log(“Success: ” + JSON.stringify(data));
vibe.vibrate(‘short’);
// Show to user
if(data.return_value>0){
UpdateMain(“Closing”);
}else if(data.return_value===0){
UpdateMain(“Opening”);
}else{
UpdateMain(“UNKN:” + data.return_value);
}
},
function(error) {
// Failure
console.log(‘Failed: ‘ + error.toString());

UpdateMain(“FAILED”);

}
);

console.log(“Comleted DoPost(): ” + new Date().getTime());
}
function DoGet(function_name){
console.log(“DoGet(): ” + new Date().getTime());

//display card
UpdateMain(“Updating”);

//make url based on function being called and device tokens
var URL = ‘https://api.spark.io/v1/devices/’ + DEVICE_ID + ‘/’ + function_name +’?access_token=’ + ACCESS_TOKEN; //identify which sparkcore and function

//log data being used
console.log(“function_name: ” + function_name);
console.log(“URL: ” + URL);

ajax(
{
url: URL,
method: ‘post’,
type: ‘json’,
},
function(data) {
// Success
console.log(“Success: ” + JSON.stringify(data.return_value));
vibe.vibrate(‘short’);
// Show to user

if (data.return_value == ‘1’) {
UpdateMain(“Door1Open”);
} else if (data.return_value == ‘2’) {
UpdateMain(“Door2Open”);
} else if (data.return_value == ‘3’) {
UpdateMain(“Open”);
} else if (data.return_value == ‘0’) {
UpdateMain(“Closed”);
} else {
UpdateMain(“UNKN: ” + data.result);
}
},
function(error) {
// Failure
console.log(‘Failed: ‘ + error.toString());
UpdateMain(“FAILED”);
}
);

console.log(“Comleted DoGet(): ” + new Date().getTime());
}
//BUTTON ACTIONS
main_window.on(‘click’, ‘up’, function(e) {
console.log(“Door1”);
DoPost(‘relayOne’,’Click’);
});

main_window.on(‘click’, ‘down’, function(e) {
console.log(“Door2”);
DoPost(‘relayTwo’,’Click’);
});
main_window.on(‘click’, ‘select’, function(e) {
console.log(“status”);
DoGet(‘status’);
});