Skip to content

Commit 3f38bd1

Browse files
committed
Initial version
1 parent 2cded4e commit 3f38bd1

File tree

4 files changed

+396
-0
lines changed

4 files changed

+396
-0
lines changed

index.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
const instance_skel = require('../../instance_skel')
2+
const upgradeScripts = require('./src/upgrades')
3+
const { getActions, executeAction } = require('./src/actions')
4+
const { getConfigFields } = require('./src/config')
5+
6+
class instance extends instance_skel {
7+
constructor(system, id, config) {
8+
super(system, id, config)
9+
10+
this.serverIp = null
11+
this.serverPassword = null
12+
13+
this.CHOICES_mode = [
14+
{ id: 'up', label: 'Count Up' },
15+
{ id: 'down', label: 'Count Down' },
16+
{ id: 'time', label: 'Time of Day' },
17+
]
18+
this.CHOICES_addsubtract = [
19+
{ id: 'add', label: 'Add Time' },
20+
{ id: 'subtract', label: 'Subtract Time' },
21+
]
22+
23+
this.actions(system) // Export actions
24+
25+
return this
26+
}
27+
28+
/**
29+
* Gets any upgrade scripts
30+
* @returns {Array}
31+
* @access public
32+
* @since 1.0.0
33+
*/
34+
static GetUpgradeScripts() {
35+
return upgradeScripts
36+
}
37+
38+
/**
39+
* Configuration fields that can be used
40+
* @returns {Array}
41+
* @access public
42+
* @since 1.0.0
43+
*/
44+
config_fields() {
45+
return getConfigFields.bind(this)()
46+
}
47+
48+
/**
49+
* Initialize variables
50+
* @access public
51+
* @since 1.0.0
52+
*/
53+
initVariables() {}
54+
55+
/**
56+
* Initialize feedbacks
57+
* @access public
58+
* @since 1.0.0
59+
*/
60+
initFeedbacks() {}
61+
62+
/**
63+
* Setup the actions
64+
* @param {Object} system
65+
* @access public
66+
* @since 1.0.0
67+
*/
68+
actions(system) {
69+
this.setActions(getActions.bind(this)(system))
70+
}
71+
72+
/**
73+
* Process configuration updates
74+
* @param {Object} config New configuration
75+
* @access public
76+
* @since 1.0.0
77+
*/
78+
updateConfig(config) {
79+
this.config = config
80+
this.status(this.STATUS_UNKNOWN)
81+
82+
if (this.config.host && this.config.password && this.config.host != '' && this.config.password != '') {
83+
this.status(this.STATUS_OK)
84+
} else {
85+
this.status(this.STATUS_WARNING, 'Missing server ip or password')
86+
}
87+
}
88+
89+
/**
90+
* Main initialization when it's ok to connect
91+
* @access public
92+
* @since 1.0.0
93+
*/
94+
init() {}
95+
96+
/**
97+
* Executes the action
98+
* @param {Object} action Action to execute
99+
* @access public
100+
* @since 1.0.0
101+
*/
102+
action(action) {
103+
return executeAction.bind(this)(action)
104+
}
105+
106+
/**
107+
* Ends session
108+
* @since 1.0.0
109+
*/
110+
destroy() {}
111+
112+
/**
113+
* Send a request to the API
114+
* @param {String} cmd The endpoint to call
115+
* @param {Array-String} parmas The URL parameters to include
116+
* @access private
117+
* @since 1.0.0
118+
*/
119+
_sendRequest(cmd) {
120+
let url = `http://${this.config.host}:1909/api/${cmd}`
121+
let headers = { password: this.config.password }
122+
123+
this.system.emit(
124+
'rest',
125+
url,
126+
{}, // Body data
127+
(err, result) => {
128+
if (err !== null) {
129+
this.log('error', `HTTP POST Request failed (${result.error.code})`)
130+
this.status(this.STATUS_ERROR, result.error.code)
131+
} else {
132+
this.log('info', result.data.toString())
133+
this.status(this.STATUS_OK)
134+
}
135+
},
136+
headers
137+
)
138+
}
139+
}
140+
141+
exports = module.exports = instance

