tessting branch-1
This commit is contained in:
@@ -12,104 +12,130 @@ import { deleteZone, setZone } from '../services/lines/zone-controller';
|
||||
|
||||
|
||||
|
||||
const cameraHandleEvent =async (event: string, socket: Socket, data: any,io:any) => {
|
||||
const cameraHandleEvent = 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.setCamera: {
|
||||
result = await createCamera(data);
|
||||
|
||||
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,
|
||||
if (result) {
|
||||
const responseEvent = EVENTS.cameraUpdateResponse
|
||||
const organization = result?.organization
|
||||
// const emitTarget = notifySender ? socket.in(organization) : socket.to(organization);
|
||||
// console.log(`👀 Active sockets in room:`, namespace.adapter.rooms.get(organization));
|
||||
if (organization) {
|
||||
socket.emit(responseEvent, {
|
||||
success: result.success,
|
||||
message: result.message,
|
||||
error: result.error,
|
||||
socketId: socket.id,
|
||||
organization:result.organization
|
||||
});
|
||||
data: result.data,
|
||||
error: result.error || null,
|
||||
socketId: socket.id,
|
||||
organization,
|
||||
});
|
||||
} else {
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
break;
|
||||
// case EVENTS.updataControlle_iot:
|
||||
// updateControlle(data);
|
||||
break;
|
||||
// case EVENTS.deleteControlle_iot:
|
||||
// deleteControlle(data);
|
||||
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:
|
||||
// console.error(`Unhandled event type: ${event}`);
|
||||
}
|
||||
}
|
||||
const EnvironmentHandleEvent =async (event: string, socket: Socket, data: any,io:any) => {
|
||||
if (!data?.organization) {
|
||||
console.warn(`Missing organization for event: ${event}`);
|
||||
return;
|
||||
}
|
||||
|
||||
let result;
|
||||
switch (event) {
|
||||
case EVENTS.setenvironment: {
|
||||
result = await setEnvironment(data);
|
||||
|
||||
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,
|
||||
if (result) {
|
||||
const responseEvent = EVENTS.EnvironmentUpdateResponse
|
||||
const organization = result?.organization
|
||||
// const emitTarget = notifySender ? socket.in(organization) : socket.to(organization);
|
||||
// console.log(`👀 Active sockets in room:`, namespace.adapter.rooms.get(organization));
|
||||
if (organization) {
|
||||
socket.emit(responseEvent, {
|
||||
success: result.success,
|
||||
message: result.message,
|
||||
error: result.error,
|
||||
socketId: socket.id,
|
||||
organization:result.organization
|
||||
});
|
||||
data: result.data,
|
||||
error: result.error || null,
|
||||
socketId: socket.id,
|
||||
organization,
|
||||
});
|
||||
} else {
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
break;
|
||||
// case EVENTS.updataControlle_iot:
|
||||
// updateControlle(data);
|
||||
break;
|
||||
// case EVENTS.deleteControlle_iot:
|
||||
// deleteControlle(data);
|
||||
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:
|
||||
// console.error(`Unhandled event type: ${event}`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
const floorItemsHandleEvent =async (event: string, socket: Socket, data: any,io:any) => {
|
||||
switch (event) {
|
||||
@@ -457,5 +483,62 @@ 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"),
|
||||
Builder: io.of('/Builder'),
|
||||
visualization: io.of('/visualization'),
|
||||
// widget:io.of('/widget')
|
||||
};
|
||||
// 🔹 Function to handle connections in a namespace
|
||||
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;
|
||||
const organization = socket.handshake.auth.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)}`);
|
||||
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}`);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// 🔹 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("visualization", namespaces.panel, panelHandleEvent);
|
||||
// handleNamespace("widget", namespaces.visualization, widgetHandleEvent);
|
||||
// handleNamespace("Builder", namespaces.Builder, modelAssetHandleEvent,cameraHandleEvent,EnvironmentHandleEvent,wallItemsHandleEvent,lineHandleEvent);
|
||||
// handleNamespace("visualization", namespaces.visualization, panelHandleEvent, widgetHandleEvent,floatHandleEvent,templateHandleEvent);
|
||||
|
||||
|
||||
return io;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user