add variables
This commit is contained in:
5
HELP.md
5
HELP.md
@@ -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 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
|
||||||
|
|||||||
52
index.js
52
index.js
@@ -32,10 +32,13 @@ instance.prototype.init = function () {
|
|||||||
self.projects = [{ id: '0', label: 'None' }]
|
self.projects = [{ id: '0', label: 'None' }]
|
||||||
|
|
||||||
self.init_presets()
|
self.init_presets()
|
||||||
|
self.update_variables()
|
||||||
self.auth()
|
self.auth()
|
||||||
self.getWorkspace()
|
self.getWorkspace()
|
||||||
self.getCurrentTimer().then((timerId) => {
|
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()
|
self.actions()
|
||||||
}
|
}
|
||||||
@@ -99,6 +102,27 @@ instance.prototype.destroy = function () {
|
|||||||
debug('destroy', self.id)
|
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 () {
|
instance.prototype.init_presets = function () {
|
||||||
var self = this
|
var self = this
|
||||||
var presets = []
|
var presets = []
|
||||||
@@ -185,8 +209,8 @@ instance.prototype.action = function (action) {
|
|||||||
switch (action.action) {
|
switch (action.action) {
|
||||||
case 'startNewTimer': {
|
case 'startNewTimer': {
|
||||||
self.getCurrentTimer().then((timerId) => {
|
self.getCurrentTimer().then((timerId) => {
|
||||||
self.log('debug', 'Current timer id ' + timerId)
|
|
||||||
if (timerId === undefined || timerId === null || self.config.alwaysStart === true) {
|
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'
|
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/start'
|
||||||
if (opt.project == '0') {
|
if (opt.project == '0') {
|
||||||
var body = '{"time_entry":{"description":"' + opt.description + '","created_with":"companion"}}'
|
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) => {
|
self.sendCommand('rest', cmd, body).then((result) => {
|
||||||
if (typeof result === 'object' && result.data !== null && result.data !== undefined) {
|
if (typeof result === 'object' && result.data !== null && result.data !== undefined) {
|
||||||
self.log('debug', 'New timer started ' + result.data.id)
|
self.log('debug', 'New timer started ' + result.data.id)
|
||||||
|
self.setVariable('timerId', result.data.id)
|
||||||
|
self.setVariable('timerDescription', result.data.description)
|
||||||
} else {
|
} else {
|
||||||
self.log('warn', 'Error starting timer')
|
self.log('warn', 'Error starting timer')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
self.log('debug', 'A timer is already running')
|
self.log('debug', 'A timer is already running ' + timerId.id)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case 'stopCurrentTimer': {
|
case 'stopCurrentTimer': {
|
||||||
self.getCurrentTimer().then((timerId) => {
|
self.getCurrentTimer().then((timerId) => {
|
||||||
self.log('debug', 'Current timer id ' + timerId)
|
self.log('debug', 'Current timer id ' + timerId.id)
|
||||||
if (timerId !== null && timerId !== undefined) {
|
if (timerId.id !== null && timerId.id !== undefined) {
|
||||||
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + timerId + '/stop'
|
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + timerId.id + '/stop'
|
||||||
self.sendCommand('rest_put', cmd).then((result) => {
|
self.sendCommand('rest_put', cmd).then((result) => {
|
||||||
if (typeof result === 'object' && result.data !== null && result.data !== undefined) {
|
if (typeof result === 'object' && result.data !== null && result.data !== undefined) {
|
||||||
self.log('debug', 'Stopped ' + result.data.id + ', duration ' + result.data.duration)
|
self.log('debug', 'Stopped ' + result.data.id + ', duration ' + result.data.duration)
|
||||||
|
self.setVariable('timerId', null)
|
||||||
|
self.setVariable('timerDescription', null)
|
||||||
} else {
|
} else {
|
||||||
self.log('warn', 'Error stopping timer')
|
self.log('warn', 'Error stopping timer')
|
||||||
}
|
}
|
||||||
@@ -231,7 +259,9 @@ instance.prototype.action = function (action) {
|
|||||||
}
|
}
|
||||||
case 'getCurrentTimer': {
|
case 'getCurrentTimer': {
|
||||||
self.getCurrentTimer().then((result) => {
|
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
|
break
|
||||||
}
|
}
|
||||||
@@ -339,11 +369,17 @@ instance.prototype.getCurrentTimer = function () {
|
|||||||
(result) => {
|
(result) => {
|
||||||
if (typeof result === 'object' && result.data !== null && result.data !== undefined) {
|
if (typeof result === 'object' && result.data !== null && result.data !== undefined) {
|
||||||
if ('id' in result.data) {
|
if ('id' in result.data) {
|
||||||
resolve(result.data.id)
|
resolve(result.data)
|
||||||
} else {
|
} else {
|
||||||
|
self.log('debug', 'Error getting current timer (no id in data)')
|
||||||
|
self.setVariable('timerId', null)
|
||||||
|
self.setVariable('timerDescription', null)
|
||||||
resolve(null)
|
resolve(null)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
self.log('debug', 'Error getting current timer (no object)')
|
||||||
|
self.setVariable('timerId', null)
|
||||||
|
self.setVariable('timerDescription', null)
|
||||||
resolve(null)
|
resolve(null)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "toggl-track",
|
"name": "toggl-track",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"api_version": "1.0.0",
|
"api_version": "1.0.0",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"Timer",
|
"Timer",
|
||||||
|
|||||||
Reference in New Issue
Block a user