add variables

This commit is contained in:
peter
2022-08-01 17:05:11 +00:00
parent 9d82ca2563
commit 0a94a230b9
3 changed files with 49 additions and 10 deletions

View File

@@ -45,4 +45,7 @@ Allow a project to be specified when starting a new timer button
Add an action to refresh the project list
Add 'Always start' configuration option
Add 'Always start' configuration option
### Version 1.0.3
Add variables for timerId and timerDescription

View File

@@ -32,10 +32,13 @@ instance.prototype.init = function () {
self.projects = [{ id: '0', label: 'None' }]
self.init_presets()
self.update_variables()
self.auth()
self.getWorkspace()
self.getCurrentTimer().then((timerId) => {
self.log('debug', 'Current timer id ' + timerId)
self.log('debug', 'Current timer id ' + timerId.id + ' ' + timerId.description)
self.setVariable('timerId', timerId.id)
self.setVariable('timerDescription', timerId.description)
})
self.actions()
}
@@ -99,6 +102,27 @@ instance.prototype.destroy = function () {
debug('destroy', self.id)
}
instance.prototype.update_variables = function (system) {
var self = this
var variables = []
variables.push(
{
label: 'Current Timer Id',
name: 'timerId',
},
{
label: 'Current Timer Description',
name: 'timerDescription',
}
)
self.setVariableDefinitions(variables)
self.setVariable('timerId', null)
self.setVariable('timerDescription', null)
}
instance.prototype.init_presets = function () {
var self = this
var presets = []
@@ -185,8 +209,8 @@ instance.prototype.action = function (action) {
switch (action.action) {
case 'startNewTimer': {
self.getCurrentTimer().then((timerId) => {
self.log('debug', 'Current timer id ' + timerId)
if (timerId === undefined || timerId === null || self.config.alwaysStart === true) {
// no timer currently running or we want to restart it
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/start'
if (opt.project == '0') {
var body = '{"time_entry":{"description":"' + opt.description + '","created_with":"companion"}}'
@@ -201,24 +225,28 @@ instance.prototype.action = function (action) {
self.sendCommand('rest', cmd, body).then((result) => {
if (typeof result === 'object' && result.data !== null && result.data !== undefined) {
self.log('debug', 'New timer started ' + result.data.id)
self.setVariable('timerId', result.data.id)
self.setVariable('timerDescription', result.data.description)
} else {
self.log('warn', 'Error starting timer')
}
})
} else {
self.log('debug', 'A timer is already running')
self.log('debug', 'A timer is already running ' + timerId.id)
}
})
break
}
case 'stopCurrentTimer': {
self.getCurrentTimer().then((timerId) => {
self.log('debug', 'Current timer id ' + timerId)
if (timerId !== null && timerId !== undefined) {
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + timerId + '/stop'
self.log('debug', 'Current timer id ' + timerId.id)
if (timerId.id !== null && timerId.id !== undefined) {
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + timerId.id + '/stop'
self.sendCommand('rest_put', cmd).then((result) => {
if (typeof result === 'object' && result.data !== null && result.data !== undefined) {
self.log('debug', 'Stopped ' + result.data.id + ', duration ' + result.data.duration)
self.setVariable('timerId', null)
self.setVariable('timerDescription', null)
} else {
self.log('warn', 'Error stopping timer')
}
@@ -231,7 +259,9 @@ instance.prototype.action = function (action) {
}
case 'getCurrentTimer': {
self.getCurrentTimer().then((result) => {
self.log('debug', 'Current timer id ' + result)
self.log('debug', 'Current timer id ' + result.id)
self.setVariable('timerId', result.id)
self.setVariable('timerDescription', result.description)
})
break
}
@@ -339,11 +369,17 @@ instance.prototype.getCurrentTimer = function () {
(result) => {
if (typeof result === 'object' && result.data !== null && result.data !== undefined) {
if ('id' in result.data) {
resolve(result.data.id)
resolve(result.data)
} else {
self.log('debug', 'Error getting current timer (no id in data)')
self.setVariable('timerId', null)
self.setVariable('timerDescription', null)
resolve(null)
}
} else {
self.log('debug', 'Error getting current timer (no object)')
self.setVariable('timerId', null)
self.setVariable('timerDescription', null)
resolve(null)
}
},

View File

@@ -1,6 +1,6 @@
{
"name": "toggl-track",
"version": "1.0.2",
"version": "1.0.3",
"api_version": "1.0.0",
"keywords": [
"Timer",