Added debug logs, typed notif
MODIFY
src/server/rest/CentralRestServer.ts
src/server/rest/CentralRestServer.ts
@@ -8,12 +8,14 @@ import ChangeNotification from '../../types/ChangeNotification';
import ChargingStationConfiguration from '../../types/configuration/ChargingStationConfiguration';
import Configuration from '../../utils/Configuration';
import Constants from '../../utils/Constants';
+import { Data } from '../../types/GlobalType';
import HttpStatusCodes from 'http-status-codes';
import Logging from '../../utils/Logging';
import { ServerAction } from '../../types/Server';
import SessionHashService from './service/SessionHashService';
-import SingleChangeNotification from '../../types/SingleChangeNotification';
+import SingleChangeNotification, { NotificationData } from '../../types/SingleChangeNotification';
import UserToken from '../../types/UserToken';
+import Utils from '../../utils/Utils';
import cluster from 'cluster';
import expressTools from '../ExpressTools';
import http from 'http';
@@ -21,7 +23,7 @@ import morgan from 'morgan';
import sanitize from 'express-sanitizer';
import socketio from 'socket.io';
import socketioJwt from 'socketio-jwt';
-import util from 'util';
+
const MODULE_NAME = 'CentralRestServer';
@@ -43,13 +45,10 @@ export default class CentralRestServer {
// Keep params
CentralRestServer.centralSystemRestConfig = centralSystemRestConfig;
this.chargingStationConfig = chargingStationConfig;
-
// Initialize express app
this.expressApplication = expressTools.initApplication('2mb');
-
// Mount express-sanitizer middleware
this.expressApplication.use(sanitize());
-
// Log to console
if (CentralRestServer.centralSystemRestConfig.debug) {
// Log
@@ -69,16 +68,12 @@ export default class CentralRestServer {
})
);
}
-
// Authentication
this.expressApplication.use(CentralRestServerAuthentication.initialize());
-
// Auth services
this.expressApplication.all('/client/auth/:action', CentralRestServerAuthentication.authService.bind(this));
-
// Secured API
this.expressApplication.all('/client/api/:action', CentralRestServerAuthentication.authenticate(), CentralRestServerService.restServiceSecured.bind(this));
-
// Util API
this.expressApplication.all('/client/util/:action', CentralRestServerService.restServiceUtil.bind(this));
// Workaround URL encoding issue
@@ -88,7 +83,6 @@ export default class CentralRestServer {
req.query.FileName = 'r7_update_3.3.0.10_d4.epk';
await CentralRestServerService.restServiceUtil(req, res, next);
});
-
// Catchall for util with logging
this.expressApplication.all(['/client/util/*', '/client%2Futil%2F*'], (req: Request, res: Response) => {
Logging.logDebug({
@@ -100,7 +94,6 @@ export default class CentralRestServer {
});
res.sendStatus(HttpStatusCodes.NOT_FOUND);
});
-
// Create HTTP server to serve the express app
CentralRestServer.restHttpServer = expressTools.createHttpServer(CentralRestServer.centralSystemRestConfig, this.expressApplication);
}
@@ -115,7 +108,7 @@ export default class CentralRestServer {
message: logMsg
});
// eslint-disable-next-line no-console
- console.log(logMsg.replace('...', '') + ` ${cluster.isWorker ? 'in worker ' + cluster.worker.id : 'in master'}...`);
+ console.log(`Starting REST SocketIO Server ${cluster.isWorker ? 'in worker ' + cluster.worker.id : 'in master...'}`);
// Init Socket IO
CentralRestServer.socketIOServer = socketio(CentralRestServer.restHttpServer, { pingTimeout: 15000, pingInterval: 30000 });
CentralRestServer.socketIOServer.use((socket: socketio.Socket, next) => {
@@ -134,9 +127,10 @@ export default class CentralRestServer {
decodedPropertyName: 'decoded_token',
}));
// Handle Socket IO connection
- CentralRestServer.socketIOServer.on('connect', (socket: SocketIOJwt) => {
+ CentralRestServer.socketIOServer.on('connection', (socket: SocketIOJwt) => {
const userToken: UserToken = socket.decoded_token;
if (!userToken || !userToken.tenantID) {
+ console.error('SocketIO client is trying to connect without token');
Logging.logWarning({
tenantID: Constants.DEFAULT_TENANT,
module: MODULE_NAME, method: 'startSocketIO',
@@ -146,21 +140,40 @@ export default class CentralRestServer {
});
socket.disconnect(true);
} else {
+ // Join Tenant Room
+ socket.join(userToken.tenantID, (error) => {
+ if (error) {
+ console.error(`${userToken.tenantName} - ${Utils.buildUserFullName(userToken, false)} - SocketIO error when trying to join a room: ${error}`);
+ Logging.logError({
+ tenantID: userToken.tenantID,
+ module: MODULE_NAME, method: 'startSocketIO',
+ action: ServerAction.SOCKET_IO,
+ user: userToken.id,
+ message: `${userToken.tenantName} - ${Utils.buildUserFullName(userToken, false)} - SocketIO error when trying to join a room '${userToken.tenantID}': ${error}`,
+ detailedMessages: { error, socketIOid: socket.id, socketIOHandshake: socket.handshake }
+ });
+ socket.disconnect(true);
+ } else {
+ console.log(`${userToken.tenantName} - ${Utils.buildUserFullName(userToken, false)} - SocketIO client is connected on room '${userToken.tenantID}'`);
Logging.logDebug({
tenantID: userToken.tenantID,
module: MODULE_NAME, method: 'startSocketIO',
action: ServerAction.SOCKET_IO,
- message: 'SocketIO client is connected',
+ user: userToken.id,
+ message: `${userToken.tenantName} - ${Utils.buildUserFullName(userToken, false)} - SocketIO client is connected on room '${userToken.tenantID}'`,
detailedMessages: { socketIOid: socket.id, socketIOHandshake: socket.handshake }
});
- socket.join(userToken.tenantID);
+ }
+ });
// Handle Socket IO disconnection
socket.on('disconnect', (reason: string) => {
+ console.log(`${userToken.tenantName} - ${Utils.buildUserFullName(userToken, false)} - SocketIO client is disconnected: ${reason}`);
Logging.logDebug({
tenantID: userToken.tenantID,
module: MODULE_NAME, method: 'startSocketIO',
action: ServerAction.SOCKET_IO,
- message: `SocketIO client is disconnected: ${reason}`,
+ user: userToken.id,
+ message: `${userToken.tenantName} - ${Utils.buildUserFullName(userToken, false)} - SocketIO client is disconnected: ${reason}`,
detailedMessages: { socketIOid: socket.id, socketIOHandshake: socket.handshake }
});
});
@@ -191,7 +204,7 @@ export default class CentralRestServer {
expressTools.startServer(CentralRestServer.centralSystemRestConfig, CentralRestServer.restHttpServer, 'REST', MODULE_NAME);
}
- public notifyUser(tenantID: string, action: Action, data): void {
+ public notifyUser(tenantID: string, action: Action, data: NotificationData): void {
// On User change rebuild userHashID
if (data && data.id) {
SessionHashService.rebuildUserHashID(tenantID, data.id).catch(() => {});
@@ -211,7 +224,7 @@ export default class CentralRestServer {
});
}
- public notifyTenant(tenantID: string, action: Action, data): void {
+ public notifyTenant(tenantID: string, action: Action, data: NotificationData): void {
// On Tenant change rebuild tenantHashID
if (data && data.id) {
SessionHashService.rebuildTenantHashID(data.id).catch(() => {});
@@ -231,7 +244,7 @@ export default class CentralRestServer {
});
}
- public notifySite(tenantID: string, action: Action, data): void {
+ public notifySite(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -247,7 +260,7 @@ export default class CentralRestServer {
});
}
- public notifySiteArea(tenantID: string, action: Action, data): void {
+ public notifySiteArea(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -263,7 +276,7 @@ export default class CentralRestServer {
});
}
- public notifyCompany(tenantID: string, action: Action, data): void {
+ public notifyCompany(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -279,7 +292,7 @@ export default class CentralRestServer {
});
}
- public notifyAsset(tenantID: string, action: Action, data): void {
+ public notifyAsset(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -295,7 +308,7 @@ export default class CentralRestServer {
});
}
- public notifyTransaction(tenantID: string, action: Action, data): void {
+ public notifyTransaction(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -311,7 +324,7 @@ export default class CentralRestServer {
});
}
- public notifyChargingStation(tenantID: string, action: Action, data): void {
+ public notifyChargingStation(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -336,7 +349,7 @@ export default class CentralRestServer {
});
}
- public notifyRegistrationToken(tenantID: string, action: Action, data): void {
+ public notifyRegistrationToken(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -352,7 +365,7 @@ export default class CentralRestServer {
});
}
- public notifyInvoice(tenantID: string, action: Action, data): void {
+ public notifyInvoice(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -368,7 +381,7 @@ export default class CentralRestServer {
});
}
- public notifyCar(tenantID: string, action: Action, data): void {
+ public notifyCar(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -384,7 +397,7 @@ export default class CentralRestServer {
});
}
- public notifyCarCatalog(tenantID: string, action: Action, data): void {
+ public notifyCarCatalog(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -400,7 +413,7 @@ export default class CentralRestServer {
});
}
- public notifyChargingProfile(tenantID: string, action: Action, data): void {
+ public notifyChargingProfile(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -416,7 +429,7 @@ export default class CentralRestServer {
});
}
- public notifyOcpiEndpoint(tenantID: string, action: Action, data): void {
+ public notifyOcpiEndpoint(tenantID: string, action: Action, data: NotificationData): void {
// Add in buffer
this.addSingleChangeNotificationInBuffer({
'tenantID': tenantID,
@@ -434,43 +447,38 @@ export default class CentralRestServer {
private addChangeNotificationInBuffer(notification: ChangeNotification) {
let dups = false;
- // Add in buffer
+ // Handle dups in buffer
for (const currentNotification of CentralRestServer.changeNotifications) {
- // Same Entity and Action?
+ // Same notification
if (currentNotification.tenantID === notification.tenantID &&
currentNotification.entity === notification.entity &&
currentNotification.action === notification.action) {
- // Yes
dups = true;
break;
}
}
- // Found dups?
if (!dups) {
- // No: Add it
+ // Add it
CentralRestServer.changeNotifications.push(notification);
}
}
private addSingleChangeNotificationInBuffer(notification: SingleChangeNotification) {
let dups = false;
- // Add in buffer
+ // Handle dups in buffer
for (const currentNotification of CentralRestServer.singleChangeNotifications) {
- // Same Entity and Action?
+ // Same notification
if (currentNotification.tenantID === notification.tenantID &&
currentNotification.entity === notification.entity &&
currentNotification.action === notification.action &&
currentNotification.data.id === notification.data.id &&
- currentNotification.data.type === notification.data.type
- ) {
- // Yes
+ currentNotification.data.type === notification.data.type) {
dups = true;
break;
}
}
- // Found dups?
if (!dups) {
- // No: Add it
+ // Add it
CentralRestServer.singleChangeNotifications.push(notification);
}
}

MODIFY
src/types/ChangeNotification.ts
src/types/ChangeNotification.ts
import { Action, Entity } from './Authorization';
+import { Data } from './GlobalType';
export default interface ChangeNotification {
tenantID: string;
entity: Entity;
action: Action;
+ data?: Data;
}

MODIFY
src/types/GlobalType.ts
src/types/GlobalType.ts
@@ -5,6 +5,10 @@ import bluebird from 'bluebird';
import path from 'path';
import Global = NodeJS.Global;
+export interface Data {
+ id: string;
+}
+
export interface KeyValue {
key: string;
value: string;

MODIFY
src/types/SingleChangeNotification.ts
src/types/SingleChangeNotification.ts
import ChangeNotification from './ChangeNotification';
+import { Data } from './GlobalType';
export default interface SingleChangeNotification extends ChangeNotification{
- data: {
+ data: NotificationData;
+}
+
+export interface NotificationData {
id: string;
type: string;
- };
+ data?: Data;
}

MODIFY
src/utils/Utils.ts
src/utils/Utils.ts
@@ -822,7 +822,7 @@ export default class Utils {
return results;
}
- public static buildUserFullName(user: User, withID = true, withEmail = false, invertedName = false): string {
+ public static buildUserFullName(user: User|UserToken, withID = true, withEmail = false, invertedName = false): string {
let fullName: string;
if (!user || !user.name) {
return 'Unknown';
@@ -841,8 +841,8 @@ export default class Utils {
fullName = user.name;
}
}
- if (withID && user.iNumber) {
- fullName += ` (${user.iNumber})`;
+ if (withID && user.id) {
+ fullName += ` (${user.id})`;
}
if (withEmail && user.email) {
fullName += `; ${user.email}`;

Added debug logs, typed notif