Skip to content

Commit

Permalink
fix: rename internal types
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio1988 committed Dec 6, 2023
1 parent 843dc14 commit cb4ecce
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 170 deletions.
4 changes: 2 additions & 2 deletions packages/client/src/app/jobs/executeJobModal.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Button, Modal, Table, Text } from '@nextui-org/react';
import { MitmControlDTO } from '@rotom/connections';
import { DeviceControlDTO } from '@rotom/connections';
import { Selection } from '@react-types/shared/src/selection';
import { toast } from 'react-toastify';
import { useCallback, useState } from 'react';

interface ExecuteJobModalProps {
closeModal: () => void;
devices?: MitmControlDTO[];
devices?: DeviceControlDTO[];
jobId: string;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/app/jobs/jobsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Text } from '@nextui-org/react';
import { JobsDTO, JobsStatusDTO, MitmControlDTO, StatusDTO } from '@rotom/types';
import { JobsDTO, JobsStatusDTO, DeviceControlDTO, StatusDTO } from '@rotom/types';
import { useQuery } from '@tanstack/react-query';
import { JobsTable } from './jobsTable';
import { JobsStatusesTable } from './jobsStatusesTable';
Expand All @@ -19,7 +19,7 @@ export const JobsPage = (): JSX.Element => {
refetchInterval: 5000,
});

