add always start option
This commit is contained in:
4
HELP.md
4
HELP.md
@@ -14,7 +14,7 @@ Paste this token into the config page of this module and click save. If all is w
|
|||||||
|
|
||||||
**Start New Timer**
|
**Start New Timer**
|
||||||
|
|
||||||
Start a new timer running with the description set in the action and store the ID. If a list of projects has been retrieved on startup you can choose a project.
|
Start a new timer running with the description set in the action and store the ID. If a list of projects has been retrieved on startup you can choose a project. By default a new timer can't be started if one is already running. This behaviour can be changed by ticking the 'Always Start' option in the module configuration.
|
||||||
|
|
||||||
**Get Current Timer**
|
**Get Current Timer**
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ Retrives the current list of projects from the toggl server. This action runs au
|
|||||||
|
|
||||||
## Presets
|
## 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.
|
Presets are available for **Start Timer** and **Stop Timer**.
|
||||||
|
|
||||||
## History
|
## History
|
||||||
|
|
||||||
|
|||||||
91
index.js
91
index.js
@@ -18,7 +18,6 @@ instance.prototype.updateConfig = function (config) {
|
|||||||
|
|
||||||
self.auth()
|
self.auth()
|
||||||
self.getWorkspace()
|
self.getWorkspace()
|
||||||
self.getCurrentTimer()
|
|
||||||
self.actions()
|
self.actions()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,7 +27,6 @@ instance.prototype.init = function () {
|
|||||||
debug = self.debug
|
debug = self.debug
|
||||||
log = self.log
|
log = self.log
|
||||||
|
|
||||||
self.currentTimer = null
|
|
||||||
self.workspace = null
|
self.workspace = null
|
||||||
self.workspaceName = null
|
self.workspaceName = null
|
||||||
self.projects = [{ id: '0', label: 'None' }]
|
self.projects = [{ id: '0', label: 'None' }]
|
||||||
@@ -36,7 +34,10 @@ instance.prototype.init = function () {
|
|||||||
self.init_presets()
|
self.init_presets()
|
||||||
self.auth()
|
self.auth()
|
||||||
self.getWorkspace()
|
self.getWorkspace()
|
||||||
self.getCurrentTimer()
|
self.getCurrentTimer().then((result) => {
|
||||||
|
self.log('debug', 'Current timer id ' + result)
|
||||||
|
}
|
||||||
|
)
|
||||||
self.actions()
|
self.actions()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,12 +69,30 @@ instance.prototype.config_fields = function () {
|
|||||||
width: 12,
|
width: 12,
|
||||||
default: '',
|
default: '',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
type: 'checkbox',
|
||||||
|
id: 'alwaysStart',
|
||||||
|
label: 'Enable',
|
||||||
|
width: 1,
|
||||||
|
default: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
id: 'alwaysStartTxt',
|
||||||
|
label: 'Always start a new timer even if there is one already running',
|
||||||
|
width: 11,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'text',
|
||||||
|
id: 'break',
|
||||||
|
label: '',
|
||||||
|
width: 12,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.prototype.destroy = function () {
|
instance.prototype.destroy = function () {
|
||||||
var self = this
|
var self = this
|
||||||
self.currentTimer = null
|
|
||||||
debug('destroy', self.id)
|
debug('destroy', self.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,6 +181,9 @@ instance.prototype.action = function (action) {
|
|||||||
|
|
||||||
switch (action.action) {
|
switch (action.action) {
|
||||||
case 'startNewTimer': {
|
case 'startNewTimer': {
|
||||||
|
self.getCurrentTimer().then((timerId) => {
|
||||||
|
self.log('debug', 'Current timer id ' + timerId)
|
||||||
|
if (timerId === undefined || timerId == null || self.config.alwaysStart == true) {
|
||||||
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"}}'
|
||||||
@@ -174,36 +196,42 @@ instance.prototype.action = function (action) {
|
|||||||
'"}}'
|
'"}}'
|
||||||
}
|
}
|
||||||
self.sendCommand('rest', cmd, body).then((result) => {
|
self.sendCommand('rest', cmd, body).then((result) => {
|
||||||
console.log('start: ' + JSON.stringify(result, null, 4))
|
if (typeof result == 'object' && result.data !== null && result.data !== undefined) {
|
||||||
if (typeof result == 'object' && result.data !== null) {
|
self.log('debug','New timer started ' + result.data.id)
|
||||||
console.log('new timer ' + result.data.id)
|
|
||||||
self.currentTimer = result.data.id
|
|
||||||
} else {
|
} else {
|
||||||
console.log('error starting timer')
|
self.log('warn','Error starting timer')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
self.log('debug','A timer is already running')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case 'stopCurrentTimer': {
|
case 'stopCurrentTimer': {
|
||||||
console.log('stopping timer: ' + self.currentTimer)
|
self.getCurrentTimer().then((timerId) => {
|
||||||
if (self.currentTimer != null && self.currentTimer != undefined) {
|
self.log('debug', 'Current timer id ' + timerId)
|
||||||
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + self.currentTimer + '/stop'
|
if (timerId !== null && timerId !== undefined) {
|
||||||
|
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + timerId + '/stop'
|
||||||
self.sendCommand('rest_put', cmd).then((result) => {
|
self.sendCommand('rest_put', cmd).then((result) => {
|
||||||
// console.log('stop: ' + JSON.stringify(result, null, 4))
|
if (typeof result == 'object' && result.data !== null && result.data !== undefined) {
|
||||||
if (typeof result == 'object' && result.data !== null) {
|
self.log('debug', 'Stopped ' + result.data.id + ', duration ' + result.data.duration)
|
||||||
console.log('stopped ' + result.data.id + ' duration ' + result.data.duration)
|
|
||||||
self.currentTimer = null
|
|
||||||
} else {
|
} else {
|
||||||
console.log('error stopping timer')
|
self.log('warn', 'Error stopping timer')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
self.log('warn', 'No running timer to stop or running timer ID unknown')
|
self.log('warn', 'No running timer to stop or running timer id unknown')
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case 'getCurrentTimer': {
|
case 'getCurrentTimer': {
|
||||||
self.getCurrentTimer()
|
self.getCurrentTimer().then((result) => {
|
||||||
|
self.log('debug', 'Current timer id ' + result)
|
||||||
|
}
|
||||||
|
)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case 'refreshProjects': {
|
case 'refreshProjects': {
|
||||||
@@ -305,21 +333,20 @@ instance.prototype.getCurrentTimer = function () {
|
|||||||
var self = this
|
var self = this
|
||||||
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/current'
|
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/current'
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
self.sendCommand('rest_get', cmd).then(
|
self.sendCommand('rest_get', cmd).then(
|
||||||
(result) => {
|
(result) => {
|
||||||
// console.log('result ' + JSON.stringify(result, null, 4))
|
if (typeof result === 'object' && result.data !== null && result.data !== undefined) {
|
||||||
if (typeof result === 'object' && result.data !== null) {
|
|
||||||
// console.log('result ' + result.data)
|
|
||||||
if ('id' in result.data) {
|
if ('id' in result.data) {
|
||||||
self.currentTimer = result.data.id
|
console.log('current timer: ' + result.data.id)
|
||||||
console.log('current timer: ' + self.currentTimer)
|
resolve(result.data.id)
|
||||||
self.log('debug', 'Current timer id ' + self.currentTimer)
|
} else {
|
||||||
|
console.log('getCurrentTimer: No timer id found')
|
||||||
|
resolve(null)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log(result)
|
|
||||||
console.log('getCurrentTimer: No timer running')
|
console.log('getCurrentTimer: No timer running')
|
||||||
self.log('debug', 'No timer running')
|
resolve(null)
|
||||||
self.currentTimer = null
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
@@ -327,6 +354,8 @@ instance.prototype.getCurrentTimer = function () {
|
|||||||
self.log('debug', 'Error getting current timer')
|
self.log('debug', 'Error getting current timer')
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.prototype.sendCommand = function (mode, command, body = '') {
|
instance.prototype.sendCommand = function (mode, command, body = '') {
|
||||||
@@ -341,9 +370,12 @@ instance.prototype.sendCommand = function (mode, command, body = '') {
|
|||||||
command,
|
command,
|
||||||
(err, { data, error, response }) => {
|
(err, { data, error, response }) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
self.status(self.STATUS_ERROR)
|
||||||
|
console.log(error)
|
||||||
reject(error)
|
reject(error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
self.status(self.STATUS_OK)
|
||||||
resolve(data)
|
resolve(data)
|
||||||
},
|
},
|
||||||
self.header
|
self.header
|
||||||
@@ -360,9 +392,12 @@ instance.prototype.sendCommand = function (mode, command, body = '') {
|
|||||||
body,
|
body,
|
||||||
(err, { data, error, response }) => {
|
(err, { data, error, response }) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
self.status(self.STATUS_ERROR)
|
||||||
|
console.log(error)
|
||||||
reject(error)
|
reject(error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
self.status(self.STATUS_OK)
|
||||||
resolve(data)
|
resolve(data)
|
||||||
},
|
},
|
||||||
self.header
|
self.header
|
||||||
|
|||||||
Reference in New Issue
Block a user