From 88d3c3fd2dff23d0f0c3451aca08898fc42cd9f2 Mon Sep 17 00:00:00 2001 From: peter Date: Sat, 26 Feb 2022 10:41:21 +0000 Subject: [PATCH] version 1.0.0 --- HELP.md | 28 +++++- index.js | 253 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 index.js diff --git a/HELP.md b/HELP.md index cf90a5b..be47318 100644 --- a/HELP.md +++ b/HELP.md @@ -2,9 +2,33 @@ This module allows you to start and stop [Toggl Track](https://track.toggl.com/) timers. -Before using this module you must create a Toggl Track account. Then go to your Toggl profile settings page and copy the API token. Paste this token into the config page of this module and click save. If all is well the module status will turn green. +## Configuration -Look for this section on your toggl profile. +Before using this module you must create a Toggl Track account. Then go to your Toggl profile settings page and copy the API token. Look for this section on your toggl profile. ![api token](api_token.png) +Paste this token into the config page of this module and click save. If all is well the module status will turn green. + +## Actions + +**Start New Timer** + +Start a new timer running with the description set in the action and store the ID + +**Get Current Timer** + +Companion only knows the ID of timers it has started, if a timer is started from another application or the toggle website then this action will get the ID so Companion knows about it. + +**Stop Current Timer** + +Attempt to stop the current timer. This will fail if Companion doesn't know the ID of the currently running timer. + +## Presets + +Presets are available for **Start Timer** and **Stop Timer**. The **Stop Timer** preset also includes both the **Get Current Timer** and **Stop Current Timer** actions to ensure reliable operation. + +## History + +### Version 1.0.0 +First release \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..61b5bef --- /dev/null +++ b/index.js @@ -0,0 +1,253 @@ +var instance_skel = require('../../instance_skel') + +var debug +var log + +function instance(system, id, config) { + var self = this + + // super-constructor + instance_skel.apply(this, arguments) + + return self +} + +instance.prototype.updateConfig = function (config) { + var self = this + self.config = config + + self.auth() + self.getCurrentTimer() + self.actions() +} + +instance.prototype.init = function () { + var self = this + + debug = self.debug + log = self.log + + self.currentTimer = null + + self.init_presets() + self.auth() + self.getCurrentTimer() + self.actions() +} + +instance.prototype.auth = function () { + var self = this + + auth = Buffer.from(self.config.apiToken + ':' + 'api_token').toString('base64') + self.header = [] + self.header['Content-Type'] = 'application/json' + self.header['Authorization'] = 'Basic ' + auth + + console.log(self.header) + +} + +instance.prototype.config_fields = function () { + var self = this + return [ + { + type: 'text', + id: 'info', + width: 12, + label: 'Information', + value: 'This module is for the toggl track service', + }, + { + type: 'textinput', + id: 'apiToken', + label: 'Personal API Token from your Toggl user profile (required)', + width: 12, + default: '', + }, + ] +} + +instance.prototype.destroy = function () { + var self = this + self.currentTimer = null + debug('destroy', self.id) +} + +instance.prototype.init_presets = function () { + var self = this + var presets = [] + + presets.push({ + category: 'Timer', + label: 'Start', + bank: { + style: 'text', + text: 'Start Timer', + size: '18', + color: 16777215, + bgcolor: 0, + }, + actions: [ + { + action: 'startNewTimer', + options: { + description: '', + } + }, + ], + }) + + presets.push({ + category: 'Timer', + label: 'Stop', + bank: { + style: 'text', + text: 'Stop Timer', + size: '18', + color: 16777215, + bgcolor: 0, + }, + actions: [ + { + action: 'getCurrentTimer', + }, + { + action: 'stopCurrentTimer', + delay: 250, + }, + ], + }) + + self.setPresetDefinitions(presets) +} + +instance.prototype.actions = function (system) { + var self = this + + self.setActions({ + startNewTimer: { + label: 'Start New Timer', + options: [ + { + type: 'textinput', + label: 'Description', + id: 'description', + default: '', + }, + ], + }, + getCurrentTimer: { + label: 'Get Current Timer', + }, + stopCurrentTimer: { + label: 'Stop Current Timer', + }, + }) +} + +instance.prototype.action = function (action) { + var self = this + const opt = action.options + + switch (action.action) { + case 'startNewTimer': { + var restCmd = 'rest' // post + var cmd = 'https://api.track.toggl.com/api/v8/time_entries/start' + var body = '{"time_entry":{"description":"' + opt.description + '","created_with":"companion"}}' + console.log(body) + break + } + case 'stopCurrentTimer': { + var restCmd = 'rest_put' + console.log(self.currentTimer) + + if (self.currentTimer != null && self.currentTimer != undefined) { + var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + self.currentTimer + '/stop' + console.log(cmd) + } else { + self.log('warn','No running timer to stop or running timer ID unknown') + return + } + break + } + case 'getCurrentTimer': { + self.getCurrentTimer() + return + break + } + default: + return + break + } + + self.system.emit(restCmd, cmd, body, function (err, result) { + + if (err !== null) { + console.log(result.statusCode) + console.log('HTTP Request failed (' + result.error.code + ')') + self.status(self.STATUS_ERROR, result.error.code); + } else { + console.log(typeof(result.data)) + console.log(result.statusCode) + if (!self.auth_error) { + self.status(self.STATUS_OK) + if (typeof result.data === 'object') { + if (typeof result.data.data === 'object') { + self.interpretData(result.data.data) + } + } else { + self.currentTimer = null + console.log(result.data) + self.log('debug',result.data) + } + } + } + }, self.header) + +} + +instance.prototype.getCurrentTimer = function () { + var self = this + var cmd = 'https://api.track.toggl.com/api/v8/time_entries/current' + + self.system.emit('rest_get', cmd, function (err, result) { + if (err !== null) { + console.log('HTTP Request failed (' + result.error.code + ')') + self.status(self.STATUS_ERROR, result.error.code) + } else if (result.response.statusCode == 200) { + console.log('status:' + result.response.statusCode) + self.status(self.STATUS_OK) + if (typeof result.data.data === 'object' && result.data.data != null) { + if ('id' in result.data.data) { + self.currentTimer = result.data.data.id + console.log(self.currentTimer) + self.log('debug','Current timer id ' + self.currentTimer) + } + } else { + console.log(result.data) + self.log('debug','No timer running') + self.currentTimer = null + } + } else { + console.log('error: ' + result.response.statusCode) + self.status(self.STATUS_ERROR, result.response.statusCode) + self.log('warn','Unable to connect to toggl, check your API token is correct') + self.currentTimer = null + } + }, self.header) +} + +instance.prototype.interpretData = function (data) { + var self = this; + + console.log(data) + + if ('id' in data) { + console.log(data.id) + self.currentTimer = data.id + self.log('debug','timer id ' + data.id) + } +} + +instance_skel.extendedBy(instance) +exports = module.exports = instance