Files
Dwinzo-Backend-V0.0/src/socket-server/socket/socketManager.ts

565 lines
18 KiB
TypeScript
Raw Normal View History

2025-01-30 12:44:04 +05:30
import { Server, Socket } from 'socket.io';
2025-03-28 12:45:59 +05:30
import { EVENTS } from './events.ts';
import { createCamera } from '../services/camera/camera-Controller.ts';
import { setEnvironment } from '../services/environments/environments-controller.ts';
import { deleteFloorItems, setFloorItems } from '../services/assets/flooritem-Controller.ts';
import { deleteWallItems, setWallItems } from '../services/assets/wallitem-Controller.ts';
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';
2025-03-28 18:53:20 +05:30
import { addPanel } from '../services/visualization/panel-Services.ts';
2025-01-30 12:44:04 +05:30
const cameraHandleEvent =async (event: string, socket: Socket, data: any,io:any) => {
switch (event) {
case EVENTS.setCamera:
const result = await createCamera(data,);
// console.log('result: ', result);
if (result.success) {
// console.log('result.success: ', result.success);
// if (result.message === 'Camera updated') {
// Emit update response
io.emit(EVENTS.cameraUpdateResponse, {
success: true,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});
} else if (result.message === 'Camera created') {
// Emit create response
io.emit(EVENTS.cameraCreateResponse, {
success: true,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});
// }
} else {
// Emit error response
socket.emit(EVENTS.cameraError, {
success: false,
message: result.message,
error: result.error,
socketId: socket.id,
organization:result.organization
});
}
break;
// case EVENTS.updataControlle_iot:
// updateControlle(data);
break;
// case EVENTS.deleteControlle_iot:
// deleteControlle(data);
break;
default:
// console.error(`Unhandled event type: ${event}`);
}
}
const EnvironmentHandleEvent =async (event: string, socket: Socket, data: any,io:any) => {
switch (event) {
case EVENTS.setenvironment:
const result = await setEnvironment(data,);
// console.log('result: ', result);
if (result.success) {
// if (result.message === 'Camera updated') {
// Emit update response
io.emit(EVENTS.EnvironmentUpdateResponse, {
success: true,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});
// } else if (result.message === 'evironments created') {
// // Emit create response
// io.emit(EVENTS.cameraCreateResponse, {
// success: true,
// message: result.message,
// data: result.data,
// });
// }
} else {
// Emit error response
socket.emit(EVENTS.cameraError, {
success: false,
message: result.message,
error: result.error,
socketId: socket.id,
organization:result.organization
});
}
break;
// case EVENTS.updataControlle_iot:
// updateControlle(data);
break;
// case EVENTS.deleteControlle_iot:
// deleteControlle(data);
break;
default:
// console.error(`Unhandled event type: ${event}`);
}
}
const floorItemsHandleEvent =async (event: string, socket: Socket, data: any,io:any) => {
switch (event) {
case EVENTS.setFloorItems:{
const result = await setFloorItems(data);
// console.log('result: ', result);
if (result.success) {
io.emit(EVENTS.FloorItemsUpdateResponse, {
success: true || false,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});
} else {
// Emit error response
socket.emit(EVENTS.floorItemError, {
success: false,
message: result.message,
error: result.error,
socketId: socket.id,
organization:result.organization
});
}
break;}
case EVENTS.deleteFloorItems:{
const result = await deleteFloorItems(data);
// console.log('result: ', result);
if (result.success) {
io.emit(EVENTS.FloorItemsDeleteResponse, {
success: true || false,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});
} else {
// Emit error response
socket.emit(EVENTS.floorItemError, {
success: false,
message: result.message,
error: result.error,
socketId: socket.id,
organization:result.organization
});
}
break;}
default:
// console.error(`Unhandled event type: ${event}`);
}
}
const wallItemsHandleEvent =async (event: string, socket: Socket, data: any,io:any) => {
switch (event) {
case EVENTS.setWallItems:{
const result = await setWallItems(data);
// console.log('result: ', result);
if (result.success) {
io.emit(EVENTS.wallItemsUpdateResponse, {
success: true,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});
} else {
// Emit error response
socket.emit(EVENTS.wallItemError, {
success: false,
message: result.message,
error: result.error,
});
}
break;
}
case EVENTS.deleteWallItems:{
const result = await deleteWallItems(data);
// console.log('result: ', result);
if (result.success) {
io.emit(EVENTS.wallItemsDeleteResponse, {
success: true || false,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});
} else {
// Emit error response
socket.emit(EVENTS.wallItemError, {
success: false,
message: result.message,
error: result.error,
});
}
break;
}
default:
// console.error(`Unhandled event type: ${event}`);
}
}
const lineHandleEvent =async (event: string, socket: Socket, data: any,io:any) => {
switch (event) {
case EVENTS.createLine:{
const result = await createLineItems(data);
// console.log('result: ', result);
if (result.success) {
io.emit(EVENTS.createLineResponse, {
success: true || false,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});
} else {
// Emit error response
// socket.emit(EVENTS.wallItemError, {
// success: false,
// message: result.message,
// error: result.error,
// });
}
break;
}
case EVENTS.updateLine: {
const result = await updateLineItems(data);
// console.log('result: ', result);
if (result.success) {
io.emit(EVENTS.updateLineResponse, {
success: true || false,
message: result.message,
data: result.data,
socketId: socket.id,
organization: result.organization
});
}
break;
}
case EVENTS.deleteLine:{
const result = await deleteLineItems(data);
// console.log('result: ', result);
if (result.success) {
io.emit(EVENTS.deleteLineResponse, {
success: true || false,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});
} else {
// Emit error response
// socket.emit(EVENTS.wallItemError, {
// success: false,
// message: result.message,
// error: result.error,
// });
}
break;
}
case EVENTS.deletePoint:{
const result = await deleteLinPoiteItems(data);
// console.log('result: ', result);
if (result.success) {
io.emit(EVENTS.deletePointResponse, {
success: true || false,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});
} else {
// Emit error response
// socket.emit(EVENTS.wallItemError, {
// success: false,
// message: result.message,
// error: result.error,
// });
}
break;
}
case EVENTS.deleteLineLayer:{
const result = await deleteLayer(data);
// console.log('result: ', result);
if (result.success) {
io.emit(EVENTS.deleteLineLayerResponse, {
success: true || false,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});
} else {
// Emit error response
// socket.emit(EVENTS.wallItemError, {
// success: false,
// message: result.message,
// error: result.error,
// });
}
break;
}
default:
// console.error(`Unhandled event type: ${event}`);
}
}
const userStatus =async (event: string, socket: Socket, data: any,io:any) => {
switch (event) {
case EVENTS.connection: {
// console.log('EVENTS.connection: ', EVENTS.connection);
// console.log('event: ', event);
const result = await activeUsers(data);
if (result?.success) {
// console.log('result.success: ', result.success)
io.emit(EVENTS.userConnect, {
success: true || false,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});}
break;
}
case EVENTS.disconnect: {
// console.log('EVENTS.disconnect: ', EVENTS.connection);
// console.log('event: ', event);
const result = await activeUserOffline(data);
if (result?.success) {
// console.log('result.success: ', result.success)
io.emit(EVENTS.userDisConnect, {
success: true || false,
message: result.message,
data: result.data,
socketId: socket.id,
organization:result.organization
});}
break;
}
}
}
2025-03-18 17:46:24 +05:30
const zoneHandleEvent =async (event: string, socket: Socket, data: any,io:any)=>{
switch (event) {
case EVENTS.setZone:{
const result=await setZone(data)
if (result?.success) {
io.emit(EVENTS.zoneUpdateRespones, {
success: true || false,
message: result.message,
data: result.data,
error: result.error,
socketId: socket.id,
organization:result.organization
});
}
break}
case EVENTS.deleteZone:{
const result=await deleteZone(data)
if (result?.success) {
io.emit(EVENTS.ZoneDeleteRespones, {
success: true || false,
message: result.message,
data: result.data,
error: result.error,
socketId: socket.id,
organization:result.organization
});
}
break}
default:
break;
}
}
2025-03-28 18:53:20 +05:30
const panelHandleEvent=async(event: string, socket: Socket, data: any,namespace: any, notifySender: boolean = false)=>{
if (!data?.organization) {
console.warn(`Missing organization for event: ${event}`);
return;
}
let result;
switch (event) {
case EVENTS.addPanel:
result = await addPanel(data);
console.log('result: ', result);
if (result) {
console.log('result?.success: ', result.organization);
const responseEvent = EVENTS.panelUpdateRespones
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);
socket.to(organization).emit(responseEvent, {
success: result.success,
message: result.message,
data: result.data,
error: result.error,
socketId: socket.id,
organization: result.organization,
});
}
break;
default:
return;
}
}
2025-01-30 12:44:04 +05:30
export const initSocketServer = (httpServer: any) => {
const io = new Server(httpServer, {
cors: {
origin: '*', // Allow CORS for all origins (adjust in production)
methods: ['GET', 'POST'],
},
});
2025-03-28 18:53:20 +05:30
2025-01-30 12:44:04 +05:30
// Listen for new connections
io.on(EVENTS.connection, (socket: Socket) => {
// console.log(`New client connected: ${socket.id}`);
// console.log(socket.handshake.auth);
userStatus(EVENTS.connection, socket, socket.handshake.auth,io);
// Handle all incoming events with the handleEvent function
socket.onAny((event: string, data: any,) => {
cameraHandleEvent(event, socket, data,io);
EnvironmentHandleEvent(event, socket, data,io);
floorItemsHandleEvent(event, socket, data,io);
wallItemsHandleEvent(event, socket, data,io);
lineHandleEvent(event, socket, data,io);
2025-03-18 17:46:24 +05:30
zoneHandleEvent(event, socket, data,io);
2025-01-30 12:44:04 +05:30
});
socket.on(EVENTS.disconnect, (reason: string) => {
// console.log(`Client disconnected: ${socket.id}, Reason: ${reason}`);
// console.log(socket.handshake.auth);
userStatus(EVENTS.disconnect, socket, socket.handshake.auth,io);
// Perform cleanup or other necessary actions
});
});
2025-03-28 18:53:20 +05:30
// 🔹 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')
};
// 🔹 Function to handle connections in a namespace
const handleNamespace = (namespaceName: string, namespace: any, eventHandler: 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}`);
if (organization) {
socket.join(organization);
console.log(`🔹 Socket ${socket.id} joined room: ${organization}`);
// Debug: Check 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`
});
socket.on("disconnect", (reason: string) => {
console.log(`❌ Client disconnected from ${namespaceName}: ${socket.id}, Reason: ${reason}`);
});
});
};
// 🔹 Apply namespace handlers
handleNamespace("camera", namespaces.camera, cameraHandleEvent);
handleNamespace("environment", namespaces.environment, EnvironmentHandleEvent);
handleNamespace("floorItems", namespaces.floorItems, floorItemsHandleEvent);
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');
// 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}`);
// });
// })
2025-01-30 12:44:04 +05:30
return io;
};