RT-Vis 2d and floating widget API collaboration completed, template Save and get API completed. SOCKET 2d panel and widgets events completed
This commit is contained in:
@@ -7,7 +7,8 @@ import { deleteWallItems, setWallItems } from '../services/assets/wallitem-Contr
|
||||
import { deleteLineItems, deleteLinPoiteItems, updateLineItems ,createLineItems, deleteLayer} from '../services/lines/line-Controller.ts';
|
||||
import { activeUserOffline, activeUsers } from '../services/users/user-controller.ts';
|
||||
import { deleteZone, setZone } from '../services/lines/zone-controller.ts';
|
||||
import { addPanel } from '../services/visualization/panel-Services.ts';
|
||||
import { addPanel, panelDelete } from '../services/visualization/panel-Services.ts';
|
||||
import { addWidget, Widgetdelete } from '../services/visualization/widget-Services.ts';
|
||||
|
||||
|
||||
|
||||
@@ -432,7 +433,7 @@ const panelHandleEvent=async(event: string, socket: Socket, data: any,namespace:
|
||||
|
||||
let result;
|
||||
switch (event) {
|
||||
case EVENTS.addPanel:
|
||||
case EVENTS.addPanel:{
|
||||
result = await addPanel(data);
|
||||
console.log('result: ', result);
|
||||
|
||||
@@ -443,7 +444,7 @@ const panelHandleEvent=async(event: string, socket: Socket, data: any,namespace:
|
||||
const organization=result?.organization
|
||||
console.log('organization: ', organization);
|
||||
// const emitTarget = notifySender ? socket.in(organization) : socket.to(organization);
|
||||
console.log(`👀 Active sockets in room:`, namespace.adapter.rooms.get(organization));
|
||||
// console.log(`👀 Active sockets in room:`, namespace.adapter.rooms.get(organization));
|
||||
// console.log('emitTarget: ', emitTarget);
|
||||
|
||||
socket.to(organization).emit(responseEvent, {
|
||||
@@ -455,7 +456,34 @@ const panelHandleEvent=async(event: string, socket: Socket, data: any,namespace:
|
||||
organization: result.organization,
|
||||
});
|
||||
}
|
||||
break;
|
||||
break;}
|
||||
case EVENTS.deletePanel: {
|
||||
const result = await panelDelete(data)
|
||||
if (result) {
|
||||
// console.log('result?.success: ', result.organization);
|
||||
const responseEvent = EVENTS.PanelDeleteRespones
|
||||
// console.log('responseEvent: ', responseEvent);
|
||||
const organization = result?.organization
|
||||
// console.log('organization: ', organization);
|
||||
// const emitTarget = notifySender ? socket.in(organization) : socket.to(organization);
|
||||
// console.log(`👀 Active sockets in room:`, namespace.adapter.rooms.get(organization));
|
||||
// console.log('emitTarget: ', emitTarget);
|
||||
if (organization) {
|
||||
socket.emit(responseEvent, {
|
||||
success: result.success,
|
||||
message: result.message,
|
||||
data: result.data,
|
||||
error: result.error || null,
|
||||
socketId: socket.id,
|
||||
organization,
|
||||
});
|
||||
} else {
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
default:
|
||||
return;
|
||||
@@ -463,6 +491,77 @@ const panelHandleEvent=async(event: string, socket: Socket, data: any,namespace:
|
||||
|
||||
|
||||
}
|
||||
const widgetHandleEvent = async (event: string, socket: Socket, data: any, namespace: any, notifySender: boolean = false) => {
|
||||
// console.log('data: ', data);
|
||||
if (!data?.organization) {
|
||||
console.warn(`Missing organization for event: ${event}`);
|
||||
return;
|
||||
}
|
||||
|
||||
let result;
|
||||
switch (event) {
|
||||
case EVENTS.addWidget: {
|
||||
result = await addWidget(data);
|
||||
// console.log('result: ', result);
|
||||
|
||||
if (result) {
|
||||
// console.log('result?.success: ', result.organization);
|
||||
const responseEvent = EVENTS.widgetUpdateRespones
|
||||
// console.log('responseEvent: ', responseEvent);
|
||||
const organization = result?.organization
|
||||
// console.log('organization: ', organization);
|
||||
// const emitTarget = notifySender ? socket.in(organization) : socket.to(organization);
|
||||
// console.log(`👀 Active sockets in room:`, namespace.adapter.rooms.get(organization));
|
||||
// console.log('emitTarget: ', emitTarget);
|
||||
if (organization) {
|
||||
socket.to(organization).emit(responseEvent, {
|
||||
success: result.success,
|
||||
message: result.message,
|
||||
data: result.data,
|
||||
error: result.error || null,
|
||||
socketId: socket.id,
|
||||
organization,
|
||||
});
|
||||
} else {
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
case EVENTS.deleteWidget: {
|
||||
const result = await Widgetdelete(data)
|
||||
if (result) {
|
||||
// console.log('result?.success: ', result.organization);
|
||||
const responseEvent = EVENTS.widgetDeleteRespones
|
||||
// console.log('responseEvent: ', responseEvent);
|
||||
const organization = result?.organization
|
||||
// console.log('organization: ', organization);
|
||||
// const emitTarget = notifySender ? socket.in(organization) : socket.to(organization);
|
||||
// console.log(`👀 Active sockets in room:`, namespace.adapter.rooms.get(organization));
|
||||
// console.log('emitTarget: ', emitTarget);
|
||||
if (organization) {
|
||||
socket.emit(responseEvent, {
|
||||
success: result.success,
|
||||
message: result.message,
|
||||
data: result.data,
|
||||
error: result.error || null,
|
||||
socketId: socket.id,
|
||||
organization,
|
||||
});
|
||||
} else {
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export const initSocketServer = (httpServer: any) => {
|
||||
const io = new Server(httpServer, {
|
||||
cors: {
|
||||
@@ -497,40 +596,42 @@ userStatus(EVENTS.connection, socket, socket.handshake.auth,io);
|
||||
});
|
||||
});
|
||||
|
||||
// 🔹 Create different namespaces
|
||||
const namespaces = {
|
||||
camera: io.of("/camera"),
|
||||
environment: io.of("/environment"),
|
||||
floorItems: io.of("/floorItems"),
|
||||
wallItems: io.of("/wallItems"),
|
||||
line: io.of("/line"),
|
||||
zone: io.of("/zone"),
|
||||
panel:io.of('/panel')
|
||||
};
|
||||
// 🔹 Create different namespaces
|
||||
const namespaces = {
|
||||
camera: io.of("/camera"),
|
||||
environment: io.of("/environment"),
|
||||
floorItems: io.of("/floorItems"),
|
||||
wallItems: io.of("/wallItems"),
|
||||
line: io.of("/line"),
|
||||
zone: io.of("/zone"),
|
||||
visualization: io.of('/visualization'),
|
||||
// widget:io.of('/widget')
|
||||
};
|
||||
// 🔹 Function to handle connections in a namespace
|
||||
const handleNamespace = (namespaceName: string, namespace: any, eventHandler: Function) => {
|
||||
const handleNamespace = (namespaceName: string, namespace: any, ...eventHandlers: Function[]) => {
|
||||
namespace.on("connection", (socket: Socket) => {
|
||||
console.log(`✅ Client connected to ${namespaceName}: ${socket.id}`);
|
||||
|
||||
|
||||
// Extract organization from query parameters
|
||||
const organization = socket.handshake.query.organization as string;
|
||||
console.log(`🔍 Received organization: ${organization}`);
|
||||
|
||||
// console.log(`🔍 Received organization: ${organization}`);
|
||||
|
||||
if (organization) {
|
||||
socket.join(organization);
|
||||
console.log(`🔹 Socket ${socket.id} joined room: ${organization}`);
|
||||
|
||||
// console.log(`🔹 Socket ${socket.id} joined room: ${organization}`);
|
||||
|
||||
// Debug: Check rooms
|
||||
console.log(`🛠️ Current rooms for ${socket.id}:`, socket.rooms);
|
||||
// console.log(`🛠️ Current rooms for ${socket.id}:`, socket.rooms);
|
||||
} else {
|
||||
console.warn(`⚠️ Warning: Socket ${socket.id} did not provide an organization`);
|
||||
}
|
||||
|
||||
|
||||
socket.onAny((event: string, data: any) => {
|
||||
console.log(`📩 Event received: ${event}, Data: ${JSON.stringify(data)}`);
|
||||
eventHandler(event, socket, data, namespace); // Pass `namespace` instead of `io`
|
||||
// console.log(`📩 Event received: ${event}, Data: ${JSON.stringify(data)}`);
|
||||
eventHandlers.forEach(handler => handler(event, socket, data, namespace));
|
||||
// eventHandler(event, socket, data, namespace); // Pass `namespace` instead of `io`
|
||||
});
|
||||
|
||||
|
||||
socket.on("disconnect", (reason: string) => {
|
||||
console.log(`❌ Client disconnected from ${namespaceName}: ${socket.id}, Reason: ${reason}`);
|
||||
});
|
||||
@@ -544,21 +645,10 @@ userStatus(EVENTS.connection, socket, socket.handshake.auth,io);
|
||||
handleNamespace("wallItems", namespaces.wallItems, wallItemsHandleEvent);
|
||||
handleNamespace("line", namespaces.line, lineHandleEvent);
|
||||
handleNamespace("zone", namespaces.zone, zoneHandleEvent);
|
||||
handleNamespace("panel", namespaces.panel, panelHandleEvent);
|
||||
// 📌 **Create a separate namespace for panels**
|
||||
// const panelNamespace = io.of('/panel');
|
||||
// handleNamespace("visualization", namespaces.panel, panelHandleEvent);
|
||||
// handleNamespace("widget", namespaces.visualization, widgetHandleEvent);
|
||||
handleNamespace("visualization", namespaces.visualization, panelHandleEvent, widgetHandleEvent);
|
||||
|
||||
// panelNamespace.on(EVENTS.connection, (socket: Socket) => {
|
||||
// console.log(`New client connected to /panel namespace: ${socket.id}`);
|
||||
|
||||
// socket.onAny((event: string, data: any) => {
|
||||
// panelHandleEvent(event, socket, data);
|
||||
// });
|
||||
|
||||
// socket.on(EVENTS.disconnect, (reason: string) => {
|
||||
// console.log(`Client disconnected from /panel namespace: ${socket.id}, Reason: ${reason}`);
|
||||
// });
|
||||
// })
|
||||
|
||||
return io;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user