-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvserver.js
170 lines (138 loc) · 4.55 KB
/
vserver.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
// server.js
const net = require('net');
var tpidefs = require('./tpi.js');
var EventEmitter = require('events');
var config;
var vs;
class EVL4Simulator {
constructor()
{
this.simulatedAlarmState = '0';
this.TPI_TO_MESSAGE = {
'2':'away',
'6':'bypass',
'1': 'disarm',
'33': 'night',
'3' : 'stay'
}
};
simulateCommand(packet) {
var tpi_cmd = packet.match(/\^(.+)\$/); // pull out everything between the ^ sand $.
if (tpi_cmd != null)
{
if(command == tpi_cmd[1].split(',')[0])
if ((command == tpidefs.alarmcommand.away) || (command == tpidefs.alarmcommand.night) || (command == tpidefs.alarmcommand.instant) || (command == tpidefs.alarmcommand.max) || (command == tpidefs.alarmcommand.stay)) {
this.simulatedAlarmState = command;
} else if (command == tpidefs.alarmcommand.disarm) {
this.simulatedAlarmState = '0';
}
}
return '^' + command + ',00 $\r\n';
}
simulateMessage()
{
var evl_ret;
if (this.simulatedAlarmState == '0') {
evl_ret = '%00,01,1C08,08,00, VIRTUAL SYSTEM Ready to Arm $\r\n';
}
else {
evl_ret = '%00,01,1C08,08,00, VIRTUAL SYSTEM '+this.TPI_TO_MESSAGE [this.simulatedAlarmState ] +' $\r\n';
}
}
disarm() {
this.simulatedAlarmState = false;
console.log("Alarm disarmed.");
}
armed() {
this.simulatedAlarmState = true;
console.log("Alarm armed.");
}
simulateIntrusion(){
if (this.armed) {
console.log("Intrusion detected. Triggering full alarm.");
}
}
}
// Example usage: const evl4 = new EVL4Simulator(); // Arm the system evl4.arm(); // Activate night mode evl4.activateNightMode(); // Simulate an intrusionevl4.simulateIntrusion(); // Disarm the system evl4.disarm();
class VirtualEnvisaLink extends EventEmitter {
port;
password;
vServer;
envisaClients = [];
evlEmulator;
constructor(log, config) {
this.log = log;
//this.port = config.port ? config.port : 4025;
//this.password = config.password ? config.password : "user";
//this.bEmulatormode = config.emulator ? config.emulator : false;
this.port = 4025;
this.password = "user";
this.bEmulatormode = true;
this.vServer;
this.envisaClients;
this.evlEmulator = new EVL4Simulator();
};
initVisualServer() {
this.vServer = net.createServer( (socket) => {
// 'connection' listener.
// Send login prompt to client
socket.setEncoding('utf8');
// Put this new client in the list
socket.name = socket.remoteAddress + ":" + socket.remotePort;
socket.isAuthenticated = false;
console.log(`${socket.name} connected.`);
// Add this client to broadcast list
this.envisaClients.push(socket);
socket.write("Login: \r\n");
this.bClientConnected = 0;
socket.on('data', (data) => {
console.log(`Received data from client: ${data}`);
var strData = data.toString();
if (socket.isAuthenticated == false) {
// confirm password
if (strData.trim() == this.password) {
socket.isAuthenticated = true;
// If emulator mode generate internal messages
if (this.bEmulatormode) {this.tpiMessageEmulator();}
}
else {
// Incorrect password and terminate connection
socket.write("FAILED\r\n");
socket.end();
}
}
else {
if(strData.substring(0,1)!='%'){
// emit command sent by the client
this.emit('appcommand', {
data: strData.trim()
});
}
}
});
socket.on('end', () => {
// Clean up client list
this.envisaClients.splice(this.envisaClients.indexOf(socket), 1);
console.log(`${socket.name} disconnected.`);
});
socket.on("error", (error) => {
console.log(`Socket Error: ${error.message}`);
});
});
this.vServer.listen(this.port, () => {
console.log(`Envisalink virtual server listening on port ${this.port}`);
});
}
broadcastMessage(tpimessage) {
if (this.envisaClients.length>=1) {
this.envisaClients.forEach(function (eClient) {
if (eClient.isAuthenticated) { eClient.write(tpimessage); }
});
}
}
tpiMessageEmulator() {
var myVar = setInterval(function(){vs.broadcastMessage(this.evlEmulator.simulateMessage())}, 3000);
}
}
vs = new VirtualEnvisaLink(console.log, config);
vs.initVisualServer();