assetmodel creation and adding for namespace socket
This commit is contained in:
@@ -33,8 +33,13 @@ export interface assetData extends Document {
|
||||
];
|
||||
}[];
|
||||
}[];
|
||||
assetPosition: number[];
|
||||
assetRotation: number[];
|
||||
position: [];
|
||||
// rotation: [];
|
||||
rotation: {
|
||||
x: number;
|
||||
y: number;
|
||||
z: number;
|
||||
};
|
||||
speed: number | string;
|
||||
}
|
||||
|
||||
@@ -66,16 +71,16 @@ const assetDataSchema: Schema = new Schema({
|
||||
},
|
||||
},
|
||||
],
|
||||
assetPosition: { type: [Number] },
|
||||
assetRotation: { type: [Number] },
|
||||
position: { type: Array},
|
||||
// rotation: { type: Array},
|
||||
rotation: {
|
||||
x: { type: Number },
|
||||
y: { type: Number },
|
||||
z: { type: Number },
|
||||
},
|
||||
speed: { type: Schema.Types.Mixed },
|
||||
isLocked: { type: Boolean },
|
||||
isVisible: { type: Boolean },
|
||||
// rotation: {
|
||||
// x: { type: Number },
|
||||
// y: { type: Number },
|
||||
// z: { type: Number },
|
||||
// },
|
||||
});
|
||||
|
||||
// export default floorItemsModel;
|
||||
|
||||
@@ -3,10 +3,10 @@ import actionModel from "../../../shared/model/simulation/actionmodel.ts";
|
||||
import triggerModel from "../../../shared/model/simulation/triggersmodel.ts";
|
||||
|
||||
export const setAssetModel = async (data: any) => {
|
||||
const {modeluuid, modelname,assetPosition, eventData,modelfileID,assetRotation,isLocked,isVisible,organization, }= data
|
||||
const {modeluuid, modelname,position,rotation, eventData,modelfileID,isLocked,isVisible,organization, }= data
|
||||
console.log('data: ', data);
|
||||
// const points=eventData.points
|
||||
// const speed=eventData.speed
|
||||
// const position=data.position
|
||||
// const rotation=data.rotation
|
||||
try {
|
||||
const findvalue = await assetModel(organization).findOne({
|
||||
modeluuid: modeluuid,
|
||||
@@ -14,24 +14,26 @@ export const setAssetModel = async (data: any) => {
|
||||
});
|
||||
|
||||
if (findvalue) {
|
||||
console.log('findvalue: ', findvalue);
|
||||
const updatevalue = await assetModel(organization).findOneAndUpdate(
|
||||
{ modeluuid: modeluuid, modelname: modelname },
|
||||
{
|
||||
assetPosition: assetPosition,
|
||||
assetRotation: assetRotation,
|
||||
position: position,
|
||||
rotation: rotation,
|
||||
isVisible: isVisible,
|
||||
isLocked: isLocked,
|
||||
},
|
||||
{ new: true }
|
||||
);
|
||||
return { success: true, message: 'Model updated', data: updatevalue, organization: organization }
|
||||
console.log('updatevalue: ', updatevalue);
|
||||
return { success: true, message: 'Model updated successfully', data: updatevalue, organization: organization }
|
||||
} else {
|
||||
let assetData: any = {
|
||||
modeluuid,
|
||||
modelname,
|
||||
assetPosition,
|
||||
position,
|
||||
modelfileID,
|
||||
assetRotation,
|
||||
rotation,
|
||||
isLocked,
|
||||
isVisible,
|
||||
};
|
||||
@@ -93,10 +95,33 @@ export const setAssetModel = async (data: any) => {
|
||||
const assetDoc = await assetModel(organization).create(assetData);
|
||||
await assetDoc.save();
|
||||
// await assetDoc.save();
|
||||
return { success: true, message:"Model stored successfully", data: assetDoc, organization: organization }
|
||||
// if(assetDoc.)
|
||||
const assetDatas={
|
||||
modeluuid:assetDoc.modeluuid,modelname:assetDoc.modelname,modelfileID:assetDoc.modelfileID,position:assetDoc.position,rotation:assetDoc.rotation,isLocked:assetDoc.isLocked,isVisible:assetDoc.isVisible
|
||||
,eventData:{points:assetDoc.points,type:assetDoc.type,speed:assetDoc.speed}
|
||||
}
|
||||
return { success: true, message:"Model created successfully", data: assetDatas, organization: organization }
|
||||
}
|
||||
} catch (error:any) {
|
||||
console.error("Error creating flooritems:", error);
|
||||
// console.error("Error creating flooritems:", error);
|
||||
return { success: false, message: error?.message || "Error occurred while ModelAsset", error, organization: organization }
|
||||
}
|
||||
}
|
||||
export const deleteAssetModel = async (data: any)=>{
|
||||
const { modeluuid,modelname,organization } = data;
|
||||
try {
|
||||
|
||||
const findValue = await assetModel(organization).findOneAndDelete({modeluuid:modeluuid,modelname:modelname})
|
||||
if (!findValue) {
|
||||
return { success: false, message: 'model not found',organization:organization }
|
||||
|
||||
} else {
|
||||
|
||||
return { success: true, message: 'Model deleted successfully', data: findValue,organization:organization }
|
||||
}
|
||||
} catch (error) {
|
||||
// console.error('Error get flooritems:', error);
|
||||
return { success: false, message: 'Failed to delete asset', error,organization:organization }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import cameraModel from "../../../shared/model/camera/camera-Model.ts";
|
||||
|
||||
export const createCamera = async (data: any,) => {
|
||||
const { userId, position, target, organization,rotation } = data
|
||||
console.log('data: ', data);
|
||||
try {
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import zoneModel from "../../../shared/model/lines/zone-Model.ts";
|
||||
|
||||
import zoneModel from "../../../shared/model/builder/lines/zone-Model.ts";
|
||||
export const setZone = async (data: any) => {
|
||||
try {
|
||||
const { organization, userId, zoneData } = data
|
||||
console.log('data: ', data);
|
||||
try {
|
||||
const zoneId = zoneData.zoneId
|
||||
const points = zoneData.points
|
||||
const zoneName = zoneData.zoneName
|
||||
@@ -18,7 +16,7 @@ export const setZone = async (data: any) => {
|
||||
return { success: true, message: 'zone updated', data: updateZone, organization: organization }
|
||||
} else {
|
||||
const zoneCreate = await zoneModel(organization).create({
|
||||
zoneId, createBy: userId, zoneName: zoneName, points, layer, viewPortCenter, viewPortposition
|
||||
zoneId, createdBy: userId, zoneName: zoneName, points, layer, viewPortCenter, viewPortposition
|
||||
})
|
||||
const createdZone = await zoneModel(organization)
|
||||
.findById(zoneCreate._id)
|
||||
@@ -28,18 +26,18 @@ export const setZone = async (data: any) => {
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('error: ', error);
|
||||
return { success: false, message: 'Zone not found', error }
|
||||
return { success: false, message: 'Zone not found', error , organization: organization}
|
||||
}
|
||||
}
|
||||
|
||||
export const deleteZone = async (data: any) => {
|
||||
try {
|
||||
const { organization, userId, zoneId } = data
|
||||
try {
|
||||
|
||||
const findZoneId = await zoneModel(organization).findOne({ zoneId: zoneId })
|
||||
if (findZoneId) {
|
||||
const deleteZone = await zoneModel(organization).findOneAndDelete(
|
||||
{ zoneId: zoneId, createBy: userId }
|
||||
{ zoneId: zoneId, createdBy: userId }
|
||||
).select("-_id -__v")
|
||||
return { success: true, message: 'zone deleted', data: deleteZone, organization: organization }
|
||||
} else {
|
||||
@@ -48,6 +46,6 @@ export const deleteZone = async (data: any) => {
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('error: ', error);
|
||||
return { success: false, message: 'Zone not found', error }
|
||||
return { success: false, message: 'Zone not found', error, organization: organization }
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import userModel from "../../../shared/model/user-Model.ts"
|
||||
|
||||
export const activeUsers = async (data: any) => {
|
||||
const {organization}=data
|
||||
// console.log('data: ', data);
|
||||
try {
|
||||
if (data && data.email) {
|
||||
|
||||
@@ -43,7 +44,7 @@ export const activeUsers = async (data: any) => {
|
||||
// Handle the error or return a default value
|
||||
// Example: Return an error response if the email is invalid
|
||||
|
||||
return { success: false, message: 'Email is missing or invalid', }
|
||||
return { success: false, message: 'Email is missing or invalid', organization:organization }
|
||||
// return res.status(400).send({ message: 'Email is missing or invalid' });
|
||||
}
|
||||
|
||||
@@ -99,3 +100,38 @@ export const activeUserOffline = async (data: any) => {
|
||||
// return { success: false, message: error}
|
||||
}
|
||||
}
|
||||
type OnlineUsersMap = Map<string, Set<string>>;
|
||||
|
||||
// export const addUserToOnlineList = async (
|
||||
// organization: string,
|
||||
// email: string,
|
||||
// onlineUsers: OnlineUsersMap,
|
||||
// socket: any,
|
||||
// namespace: string
|
||||
// ) => {
|
||||
// if (!organization || !email) return;
|
||||
// console.log('organization: ', organization);
|
||||
|
||||
// // Ensure the organization entry exists
|
||||
// if (!onlineUsers.has(organization)) {
|
||||
// onlineUsers.set(organization, new Set());
|
||||
// }
|
||||
// const findUser = await userModel(organization).findOne({email})
|
||||
// if (!findUser) {
|
||||
// return { success: false, message: "User not found", organization };
|
||||
// }
|
||||
// const userId = findUser._id;
|
||||
// console.log(`🔍 Found user with ID: ${userId}`);
|
||||
// // Add user to the online set
|
||||
// onlineUsers.get(organization)!.add(userId);
|
||||
|
||||
// console.log(`✅ User ${userId} is online (Org: ${organization})`);
|
||||
|
||||
// // Emit updated online users list
|
||||
// socket.emit("users:online", {
|
||||
// organization,
|
||||
// users: [...onlineUsers.get(organization)!],
|
||||
// });
|
||||
// };
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@ export const EVENTS = {
|
||||
connection: "connection",
|
||||
disconnect:"disconnect",
|
||||
//userActiveStatus
|
||||
userConnect:"userConnectRespones",
|
||||
userDisConnect:"userDisConnectRespones",
|
||||
userConnect:"userConnectResponse",
|
||||
userDisConnect:"userDisConnectResponse",
|
||||
// Room management events
|
||||
joinRoom: 'joinRoom',
|
||||
createroom: "createRoom", // When a client joins a room
|
||||
@@ -73,4 +73,8 @@ export const EVENTS = {
|
||||
//model-asset
|
||||
setAssetModel: "v2:model-asset:add",
|
||||
assetUpdateRespones: "model-asset:response:updates",
|
||||
deleteAssetModel:"v2:model-asset:delete",
|
||||
assetDeleteRespones: "model-asset:response:updates",
|
||||
|
||||
|
||||
}
|
||||
@@ -5,11 +5,13 @@ import { setEnvironment } from '../services/environments/environments-controller
|
||||
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 { 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';
|
||||
|
||||
|
||||
|
||||
@@ -31,7 +33,7 @@ const cameraHandleEvent = async (event: string, socket: Socket, data: any, names
|
||||
// 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, {
|
||||
socket.to(organization).emit(responseEvent, {
|
||||
success: result.success,
|
||||
message: result.message,
|
||||
data: result.data,
|
||||
@@ -165,7 +167,8 @@ const floorItemsHandleEvent =async (event: string, socket: Socket, data: any,io:
|
||||
organization: result.organization
|
||||
});
|
||||
}
|
||||
break;}
|
||||
break;
|
||||
}
|
||||
case EVENTS.deleteFloorItems: {
|
||||
const result = await deleteFloorItems(data);
|
||||
// console.log('result: ', result);
|
||||
@@ -191,265 +194,317 @@ const floorItemsHandleEvent =async (event: string, socket: Socket, data: any,io:
|
||||
organization: result.organization
|
||||
});
|
||||
}
|
||||
break;}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
// console.error(`Unhandled event type: ${event}`);
|
||||
}
|
||||
}
|
||||
const wallItemsHandleEvent =async (event: string, socket: Socket, data: any,io:any) => {
|
||||
switch (event) {
|
||||
if (!data?.organization) {
|
||||
console.warn(`Missing organization for event: ${event}`);
|
||||
return;
|
||||
}
|
||||
|
||||
let result;
|
||||
switch (event) {
|
||||
case EVENTS.setWallItems: {
|
||||
const result = await setWallItems(data);
|
||||
// console.log('result: ', result);
|
||||
if (result.success) {
|
||||
io.emit(EVENTS.wallItemsUpdateResponse, {
|
||||
success: true,
|
||||
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:result.organization
|
||||
|
||||
organization,
|
||||
});
|
||||
|
||||
} else {
|
||||
// Emit error response
|
||||
socket.emit(EVENTS.wallItemError, {
|
||||
success: false,
|
||||
message: result.message,
|
||||
error: result.error,
|
||||
});
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break
|
||||
}
|
||||
case EVENTS.deleteWallItems: {
|
||||
const result = await deleteWallItems(data);
|
||||
// console.log('result: ', result);
|
||||
if (result.success) {
|
||||
|
||||
|
||||
io.emit(EVENTS.wallItemsDeleteResponse, {
|
||||
success: true || false,
|
||||
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:result.organization
|
||||
|
||||
organization,
|
||||
});
|
||||
|
||||
} else {
|
||||
// Emit error response
|
||||
socket.emit(EVENTS.wallItemError, {
|
||||
success: false,
|
||||
message: result.message,
|
||||
error: result.error,
|
||||
});
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
default:
|
||||
// console.error(`Unhandled event type: ${event}`);
|
||||
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: {
|
||||
const result = await createLineItems(data);
|
||||
// console.log('result: ', result);
|
||||
if (result.success) {
|
||||
result = await createLineItems(data);
|
||||
|
||||
|
||||
io.emit(EVENTS.createLineResponse, {
|
||||
success: true || false,
|
||||
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:result.organization
|
||||
|
||||
organization,
|
||||
});
|
||||
|
||||
} else {
|
||||
// Emit error response
|
||||
// socket.emit(EVENTS.wallItemError, {
|
||||
// success: false,
|
||||
// message: result.message,
|
||||
// error: result.error,
|
||||
// });
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break
|
||||
}
|
||||
case EVENTS.updateLine: {
|
||||
const result = await updateLineItems(data);
|
||||
// console.log('result: ', result);
|
||||
if (result.success) {
|
||||
|
||||
|
||||
io.emit(EVENTS.updateLineResponse, {
|
||||
success: true || false,
|
||||
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: result.organization
|
||||
|
||||
organization,
|
||||
});
|
||||
|
||||
} else {
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break
|
||||
}
|
||||
case EVENTS.deleteLine: {
|
||||
const result = await deleteLineItems(data);
|
||||
// console.log('result: ', result);
|
||||
if (result.success) {
|
||||
|
||||
|
||||
io.emit(EVENTS.deleteLineResponse, {
|
||||
success: true || false,
|
||||
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:result.organization
|
||||
|
||||
organization,
|
||||
});
|
||||
|
||||
} else {
|
||||
// Emit error response
|
||||
// socket.emit(EVENTS.wallItemError, {
|
||||
// success: false,
|
||||
// message: result.message,
|
||||
// error: result.error,
|
||||
// });
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
break
|
||||
}
|
||||
case EVENTS.deletePoint: {
|
||||
const result = await deleteLinPoiteItems(data);
|
||||
// console.log('result: ', result);
|
||||
if (result.success) {
|
||||
|
||||
io.emit(EVENTS.deletePointResponse, {
|
||||
success: true || false,
|
||||
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:result.organization
|
||||
|
||||
organization,
|
||||
});
|
||||
|
||||
} else {
|
||||
// Emit error response
|
||||
// socket.emit(EVENTS.wallItemError, {
|
||||
// success: false,
|
||||
// message: result.message,
|
||||
// error: result.error,
|
||||
// });
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
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
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
// console.error(`Unhandled event type: ${event}`);
|
||||
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: {
|
||||
// console.log('EVENTS.connection: ', EVENTS.connection);
|
||||
// console.log('event: ', event);
|
||||
const result = await activeUsers(data);
|
||||
if (result?.success) {
|
||||
// console.log('result.success: ', result.success)
|
||||
result = await activeUsers(data);
|
||||
// console.log('result: ', result);
|
||||
|
||||
io.emit(EVENTS.userConnect, {
|
||||
success: true || false,
|
||||
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:result.organization
|
||||
|
||||
});}
|
||||
break;
|
||||
organization,
|
||||
});
|
||||
} else {
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
}
|
||||
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,
|
||||
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:result.organization
|
||||
|
||||
});}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
organization,
|
||||
});
|
||||
|
||||
} else {
|
||||
console.warn(`Organization missing in response for event: ${event}`);
|
||||
}
|
||||
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}
|
||||
break
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
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) {
|
||||
@@ -461,14 +516,9 @@ const panelHandleEvent=async(event: string, socket: Socket, data: any,namespace:
|
||||
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);
|
||||
@@ -482,7 +532,8 @@ 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) {
|
||||
@@ -586,7 +637,203 @@ const widgetHandleEvent = async (event: string, socket: Socket, data: any, names
|
||||
}
|
||||
|
||||
}
|
||||
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, {
|
||||
@@ -621,6 +868,105 @@ userStatus(EVENTS.connection, 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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user