-
Notifications
You must be signed in to change notification settings - Fork 5
/
read_socket.js
222 lines (176 loc) · 5.96 KB
/
read_socket.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Read and decode CCTV PTZ commands from a TCP Socket
* Copyright 2017,2018 Roger Hardiman
*
* use -p to set the port to listen on
*/
// External Dependencies
var fs = require('fs');
var net = require('net');
var dateTime = require('node-datetime');
var PelcoD_Decoder = require('./pelcod_decoder').PelcoD_Decoder;
try {
var Extra_Decoder_1 = require('./extra_decoder_1');
} catch (err) {
// ignore this optional extra decoder
}
var version = require('./package.json').version;
var args = require('commander');
// Command line arguments
args.version(version);
args.description('Pelco D, Pelco P, BBV422, Philips/Bosch, Vicon, Forward Vision, Pansonic and American Dynamics/Sensormatic parser');
args.option('-v, --verbose','Verbose mode. Show all data bytes');
args.option('-r, --remote <hostname>','Hostname of Remote TCP Serial Server (raw TCP stream)');
args.option('-p, --port <number>','TCP Port to listen on (or Port at Remote Site)');
args.option('--nolog','Do not write to the log file. Default is to write logs');
args.parse(process.argv);
// Initial message
console.log('');
console.log('CCTV Telemetry Decoder');
console.log('Pelco D, Pelco P, BBV, Bosch, Philips, Forward Vision, Vicon, Panasonic, American Dynamics, Sensormatic');
console.log('(c) Roger Hardiman 2017 www.rjh.org.uk');
console.log('Use -h for help');
console.log('');
// List available serial ports
if (args.list || (!args.port)) {
if (!args.list) {
console.log('ERROR: No port number to listen on specified');
}
return;
}
// Log File
var log_fd;
// User Settings
if (args.port) port = args.port;
// Initialise Decoders
if (PelcoD_Decoder) var pelco_d_decoder = new PelcoD_Decoder();
if (Extra_Decoder_1) var extra_decoder_1 = new Extra_Decoder_1();
// Open log file
var now = dateTime.create();
var filename = 'log_' + now.format('Y_m_d_H_M_S') + '.txt';
if (args.nolog) {
console.log('Log file disabled');
} else {
fs.open(filename,'w',function(err,fd) {
if (err) {
console.log('ERROR - cannot create log file ' + filename);
console.log(err);
console.log('');
process.exit(1);
}
log_fd = fd;
console.log('Log File Open ('+filename+')');
});
}
// Open Port.
if (args.remote) {
// CONNECT TO REMOTE SITE
var sock = new net.Socket();
console.log('Connecting to '+ args.remote + ':' + args.port);
sock.connect(args.port, args.remote, function() {
console.log('Connected to remote site');
// Callback - Data
sock.on('data', function(buffer) {
var now = dateTime.create();
var nowString = now.format('H:M:S.N');
var msg = nowString + 'Rx' + BufferToHexString(buffer) + '\r\n';
// write to console
if (args.verbose) console.log(msg);
// write to log file if 'fd' is not undefined
if (log_fd) {
fs.write(log_fd,msg,function(err) {
if (err) console.log('Error writing to file');
});
}
// pass to each decoder
if (pelco_d_decoder) pelco_d_decoder.processBuffer(buffer);
if (extra_decoder_1) extra_decoder_1.processBuffer(buffer);
});
// Callback - Close
sock.on('close', function(data) {
console.log('Network close from ' + sock.remoteAddress + ':' + sock.remotePort + ' received');
});
// Callback - Error
sock.on('error', function(err) {
console.log('Network error ' + err);
});
});
} else {
// LISTEN FOR INCOMING CONNECTIONS
// Open Port.
var server = new net.createServer(function(sock) {
console.log('Network Connection from ' + sock.remoteAddress + ':' + sock.remotePort + ' received');
// Callback - Data
sock.on('data', function(buffer) {
var now = dateTime.create();
var nowString = now.format('H:M:S.N');
var msg = nowString + 'Rx' + BufferToHexString(buffer) + '\r\n';
// write to console
if (args.verbose) console.log(msg);
// write to log file if 'fd' is not undefined
if (log_fd) {
fs.write(log_fd,msg,function(err) {
if (err) console.log('Error writing to file');
});
}
// pass to each decoder
if (pelco_d_decoder) pelco_d_decoder.processBuffer(buffer);
if (extra_decoder_1) extra_decoder_1.processBuffer(buffer);
});
// Callback - Close
sock.on('close', function(data) {
console.log('Network close from ' + sock.remoteAddress + ':' + sock.remotePort + ' received');
});
// Callback - Error
sock.on('error', function(err) {
console.log('Network error ' + err);
});
});
server.listen(port,'127.0.0.1');
}
// Callback - Decoded protocol
pelco_d_decoder.on('log', function(message) {
var now = dateTime.create();
var nowString = now.format('H:M:S.N');
var msg = nowString + '=>' + message;
// show on console
console.log(msg);
// Write to file
if (log_fd) {
fs.write(log_fd,msg+'\r\n',function(err) {
if (err) console.log('Error writing to file');
});
}
});
try{
extra_decoder_1.on('log', function(message) {
var now = dateTime.create();
var nowString = now.format('H:M:S.N');
var msg = nowString + '=>' + message;
// show on console
console.log(msg);
// Write to file
if (log_fd) {
fs.write(log_fd,msg+'\r\n',function(err) {
if (err) console.log('Error writing to file');
});
}
});
} catch (err) {}
// helper functions
var last_byte = '';
function BufferToHexString(buffer) {
var byte_string = '';
for (var i = 0; i < buffer.length; i++) {
byte_string += '[' + DecToHexPad(buffer[i],2) + ']';
}
return byte_string;
}
// helper functions
function DecToHexPad(decimal,size) {
var ret_string = decimal.toString('16');
while (ret_string.length < size) {
ret_string = '0' + ret_string;
}
return ret_string;
}