-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathevents.with.filter.ts
More file actions
166 lines (154 loc) · 5.5 KB
/
Copy pathevents.with.filter.ts
File metadata and controls
166 lines (154 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
import { NotificationMessage, Onvif, PullPointSubscription, Subscription } from '../src';
import { createServer } from 'http';
import { parseStringPromise } from 'xml2js';
// eslint-disable-next-line @typescript-eslint/no-require-imports
const keypress = require('keypress');
// the IP Address and Port for a HTTP Server that the camera will send events to. Change this.
const EVENT_RECEIVER_IP_ADDRESS = '192.168.1.13';
const EVENT_RECEIVER_PORT = 8086;
// My Tapo C220
const cam = new Onvif({
hostname: '192.168.1.14',
username: 'administrator',
password: 'password',
port: 2020,
});
// Happytimesoft
// const cam = new Onvif({
// hostname: '192.168.1.13',
// username: 'admin',
// password: 'admin',
// port: 8000,
// });
process.stdout.write(`
\x1b[38;2;138;43;226m ______ _
\x1b[38;2;161;34;206m| ____| | |
\x1b[38;2;185;26;187m| |__ __ __ ___ _ __ | |_ ___
\x1b[38;2;208;17;167m| __| \\ \\ / // _ \\| '_ \\ | __|/ __|
\x1b[38;2;232;9;148m| |____ \\ V /| __/| | | | | |_ \\__ \\
\x1b[38;2;255;0;128m|______| \\_/ \\___||_| |_| \\__||___/
\x1b[0m
Press:
--- Common sub ---
c - toggle common event handler
--- Pull-point sub ---
s - subscribe
u - unsubscribe
e - event info
--- Base sub with own server ---
b - toggle base event handler (and own http-server)
Ctrl+C - exit
`);
const server = createServer((req, res) => {
const buf: any[] = [];
req.on('data', (data) => {
buf.push(data);
});
req.on('end', async () => {
const msg = await parseStringPromise(Buffer.concat(buf).toString());
msg['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['wsnt:Notify'][0]['wsnt:NotificationMessage'].forEach((txt: any) => {
process.stdout.write(
`${new Date().toLocaleTimeString()} ${JSON.stringify(txt['wsnt:Message'][0]['tt:Message'][0]['tt:Data'][0]['tt:SimpleItem'][0].$)}\n`,
);
});
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World!\n');
});
});
let pushSub: PullPointSubscription | null = null;
(async () => {
let eventCounter = 0;
// My poor Tapo C220 V1 cam supports ONVIF very badly, I decided to count errors in the PullMessages realization
// https://community.tp-link.com/en/smart-home/forum/topic/867526?replyId=1705912
let tapoErrorCounter = 0;
await cam.connect();
const sub = new Subscription(cam, {
filter: {
topicExpression: [
{
expression: 'tns1:RuleEngine/CellMotionDetector/Motion',
dialect: 'http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet',
},
],
},
// filter: {
// topicExpression: [
// {
// expression: 'tns1:Device/Trigger/Relay',
// dialect: 'http://www.onvif.org/ver10/tev/topicExpression/ConcreteSet',
// },
// ],
// },
});
keypress(process.stdin);
process.stdin.setRawMode(true);
process.stdin.resume();
const globalHandler = (msg: NotificationMessage) => {
process.stdout.write(
`${new Date().toLocaleTimeString()} common ${msg.topic._} ${JSON.stringify(msg.message.message.data)}\n`,
);
};
process.stdin.on('keypress', async (c, key) => {
if (key.ctrl && key.name === 'c') {
// eslint-disable-next-line n/no-process-exit
process.exit();
}
if (key.name === 'c') {
if (cam.listeners('event').length === 0) {
process.stdout.write(`${new Date().toLocaleTimeString()} common sub started\n`);
cam.on('event', globalHandler);
} else {
process.stdout.write(`${new Date().toLocaleTimeString()} common sub stopped\n`);
cam.off('event', globalHandler);
}
}
if (key.name === 's') {
sub.on('data', (data) => {
eventCounter += 1;
process.stdout.write(
`${new Date().toLocaleTimeString()} only motion ${data.topic._} ${JSON.stringify(data.message.message.data)}\n`,
);
});
sub.on('connectionError', (error) => {
tapoErrorCounter += 1;
});
await sub.subscribe();
process.stdout.write(
`${new Date().toLocaleTimeString()} Pull-point at ${sub.subscription?.subscriptionReference.address}\n`,
);
}
if (key.name === 'u') {
process.stdout.write(`${JSON.stringify(await sub.unsubscribe())}\n`);
process.stdout.write(`${new Date().toLocaleTimeString()} Unsubscribed ${sub.listenerCount('data')}\n`);
}
if (key.name === 'e') {
process.stdout.write(
`${new Date().toLocaleTimeString()} ${sub.subscription?.subscriptionReference.address} ${eventCounter} ${
tapoErrorCounter
} ${sub.subscription?.terminationTime.toLocaleTimeString()} ${sub.eventReconnectMs}\n`,
);
}
if (key.name === 'b') {
if (pushSub) {
await cam.events.unsubscribe(pushSub);
pushSub = null;
server.close();
process.stdout.write(`${new Date().toLocaleTimeString()} Base event server stopped listening\n`);
return;
}
server.listen(EVENT_RECEIVER_PORT, EVENT_RECEIVER_IP_ADDRESS, () => {
process.stdout.write(
`${new Date().toLocaleTimeString()} Base event server is listening on ${EVENT_RECEIVER_IP_ADDRESS}:${EVENT_RECEIVER_PORT}\n`,
);
});
pushSub = await cam.events.subscribe({
renew: false,
url: `http://${EVENT_RECEIVER_IP_ADDRESS}:${EVENT_RECEIVER_PORT}`,
terminationTime: 5 * 60 * 1000,
});
process.stdout.write(
`${new Date().toLocaleTimeString()} subscribed for 5min ${pushSub.subscriptionReference.address}\n`,
);
}
});
})();