Skip to content

Commit

Permalink
Add sun phase to weather data - and have a (currently) simple app to …
Browse files Browse the repository at this point in the history
…execute a macro when sunset or sunrise occurs. Start some work on Gerty to answer preset questions.
  • Loading branch information
imbrianj committed Jun 25, 2015
1 parent 0de9170 commit 87ddbc2
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 8 deletions.
13 changes: 10 additions & 3 deletions apps/gerty.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,29 @@ module.exports = (function () {
gertyRunCommand = require(__dirname + '/gerty/runCommand'),
gertyMood = require(__dirname + '/gerty/mood'),
utterance = translate.findSynonyms('gerty', controllers.config.language),
text = '';
text = '',
acted = false;

// If it's a command explicitly sent to Gerty to act on.
if((controllers[device]) && (controllers[device].config) && (controllers[device].config.typeClass === 'gerty') && (command.indexOf('text-') === 0)) {
text = command.replace('text-', '').toUpperCase();

gertyMood.setEmotion(text, device, controllers.config.language);

if(gertyRunCommand.setDevice(text, controllers, device, config.macros, controllers.config.language)) {
acted = gertyRunCommand.setDevice(text, controllers, device, config.macros, controllers.config.language);

if(acted === true) {
utterance = utterance.AFFIRMATIVE[Math.floor(Math.random() * utterance.AFFIRMATIVE.length)];
}

else {
else if(acted === false) {
utterance = utterance.NEGATIVE[Math.floor(Math.random() * utterance.NEGATIVE.length)];
}

else {
utterance = acted || '';
}

for(device in controllers) {
if(device !== 'config') {
if(controllers[device].config.typeClass === 'speech') {
Expand Down
58 changes: 58 additions & 0 deletions apps/sunPhase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*jslint white: true */
/*global module, require, console */

/**
* Copyright (c) 2014 [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

/**
* @author [email protected]
* @fileoverview Execute specified macros based on sunset or sunrise.
*/

module.exports = (function () {
'use strict';

return {
version : 20150624,

lastState : null,

sunPhase : function(device, command, controllers, values, config) {
var state = values.phase,
newPhase = '',
rawMacro,
macro,
runCommand;

if(state !== this.lastState) {
this.lastState = state;
runCommand = require(__dirname + '/../../lib/runCommand');
newPhase = state === 'Day' ? 'Sunrise' : 'Sunset';
rawMacro = config.macros[newPhase].split(';');

for(macro in rawMacro) {
runCommand.macroCommands(rawMacro[macro]);
}
}
}
};
}());
2 changes: 2 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ exports.config = {
// It's also available in places that don't have Zip codes.
// Find your location's WOEID from: http://woeid.factormystic.net/
woeid : 12798963,
apps : { 'Sun Phase' : { id : 'sunPhase',
macros : { 'sunset' : 'smartthings=subdevice-mode-Night' } } },
disabled : true
},

Expand Down
57 changes: 54 additions & 3 deletions controllers/weather.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = (function () {
* @fileoverview Basic weather information, courtesy of Yahoo.
*/
return {
version : 20140813,
version : 20150624,

inputs : ['list'],

Expand Down Expand Up @@ -64,8 +64,58 @@ module.exports = (function () {
runCommand.runCommand(controller.config.deviceId, 'list', controller.config.deviceId);
},

/**
* Accept a plain-text time ("7:52 pm") and find the unix timestamp for that
* time on the current day.
*/
formatTime : function (time) {
var hour = time.split(':')[0],
minute = time.split(':')[1].split(' ')[0],
ampm = time.split(' ')[1];

hour = ampm === 'pm' ? (hour + 12) : hour;

return { hour : hour, minute : minute };
},

/**
* Accept sunrise and sunset times as deliverd from the API - and determine
* what the current sun phase is. Can either be "Day" or "Night".
*/
findSunPhase : function (sunriseRaw, sunsetRaw) {
var sunrise = this.formatTime(sunriseRaw),
sunset = this.formatTime(sunsetRaw),
now = new Date(),
unixNow = now.getTime(),
year = now.getFullYear(),
month = now.getMonth(),
date = now.getDate(),
state = '';

sunrise.unix = new Date(year, month, date, sunrise.hour, sunrise.minute);
sunset.unix = new Date(year, month, date, sunrise.hour, sunrise.minute);

// The sun hasn't come up yet.
if(sunrise.unix > unixNow) {
state = 'Night';
}

// The sun has come up - but has not gone down.
else if(sunset.unix > unixNow) {
state = 'Day';
}

// The sun has gone down.
else {
state = 'Night';
}

return state;
},

send : function (config) {
var https = require('https'),
var that = this,
https = require('https'),
weather = {},
dataReply = '',
request;
Expand Down Expand Up @@ -112,7 +162,8 @@ module.exports = (function () {
'sunrise' : city.astronomy.sunrise,
'sunset' : city.astronomy.sunset,
'code' : city.item.condition.code,
'forecast' : city.item.forecast
'forecast' : city.item.forecast,
'phase' : that.findSunPhase(city.astronomy.sunrise, city.astronomy.sunset)
};

deviceState.updateState(weather.deviceId, 'weather', { state : 'ok', value : weatherData });
Expand Down
2 changes: 1 addition & 1 deletion css/combo.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/combo.min.js

Large diffs are not rendered by default.

0 comments on commit 87ddbc2

Please sign in to comment.