-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (141 loc) · 4.08 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Eaton ePDU Companion Module
const { InstanceBase, InstanceStatus, runEntrypoint } = require('@companion-module/base')
const UpgradeScripts = require('./src/upgrades')
const { Telnet } = require('telnet-client')
const config = require('./src/config')
const actions = require('./src/actions')
const feedbacks = require('./src/feedbacks')
const variables = require('./src/variables')
const presets = require('./src/presets')
const utils = require('./src/utils')
const constants = require('./src/constants')
class moduleInstance extends InstanceBase {
constructor(internal) {
super(internal)
// Assign the methods from the listed files to this class
Object.assign(this, {
...config,
...actions,
...feedbacks,
...variables,
...presets,
...utils,
...constants
})
this.connection = undefined;
this.connectionReady = false;
this.pollTimer = undefined;
this.OUTLET_STATES = [];
this.CHOICES_OUTLETS = [];
}
async destroy() {
if (this.connection) {
this.connection.end()
delete this.connection
}
if (this.pollTimer !== undefined) {
clearInterval(this.pollTimer)
delete this.pollTimer
}
}
async init(config) {
this.updateStatus(InstanceStatus.Connecting)
this.configUpdated(config)
}
async configUpdated(config) {
// polling is running and polling has been de-selected by config change
if (this.pollTimer !== undefined) {
clearInterval(this.pollTimer)
delete this.pollTimer
}
this.config = config
//create outlet choices
this.CHOICES_OUTLETS = [];
for (let i = 1; i <= this.config.outlet_count; i++) {
let outletObj = {
id: i,
label: `Outlet ${i}`
}
this.CHOICES_OUTLETS.push(outletObj);
}
this.initActions()
this.initFeedbacks()
this.initVariables()
this.initPresets()
this.initTelnet()
}
async initTelnet() {
let self = this;
this.connection = new Telnet();
const params = {
host: this.config.host,
port: 23,
loginPrompt: 'Enter Login: ',
passwordPrompt: 'Enter Password: ',
username: `${this.config.username}\r\n`,
password: `${this.config.password}\r\n`,
shellPrompt: 'pdu#0>', // or negotiationMandatory: false
negotiationMandatory: false,
timeout: 1500
}
this.connection.on('data', function (data) {
self.processResponse(data.toString());
});
this.connection.on('ready', async function (prompt) {
if (prompt === 'pdu#0>') {
self.connectionReady = true;
self.updateStatus(InstanceStatus.Ok)
}
else {
self.connectionReady = false;
self.updateStatus(InstanceStatus.Error, 'Invalid prompt received from ePDU')
self.log('deubg', 'Invalid prompt received from ePDU, retrying in 5 seconds');
setTimeout(self.init, 5000, self.config);
}
});
this.connection.on('error', function (error) {
console.log('socket error:', error)
});
try {
await this.connection.connect(params);
}
catch (error) {
this.log('error', `Error connecting to Eaton ePDU: ${error.toString()}`);
}
}
controlOutlet(outlet, state, delay = 0) {
let command = 'DelayBeforeStartup';
if (state == false) {
command = 'DelayBeforeShutdown';
}
this.log('info', `Setting Outlet ${outlet} to ${(state ? 'On' : 'False')} with delay of ${delay} seconds`);
this.sendCommand(`set PDU.OutletSystem.Outlet[${outlet}].${command} ${delay}\r\n`);
}
cycleOutlet(outlet) {
let command = 'ToggleControl 1';
this.log('info', `Cycling/Rebooting Outlet ${outlet}`);
this.sendCommand(`set PDU.OutletSystem.Outlet[${outlet}].${command}\r\n`);
}
async sendCommand(cmd) {
if (this.connectionReady) {
this.log('debug', cmd);
let res = await this.connection.exec(`${cmd}\r\n`);
}
else {
this.log('warning', 'Connection not ready, unable to send command at this time.');
}
}
processResponse(response) {
//process the response
this.checkFeedbacks()
this.checkVariables()
}
initPolling() {
if (this.pollTimer === undefined && this.config.poll_interval > 0) {
this.pollTimer = setInterval(() => {
//send commands to get outlet data
}, this.config.poll_interval)
}
}
}
runEntrypoint(moduleInstance, UpgradeScripts)