Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mds-web-socket] extensible entities #310

Merged
merged 18 commits into from
May 19, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions packages/mds-web-sockets/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import logger from '@mds-core/mds-logger'
import { seconds, getEnvVar } from '@mds-core/mds-utils'
import WebSocket from 'ws'
import { setWsHeartbeat } from 'ws-heartbeat/server'
import { Telemetry, VehicleEvent } from '@mds-core/mds-types'
import { ApiServer, HttpServer } from '@mds-core/mds-api-server'
import { initializeNatsSubscriber } from '@mds-core/mds-stream/nats/nats'
import { Clients } from './clients'
import { ENTITY_TYPE } from './types'

type Json = boolean | number | string | null | JsonArray | JsonMap
twelch marked this conversation as resolved.
Show resolved Hide resolved
interface JsonMap {
[key: string]: Json
}
type JsonArray = Array<Json>

export const WebSocketServer = () => {
const server = HttpServer(ApiServer(app => app))

Expand Down Expand Up @@ -46,15 +51,6 @@ export const WebSocketServer = () => {
staleClients.forEach(client => client.close())
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function writeTelemetry(telemetry: Telemetry) {
pushToClients('TELEMETRIES', JSON.stringify(telemetry))
}

function writeEvent(event: VehicleEvent) {
pushToClients('EVENTS', JSON.stringify(event))
}

wss.on('connection', (ws: WebSocket) => {
ws.on('message', async (data: WebSocket.Data) => {
const message = data.toString().trim().split('%')
Expand All @@ -65,14 +61,15 @@ export const WebSocketServer = () => {
if (clients.isAuthenticated(ws)) {
if (args.length === 2) {
const [entity, payload] = args
// Limit messages to only supported entities
switch (entity) {
twelch marked this conversation as resolved.
Show resolved Hide resolved
case 'EVENTS': {
const event = JSON.parse(payload)
return writeEvent(event)
return pushToClients(entity, event)
}
case 'TELEMETRIES': {
const telemetry = JSON.parse(payload)
return writeTelemetry(telemetry)
return pushToClients(entity, telemetry)
}
default: {
return ws.send(`Invalid entity: ${entity}`)
Expand Down Expand Up @@ -105,16 +102,14 @@ export const WebSocketServer = () => {
TENANT_ID: 'mds'
})

const processor = async (type: string, data: VehicleEvent | Telemetry) => {
const processor = async (type: string, data: Json) => {
switch (type) {
case 'event': {
await writeEvent(data as VehicleEvent)
return
}
case 'telemetry': {
await writeTelemetry(data as Telemetry)
return
}
case 'event':
await pushToClients('EVENTS', JSON.stringify(data))
break
case 'telemetry':
await pushToClients('TELEMETRIES', JSON.stringify(data))
break
default:
logger.error(`Unprocessable entity of type: ${type} and data: ${JSON.stringify(data)}`)
}
Expand Down