973 lines
36 KiB
TypeScript
973 lines
36 KiB
TypeScript
import { Server, Socket } from 'socket.io';
|
|
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';
|
|
import { addPanel, panelDelete } from '../services/visualization/panel-Services.ts';
|
|
import { addWidget, Widgetdelete } from '../services/visualization/widget-Services.ts';
|
|
import { addfloat } from '../services/visualization/floatWidgetService.ts';
|
|
import { addTemplate } from '../services/visualization/templateServices.ts';
|
|
import { deleteAssetModel, setAssetModel } from '../services/assets/asset-Controller.ts';
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
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.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
|
|
// }
|
|
|
|
|
|
}
|
|
}
|
|
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);
|
|
|
|
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,
|
|
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;
|
|
}
|
|
|
|
|
|
}
|
|
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) => {
|
|
if (!data?.organization) {
|
|
console.warn(`Missing organization for event: ${event}`);
|
|
return;
|
|
}
|
|
|
|
let result;
|
|
switch (event) {
|
|
case EVENTS.setWallItems: {
|
|
result = await setWallItems(data);
|
|
|
|
if (result) {
|
|
const responseEvent = EVENTS.wallItemsUpdateResponse
|
|
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.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.deleteWallItems: {
|
|
const result = await deleteWallItems(data)
|
|
if (result) {
|
|
// console.log('result?.success: ', result.organization);
|
|
const responseEvent = EVENTS.wallItemsDeleteResponse
|
|
// 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
|
|
}
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
}
|
|
const lineHandleEvent =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.createLine: {
|
|
result = await createLineItems(data);
|
|
|
|
if (result) {
|
|
const responseEvent = EVENTS.createLineResponse
|
|
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.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.updateLine: {
|
|
const result = await updateLineItems(data)
|
|
if (result) {
|
|
// console.log('result?.success: ', result.organization);
|
|
const responseEvent = EVENTS.updateLineResponse
|
|
// 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.deleteLine: {
|
|
const result = await deleteLineItems(data)
|
|
if (result) {
|
|
// console.log('result?.success: ', result.organization);
|
|
const responseEvent = EVENTS.deleteLineResponse
|
|
// 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.deletePoint: {
|
|
const result = await deleteLinPoiteItems(data)
|
|
if (result) {
|
|
// console.log('result?.success: ', result.organization);
|
|
const responseEvent = EVENTS.deletePointResponse
|
|
// 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
|
|
}
|
|
default:
|
|
return;
|
|
}
|
|
|
|
|
|
}
|
|
const userStatus =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.connection: {
|
|
result = await activeUsers(data);
|
|
// console.log('result: ', result);
|
|
|
|
if (result) {
|
|
const responseEvent = EVENTS.userConnect
|
|
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.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.disconnect: {
|
|
const result = await activeUserOffline(data)
|
|
if (result) {
|
|
// console.log('result?.success: ', result.organization);
|
|
const responseEvent = EVENTS.userDisConnect
|
|
// 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 || "disconnectError",
|
|
socketId: socket.id,
|
|
organization,
|
|
});
|
|
} else {
|
|
console.warn(`Organization missing in response for event: ${event}`);
|
|
}
|
|
}
|
|
break
|
|
}
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
}
|
|
const zoneHandleEvent = 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.setZone: {
|
|
result = await setZone(data);
|
|
|
|
if (result) {
|
|
const responseEvent = EVENTS.zoneUpdateRespones
|
|
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.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.deleteZone: {
|
|
const result = await deleteZone(data)
|
|
if (result) {
|
|
// console.log('result?.success: ', result.organization);
|
|
const responseEvent = EVENTS.ZoneDeleteRespones
|
|
// 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
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
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);
|
|
if (result) {
|
|
const responseEvent = EVENTS.panelUpdateRespones
|
|
const organization = result?.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;
|
|
}
|
|
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;
|
|
}
|
|
|
|
|
|
}
|
|
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;
|
|
}
|
|
|
|
}
|
|
const floatHandleEvent = 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.addFloat: {
|
|
result = await addfloat(data);
|
|
// console.log('result: ', result);
|
|
|
|
if (result) {
|
|
// console.log('result?.success: ', result.organization);
|
|
const responseEvent = EVENTS.floatUpdateRespones
|
|
// 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
|
|
}
|
|
// 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;
|
|
}
|
|
|
|
}
|
|
const templateHandleEvent = 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.addTemplate: {
|
|
result = await addTemplate(data);
|
|
|
|
if (result) {
|
|
const responseEvent = EVENTS.templateUpdateRespones
|
|
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.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;
|
|
}
|
|
|
|
}
|
|
const modelAssetHandleEvent = 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.setAssetModel: {
|
|
result = await setAssetModel(data);
|
|
|
|
if (result) {
|
|
const responseEvent = EVENTS.assetUpdateRespones
|
|
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.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.deleteAssetModel: {
|
|
const result = await deleteAssetModel(data)
|
|
if (result) {
|
|
// console.log('result?.success: ', result.organization);
|
|
const responseEvent = EVENTS.assetDeleteRespones
|
|
// 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
|
|
}
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
}
|
|
|
|
export const initSocketServer = (httpServer: any) => {
|
|
const io = new Server(httpServer, {
|
|
cors: {
|
|
origin: '*', // Allow CORS for all origins (adjust in production)
|
|
methods: ['GET', 'POST'],
|
|
},
|
|
});
|
|
|
|
|
|
// 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);
|
|
zoneHandleEvent(event, socket, data, io);
|
|
|
|
|
|
});
|
|
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
|
|
});
|
|
});
|
|
// 🔹 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}`);
|
|
// });
|
|
// });
|
|
// };
|
|
const onlineUsers = new Map<string, Set<string>>();
|
|
|
|
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,email} = socket.handshake.auth
|
|
console.log(`🔍 Received organization: ${organization}`);
|
|
|
|
if (organization) {
|
|
socket.join(organization);
|
|
// console.log(`🔹 Socket ${socket.id} joined room: ${organization}`);
|
|
} else {
|
|
// console.warn(`⚠️ Warning: Socket ${socket.id} did not provide an organization`);
|
|
}
|
|
// addUserToOnlineList(organization, socket.id, onlineUsers, socket, namespaceName);
|
|
// Handle all events
|
|
userStatus(EVENTS.connection, socket, socket.handshake.auth, socket);
|
|
|
|
socket.onAny((event: string, data: any) => {
|
|
eventHandlers.forEach(handler => handler(event, socket, data, namespace,io));
|
|
});
|
|
|
|
// Handle disconnection
|
|
socket.on("disconnect", (reason: string) => {
|
|
userStatus(EVENTS.disconnect, socket, socket.handshake.auth, socket);
|
|
|
|
// console.log(`❌ Client disconnected from ${namespaceName}: ${socket.id}, Reason: ${reason}`);
|
|
});
|
|
|
|
// Handle reconnection (Auto rejoin)
|
|
socket.on("reconnect", (attempt: number) => {
|
|
// console.log(`🔄 Socket ${socket.id} reconnected to ${namespaceName} (Attempt ${attempt})`);
|
|
if (organization) {
|
|
socket.join(organization);
|
|
// console.log(`♻️ Rejoined room: ${organization}`);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
|
|
// 🔹 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, userStatus,modelAssetHandleEvent, cameraHandleEvent, EnvironmentHandleEvent, wallItemsHandleEvent, lineHandleEvent,zoneHandleEvent);
|
|
handleNamespace("visualization", namespaces.visualization, panelHandleEvent, widgetHandleEvent, floatHandleEvent, templateHandleEvent);
|
|
|
|
return io;
|
|
};
|