const { data: devices, refetch: refetchDevices } = useQuery<StatusDTO, Error, MitmControlDTO[]>(
const { data: devices, refetch: refetchDevices } = useQuery<StatusDTO, Error, DeviceControlDTO[]>(
['status'],
fetchStatus,
{
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/app/jobs/jobsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Button, Loading, Modal, Table, useModal } from '@nextui-org/react';
import { JobsDTO, MitmControlDTO } from '@rotom/types';
import { JobsDTO, DeviceControlDTO } from '@rotom/types';
import { useState } from 'react';

import { ExecuteJobModal } from './executeJobModal';

interface JobsTableProps {
devices?: MitmControlDTO[];
devices?: DeviceControlDTO[];
isLoading: boolean;
jobs: JobsDTO;
refetchDevices: () => void;
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/app/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createServer, Model, Factory } from 'miragejs';
import { faker } from '@faker-js/faker';
import { StatusDTO, WorkerDTO, MitmControlDTO } from '@rotom/types';
import { StatusDTO, WorkerDTO, DeviceControlDTO } from '@rotom/types';

const getRecentTimestamp = (): number => {
const thirtyMinutesAgo = new Date();
Expand All @@ -14,7 +14,7 @@ export const makeServer = ({ environment = 'test' } = {}) => {
environment,

models: {
device: Model.extend<Partial<MitmControlDTO>>({ version: '15' }),
device: Model.extend<Partial<DeviceControlDTO>>({ version: '15' }),
worker: Model.extend<Partial<WorkerDTO>>({}),
},

Expand All @@ -24,7 +24,7 @@ export const makeServer = ({ environment = 'test' } = {}) => {
},

factories: {
device: Factory.extend<Partial<MitmControlDTO>>({
device: Factory.extend<Partial<DeviceControlDTO>>({
dateLastMessageReceived() {
return getRecentTimestamp();
},
Expand Down Expand Up @@ -61,7 +61,7 @@ export const makeServer = ({ environment = 'test' } = {}) => {
workerId(i) {
return faker.vehicle.vrm() + `${i}`;
},
mitm(i) {
worker(i) {
return {
dateLastMessageReceived: getRecentTimestamp(),
dateLastMessageSent: getRecentTimestamp(),
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/app/status/devicesTable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { memo, useCallback, useMemo, useState } from 'react';
import { StatusDTO, MitmControlDTO } from '@rotom/types';
import { StatusDTO, DeviceControlDTO } from '@rotom/types';
import { Table, Dropdown, SortDescriptor } from '@nextui-org/react';
import { toast } from 'react-toastify';

Expand Down Expand Up @@ -57,7 +57,7 @@ export const DevicesTable = ({ devices, workers }: StatusDTO): JSX.Element => {
[],
);

const list = useTableSort<MitmControlDTO>({
const list = useTableSort<DeviceControlDTO>({
items: devices,
initialSortDescriptor,
});
Expand Down
12 changes: 6 additions & 6 deletions packages/client/src/app/status/workersTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export const WorkersTable = ({ workers }: { workers: StatusDTO['workers'] }): JS
return list.items.filter(
(worker) =>
!lowercaseSearch ||
worker.mitm.origin?.toLowerCase().includes(lowercaseSearch) ||
worker.worker.origin?.toLowerCase().includes(lowercaseSearch) ||
worker.workerId.toLowerCase().includes(lowercaseSearch) ||
worker.scanner?.workerName.toLowerCase().includes(lowercaseSearch),
worker.controller?.workerName.toLowerCase().includes(lowercaseSearch),
);
}, [search, list.items]);

Expand Down Expand Up @@ -60,15 +60,15 @@ export const WorkersTable = ({ workers }: { workers: StatusDTO['workers'] }): JS
<Table.Body loadingState={list.loadingState}>
{filteredItems.map((worker, index) => (
<Table.Row key={`${worker.workerId}-${index}`}>
<Table.Cell>{worker.mitm.origin}</Table.Cell>
<Table.Cell>{worker.worker.origin}</Table.Cell>
<Table.Cell>{worker.workerId}</Table.Cell>
<Table.Cell>{worker.isAllocated ? '✅' : '❌'}</Table.Cell>
<Table.Cell>{worker.scanner?.workerName}</Table.Cell>
<Table.Cell>{worker.controller?.workerName}</Table.Cell>
<Table.Cell>
<RelativeTimeLabel timestamp={worker.mitm.dateLastMessageReceived} />
<RelativeTimeLabel timestamp={worker.worker.dateLastMessageReceived} />
</Table.Cell>
<Table.Cell>
<RelativeTimeLabel timestamp={worker.mitm.dateLastMessageSent} />
<RelativeTimeLabel timestamp={worker.worker.dateLastMessageSent} />
</Table.Cell>
</Table.Row>
))}
Expand Down
2 changes: 1 addition & 1 deletion packages/connections/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# connections

This library is a node library to handle connections with MITM and Scanner
This library is a node library to handle connections with MITM and Controller
6 changes: 3 additions & 3 deletions packages/connections/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './lib/scannerConnection';
export * from './lib/mitmControlConnection';
export * from './lib/mitmWorkerConnection';
export * from './lib/controllerConnection';
export * from './lib/deviceControlConnection';
export * from './lib/deviceWorkerConnection';
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { EventEmitter } from 'events';
import { WebSocket } from 'ws';
import { Logger } from 'winston';
import { MitmWorkerConnection } from './mitmWorkerConnection';
import { DTO } from './utils/type';
import { WebSocket } from 'ws';
import { DeviceWorkerConnection } from './deviceWorkerConnection';
import { RotomProtos } from './utils/mitmProto';
import { DTO } from './utils/type';
import MitmRequest = RotomProtos.MitmRequest;
//import MitmCommand = RotomProtos.MitmCommand;

let instanceNo = 0;

export type ScannerConnectionDTO = Omit<DTO<ScannerConnection>, 'ws' | 'log' | 'heartbeatHandle' | 'mitmConnection'>;
export type ControllerConnectionDTO = Omit<
DTO<ControllerConnection>,
'ws' | 'log' | 'heartbeatHandle' | 'deviceWorkerConnection'
>;

export class ScannerConnection extends EventEmitter {
_mitm_disconnect_handler: () => void;
_mitm_message_handler: (data: ArrayBuffer) => void;
export class ControllerConnection extends EventEmitter {
_device_disconnect_handler: () => void;
_device_message_handler: (data: ArrayBuffer) => void;
_ws_close_handler: () => void;
_ws_message_handler: (data: ArrayBuffer) => void;
mitmConnection: MitmWorkerConnection;
deviceWorkerConnection: DeviceWorkerConnection;
ws: WebSocket;
log: Logger;
heartbeatHandle: NodeJS.Timer;
Expand All @@ -26,7 +29,7 @@ export class ScannerConnection extends EventEmitter {
isAlive: boolean;
loginListener: number;

constructor(log: Logger, ws: WebSocket, mitmConnection: MitmWorkerConnection) {
constructor(log: Logger, ws: WebSocket, deviceWorkerConnection: DeviceWorkerConnection) {
super();
this.ws = ws;
this.log = log;
Expand All @@ -38,16 +41,16 @@ export class ScannerConnection extends EventEmitter {
this.instanceNo = instanceNo++;

this._ws_message_handler = (data: ArrayBuffer) => this.#handleScannerMessage(data);
this._ws_close_handler = () => this.#handleScannerDisconnection();
this._ws_close_handler = () => this.#handleControllerDisconnection();
ws.on('message', this._ws_message_handler);
ws.on('close', this._ws_close_handler);
ws.on('pong', () => this.heartbeat());

this.mitmConnection = mitmConnection;
this._mitm_message_handler = (data: ArrayBuffer) => this.#handleMitmMessage(data);
this._mitm_disconnect_handler = () => this.#handleMitmDisconnection();
this.mitmConnection.on('received', this._mitm_message_handler);
this.mitmConnection.on('disconnected', this._mitm_disconnect_handler);
this.deviceWorkerConnection = deviceWorkerConnection;
this._device_message_handler = (data: ArrayBuffer) => this.#handleMitmMessage(data);
this._device_disconnect_handler = () => this.#handleMitmDisconnection();
this.deviceWorkerConnection.on('received', this._device_message_handler);
this.deviceWorkerConnection.on('disconnected', this._device_disconnect_handler);

this.heartbeatHandle = setInterval(() => this.checkHeartbeat(), 30000);
}
Expand All @@ -73,15 +76,15 @@ export class ScannerConnection extends EventEmitter {
* @returns {string}
*/
get workerId(): string {
return this.mitmConnection.workerId as string;
return this.deviceWorkerConnection.workerId as string;
}

/**
* origin of connected Mitm device
* @returns {string}
*/
get origin() {
return this.mitmConnection.origin;
return this.deviceWorkerConnection.origin;
}

/**
Expand Down Expand Up @@ -110,7 +113,7 @@ export class ScannerConnection extends EventEmitter {
}
}

this.mitmConnection.send(message);
this.deviceWorkerConnection.send(message);
}

/**
Expand All @@ -124,33 +127,33 @@ export class ScannerConnection extends EventEmitter {
this.ws.send(message, { binary: true });
}

#handleScannerDisconnection() {
#handleControllerDisconnection() {
this.isAlive = false;
clearInterval(this.heartbeatHandle);

// Tell users scanner is disconnected
this.emit('disconnected', this);
this.#disconnectFromMitmWebsocket();
this.#disconnectFromDeviceWebsocket();
}

/**
* Disconnect scanner
*/
disconnect() {
this.#disconnectFromMitmWebsocket();
this.#disconnectFromDeviceWebsocket();
this.ws.close(3000, 'Device has been disconnected');
}

/**
* Disconnect associated mitm
* Disconnect associated device
*/
disconnectMitm() {
this.mitmConnection.ws.close(3005, 'Scanner disconnected');
disconnectDevice() {
this.deviceWorkerConnection.ws.close(3005, 'Controller disconnected');
}

#disconnectFromMitmWebsocket() {
this.mitmConnection.removeListener('received', this._mitm_message_handler);
this.mitmConnection.removeListener('disconnected', this._mitm_disconnect_handler);
#disconnectFromDeviceWebsocket() {
this.deviceWorkerConnection.removeListener('received', this._device_message_handler);
this.deviceWorkerConnection.removeListener('disconnected', this._device_disconnect_handler);
}

heartbeat() {
Expand Down Expand Up @@ -183,7 +186,7 @@ export class ScannerConnection extends EventEmitter {
this.ws.ping();
}

serialize(): ScannerConnectionDTO {
serialize(): ControllerConnectionDTO {
return {
dateLastMessageSent: this.dateLastMessageSent,
instanceNo: this.instanceNo,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events';
import { WebSocket } from 'ws';
import { Logger } from 'winston';
import { WebSocket } from 'ws';

import { DTO } from './utils/type';

Expand All @@ -19,15 +19,15 @@ class Deferred {
}
}

export type MitmControlDTO = Omit<DTO<MitmControlConnection>, 'ws' | 'log' | 'heartbeatHandle'>;
export type DeviceControlDTO = Omit<DTO<DeviceControlConnection>, 'ws' | 'log' | 'heartbeatHandle'>;

interface MemoryStatus {
memFree: number;
memMitm: number;
memStart: number;
}

export class MitmControlConnection extends EventEmitter {
export class DeviceControlConnection extends EventEmitter {
deviceId?: string;
log: Logger;
init: boolean;
Expand Down Expand Up @@ -91,7 +91,7 @@ export class MitmControlConnection extends EventEmitter {
this.noMessagesReceived = 0;
this.responses = {};
} catch (e) {
this.log.error(`MITM /control - error decoding welcome message, disconnecting`);
this.log.error(`Device /control - error decoding welcome message, disconnecting`);
this.ws.close();
return;
}
Expand All @@ -112,13 +112,13 @@ export class MitmControlConnection extends EventEmitter {
promise.reject(`Status ${response.status} ${response.body?.errorReason ?? ''}`);
}
} else {
this.log.warn(`${this.deviceId}: <MITMC Unrecognised response ${message.toString()}`);
this.log.warn(`${this.deviceId}: <MITMC Unrecognized response ${message.toString()}`);
}
}
}

send(message: string) {
this.log.debug(`${this.deviceId}: MITMC> ${message}`);
this.log.debug(`${this.deviceId}: DEVICEC> ${message}`);
this.noMessagesSent++;
this.dateLastMessageSent = Date.now();
this.ws.send(message.toString());
Expand All @@ -131,7 +131,7 @@ export class MitmControlConnection extends EventEmitter {
checkHeartbeat() {
if (!this.isAlive) {
// Pong has not been received in last interval seconds
this.log.warn(`${this.deviceId}/${this.instanceNo}: MITM - No response to ping - forcing disconnect`);
this.log.warn(`${this.deviceId}/${this.instanceNo}: DEVICE - No response to ping - forcing disconnect`);
clearInterval(this.heartbeatHandle);
this.ws.terminate();

Expand Down Expand Up @@ -209,7 +209,7 @@ export class MitmControlConnection extends EventEmitter {
return this.responses[id].promise;
}

serialize(): MitmControlDTO {
serialize(): DeviceControlDTO {
return {
dateLastMessageReceived: this.dateLastMessageReceived,
dateLastMessageSent: this.dateLastMessageSent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { EventEmitter } from 'events';
import { WebSocket } from 'ws';
import { Logger } from 'winston';
import { WebSocket } from 'ws';

import { DTO } from './utils/type';
import { RotomProtos } from './utils/mitmProto';
import { DTO } from './utils/type';
import WelcomeMessage = RotomProtos.WelcomeMessage;

let instanceNo = 0;

export type MitmWorkerDTO = Omit<DTO<MitmWorkerConnection>, 'ws' | 'log' | 'heartbeatHandle'>;
export type DeviceWorkerDTO = Omit<DTO<DeviceWorkerConnection>, 'ws' | 'log' | 'heartbeatHandle'>;

export class MitmWorkerConnection extends EventEmitter {
export class DeviceWorkerConnection extends EventEmitter {
workerId?: string;
deviceId?: string;
userAgent?: string;
Expand Down Expand Up @@ -125,7 +125,7 @@ export class MitmWorkerConnection extends EventEmitter {
this.emit('disconnected', this);
}

serialize(): MitmWorkerDTO {
serialize(): DeviceWorkerDTO {
return {
dateLastMessageReceived: this.dateLastMessageReceived,
dateLastMessageSent: this.dateLastMessageSent,
Expand Down
Loading

0 comments on commit cb4ecce

Please sign in to comment.