hoffmation-base 3.2.3-alpha.8 → 3.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { CameraDevice } from '../index';
2
- import { ProtectCameraConfig, ProtectEventPacket } from 'unifi-protect';
2
+ import { ProtectCameraConfig, ProtectEventAdd, ProtectEventPacket } from 'unifi-protect';
3
3
  export declare class OwnUnifiCamera extends CameraDevice {
4
4
  /**
5
5
  * The name of the camera in Unifi
@@ -18,7 +18,7 @@ export declare class OwnUnifiCamera extends CameraDevice {
18
18
  /**
19
19
  * @inheritDoc
20
20
  */
21
- update(packet: ProtectEventPacket): void;
21
+ update(packet: ProtectEventPacket, baseEvent?: ProtectEventAdd): void;
22
22
  private checkForMotionUpdate;
23
23
  initialize(data: ProtectCameraConfig): void;
24
24
  protected resetPersonDetectedState(): void;
@@ -21,26 +21,29 @@ class OwnUnifiCamera extends index_1.CameraDevice {
21
21
  /**
22
22
  * @inheritDoc
23
23
  */
24
- update(packet) {
25
- this.checkForMotionUpdate(packet);
24
+ update(packet, baseEvent) {
25
+ this.checkForMotionUpdate(packet, baseEvent);
26
26
  this._lastUpdate = new Date();
27
27
  }
28
- checkForMotionUpdate(packet) {
29
- var _a;
28
+ checkForMotionUpdate(packet, baseEvent) {
29
+ var _a, _b, _c;
30
30
  const payload = packet.payload;
31
- const eventAdd = payload;
31
+ const eventAddInfo = baseEvent !== null && baseEvent !== void 0 ? baseEvent : payload;
32
+ const payloadAsEventAdd = payload;
32
33
  if (packet.header.modelKey !== 'smartDetectObject' &&
33
34
  (packet.header.modelKey !== 'event' ||
34
- !['smartDetectLine', 'smartDetectZone'].includes(payload.type) ||
35
- !eventAdd.smartDetectTypes.length)) {
35
+ !['smartDetectLine', 'smartDetectZone'].includes(eventAddInfo.type) ||
36
+ ((_b = (_a = payloadAsEventAdd.smartDetectTypes) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0 === 0))) {
36
37
  return;
37
38
  }
38
39
  this.log(enums_1.LogLevel.Debug, `Update for "${packet.header.modelKey}" to value: ${JSON.stringify(payload)}`);
39
- const detectedTypes = packet.header.modelKey === 'smartDetectObject' ? [eventAdd.type] : ((_a = eventAdd === null || eventAdd === void 0 ? void 0 : eventAdd.smartDetectTypes) !== null && _a !== void 0 ? _a : []);
40
+ const detectedTypes = packet.header.modelKey === 'smartDetectObject'
41
+ ? [payloadAsEventAdd.type]
42
+ : ((_c = payloadAsEventAdd === null || payloadAsEventAdd === void 0 ? void 0 : payloadAsEventAdd.smartDetectTypes) !== null && _c !== void 0 ? _c : []);
40
43
  for (const smartDetectType of detectedTypes) {
41
44
  switch (smartDetectType) {
42
45
  case 'licensePlate':
43
- this.log(enums_1.LogLevel.Debug, `Detected "licensePlate": ${JSON.stringify(eventAdd.metadata.licensePlate)}`);
46
+ this.log(enums_1.LogLevel.Debug, `Detected "licensePlate": ${JSON.stringify(payloadAsEventAdd.metadata.licensePlate)}`);
44
47
  break;
45
48
  case 'person':
46
49
  this.onNewPersonDetectedValue(true);
@@ -9,6 +9,7 @@ export declare class UnifiProtect implements iDisposable {
9
9
  static readonly ownCameras: Map<string, OwnUnifiCamera>;
10
10
  private _deviceStates;
11
11
  private _idMap;
12
+ private _eventMap;
12
13
  private _lastUpdate;
13
14
  constructor(settings: iUnifiProtectOptions);
14
15
  private reconnect;
@@ -16,5 +17,6 @@ export declare class UnifiProtect implements iDisposable {
16
17
  static addDevice(camera: OwnUnifiCamera): void;
17
18
  private initialize;
18
19
  private onMessage;
20
+ private rememberAddEvent;
19
21
  private initializeCamera;
20
22
  }
@@ -11,6 +11,7 @@ class UnifiProtect {
11
11
  this.unifiLogger = new unifi_logger_1.UnifiLogger(enums_1.LogSource.UnifiProtect);
12
12
  this._deviceStates = new Map();
13
13
  this._idMap = new Map();
14
+ this._eventMap = new Map();
14
15
  this._lastUpdate = new Date(0);
15
16
  this._api = new unifi_protect_1.ProtectApi(this.unifiLogger);
16
17
  this.reconnect(settings);
@@ -23,6 +24,7 @@ class UnifiProtect {
23
24
  }, 5 * 60 * 1000);
24
25
  }
25
26
  reconnect(settings) {
27
+ this._eventMap = new Map();
26
28
  this._api
27
29
  .login(settings.nvrAddress, settings.username, settings.password)
28
30
  .then((_loggedIn) => {
@@ -52,7 +54,7 @@ class UnifiProtect {
52
54
  this._api.on('message', this.onMessage.bind(this));
53
55
  }
54
56
  onMessage(packet) {
55
- var _a, _b;
57
+ var _a, _b, _c, _d;
56
58
  const payload = packet.payload;
57
59
  this._lastUpdate = new Date();
58
60
  switch (packet.header.modelKey) {
@@ -61,8 +63,15 @@ class UnifiProtect {
61
63
  default:
62
64
  // Lookup the device.
63
65
  let id = packet.header.id;
66
+ let baseEvent;
64
67
  if (packet.header.action === 'add') {
65
- id = (_a = packet.payload.camera) !== null && _a !== void 0 ? _a : packet.payload.cameraId;
68
+ const addEvent = packet.payload;
69
+ id = (_a = addEvent.camera) !== null && _a !== void 0 ? _a : addEvent.cameraId;
70
+ this.rememberAddEvent(packet, addEvent);
71
+ }
72
+ else if (packet.header.action === 'update' && this._eventMap.has(id)) {
73
+ baseEvent = this._eventMap.get(packet.header.id);
74
+ id = (_c = (_b = baseEvent === null || baseEvent === void 0 ? void 0 : baseEvent.camera) !== null && _b !== void 0 ? _b : baseEvent === null || baseEvent === void 0 ? void 0 : baseEvent.cameraId) !== null && _c !== void 0 ? _c : '';
66
75
  }
67
76
  const ownName = this._idMap.get(id);
68
77
  if (!ownName) {
@@ -70,14 +79,22 @@ class UnifiProtect {
70
79
  }
71
80
  const ownCamera = UnifiProtect.ownCameras.get(ownName);
72
81
  if (ownCamera !== undefined) {
73
- ownCamera.update(packet);
82
+ ownCamera.update(packet, baseEvent);
74
83
  break;
75
84
  }
76
85
  break;
77
86
  }
78
87
  // Update the internal list we maintain.
79
88
  if (packet.header.action === 'update') {
80
- this._deviceStates.set(packet.header.id, Object.assign((_b = this._deviceStates.get(packet.header.id)) !== null && _b !== void 0 ? _b : {}, payload));
89
+ this._deviceStates.set(packet.header.id, Object.assign((_d = this._deviceStates.get(packet.header.id)) !== null && _d !== void 0 ? _d : {}, payload));
90
+ }
91
+ }
92
+ rememberAddEvent(packet, addEvent) {
93
+ if (packet.header.modelKey === 'event') {
94
+ this._eventMap.set(addEvent.id, addEvent);
95
+ utils_1.Utils.guardedTimeout(() => {
96
+ this._eventMap.delete(addEvent.id);
97
+ }, 5 * 60 * 1000, this);
81
98
  }
82
99
  }
83
100
  initializeCamera(data) {