diff --git a/README.md b/README.md index 1cac666..c2634d3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -SDK2 - ### Homey CountDown timer This is a CountDown timer which you can use to initiate flows. @@ -79,6 +77,7 @@ If you like the app, consider a donation to support development ### Changelog +- V2.0.1 2018-12-07 : Bugfix for one trigger card - V2.0.0 2018-12-07 : SDK2 enabled - V1.2.5 2018-03-18 : Bugfix adjust timer - V1.2.4 2018-03-17 : Revert part of input validation, bug in action and conditions cards diff --git a/app.json b/app.json index 1285459..05708d0 100644 --- a/app.json +++ b/app.json @@ -3,7 +3,7 @@ "name": { "en": "CountDown" }, - "version": "2.0.0", + "version": "2.0.1", "compatibility": ">=1.5.0", "sdk": 2, "description": { @@ -115,9 +115,10 @@ "en": "Timer value has changed", "nl": "Zandloper waarde is veranderd" }, - "tokens": [ + "token": [ { "name": "variable", + "type": "string", "title": { "en": "name timer", "nl": "naam zandloper" @@ -125,7 +126,7 @@ }, { "name": "value", - "type": "number", + "type": "string", "title": { "en": "value", "nl": "waarde" @@ -257,7 +258,7 @@ }, { "name": "value", - "type": "text", + "type": "number", "placeholder": { "en": "seconds", "nl": "seconden" @@ -282,7 +283,7 @@ }, { "name": "valuemin", - "type": "text", + "type": "number", "placeholder": { "en": "min. seconds", "nl": "min. seconden" @@ -290,7 +291,7 @@ }, { "name": "valuemax", - "type": "text", + "type": "number", "placeholder": { "en": "max. seconds", "nl": "max. seconden" @@ -315,7 +316,7 @@ }, { "name": "value", - "type": "text", + "type": "number", "placeholder": { "en": "seconds", "nl": "seconden" diff --git a/lib/flow/actions.js b/lib/flow/actions.js deleted file mode 100644 index 4a33c7a..0000000 --- a/lib/flow/actions.js +++ /dev/null @@ -1,121 +0,0 @@ -var util = require('../util/util.js'); -var variableManager = require('../variablemanagement/variablemanagement.js'); - -exports.createActions = function () { - - - Homey.manager('flow').on('action.set_countdown_timer', function (callback, args) { - Homey.log('set countdown timer'); - var setdate = getShortDate(); - if (args.variable && args.variable.name) { - var variable = variableManager.getvariable(args.variable.name); - var tokens = { 'variable' : args.variable.name }; - var state = { 'variable' : args.variable.name }; - Homey.log(tokens); - Homey.manager('flow').trigger('countdown_started', tokens, state); - if (variable) { - variableManager.updatevariable(args.variable.name, parseInt(args.value), 'number',setdate); - callback(null, true); - return; - } - } - callback(null, false); - -}); - -Homey.manager('flow').on('action.set_random_countdown_timer', function (callback, args) { - var setdate = getShortDate(); - if (args.variable && args.variable.name) { - var variable = variableManager.getvariable(args.variable.name); - if (variable) { - //the *1 is to make a number of args.valuemin - var newtimer = Math.floor(Math.random() * (args.valuemax - args.valuemin + 1) + args.valuemin*1); - variableManager.updatevariable(args.variable.name, newtimer, 'number',setdate); - callback(null, true); - return; - } - } - callback(null, false); - -}); - -Homey.manager('flow').on('action.adjust_countdown_timer', function (callback, args) { - Homey.log('adjust countdown timer'); - Homey.log(args.value); - var setdate = getShortDate(); - if (args.variable && args.variable.name) { - var variable = variableManager.getvariable(args.variable.name); - var tokens = { 'variable' : args.variable.name }; - var state = { 'variable' : args.variable.name }; - //Homey.log(tokens); - Homey.log('args.value ' + args.value); - Homey.log(Number(args.value)) - Homey.log('variable.value ' + Number(variable.value)) - var newTimervalue = Number(args.value) + Number(variable.value); - Homey.log('new value: ' + newTimervalue); - if (newTimervalue < 0 ) { newTimervalue = 0 }; - //Homey.manager('flow').trigger('countdown_started', tokens, state); - if (variable) { - variableManager.updatevariable(args.variable.name, newTimervalue, 'number',setdate); - callback(null, true); - return; - } - } - callback(null, false); -}); - - Homey.manager('flow').on('action.stop_countdown_timer', function (callback, args) { - Homey.log('stop countdown timer'); - var setdate = getShortDate(); - if (args.variable && args.variable.name) { - var variable = variableManager.getvariable(args.variable.name); - var tokens = { 'variable' : args.variable.name }; - var state = { 'variable' : args.variable.name }; - Homey.manager('flow').trigger('countdown_stopped', tokens, state); - if (variable) { - variableManager.updatevariable(args.variable.name, -1, 'number',setdate); - callback(null, true); - return; - } - } - callback(null, false); -}); - -Homey.manager('flow').on('action.stop_all_countdown_timers', function (callback, args) { - Homey.log('stop all countdown timers'); - var setdate = getShortDate(); - var currentVariables= variableManager.getvariables(); - //Homey.log(currentVariables); - currentVariables.forEach(function( obj) { - Homey.log(obj.name); - Homey.log(obj.value); - var tokens = { 'variable' : obj.name, 'value' : obj.value }; - var state = { 'variable' : obj.name }; - Homey.manager('flow').trigger('countdown_stopped', tokens, state); - if (obj) { - variableManager.updatevariable(obj.name, -1, 'number',setdate); - callback(null, true); - return; - } - }) - callback(null, false); -}); -} - -function isNumber(obj) { return !isNaN(parseFloat(obj)) } - -function iskBoolean(bool) { - return typeof bool === 'boolean' || - (typeof bool === 'object' && typeof bool.valueOf() === 'boolean'); -} - -function getShortDate() { - now = new Date(); - year = "" + now.getFullYear(); - month = "" + (now.getMonth() + 1); if (month.length == 1) { month = "0" + month; } - day = "" + now.getDate(); if (day.length == 1) { day = "0" + day; } - hour = "" + now.getHours(); if (hour.length == 1) { hour = "0" + hour; } - minute = "" + now.getMinutes(); if (minute.length == 1) { minute = "0" + minute; } - second = "" + now.getSeconds(); if (second.length == 1) { second = "0" + second; } - return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; -} diff --git a/lib/flow/conditions.js b/lib/flow/conditions.js deleted file mode 100644 index e958f23..0000000 --- a/lib/flow/conditions.js +++ /dev/null @@ -1,53 +0,0 @@ -var util = require('../util/util.js'); -var variableManager = require('../variablemanagement/variablemanagement.js'); - -exports.createConditions = function (variables) { - - Homey.manager('flow').on('condition.timer_running', function (callback, args) { - if (args.variable) { - var variable = variableManager.getvariable(args.variable.name); - if (variable && variable.value < 0) { - Homey.log('condition trigger: timer_running'); - callback(null, true); - return; - } - } - callback(null, false); - }); - - Homey.manager('flow').on('condition.timer_matches_number', function (callback, args) { - if (args.variable) { - var variable = variableManager.getvariable(args.variable.name); - if (variable && variable.value === args.value) { - Homey.log('condition trigger: timer_matches_number'); - callback(null, true); - return; - } - } - callback(null, false); - }); - - Homey.manager('flow').on('condition.timer_less_than_number', function (callback, args) { - if (args.variable) { - var variable = variableManager.getvariable(args.variable.name); - if (variable && Number(variable.value) < Number(args.value)) { - Homey.log('condition trigger: timer_less_than_number'); - callback(null, true); - return; - } - } - callback(null, false); - }); - - Homey.manager('flow').on('condition.timer_greater_than_number', function (callback, args) { - if (args.variable) { - var variable = variableManager.getvariable(args.variable.name); - if (variable && Number(variable.value) > Number(args.value)) { - Homey.log('condition trigger: timer_less_than_number'); - callback(null, true); - return; - } - } - callback(null, false); - }); -}; diff --git a/lib/flow/triggers.js b/lib/flow/triggers.js index 314aa18..3294370 100644 --- a/lib/flow/triggers.js +++ b/lib/flow/triggers.js @@ -21,8 +21,8 @@ module.exports = { countdown_to_zero .registerRunListener((args, state) => { console.log('countdown_to_zero') - console.log(args); // this is the user input - console.log(state); // this is the state parameter, as passed in trigger() + //console.log(args); // this is the user input + //console.log(state); // this is the state parameter, as passed in trigger() // If true, this flow should run return Promise.resolve( args.variable.name === state.variable ); @@ -38,9 +38,8 @@ module.exports = { countdown_started .registerRunListener((args, state) => { console.log('countdown_started') - console.log(args); // this is the user input - console.log(state); // this is the state parameter, as passed in trigger() - + //console.log(args); // this is the user input + //console.log(state); // this is the state parameter, as passed in trigger() // If true, this flow should run return Promise.resolve( args.variable.name === state.variable ); }) @@ -50,15 +49,13 @@ module.exports = { return Promise.resolve(variableManager.getVariables().filter(util.contains(query))); }); - - //countdown_stopped let countdown_stopped = new Homey.FlowCardTrigger('countdown_stopped'); countdown_stopped .registerRunListener((args, state) => { console.log('countdown_stopped') - console.log(args); // this is the user input - console.log(state); // this is the state parameter, as passed in trigger() + //console.log(args); // this is the user input + // console.log(state); // this is the state parameter, as passed in trigger() // If true, this flow should run return Promise.resolve( args.variable.name === state.variable ); @@ -69,23 +66,22 @@ module.exports = { return Promise.resolve(variableManager.getVariables().filter(util.contains(query))); }); -// countdown_timer_changed - let countdown_timer_changed = new Homey.FlowCardTrigger('countdown_timer_changed'); - countdown_timer_changed - .registerRunListener((args, state) => { - console.log('countdown_timer_changed') + //countdown_timer_changed + let countdown_timer_changed = new Homey.FlowCardTrigger('countdown_timer_changed'); + countdown_timer_changed + .registerRunListener((args, state) => { + //console.log('countdown_timer_changed') + //console.log(args); // this is the user input + // console.log(state); // this is the state parameter, as passed in trigger() + // If true, this flow should run + return Promise.resolve( args.variable.name === state.variable ); + }) + .register() + .getArgument('variable') + .registerAutocompleteListener((query, args) => { + return Promise.resolve(variableManager.getVariables().filter(util.contains(query))); + }); - console.log(args); // this is the user input - console.log(state); // this is the state parameter, as passed in trigger() - - // If true, this flow should run - return Promise.resolve( args.variable.name === state.variable ); - }) - .register() - .getArgument('variable') - .registerAutocompleteListener((query, args) => { - return Promise.resolve(variableManager.getVariables().filter(util.contains(query))); - }); ///// CONDITIONS //// @@ -95,9 +91,9 @@ module.exports = { timer_running .registerRunListener((args, state) => { console.log('timer_running') - console.log(args.variable); // this is the user input + //console.log(args.variable); // this is the user input var result = variableManager.getVariable(args.variable.name); - console.log(result) + //console.log(result) //console.log(state); // this is the state parameter, as passed in trigger() // If true, this flow should run return Promise.resolve(args && result.value == -1) @@ -112,14 +108,16 @@ module.exports = { let timer_matches_number = new Homey.FlowCardCondition('timer_matches_number'); timer_matches_number .registerRunListener((args, state) => { - console.log('timer_matches_number') - console.log(args.variable); // this is the user input - console.log(args.value) + console.log('timer_matches_number' + args.variable.name) + //console.log(args.variable); // this is the user input + //console.log(args.value) var result = variableManager.getVariable(args.variable.name); - console.log(result) + //console.log(result) //console.log(state); // this is the state parameter, as passed in trigger() // If true, this flow should run - return Promise.resolve(args && result.value == args.value) + if (result) { + return Promise.resolve(args && result.value == args.value) + } }) .register() .getArgument('variable') @@ -131,11 +129,11 @@ module.exports = { let timer_less_than_number = new Homey.FlowCardCondition('timer_less_than_number'); timer_less_than_number .registerRunListener((args, state) => { - console.log('timer_less_than_number') - console.log(args.variable); // this is the user input - console.log(state) + console.log('timer_less_than_number ' + args.variable.name) + //console.log(args.variable); // this is the user input + //console.log(state) var result = variableManager.getVariable(args.variable.name); - console.log(result) + //console.log(result) //console.log(state); // this is the state parameter, as passed in trigger() // If true, this flow should run return Promise.resolve(args && result.value < args.value) @@ -151,10 +149,10 @@ module.exports = { timer_greater_than_number .registerRunListener((args, state) => { console.log('timer_greater_than_number') - console.log(args.variable); // this is the user input - console.log(state) + //console.log(args.variable); // this is the user input + //console.log(state) var result = variableManager.getVariable(args.variable.name); - console.log(result) + //console.log(result) //console.log(state); // this is the state parameter, as passed in trigger() // If true, this flow should run return Promise.resolve(args && result.value > args.value) @@ -171,8 +169,8 @@ module.exports = { set_countdown_timer .registerRunListener((args, state) => { console.log('set_countdown_timer') - console.log(args); // this is the user input - console.log(state); // this is the state parameter, as passed in trigger() + //console.log(args); // this is the user input + //console.log(state); // this is the state parameter, as passed in trigger() // If true, this flow should run if (args.variable && args.variable.name) { @@ -192,9 +190,9 @@ module.exports = { let set_random_countdown_timer = new Homey.FlowCardAction('set_random_countdown_timer'); set_random_countdown_timer .registerRunListener((args, state) => { - console.log('adjust_countdown_timer') - console.log(args); // this is the user input - console.log(state); // this is the state parameter, as passed in trigger() + console.log('set_random_countdown_timer') + //console.log(args); // this is the user input + //console.log(state); // this is the state parameter, as passed in trigger() // If true, this flow should run if (args.variable && args.variable.name) { @@ -215,16 +213,18 @@ module.exports = { let adjust_countdown_timer = new Homey.FlowCardAction('adjust_countdown_timer'); adjust_countdown_timer .registerRunListener((args, state) => { - console.log('adjust_countdown_timer') - console.log(args); // this is the user input - console.log(state); // this is the state parameter, as passed in trigger() + //console.log('adjust_countdown_timer ' + args.variable.name) + //console.log(args); // this is the user input + //console.log(state); // this is the state parameter, as passed in trigger() // If true, this flow should run if (args.variable && args.variable.name) { - var result = variableManager.getVariable(args.variable.name); - if (result) { + console.log('adjust_countdown_timer ' + args.variable.name) + + //var result = variableManager.getVariable(args.variable.name); + //if (result) { return Promise.resolve(variableManager.updateVariable(args.variable.name, args.value, 'number')); - } + //} } }) .register() @@ -239,8 +239,8 @@ module.exports = { stop_countdown_timer .registerRunListener((args, state) => { console.log('stop_countdown_timer') - console.log(args); // this is the user input - console.log(state); // this is the state parameter, as passed in trigger() + //console.log(args); // this is the user input + //console.log(state); // this is the state parameter, as passed in trigger() // If true, this flow should run if (args.variable && args.variable.name) { @@ -276,6 +276,6 @@ module.exports = { // .registerAutocompleteListener((query, args) => { // return Promise.resolve(variableManager.getVariables().filter(util.contains(query))); // }); - } // CreateFlowCardTriggers +} // CreateFlowCardTriggers } diff --git a/lib/old/actions.js b/lib/old/actions.js deleted file mode 100644 index b5ddad4..0000000 --- a/lib/old/actions.js +++ /dev/null @@ -1,20 +0,0 @@ -var util = require('../util/util.js'); -var variableManager = require('../variablemanagement/variablemanagement.js'); -exports.createAutocompleteActions = function () { - - Homey.manager('flow').on('action.set_countdown_timer.variable.autocomplete', function (callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); - - Homey.manager('flow').on('action.set_random_countdown_timer.variable.autocomplete', function (callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); - - Homey.manager('flow').on('action.stop_countdown_timer.variable.autocomplete', function (callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); - - Homey.manager('flow').on('action.adjust_countdown_timer.variable.autocomplete', function (callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); -} diff --git a/lib/old/conditions.js b/lib/old/conditions.js deleted file mode 100644 index 59c1a08..0000000 --- a/lib/old/conditions.js +++ /dev/null @@ -1,20 +0,0 @@ -var util = require('../util/util.js'); -var variableManager = require('../variablemanagement/variablemanagement.js'); -exports.createAutocompleteConditions = function () { - - Homey.manager('flow').on('condition.timer_running.variable.autocomplete', function (callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); - - Homey.manager('flow').on('condition.timer_matches_number.variable.autocomplete', function (callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); - - Homey.manager('flow').on('condition.timer_less_than_number.variable.autocomplete', function (callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); - - Homey.manager('flow').on('condition.timer_greater_than_number.variable.autocomplete', function (callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); -} diff --git a/lib/old/triggers.js b/lib/old/triggers.js deleted file mode 100644 index 052faa0..0000000 --- a/lib/old/triggers.js +++ /dev/null @@ -1,19 +0,0 @@ -var util = require('../util/util.js'); -var variableManager = require('../variablemanagement/variablemanagement.js'); - -exports.createAutocompleteTriggers = function (variables) { - //Triggers autocompletes - Homey.manager('flow').on('trigger.countdown_to_zero.variable.autocomplete', function(callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); - Homey.manager('flow').on('trigger.countdown_started.variable.autocomplete', function(callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); - Homey.manager('flow').on('trigger.countdown_stopped.variable.autocomplete', function(callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); - Homey.manager('flow').on('trigger.countdown_timer_changed.variable.autocomplete', function(callback, value) { - callback(null, variableManager.getVariables().filter(util.filterVariable(value, 'number'))); - }); - -} diff --git a/lib/variablemanagement/variablemanagement.js b/lib/variablemanagement/variablemanagement.js index 0da7fb8..5c99a8f 100644 --- a/lib/variablemanagement/variablemanagement.js +++ b/lib/variablemanagement/variablemanagement.js @@ -12,9 +12,6 @@ module.exports = { util.cdLog("variable manager started") var variables = getVariables(); FlowCardTrigger.CreateFlowCardTriggers(this); - - - //create tokens variables.forEach(function(variable) { createToken(variable.name, variable.value, variable.type,); @@ -23,7 +20,7 @@ module.exports = { Homey.ManagerSettings.on('set', function (action) { if (action == 'deleteall') { - deleteAllInsights(); + //deleteAllInsights(); deleteAllTokens(); Homey.ManagerSettings.set('variables', []); } @@ -103,21 +100,21 @@ function processValueChanged(variables, oldVariable, newVariable) { Homey.ManagerSettings.set('variables', variables); if (newVariable && newVariable.remove) { - removeInsights(newVariable.name); - Homey.ManagerInsights.deleteLog(newVariable.name, function(err, state) {}); + //removeInsights(newVariable.name); + //Homey.ManagerInsights.deleteLog(newVariable.name, function(err, state) {}); removeToken(newVariable.name); return; } if (newVariable && !oldVariable) { - createInsights(newVariable.name, newVariable.value, newVariable.type); + //createInsights(newVariable.name, newVariable.value, newVariable.type); createToken(newVariable.name, newVariable.value, newVariable.type); } if (newVariable && oldVariable && oldVariable.value !== newVariable.value) { console.log('Variable ' + newVariable.name + ' changed from ' + oldVariable.value + ' to ' + newVariable.value); - updateInsights(newVariable.name, newVariable.value); + //updateInsights(newVariable.name, newVariable.value); updateToken(newVariable.name, newVariable.value); } @@ -131,15 +128,18 @@ function processValueChanged(variables, oldVariable, newVariable) { getTrigger('countdown_started').trigger(null, { "variable": newVariable.name, "value": newVariable.value }); } + //countdown_timer_changed + if (newVariable.value !== oldVariable.value ){ + //console.log('variablemanager: counttimer changed') + getTrigger('countdown_timer_changed').trigger(null, { "variable": newVariable.name, "value": newVariable.value }); + Homey.ManagerFlow.getCard('trigger','countdown_timer_changed').trigger(null, { "variable": newVariable.name, "value": newVariable.value }); + } + //countdown_stopped if (newVariable.value == -1 && oldVariable.value !== 0 ){ getTrigger('countdown_stopped').trigger(null, { "variable": newVariable.name, "value": newVariable.value }); } - //countdown_timer_changed - if (newVariable.value !== oldVariable.value ){ - getTrigger('countdown_timer_changed').trigger(null, { "variable": newVariable.name, "value": newVariable.value }); - } //getTrigger('if_variable_changed').trigger(null, { "variable": newVariable.name, "value": newVariable.value }); //getTrigger('debug_any_variable_changed').trigger(null, { "variable": newVariable.name, "value": newVariable.value }); @@ -163,58 +163,6 @@ function getTrigger(name) return Homey.ManagerFlow.getCard('trigger', name);//.register(); } -function createInsights(name, value, type) { - if (type == 'number') { - Homey.ManagerInsights - .createLog(name, - { - label: { en: name }, - type: 'number', - units: { en: 'Value' }, - decimals: 2, - chart: 'stepLine' - }, - function (err, log) { log.createEntry(value); }); - } - if (type == 'boolean') { - Homey.ManagerInsights - .createLog(name, - { - label: { en: name }, - type: 'boolean', - units: { en: 'Value' }, - decimals: 0, - chart: 'column' - }, - function (err, log) { log.createEntry(value); }); - } -} -function deleteAllInsights() { - Homey.ManagerInsights.getLogs(function (err, logs) { - console.log(logs); - logs.forEach(function (log) { - Homey.ManagerInsights.deleteLog(log, function (err, state) { }); - }); - }); -} -function updateInsights(name, value) { - Homey.ManagerInsights.getLog(name, - function(err, log) { - if (log) { - log.createEntry(value); - } - }); -} - -function removeInsights(name) { - Homey.ManagerInsights.getLog(name, - function(err, log) { - if (log) { - Homey.ManagerInsights.deleteLog(log); - } - }); -} - function deleteAllTokens() { for (var i = tokens.length - 1; i >= 0; i--) { tokens[i].unregister().then(() => {}).catch(err => { console.log(err); });