From b9e66eb4b74c4e709e70afed59bddf4b8d1c9c39 Mon Sep 17 00:00:00 2001 From: sabarinathan155 Date: Tue, 1 Apr 2025 18:07:09 +0530 Subject: [PATCH] panel.widgets socket creation --- .../services/users/user-controller.ts | 2 +- .../visualization/3dWidget-Service.ts | 50 ++++ ...idgetService.ts => floatWidget-Service.ts} | 142 ++++++----- .../services/visualization/panel-Services.ts | 240 +++++++++++------- .../services/visualization/widget-Services.ts | 29 ++- src/socket-server/socket/events.ts | 4 + src/socket-server/socket/socketManager.ts | 83 +++++- 7 files changed, 389 insertions(+), 161 deletions(-) create mode 100644 src/socket-server/services/visualization/3dWidget-Service.ts rename src/socket-server/services/visualization/{floatWidgetService.ts => floatWidget-Service.ts} (61%) diff --git a/src/socket-server/services/users/user-controller.ts b/src/socket-server/services/users/user-controller.ts index 79cd857..c9e4bd0 100644 --- a/src/socket-server/services/users/user-controller.ts +++ b/src/socket-server/services/users/user-controller.ts @@ -63,7 +63,7 @@ export const activeUserOffline = async (data: any) => { const email = data.email const organization = email.split("@")[1].split(".")[0] try { - + const findUsers = await userModel(organization).findOne({email}) // console.log('findUsers: ', findUsers); if (findUsers) { diff --git a/src/socket-server/services/visualization/3dWidget-Service.ts b/src/socket-server/services/visualization/3dWidget-Service.ts new file mode 100644 index 0000000..343718e --- /dev/null +++ b/src/socket-server/services/visualization/3dWidget-Service.ts @@ -0,0 +1,50 @@ +import zoneSchema from "../../../shared/model/builder/lines/zone-Model.ts"; +import widget3dModel from "../../../shared/model/vizualization/3dwidget.ts"; +export const add3Dwidget = async (data: any) => { + const { organization, widget, zoneId } = data + + console.log('data: ', data) + try { + const existingZone = await zoneSchema(organization).findOne({ + zoneId: zoneId, + isArchive: false, + }); + if (!existingZone) + return { success: false, message: "Zone not found for the zoneId", organization: organization } + const existing3Dwidget = await widget3dModel(organization).findOne({ + widgetID: widget.id, + isArchive: false, + }); + if (existing3Dwidget) { + const update3dwidget = await widget3dModel( + organization + ).findOneAndUpdate( + { + widgetID: widget.id, + zoneId: zoneId, + isArchive: false, + }, + { position: widget.position }, + { upsert: true, new: true } + ); + if (update3dwidget) + return { success: true, message:"widget update successfully", organization: organization } + + + else return{ success: false, message: "Widget not updated", organization: organization } + } + const newWidget3d = await widget3dModel(organization).create({ + widgetName: widget.type, + widgetID: widget.id, + position: widget.position, + zoneId, + }); + if (newWidget3d) + return { success: false, message: "Widget created successfully",data:newWidget3d, organization: organization } + + } catch (error: any) { + return { success: false, message: error?.message || "Error occurred while 3Dwidget", error, organization: organization } + } + +} + diff --git a/src/socket-server/services/visualization/floatWidgetService.ts b/src/socket-server/services/visualization/floatWidget-Service.ts similarity index 61% rename from src/socket-server/services/visualization/floatWidgetService.ts rename to src/socket-server/services/visualization/floatWidget-Service.ts index 63f49df..4b39be5 100644 --- a/src/socket-server/services/visualization/floatWidgetService.ts +++ b/src/socket-server/services/visualization/floatWidget-Service.ts @@ -1,58 +1,84 @@ -import floatWidgetModel from "../../../shared/model/vizualization/3dwidget.ts"; -import zoneSchema from "../../../shared/model/builder/lines/zone-Model.ts"; - -export const addfloat = async (data: any) => { - const { organization, widget, zoneId } = data; - console.log('data: ', data); - try { - const existingZone = await zoneSchema(organization).findOne({ - zoneId: zoneId, - isArchive: false, - }); - if (!existingZone) - return { success: false, message: "Zone not found for the zoneId", organization: organization } - - const existingFloatWidget = await floatWidgetModel(organization).findOne({ - floatWidgetID: widget.id, - isArchive: false, - }); - if (existingFloatWidget) { - const updateFloatWidget = await floatWidgetModel(organization).findOneAndUpdate( - { - floatWidgetID: widget.id, - isArchive: false, - }, - { - $set: { - // Data: { - // // measurements: widget.Data.measurements || {}, - // duration: widget.Data.duration || "1h", - // }, - position: widget.position, - }, - }, - { - upsert: true, - new: true, - } - ); - return { success: true, message: "Widget updated successfully", data: updateFloatWidget, organization: organization } - } - - const newFloadWidget = await floatWidgetModel(organization).create({ - className: widget.className, - header: widget.header, - floatWidgetID: widget.id, - position: widget.position, - per: widget.per, - value: widget.value, - zoneId: zoneId, - }); - if (newFloadWidget) { - return { success: true, message: "FloatWidget created successfully", data: newFloadWidget, organization: organization } - } - } catch (error: any) { - return { success: false, message: error?.message || "Error occurred while float", error, organization: organization } - } - -} +import floatWidgetModel from "../../../shared/model/vizualization/3dwidget.ts"; +import zoneSchema from "../../../shared/model/builder/lines/zone-Model.ts"; + +export const addfloat = async (data: any) => { + const { organization, widget, zoneId } = data; + console.log('data: ', data); + try { + const existingZone = await zoneSchema(organization).findOne({ + zoneId: zoneId, + isArchive: false, + }); + if (!existingZone) + return { success: false, message: "Zone not found for the zoneId", organization: organization } + + const existingFloatWidget = await floatWidgetModel(organization).findOne({ + floatWidgetID: widget.id, + isArchive: false, + zoneId: zoneId, + + }); + if (existingFloatWidget) { + const updateFloatWidget = await floatWidgetModel(organization).findOneAndUpdate( + { + floatWidgetID: widget.id, + isArchive: false, + }, + { + $set: { + Data: { + measurements: widget?.Data.measurements, + duration: widget?.Data.duration, + }, + header: widget?.header, + position: widget?.position, + }, + }, + { + upsert: true, + new: true, + } + ); + return { success: true, message: "Widget updated successfully", data: updateFloatWidget, organization: organization } + } + + const newFloadWidget = await floatWidgetModel(organization).create({ + className: widget.className, + header: widget.header, + floatWidgetID: widget.id, + position: widget.position, + per: widget.per, + value: widget.value, + zoneId: zoneId, + }); + if (newFloadWidget) { + return { success: true, message: "FloatWidget created successfully", data: newFloadWidget, organization: organization } + } + } catch (error: any) { + return { success: false, message: error?.message || "Error occurred while float", error, organization: organization } + } + +} +export const deletefloat = async (data: any) => { + const { floatWidgetID, organization } = data; + + console.log('data: ', data); + try { + const findfloatWidget = await floatWidgetModel(organization).findOne({ + floatWidgetID: floatWidgetID, + isArchive: false, + }); + if (!findfloatWidget) + return { success: false, message: "Zone not found for the zoneId", organization: organization } + const widgetData = await floatWidgetModel(organization).updateOne( + { _id: findfloatWidget._id, isArchive: false }, + { $set: { isArchive: true } } + ); + return { success: true, message: "FloatingWidget deleted successfully", data: widgetData, organization: organization } + + } catch (error: any) { + return { success: false, message: error?.message || "Error occurred while float", error, organization: organization } + + } + +} diff --git a/src/socket-server/services/visualization/panel-Services.ts b/src/socket-server/services/visualization/panel-Services.ts index 4a24ad7..b9c6a87 100644 --- a/src/socket-server/services/visualization/panel-Services.ts +++ b/src/socket-server/services/visualization/panel-Services.ts @@ -3,103 +3,171 @@ import zoneSchema from "../../../shared/model/builder/lines/zone-Model.ts"; import widgetSchema from "../../../shared/model/vizualization/widgemodel.ts"; export const addPanel = async (data: any) => { -const{organization,zoneId,panelName,panelOrder}=data -console.log('data: ', data); -try { + const { organization, zoneId, panelName, panelOrder } = data + try { const findZone = await zoneSchema(organization).findOne({ - zoneId: zoneId, - isArchive: false, + zoneId: zoneId, + isArchive: false, }); - console.log('findZone: ', findZone); if (!findZone) { - return { success: false, message: 'Zone not found',organization: organization} + return { success: false, message: 'Zone not found', organization: organization } - } - const updatezone = await zoneSchema(organization).findOneAndUpdate( - { zoneId: zoneId, isArchive: false }, - { panelOrder: panelOrder }, - { new: true } - ); - const existingPanels = await panelSchema(organization).find({ + } + const updatezone = await zoneSchema(organization).findOneAndUpdate( + { zoneId: zoneId, isArchive: false }, + { panelOrder: panelOrder }, + { new: true } + ); + const existingPanels = await panelSchema(organization).find({ + zoneId: zoneId, + isArchive: false, + }); + + const existingPanelNames = existingPanels.map( + (panel: any) => panel.panelName + ); + + const missingPanels = panelOrder.filter( + (panelName: string) => !existingPanelNames.includes(panelName) + ); + + const createdPanels = []; + for (const panelName of missingPanels) { + const newPanel = await panelSchema(organization).create({ zoneId: zoneId, + panelName: panelName, + widgets: [], isArchive: false, }); + createdPanels.push(newPanel); + } - const existingPanelNames = existingPanels.map( - (panel: any) => panel.panelName - ); + if (createdPanels.length === 0) { + return { success: false, message: "No new panels were created. All panels already exist", organization: organization } + } + // const IDdata = createdPanels.map((ID: any) => { + // return ID._id; + // }); + // console.log("IDdata: ", createdPanels); + createdPanels + const zoneAndPanelData = await getZoneAndPanelData(organization, zoneId); + if (!zoneAndPanelData) { + return zoneAndPanelData; // If the zone and panel data retrieval fails, return the error. + } + return { success: true, message: "Panels created successfully", data: zoneAndPanelData, organization: organization } + } catch (error) { + return { success: false, message: 'Panel not found', error, organization: organization } - const missingPanels = panelOrder.filter( - (panelName: string) => !existingPanelNames.includes(panelName) - ); - - const createdPanels = []; - for (const panelName of missingPanels) { - const newPanel = await panelSchema(organization).create({ - zoneId: zoneId, - panelName: panelName, - widgets: [], - isArchive: false, - }); - createdPanels.push(newPanel); - } - - if (createdPanels.length === 0) { - return { success: false, message: "No new panels were created. All panels already exist",organization: organization} - } - // const IDdata = createdPanels.map((ID: any) => { - // return ID._id; - // }); - console.log("IDdata: ", createdPanels); - createdPanels - return { success: true, message: "Panels created successfully", data: createdPanels, organization: organization } -}catch (error) { - return { success: false, message: 'Panel not found', error,organization: organization } - -} + } } export const panelDelete = async (data: any) => { - const { organization, panelName, zoneId } = data; - console.log('data: ', data); - try { - const existingZone = await zoneSchema(organization).findOne({ - zoneId: zoneId, - isArchive: false, - }); - if (!existingZone) - return { success: false, message: 'Zone not found',organization: organization} + const { organization, panelName, zoneId } = data; + try { + const existingZone = await zoneSchema(organization).findOne({ + zoneId: zoneId, + isArchive: false, + }); + if (!existingZone) + return { success: false, message: 'Zone not found', organization: organization } - const existingPanel = await panelSchema(organization).findOne({ - zoneId: zoneId, - panelName: panelName, - isArchive: false, - }); - if (!existingPanel) - return { success: false, message: 'Panel Already Deleted',organization: organization} - const updatePanel = await panelSchema(organization).updateOne( - { _id: existingPanel._id, isArchive: false }, - { $set: { isArchive: true } } - ); - const existingWidgets = await widgetSchema(organization).find({ - panelID:existingPanel._id, - isArchive: false, - }); - - for (const widgetData of existingWidgets) { - widgetData.isArchive = true; - await widgetData.save(); - } - - if (existingZone.panelOrder.includes(existingPanel.panelName)) { - const index1 = existingZone.panelOrder.indexOf(existingPanel.panelName); - existingZone.panelOrder.splice(index1, 1); - - const panelDeleteDatas= await existingZone.save(); - return { success: true, message: 'Panel deleted successfully',data:panelDeleteDatas,organization: organization} - } + const existingPanel = await panelSchema(organization).findOne({ + zoneId: zoneId, + panelName: panelName, + isArchive: false, + }); + if (!existingPanel) + return { success: false, message: 'Panel Already Deleted', organization: organization } + const updatePanel = await panelSchema(organization).updateOne( + { _id: existingPanel._id, isArchive: false }, + { $set: { isArchive: true } } + ); + const existingWidgets = await widgetSchema(organization).find({ + panelID: existingPanel._id, + isArchive: false, + }); - } catch (error) { - return { success: false, message: 'Panel not found', error,organization: organization } } - - } \ No newline at end of file + for (const widgetData of existingWidgets) { + widgetData.isArchive = true; + await widgetData.save(); + } + + if (existingZone.panelOrder.includes(existingPanel.panelName)) { + const index1 = existingZone.panelOrder.indexOf(existingPanel.panelName); + existingZone.panelOrder.splice(index1, 1); + + const panelDeleteDatas = await existingZone.save(); + // console.log('panelDeleteDatas: ', panelDeleteDatas); + const zoneAndPanelData = await getZoneAndPanelData(organization, zoneId); + if (!zoneAndPanelData) { + return zoneAndPanelData; // If the zone and panel data retrieval fails, return the error. + } + return { success: true, message: 'Panel deleted successfully', data: zoneAndPanelData, organization: organization } + } + + } catch (error) { + return { success: false, message: 'Panel not found', error, organization: organization } + } + +} + +const getZoneAndPanelData = async (organization: string, zoneId: string) => { + // const { organization, zoneId, } = data + // console.log('data: ', data); + + try { + const existingZone = await zoneSchema(organization) + .findOne({ + zoneId: zoneId, + isArchive: false, + }) + .select( + "panelOrder zoneName zonePoints lockedPanel zoneId viewPortCenter viewPortposition" + ); + if (!existingZone) { + return { success: false, message: 'Zone not found', organization: organization } + } else { + const panelData = await panelSchema(organization).find({ + zoneId: zoneId, + isArchive: false, + }); + const zoneName = existingZone.zoneName as string; + + const widgets = await Promise.all( + panelData.map(async (data) => { + const widgetDataArray = await widgetSchema(organization).find({ + panelID: data._id, + isArchive: false, + }); + + return widgetDataArray.map((widgetData) => ({ + id: widgetData.widgetID, + type: widgetData.elementType, + title: widgetData.widgetName, + panel: widgetData.widgetside, + data: widgetData.Data || [], + })); + }) + ); + + const flattenedWidgets = widgets.flat(); + + const objectData = { + zoneName, + viewPortposition: existingZone.viewPortposition, + zoneId: existingZone.zoneId, + viewPortCenter: existingZone.viewPortCenter, + activeSides: existingZone.panelOrder || [], + panelOrder: existingZone.panelOrder || [], + lockedPanels: existingZone.lockedPanel || [], + points: existingZone.zonePoints || [], + widgets: flattenedWidgets, + }; + + return { data: objectData, } + } + } catch (error: any) { + return { success: false, message: 'Panel not found', error, organization: organization } + } + +} \ No newline at end of file diff --git a/src/socket-server/services/visualization/widget-Services.ts b/src/socket-server/services/visualization/widget-Services.ts index 043fafc..31ebad6 100644 --- a/src/socket-server/services/visualization/widget-Services.ts +++ b/src/socket-server/services/visualization/widget-Services.ts @@ -1,14 +1,21 @@ +import { title } from "process"; import panelSchema from "../../../shared/model/vizualization/panelmodel.ts"; import widgetSchema from "../../../shared/model/vizualization/widgemodel.ts"; +import zoneSchema from "../../../shared/model/builder/lines/zone-Model.ts"; export const addWidget = async (data: any) => { const { organization,panel,zoneId,widget,} = data - console.log('data: ', data); try { - const existingPanel = await panelSchema(organization).findOne({ + const existingZone = await zoneSchema(organization).findOne({ + zoneId: zoneId, + isArchive: false, + }); + if (!existingZone) + return { success: false, message: "Zone not found", organization: organization } + const existingPanel = await panelSchema(organization).findOne({ panelName: widget.panel, zoneId: zoneId, isArchive: false, - }); + }) if (!existingPanel) return { success: false, message: "panelName not found",organization: organization} @@ -44,14 +51,14 @@ export const addWidget = async (data: any) => { { upsert: true, new: true } // Upsert: create if not exists, new: return updated document ); - return { success: false, message: "Widget updated successfully",data:updateWidget,organization: organization} + return { success: true, message: "Widget updated successfully",data:updateWidget,organization: organization} } const newWidget = await widgetSchema(organization).create({ widgetID: widget.id, elementType: widget.type, // widgetOrder: widgetOrder, - widgetName: widget.widgetName, + widgetName: widget.title, panelID: existingPanel._id, widgetside: widget.panel, // Data: { @@ -62,7 +69,15 @@ export const addWidget = async (data: any) => { if (newWidget) { existingPanel.widgets.push(newWidget._id); await existingPanel.save(); - return { success: true, message: "Widget already exist for the widgetID",data:existingPanel,organization: organization} + const widgetData={ + type:newWidget.elementType, + id:newWidget.widgetID, + panel:newWidget.widgetside, + title:newWidget.widgetName, + } + const finaldata={ widgetData:widgetData,zoneId:existingZone.zoneId,zoneName:existingZone.zoneName} + // console.log('existingPanel: ', widgetData); + return { success: true, message: "Widget created successfully",data:finaldata,organization: organization} } } return { success: false, message: "Type mismatch",organization: organization} @@ -116,4 +131,4 @@ export const Widgetdelete = async (data: any) => { } catch (error: any) { return { success: false, message: error?.message || "An unknown error occurred.", error ,organization: organization} } -} \ No newline at end of file +} diff --git a/src/socket-server/socket/events.ts b/src/socket-server/socket/events.ts index 0e90c92..15f75b1 100644 --- a/src/socket-server/socket/events.ts +++ b/src/socket-server/socket/events.ts @@ -76,5 +76,9 @@ export const EVENTS = { deleteAssetModel:"v2:model-asset:delete", assetDeleteRespones: "model-asset:response:updates", + //3Dwidget + add3DWidget:"v2:viz-3D-widget:add", + widget3DUpdateRespones:"viz-widget3D:response:updates", + } \ No newline at end of file diff --git a/src/socket-server/socket/socketManager.ts b/src/socket-server/socket/socketManager.ts index 628d2b2..c22a5dc 100644 --- a/src/socket-server/socket/socketManager.ts +++ b/src/socket-server/socket/socketManager.ts @@ -9,9 +9,10 @@ import { activeUserOffline, activeUsers, } from '../services/users/user-controll 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 { addfloat, deletefloat } from '../services/visualization/floatWidget-Service.ts'; import { addTemplate } from '../services/visualization/templateServices.ts'; import { deleteAssetModel, setAssetModel } from '../services/assets/asset-Controller.ts'; +import { add3Dwidget } from '../services/visualization/3dWidget-Service.ts'; @@ -546,7 +547,7 @@ const panelHandleEvent = async (event: string, socket: Socket, data: any, namesp // console.log(`👀 Active sockets in room:`, namespace.adapter.rooms.get(organization)); // console.log('emitTarget: ', emitTarget); if (organization) { - socket.emit(responseEvent, { + socket.to(organization).emit(responseEvent, { success: result.success, message: result.message, data: result.data, @@ -674,6 +675,70 @@ const floatHandleEvent = async (event: string, socket: Socket, data: any, namesp } break } + case EVENTS.deleteFloat: { + const result = await deletefloat(data) + if (result) { + // console.log('result?.success: ', result.organization); + const responseEvent = EVENTS.floatDeleteRespones + // 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 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) { @@ -706,7 +771,7 @@ const floatHandleEvent = async (event: string, socket: Socket, data: any, namesp } } -const templateHandleEvent = async (event: string, socket: Socket, data: any, namespace: any, notifySender: boolean = false) => { +const Widget3DHandleEvent = 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}`); @@ -715,11 +780,11 @@ const templateHandleEvent = async (event: string, socket: Socket, data: any, nam let result; switch (event) { - case EVENTS.addTemplate: { - result = await addTemplate(data); + case EVENTS.add3DWidget: { + result = await add3Dwidget(data); if (result) { - const responseEvent = EVENTS.templateUpdateRespones + const responseEvent = EVENTS.widget3DUpdateRespones const organization = result?.organization // const emitTarget = notifySender ? socket.in(organization) : socket.to(organization); // console.log(`👀 Active sockets in room:`, namespace.adapter.rooms.get(organization)); @@ -877,7 +942,7 @@ export const initSocketServer = (httpServer: any) => { line: io.of("/line"), zone: io.of("/zone"), Builder: io.of('/Builder'), - visualization: io.of('/visualization'), + visualization: io.of('/Visualization'), // widget:io.of('/widget') }; // 🔹 Function to handle connections in a namespace @@ -921,7 +986,7 @@ export const initSocketServer = (httpServer: any) => { // const organization = socket.handshake.query.organization as string; const {organization,email} = socket.handshake.auth - console.log(`🔍 Received organization: ${organization}`); + // console.log(`🔍 Received organization: ${organization}`); if (organization) { socket.join(organization); @@ -966,7 +1031,7 @@ export const initSocketServer = (httpServer: any) => { // 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); + handleNamespace("Visualization", namespaces.visualization, panelHandleEvent, widgetHandleEvent, floatHandleEvent, templateHandleEvent); return io; };