NRCHKB / node-red-contrib-unifi-os

Nodes to access UniFi data using endpoints and websockets
Apache License 2.0
28 stars 1 forks source link

Add a new `unifi-protect` node #48

Closed marcus-j-davies closed 2 years ago

marcus-j-davies commented 2 years ago

@crxporter @Shaquu Below is what I have built and is ready for review.

Screenshot 2022-10-24 at 19 40 45

unifi-protect will call into

const I: Interest = {
    deviceId: this.config.cameraId,
    callback: handleUpdate,
}

Each event type is based on an EventModel

shapeProfile Is the object that is designed to match (at least) the incoming payload (it uses lodash.isMatch)

metadata Is the settings/behaviour for that event. the HTML file uses the metadata.idto subscribe to that event (supports multiple)

some models have a valueExpression property and this uses JSONata to extract a value from the payload and passes it on to the user (as payload.value)- these do not support a duration (hasDuration: false) as these are more a constant value change as opposed to an on going event

The combination of hasDuration: true with a valueExpression is marked as invalid in the typescript

Lastley, there is thumbnailSupport : enum and this states whether or not a snapshot is supported with this event. the motion detection event for example, does not seem to produce a snapshot in my early testing.

Below is the thumbnailSupport enum

export enum ThumbnailSupport {
    START_END = 0,                         /* Supports instant Start And End snapshots */
    START_WITH_DELAYED_END,                /* End snapshots need a delay - user adjustable */
    SINGLE_DELAYED,                        /* Example : A door Bell, where the snapshot is not ready right away  */
    SINGLE,                                /* Why not? */
    NONE,                                  /* No snapshots are produced with this event */
}

There is snapshotAvailability in the payloads to identify to the user the readiness of a snapshot, a 2nd pin is used for delayed snapshots - with the associated event id

https://github.com/marcus-j-davies/node-red-contrib-unifi-os/blob/protect-node/src/EventModels.ts

Some areas may have room for improvements of course, such as creating a new access controller, from within a protect node, it will try and call into the bootstrap before the bootstrap has had a chance to be fetched by the controller (it does this to fetch the cameras using an HTTPAdmin endpoint

they will get a 501 - Not Implemented because the bootstrap has not yet been received.

But other than delaying this request (for new controllers being spawned) - not sure how else it can be achieved, as waiting will add some delay to the cameras being listed.

For already running access controllers - this is not a problem (as the Bootstrap is ready before hand), that is if Protect is running on the target hardware of course else 501

marcus-j-davies commented 2 years ago

@Shaquu @crxporter I feel this can be put to BETA?

Please review the last commit.: https://github.com/NRCHKB/node-red-contrib-unifi-os/pull/48/commits/3bf7a970feb114eb873d1f2d9a7884bb5fa17069 This commit adds a robust (in testing 😅) recovery mechanism via the web socket Ping/Pong protocol.

The Web socket reconfiguring after a new update ID has been fetched - usually triggered by the 30 minutely re-login that occurs in the access controller.

image

A recovery after a network connection drop. This recovery is only put into high alert after we first successfully get a connection to the web socket, adding recovery to a web socket that was never successful could cause a continuous recovery loop, that will never end.

image

marcus-j-davies commented 2 years ago

@crxporter @Shaquu

All comments have been addressed. I have also found out that in NR, if a config node has user properties set to undefined (Evan if not required), it causes an error flag on the nodes that use it (this @Shaquu was the reason for the error indicator) normal nodes don't seem to have this problem (at least I have never seen this happen for non config nodes)

This becomes a problem if an update means new config params are added - as they will be undefined, therefore I have made them required.

Users just need to open up the config node and save to apply a value of 0, 0 sets timeouts to use defaults (90000/15000).

Bootstrap is now a type (types/Bootstrap.ts)

Lastly, my PR is based on the Main branch, since switching to dev - the PR brings in changes that were not apart of the dev branch.