src/actions.js

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
exports.getActions = function (system) {
2+
return {
3+
// Main Timer
4+
mainSet: {
5+
label: 'Main Timer - Set',
6+
options: [
7+
{
8+
type: 'textinput',
9+
label: 'Time (in sec)',
10+
id: 'sec',
11+
regex: this.REGEX_NUMBER,
12+
required: false,
13+
},
14+
{
15+
type: 'dropdown',
16+
label: 'Counter Mode',
17+
id: 'mode',
18+
default: 'down',
19+
choices: this.CHOICES_mode,
20+
},
21+
/*
22+
Not currently supported in API v1. Timers must always auto-start for now...
23+
{
24+
type: 'checkbox',
25+
label: 'Auto-Start',
26+
id: 'start',
27+
default: true,
28+
},
29+
*/
30+
],
31+
},
32+
mainStart: {
33+
label: 'Main Timer - Start',
34+
},
35+
mainStop: {
36+
label: 'Main Timer - Stop',
37+
},
38+
mainReset: {
39+
label: 'Main Timer - Reset',
40+
options: [
41+
{
42+
type: 'checkbox',
43+
label: 'Auto-Start',
44+
id: 'start',
45+
default: false,
46+
},
47+
],
48+
},
49+
mainUpdate: {
50+
label: 'Main Timer - Update',
51+
options: [
52+
{
53+
type: 'textinput',
54+
label: 'Time (in sec)',
55+
id: 'sec',
56+
regex: this.REGEX_NUMBER,
57+
required: false,
58+
},
59+
{
60+
type: 'dropdown',
61+
label: 'Update Type',
62+
id: 'mode',
63+
default: 'add',
64+
choices: this.CHOICES_addsubtract,
65+
},
66+
],
67+
},
68+
69+
// Aux Timer
70+
auxSet: {
71+
label: 'Aux Timer - Set',
72+
options: [
73+
{
74+
type: 'textinput',
75+
label: 'Time (in sec)',
76+
id: 'sec',
77+
regex: this.REGEX_NUMBER,
78+
required: false,
79+
},
80+
{
81+
type: 'dropdown',
82+
label: 'Counter Mode',
83+
id: 'mode',
84+
default: 'down',
85+
choices: this.CHOICES_mode,
86+
},
87+
/*
88+
Not currently supported in API v1. Timers must always auto-start for now...
89+
{
90+
type: 'checkbox',
91+
label: 'Auto-Start',
92+
id: 'start',
93+
default: true,
94+
},
95+
*/
96+
],
97+
},
98+
auxStart: {
99+
label: 'Aux Timer - Start',
100+
},
101+
auxStop: {
102+
label: 'Aux Timer - Stop',
103+
},
104+
auxReset: {
105+
label: 'Aux Timer - Reset',
106+
options: [
107+
{
108+
type: 'checkbox',
109+
label: 'Auto-Start',
110+
id: 'start',
111+
default: false,
112+
},
113+
],
114+
},
115+
auxUpdate: {
116+
label: 'Aux Timer - Update',
117+
options: [
118+
{
119+
type: 'textinput',
120+
label: 'Time (in sec)',
121+
id: 'sec',
122+
regex: this.REGEX_NUMBER,
123+
required: false,
124+
},
125+
{
126+
type: 'dropdown',
127+
label: 'Update Type',
128+
id: 'mode',
129+
default: 'add',
130+
choices: this.CHOICES_addsubtract,
131+
},
132+
],
133+
},
134+
135+
// Overlay
136+
overlayShow: {
137+
label: 'Overlay - Show',
138+
},
139+
overlayHide: {
140+
label: 'Overlay - Hide',
141+
},
142+
143+
// Presets
144+
presetRecall: {
145+
label: 'Preset - Recall',
146+
options: [
147+
{
148+
type: 'number',
149+
label: 'Preset ID',
150+
id: 'id',
151+
min: 1,
152+
required: true,
153+
},
154+
],
155+
},
156+
presetPrev: {
157+
label: 'Preset - Previous',
158+
},
159+
presetNext: {
160+
label: 'Preset - Next',
161+
},
162+
}
163+
}
164+
exports.executeAction = function (action) {
165+
let cmd = ''
166+
switch (action.action) {
167+
// Main Timer
168+
case 'mainSet':
169+
cmd = `timer/main/start?countmode=${action.options.mode}`
170+
if (action.options.mode !== 'time') {
171+
cmd += `&time=${action.options.sec}`
172+
}
173+
break
174+
case 'mainStart':
175+
cmd = 'timer/main/start'
176+
break
177+
case 'mainStop':
178+
cmd = 'timer/main/stop'
179+
break
180+
case 'mainReset':
181+
cmd = `timer/main/reset?start=${action.options.start}`
182+
break
183+
case 'mainUpdate':
184+
cmd = `timer/main/${action.options.mode}?time=${action.options.sec}`
185+
break
186+
187+
// Aux Timer
188+
case 'auxSet':
189+
cmd = `timer/aux/start?countmode=${action.options.mode}`
190+
if (action.options.mode !== 'time') {
191+
cmd += `&time=${action.options.sec}`
192+
}
193+
break
194+
case 'auxStart':
195+
cmd = 'timer/aux/start'
196+
break
197+
case 'auxStop':
198+
cmd = 'timer/aux/stop'
199+
break
200+
case 'auxReset':
201+
cmd = `timer/aux/reset?start=${action.options.start}`
202+
break
203+
case 'auxUpdate':
204+
cmd = `timer/aux/${action.options.mode}?time=${action.options.sec}`
205+
break
206+
207+
// Overlay
208+
case 'overlayShow':
209+
cmd = 'overlay/show'
210+
break
211+
case 'overlayHide':
212+
cmd = 'overlay/hide'
213+
break
214+
215+
// Presets
216+
case 'presetRecall':
217+
cmd = `presets/${action.options.id}/restore`
218+
break
219+
case 'presetPrev':
220+
cmd = 'presets/previous'
221+
break
222+
case 'presetNext':
223+
cmd = 'presets/next'
224+
break
225+
}
226+
this.log('debug', `Sending Command: ${cmd}`)
227+
this._sendRequest(cmd)
228+
}

src/config.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
exports.getConfigFields = function () {
2+
configValues = this
3+
return [
4+
{
5+
type: 'textinput',
6+
id: 'host',
7+
label: 'Target IP',
8+
width: 6,
9+
default: '127.0.0.1',
10+
regex: this.REGEX_IP,
11+
},
12+
{
13+
type: 'textinput',
14+
id: 'password',
15+
width: 12,
16+
label: 'Server Password',
17+
required: true,
18+
},
19+
{
20+
type: 'text',
21+
id: 'passwordInfo',
22+
width: 12,
23+
value: 'A server password is required for the API to function.<br>("Preferences > Use Server Password")',
24+
},
25+
]
26+
}

src/upgrades.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = []

0 commit comments

Comments
 (0)