Skip to content

Commit

Permalink
Remove GZIP Parsing (for now) and further socket fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-j-davies committed Jul 17, 2024
1 parent c1a2067 commit cdbca0b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 41 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-unifi-os",
"version": "1.1.0-beta.1",
"version": "1.1.0-beta.2",
"description": "Nodes to access UniFi data using endpoints and websockets",
"main": "build/nodes/unifi.js",
"scripts": {
Expand Down
9 changes: 6 additions & 3 deletions src/SharedProtectWebSocket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { logger } from '@nrchkb/logger'
import { Loggers } from '@nrchkb/logger/src/types'
import { Mutex } from 'async-mutex'
import WebSocket, { RawData } from 'ws'
import WebSocket, { OPEN, RawData } from 'ws'

import { endpoints } from './Endpoints'
import { ProtectApiUpdates } from './lib/ProtectApiUpdates'
Expand Down Expand Up @@ -77,8 +77,11 @@ export class SharedProtectWebSocket {
this.reconnectTimer = undefined
}
this.ws?.removeAllListeners()
this.ws?.close()
this.ws?.terminate()
if (this.ws?.readyState === OPEN) {
this.ws?.close()
this.ws?.terminate()
}

this.ws = undefined
}

Expand Down
50 changes: 23 additions & 27 deletions src/nodes/AccessController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ module.exports = (RED: NodeAPI) => {
) {
const self = this
const log = logger('UniFi', 'AccessController', self.name, self)


RED.nodes.createNode(self, config)
self.config = config
Expand Down Expand Up @@ -82,35 +81,32 @@ module.exports = (RED: NodeAPI) => {

// The Boostrap request
const getBootstrap = async (init?: boolean) => {

if(hasProtect){
if (hasProtect) {
self.request(self.id, bootstrapURI, 'GET', undefined, 'json')
.then((res: UnifiResponse) => {
self.bootstrapObject = res as Bootstrap

if (init) {
// Fire up a shared websocket to the Protect WS endpoint
self.protectSharedWS = new SharedProtectWebSocket(
self,
self.config,
self.bootstrapObject
)
} else {
// Update the shared websocket to the Protect WS endpoint, so we can connect to its new lastUpdateId
self.protectSharedWS?.updateLastUpdateId(
self.bootstrapObject
.then((res: UnifiResponse) => {
self.bootstrapObject = res as Bootstrap

if (init) {
// Fire up a shared websocket to the Protect WS endpoint
self.protectSharedWS = new SharedProtectWebSocket(
self,
self.config,
self.bootstrapObject
)
} else {
// Update the shared websocket to the Protect WS endpoint, so we can connect to its new lastUpdateId
self.protectSharedWS?.updateLastUpdateId(
self.bootstrapObject
)
}
})
.catch((error) => {
hasProtect = false
log.debug(
`Received error when obtaining bootstrap: ${error}, assuming this is to be expected, i.e no protect instance.`
)
}
})
.catch((error) => {
hasProtect = false
log.debug(
`Received error when obtaining bootstrap: ${error}, assuming this is to be expected, i.e no protect instance.`
)
})
})
}


}

const refresh = (init?: boolean) => {
Expand Down
9 changes: 1 addition & 8 deletions src/nodes/Request.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { logger } from '@nrchkb/logger'
import { NodeAPI } from 'node-red'
import util from 'util'
import { unzip } from 'zlib'

import AccessControllerNodeType from '../types/AccessControllerNodeType'
import RequestNodeConfigType from '../types/RequestNodeConfigType'
Expand Down Expand Up @@ -117,13 +116,7 @@ module.exports = (RED: NodeAPI) => {
})
}

if (Buffer.isBuffer(data)) {
unzip(data, (_err, result) => {
_send(
JSON.parse(result.toString()) as UnifiResponse
)
})
} else {
if (!Buffer.isBuffer(data) && typeof data !== 'string') {
_send(data)
}
})
Expand Down

0 comments on commit cdbca0b

Please sign in to comment.