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