-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·204 lines (175 loc) · 5.5 KB
/
cli.js
File metadata and controls
executable file
·204 lines (175 loc) · 5.5 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env node
import { MeianSocket, MeianDataHandler, MeianCommands } from './index.js'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import fs from 'fs'
function log (message) {
console.log(`meian-client: ${message}`)
}
function exec (host, port, username, password, zonesToQuery, commands, commandArgs, saveFile) {
if (!commands || commands.length === 0) {
log('Provided empty command list...nothing to do. Bye.')
return
}
log(`Running commands: ${JSON.stringify(commands)} (${JSON.stringify(commandArgs)}) meian-cli on host ${host}:${port} with username ${username}`)
const socket = MeianSocket(host, port, username, password, 'debug', zonesToQuery)
function dumpResponses (commandResponse, saveFile) {
const testFile = {}
Object.keys(commandResponse.payloads.data).forEach(command => {
const encrypted = commandResponse.payloads.encrypted[command][0]
const xml = commandResponse.payloads.xml[command][0]
const rawData = commandResponse.payloads.rawData[command][0]
const data = commandResponse.payloads.data[command]
testFile[command] = {
encrypted,
xml,
rawData,
data
}
})
fs.writeFileSync(saveFile, JSON.stringify(testFile, undefined, 2))
log(`File ${saveFile} created`)
}
/**
* test execution
* @param {*} commands
* @param {*} commandsArgs
*/
async function doTest (commands, commandsArgs) {
// parse list
log(`Executing commands ${JSON.stringify(commands)} (${JSON.stringify(commandsArgs)})...`)
if (!Array.isArray(commands)) {
commands = [commands]
}
// send commands
await socket.executeCommand(commands, commandsArgs)
}
if (!username || !password) {
log('Please provide a valid username and password: node ialarm-test username=myuser password=mypassword')
} else {
log('will test MeianSocket on ' + host + ':' + port)
/**
* ready to send commands
*/
socket.onConnected(async (connectionResponse) => {
log(`logged in (${connectionResponse})`)
})
// command
socket.onResponse(async (commandResponse) => {
log(JSON.stringify(commandResponse))
const { GetAlarmStatus, GetByWay, GetZone } = commandResponse.payloads.data
if (saveFile) {
dumpResponses(commandResponse, saveFile)
}
// merge responses.
if (GetAlarmStatus && GetByWay && GetZone) {
log(JSON.stringify(MeianDataHandler.getZoneStatus(GetAlarmStatus, GetByWay, GetZone, zonesToQuery)))
}
setTimeout(() => {
socket.disconnect()
}, 1000)
})
// push events
socket.onPush(async (pushResponse) => {
log(`Received push: ${JSON.stringify(pushResponse)}`)
})
socket.onDisconnected(async (disconnectionResponse) => {
log(`disconnected (type: ${disconnectionResponse})`)
log('exit script')
process.exit(0)
})
socket.onError(async (error) => {
log(`Error ${error.message} - ${JSON.stringify(error.stack)}`)
// disconnect on error
socket.disconnect('error')
})
/**
* ready to send commands
*/
socket.onConnected(async (connectionResponse) => {
log(`logged in (${connectionResponse})`)
})
// connect
socket.connect()
// wait for connection before sending data
const polling = setInterval(() => {
if (socket.connection.status.isReady()) {
clearInterval(polling)
doTest(commands, commandArgs)
} else {
log(`Connection not ready yet for receiving data - ${socket.connection.status.text()}...will try again later..`)
}
}, 1000)
}
}
// parsing args
const argv = yargs(hideBin(process.argv))
.option('commands', {
alias: 'c',
type: 'array',
description: 'List of commands to execute. Can contain args: SetByWay(2,false)',
demandOption: true
})
.option('host', {
alias: 's',
type: 'String',
description: 'ip of the alarm',
demandOption: true
})
.option('port', {
alias: 'n',
type: 'number',
description: 'TCP port',
default: 18034
})
.option('username', {
alias: 'u',
type: 'string',
description: 'Username required for logging in',
demandOption: true
})
.option('password', {
alias: 'p',
type: 'string',
description: 'Password required for logging in',
demandOption: true
})
.option('zones', {
alias: 'z',
type: 'number',
description: 'Number of configured zones',
demandOption: false,
default: 128
})
.option('output', {
alias: 'o',
type: 'string',
description: 'JSON file where to dump the output of the commands',
demandOption: false
})
.argv
const commands = []
const commandsArgs = []
argv.commands.forEach(cmd => {
const commandParser = cmd.trim().match(/([Get|Set][a-zA-Z]+)([(].+[)])?/)
const commandName = commandParser[1]
// if it's a known command
if (MeianCommands[commandName]) {
commands.push(commandName)
const currentArgs = []
if (commandParser[2]) {
let argsText = commandParser[2].substring(1)
argsText = argsText.substring(0, argsText.length - 1)
const argumentsParser = argsText.split(',')
argumentsParser.forEach(cmdArg => {
if (cmdArg.trim()) {
currentArgs.push(cmdArg.trim())
}
})
}
commandsArgs.push(currentArgs)
} else {
log(`Skipped unknown command: ${commandName}`)
}
})
exec(argv.host.trim(), argv.port, argv.username.trim(), argv.password.trim(), argv.zones, commands, commandsArgs, argv.output ? argv.output.trim() : undefined)