Prettified Code!
This commit is contained in:
120
index.js
120
index.js
@@ -15,7 +15,7 @@ function instance(system, id, config) {
|
|||||||
instance.prototype.updateConfig = function (config) {
|
instance.prototype.updateConfig = function (config) {
|
||||||
var self = this
|
var self = this
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
self.auth()
|
self.auth()
|
||||||
self.getCurrentTimer()
|
self.getCurrentTimer()
|
||||||
self.actions()
|
self.actions()
|
||||||
@@ -28,7 +28,7 @@ instance.prototype.init = function () {
|
|||||||
log = self.log
|
log = self.log
|
||||||
|
|
||||||
self.currentTimer = null
|
self.currentTimer = null
|
||||||
|
|
||||||
self.init_presets()
|
self.init_presets()
|
||||||
self.auth()
|
self.auth()
|
||||||
self.getCurrentTimer()
|
self.getCurrentTimer()
|
||||||
@@ -37,14 +37,13 @@ instance.prototype.init = function () {
|
|||||||
|
|
||||||
instance.prototype.auth = function () {
|
instance.prototype.auth = function () {
|
||||||
var self = this
|
var self = this
|
||||||
|
|
||||||
auth = Buffer.from(self.config.apiToken + ':' + 'api_token').toString('base64')
|
auth = Buffer.from(self.config.apiToken + ':' + 'api_token').toString('base64')
|
||||||
self.header = []
|
self.header = []
|
||||||
self.header['Content-Type'] = 'application/json'
|
self.header['Content-Type'] = 'application/json'
|
||||||
self.header['Authorization'] = 'Basic ' + auth
|
self.header['Authorization'] = 'Basic ' + auth
|
||||||
|
|
||||||
console.log(self.header)
|
console.log(self.header)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.prototype.config_fields = function () {
|
instance.prototype.config_fields = function () {
|
||||||
@@ -76,7 +75,7 @@ instance.prototype.destroy = function () {
|
|||||||
instance.prototype.init_presets = function () {
|
instance.prototype.init_presets = function () {
|
||||||
var self = this
|
var self = this
|
||||||
var presets = []
|
var presets = []
|
||||||
|
|
||||||
presets.push({
|
presets.push({
|
||||||
category: 'Timer',
|
category: 'Timer',
|
||||||
label: 'Start',
|
label: 'Start',
|
||||||
@@ -92,11 +91,11 @@ instance.prototype.init_presets = function () {
|
|||||||
action: 'startNewTimer',
|
action: 'startNewTimer',
|
||||||
options: {
|
options: {
|
||||||
description: '',
|
description: '',
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
presets.push({
|
presets.push({
|
||||||
category: 'Timer',
|
category: 'Timer',
|
||||||
label: 'Stop',
|
label: 'Stop',
|
||||||
@@ -160,12 +159,12 @@ instance.prototype.action = function (action) {
|
|||||||
case 'stopCurrentTimer': {
|
case 'stopCurrentTimer': {
|
||||||
var restCmd = 'rest_put'
|
var restCmd = 'rest_put'
|
||||||
console.log(self.currentTimer)
|
console.log(self.currentTimer)
|
||||||
|
|
||||||
if (self.currentTimer != null && self.currentTimer != undefined) {
|
if (self.currentTimer != null && self.currentTimer != undefined) {
|
||||||
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + self.currentTimer + '/stop'
|
var cmd = 'https://api.track.toggl.com/api/v8/time_entries/' + self.currentTimer + '/stop'
|
||||||
console.log(cmd)
|
console.log(cmd)
|
||||||
} 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')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
@@ -180,72 +179,81 @@ instance.prototype.action = function (action) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
self.system.emit(restCmd, cmd, body, function (err, result) {
|
self.system.emit(
|
||||||
|
restCmd,
|
||||||
if (err !== null) {
|
cmd,
|
||||||
console.log(result.statusCode)
|
body,
|
||||||
console.log('HTTP Request failed (' + result.error.code + ')')
|
function (err, result) {
|
||||||
self.status(self.STATUS_ERROR, result.error.code);
|
if (err !== null) {
|
||||||
} else {
|
console.log(result.statusCode)
|
||||||
console.log(typeof(result.data))
|
console.log('HTTP Request failed (' + result.error.code + ')')
|
||||||
console.log(result.statusCode)
|
self.status(self.STATUS_ERROR, result.error.code)
|
||||||
if (!self.auth_error) {
|
} else {
|
||||||
self.status(self.STATUS_OK)
|
console.log(typeof result.data)
|
||||||
if (typeof result.data === 'object') {
|
console.log(result.statusCode)
|
||||||
if (typeof result.data.data === 'object') {
|
if (!self.auth_error) {
|
||||||
self.interpretData(result.data.data)
|
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)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
self.currentTimer = null
|
|
||||||
console.log(result.data)
|
|
||||||
self.log('debug',result.data)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}, self.header)
|
self.header
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
instance.prototype.getCurrentTimer = function () {
|
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'
|
||||||
|
|
||||||
self.system.emit('rest_get', cmd, function (err, result) {
|
self.system.emit(
|
||||||
if (err !== null) {
|
'rest_get',
|
||||||
console.log('HTTP Request failed (' + result.error.code + ')')
|
cmd,
|
||||||
self.status(self.STATUS_ERROR, result.error.code)
|
function (err, result) {
|
||||||
} else if (result.response.statusCode == 200) {
|
if (err !== null) {
|
||||||
console.log('status:' + result.response.statusCode)
|
console.log('HTTP Request failed (' + result.error.code + ')')
|
||||||
self.status(self.STATUS_OK)
|
self.status(self.STATUS_ERROR, result.error.code)
|
||||||
if (typeof result.data.data === 'object' && result.data.data != null) {
|
} else if (result.response.statusCode == 200) {
|
||||||
if ('id' in result.data.data) {
|
console.log('status:' + result.response.statusCode)
|
||||||
self.currentTimer = result.data.data.id
|
self.status(self.STATUS_OK)
|
||||||
console.log(self.currentTimer)
|
if (typeof result.data.data === 'object' && result.data.data != null) {
|
||||||
self.log('debug','Current timer id ' + self.currentTimer)
|
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 {
|
} else {
|
||||||
console.log(result.data)
|
console.log('error: ' + result.response.statusCode)
|
||||||
self.log('debug','No timer running')
|
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.currentTimer = null
|
||||||
}
|
}
|
||||||
} else {
|
},
|
||||||
console.log('error: ' + result.response.statusCode)
|
self.header
|
||||||
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) {
|
instance.prototype.interpretData = function (data) {
|
||||||
var self = this;
|
var self = this
|
||||||
|
|
||||||
console.log(data)
|
console.log(data)
|
||||||
|
|
||||||
if ('id' in data) {
|
if ('id' in data) {
|
||||||
console.log(data.id)
|
console.log(data.id)
|
||||||
self.currentTimer = data.id
|
self.currentTimer = data.id
|
||||||
self.log('debug','timer id ' + data.id)
|
self.log('debug', 'timer id ' + data.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user