Skip to content

Instantly share code, notes, and snippets.

@jmsalcido
Last active December 14, 2015 13:28
Show Gist options
  • Save jmsalcido/5093524 to your computer and use it in GitHub Desktop.
Save jmsalcido/5093524 to your computer and use it in GitHub Desktop.
Script utilizado en el proyecto 1 de Computo Movil en el Instituto Tecnologico de Sonora utilizando on{x} para Android.
// Proyecto 1 de Computo Movil
// Script para el proyecto 1 de Computo Movil.
// autor: Jose Salcido
// id ITSON: 50168
// =============================================
// Escribo todos mis comentarios en ingles y mis
// variables igual por que me gusta asi, si hay
// problema avisenme por favor..
// =============================================
/*====================================================================================*/
//device.notifications.createNotification('Alarma puesta.').show();
// this set the alarm but I CANT GET IT WORKING!!! :(
device.scheduler.setTimer({
name: "alarmamaestra",
time: 0, // not sure of this one :)
interval: 60*60*1000, // set alarm every day.
exact: false },
function () { verifyAlarm() });
function verifyAlarm() {
var date = new Date();
var hour = date.getHours();
if(hour === 17) {
getLocation(1);
} else if (hour === 15) {
getLocation(1);
} else {
// NOT 6 AM.
}
}
// Call this when the device screen is unlocked.
device.screen.on("unlock", function(){
getLocation(0);
});
// Nope.
device.screen.off("off", function() {
//device.scheduler.removeTimer("alarmamaestra");
});
// getLocation function
function getLocation(option) {
// create GPS listener with update interval of 5 sec
// you can also use CELL or PASSIVE providers here
var listener = device.location.createListener('CELL', 5 * 1000);
// register on location changed
listener.on('changed', function (signal) {
// on receiving location print it and stop the provider
console.log('Lat: ' + signal.location.latitude);
console.log('Lon: ' + signal.location.longitude);
listener.stop();
var latlong = signal.location.latitude + "," + signal.location.longitude;
if(option === 0) {
getWeather(latlong);
} else {
getForecast(latlong)
}
});
// start the CELL listener
listener.start();
}
function getForecast(latlong) {
feeds.weather.get(
{
location: latlong, // this was fixed for Cd Obregon.
locationtype: 'latlong', // only using latlong
unittype: 'm' // metric units please.
}, function onSuccess(weather, textStatus, response) {
// save the forecast in a var
var rainProb = weather.now.rain;
if(rainProb >= 50) {
device.notifications.createNotification('Carnavalito, llevate un paraguas por si las moscas, valedor. La probabilidad de lluvia es: ' + rainProb + "%").show();
} else {
device.notifications.createNotification("Carnavalito, hoy no hay probabilidad de que llueva, no seas naco y no te lleves un paraguas, camara, chido. La probabilidad de lluvia es: " + rainProb + "%").show();
}
}, function onError(response, textStatus) {
console.error("Weather error from script");
});
}
// getWeather function
function getWeather(latlong) {
// Get the weather as the on.X api doc.
feeds.weather.get(
{
location: latlong, // this was fixed for Cd Obregon.
locationtype: 'latlong', // only using latlong
unittype: 'm' // metric units please.
},
// function called when the weather is available
function onSuccess(weather, textStatus, response) {
// save the temp in a var
var temperatura = weather.now.temperature;
//console.log(weather.forecasts);
// forecast then WHAT?
console.log(weather.forecasts[0].rain);
// Message variables
var buttons = [":("];
var title;
var content;
// check if the temp is greater than 29.
if(temperatura >= 30) {
// Spanish as Sinaloa and Sonora users talk spanish.
title = "Que Calor!!!";
content = "Wey, esta haciendo un chango de calor. Estamos a mas de 30 grados carnavalito.";
showMessage(title, content, buttons);
}
// check if the temp is minor than 11
else if(temperatura <= 10) {
// Spanish as Sinaloa and Sonora users talk spanish.
title = "Que Frio!!!";
content = "Wey, esta haciendo un chango de frio. Estamos a menos de 10 grados carnavalito.";
showMessage(title, content, buttons);
}
console.log("Weather for " + weather.location + " is: " + weather.now.temperature + " ºC");
},
// function called when an error ocurred.
function onError(response, textStatus) {
console.error("Weather error from script");
}
);
}
function showMessage(title, content, buttons) {
msg = device.notifications.createMessageBox(title);
msg.content = content;
msg.buttons = buttons;
msg.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment