Skip to content

Commit

Permalink
feat: zniffer (#3706)
Browse files Browse the repository at this point in the history
Co-authored-by: Dominic Griesel <[email protected]>
  • Loading branch information
robertsLando and AlCalzone authored May 30, 2024
1 parent 4e47111 commit 18ffbe2
Show file tree
Hide file tree
Showing 23 changed files with 2,969 additions and 372 deletions.
112 changes: 103 additions & 9 deletions api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import * as utils from './lib/utils'
import backupManager from './lib/BackupManager'
import { readFile, realpath } from 'fs/promises'
import { generate } from 'selfsigned'
import ZnifferManager, { ZnifferConfig } from './lib/ZnifferManager'

const createCertificate = promisify(generate)

Expand Down Expand Up @@ -158,6 +159,7 @@ socketManager.authMiddleware = function (
}

let gw: Gateway // the gateway instance
let zniffer: ZnifferManager // the zniffer instance
const plugins: CustomPlugin[] = []
let pluginsRouter: Router

Expand Down Expand Up @@ -257,6 +259,7 @@ export async function startServer(port: number | string, host?: string) {
setupInterceptor()
await loadSnippets()
await loadManager()
startZniffer(settings.zniffer)
await startGateway(settings)
}

Expand Down Expand Up @@ -424,6 +427,12 @@ async function startGateway(settings: Settings) {
restarting = false
}

function startZniffer(settings: ZnifferConfig) {
if (settings) {
zniffer = new ZnifferManager(settings, socketManager.io)
}
}

async function destroyPlugins() {
while (plugins.length > 0) {
const instance = plugins.pop()
Expand Down Expand Up @@ -619,10 +628,17 @@ function setupSocket(server: HttpServer) {
// Server: https://socket.io/docs/v4/server-application-structure/#all-event-handlers-are-registered-in-the-indexjs-file
// Client: https://socket.io/docs/v4/client-api/#socketemiteventname-args
socket.on(inboundEvents.init, (data, cb = noop) => {
let state = {} as any

if (gw.zwave) {
const state = gw.zwave.getState()
cb(state)
state = gw.zwave.getState()
}

if (zniffer) {
state.zniffer = zniffer.status()
}

cb(state)
})

socket.on(
Expand Down Expand Up @@ -730,6 +746,49 @@ function setupSocket(server: HttpServer) {

cb(result)
})

// eslint-disable-next-line @typescript-eslint/no-misused-promises
socket.on(inboundEvents.zniffer, async (data, cb = noop) => {
logger.info(`Zniffer api call: ${data.api}`)

let res: any, err: string
try {
switch (data.apiName) {
case 'start':
res = await zniffer.start()
break
case 'stop':
res = await zniffer.stop()
break
case 'clear':
res = zniffer.clear()
break
case 'getFrames':
res = zniffer.getFrames()
break
case 'setFrequency':
res = await zniffer.setFrequency(data.frequency)
break
case 'saveCaptureToFile':
res = await zniffer.saveCaptureToFile()
break
default:
throw new Error(`Unknown ZNIFFER api ${data.apiName}`)
}
} catch (error) {
logger.error('Error while calling ZNIFFER api', error)
err = error.message
}

const result = {
success: !err,
message: err || 'Success ZNIFFER api call',
result: res,
api: data.apiName,
}

cb(result)
})
})

// emitted every time a new client connects/disconnects
Expand Down Expand Up @@ -1074,15 +1133,50 @@ app.post(
}
// TODO: validate settings using calss-validator
const settings = req.body

const actualSettings = jsonStore.get(store.settings) as Settings

const shouldRestartGw = !utils.deepEqual(
{
zwave: actualSettings.zwave,
gateway: actualSettings.gateway,
mqtt: actualSettings.mqtt,
},
{
zwave: settings.zwave,
gateway: settings.gateway,
mqtt: settings.mqtt,
},
)

const shouldRestartZniffer = !utils.deepEqual(
actualSettings.zniffer,
settings.zniffer,
)

// nothing changed, consider it a forced restart
const restartAll = !shouldRestartGw && !shouldRestartZniffer

restarting = true
await jsonStore.put(store.settings, settings)
await gw.close()
await destroyPlugins()
// reload loggers settings
setupLogging(settings)
// restart clients and gateway
await startGateway(settings)
backupManager.init(gw.zwave)

if (restartAll || shouldRestartGw) {
await gw.close()

await destroyPlugins()
// reload loggers settings
setupLogging(settings)
// restart clients and gateway
await startGateway(settings)
backupManager.init(gw.zwave)
}

if (restartAll || shouldRestartZniffer) {
if (zniffer) {
await zniffer.close()
}
startZniffer(settings.zniffer)
}

res.json({
success: true,
Expand Down
2 changes: 2 additions & 0 deletions api/config/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { GatewayConfig } from '../lib/Gateway'
import { MqttConfig } from '../lib/MqttClient'
import { ZnifferConfig } from '../lib/ZnifferManager'
import { ZwaveConfig, deviceConfigPriorityDir } from '../lib/ZwaveClient'

export type StoreKeys = 'settings' | 'scenes' | 'nodes' | 'users'
Expand All @@ -21,6 +22,7 @@ export interface Settings {
mqtt?: MqttConfig
zwave?: ZwaveConfig
gateway?: GatewayConfig
zniffer?: ZnifferConfig
}

const store: Record<StoreKeys, StoreFile> = {
Expand Down
3 changes: 3 additions & 0 deletions api/lib/SocketEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export enum socketEvents {
grantSecurityClasses = 'GRANT_SECURITY_CLASSES',
validateDSK = 'VALIDATE_DSK',
inclusionAborted = 'INCLUSION_ABORTED',
znifferFrame = 'ZNIFFER_FRAME',
znifferState = 'ZNIFFER_STATE',
}

// events from client ---> server
Expand All @@ -27,4 +29,5 @@ export enum inboundEvents {
zwave = 'ZWAVE_API', // call a zwave api
hass = 'HASS_API', // call an hass api
mqtt = 'MQTT_API', // call an mqtt api
zniffer = 'ZNIFFER_API', // call a zniffer api
}
1 change: 1 addition & 0 deletions api/lib/SocketManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface SocketManagerEventCallbacks {
[inboundEvents.zwave]: (socket: Socket, data: any) => void
[inboundEvents.hass]: (socket: Socket, data: any) => void
[inboundEvents.mqtt]: (socket: Socket, data: any) => void
[inboundEvents.zniffer]: (socket: Socket, data: any) => void
clients: (
event: 'connection' | 'disconnect',
sockets: Map<string, Socket>,
Expand Down
Loading

0 comments on commit 18ffbe2

Please sign in to comment.