diff --git a/src/controllers/createConnectionController.js b/src/controllers/createConnectionController.js index 28540979..fb513272 100644 --- a/src/controllers/createConnectionController.js +++ b/src/controllers/createConnectionController.js @@ -20,8 +20,28 @@ exports.createConnections = async (req, body) => { const tankInfo = await Tank.findOne({ customerId: customerId.toString(),tankName:tankname }) const usertobeInserted = req.body; tankInfo.connections.source = tankInfo.tankName; - if (usertobeInserted.inputConnections) tankInfo.connections.inputConnections = usertobeInserted.inputConnections; - if (usertobeInserted.outputConnections) tankInfo.connections.outputConnections = usertobeInserted.outputConnections; + if (usertobeInserted.inputConnections) { + // update inputismotor field for each input connection + tankInfo.connections.inputConnections = usertobeInserted.inputConnections.map(connection => { + return { + inputConnections: connection.inputConnections, + input_type: connection.input_type, + motor_status: connection.motor_status || "0", + inputismotor: connection.inputismotor // default to false if not specified + }; + }); + } + + if (usertobeInserted.outputConnections) { + // update outputismotor field for each output connection + tankInfo.connections.outputConnections = usertobeInserted.outputConnections.map(connection => { + return { + outputConnections: connection.outputConnections, + output_type: connection.output_type, + outputismotor: connection.outputismotor // default to false if not specified + }; + }); + } const connection_data = req.body.outputConnections for (const data of connection_data){ @@ -78,8 +98,31 @@ exports.updateconnectionInfo = async (req, reply) => { const updateData = req.body; tankInfo.connections.source = tankInfo.tankName; - if (updateData.inputConnections) tankInfo.connections.inputConnections = updateData.inputConnections; - if (updateData.outputConnections) tankInfo.connections.outputConnections = updateData.outputConnections; + // if (updateData.inputConnections) tankInfo.connections.inputConnections = updateData.inputConnections; + // if (updateData.outputConnections) tankInfo.connections.outputConnections = updateData.outputConnections; + // Update the tank's input connections if provided + if (updateData.inputConnections) { + tankInfo.connections.inputConnections = updateData.inputConnections.map(connection => { + return { + inputConnections: connection.inputConnections, + input_type: connection.input_type, + motor_status: connection.motor_status || "0", + inputismotor: connection.inputismotor || false // default to false if not specified + }; + }); + } + + // Update the tank's output connections if provided + if (updateData.outputConnections) { + tankInfo.connections.outputConnections = updateData.outputConnections.map(connection => { + return { + outputConnections: connection.outputConnections, + output_type: connection.output_type, + outputismotor: connection.outputismotor || false // default to false if not specified + }; + }); + } + const tank_connections = await tankInfo.save(); return tank_connections; } catch (err) { diff --git a/src/index.js b/src/index.js index aad38643..5941ddad 100644 --- a/src/index.js +++ b/src/index.js @@ -358,8 +358,6 @@ fastify.register(require("./routes/forTestingRoute")); const start = async () => { try { - - await fastify.listen(3000, "0.0.0.0"); fastify.log.info(`listening on ${fastify.server.address().port}`); fastify.log.info(`server listening on ${fastify.config}`); diff --git a/src/models/tanks.js b/src/models/tanks.js index 44c86290..a20ec760 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -9,6 +9,27 @@ const ObjectId = Schema.Types.ObjectId; // Store a random password reset code const code = Math.floor(100000 + Math.random() * 900000); const RoleSchema = new Schema({ name: String }); +// const tanksSchema = new mongoose.Schema({ +// hardwareId: { type: String }, +// hardwareId_type: { type: String }, +// hardwareId_company: { type: String }, +// customerId: { type: String, default: null }, +// tankName: { type: String, default: null }, +// blockName: { type: String, default: null }, +// capacity: { type: String, default: "0" }, +// typeOfWater: { type: String, default: null }, +// waterlevel: { type: String, default: "0" }, +// tankLocation: { type: String, default: null }, +// motor_status: { type: String, default: 0 }, +// connections: { +// source: { type: String}, +// inputConnections: [{ inputConnections: String,input_type:String,motor_status: { type: String, default: "0" } }], +// outputConnections: [{ outputConnections: String,output_type:String }] +// } + + +// }); + const tanksSchema = new mongoose.Schema({ hardwareId: { type: String }, hardwareId_type: { type: String }, @@ -20,15 +41,28 @@ const tanksSchema = new mongoose.Schema({ typeOfWater: { type: String, default: null }, waterlevel: { type: String, default: "0" }, tankLocation: { type: String, default: null }, - motor_status: { type: String, default: 0 }, + motor_status: { type: String, default: "0" }, connections: { - source: { type: String}, - inputConnections: [{ inputConnections: String,input_type:String,motor_status: { type: String, default: "0" } }], - outputConnections: [{ outputConnections: String,output_type:String }] + source: { type: String }, + inputConnections: [ + { + inputConnections: { type: String }, + input_type: { type: String }, + inputismotor: { type: Boolean }, + motor_status: { type: String, default: "0" } + } + ], + outputConnections: [ + { + outputConnections: { type: String }, + output_type: { type: String }, + outputismotor: { type: Boolean }, + motor_status: { type: String, default: "0" } + } + ] } - - -}); + }); + const motordataSchema = new mongoose.Schema({ diff --git a/src/routes/createConnectionsRoute.js b/src/routes/createConnectionsRoute.js index 502ac2b4..457fa5a1 100644 --- a/src/routes/createConnectionsRoute.js +++ b/src/routes/createConnectionsRoute.js @@ -3,67 +3,154 @@ const createConnectionController = require("../controllers/createConnectionContr module.exports = function (fastify, opts, next) { + // fastify.route({ + // method: "POST", + // url: "/api/createConnections/:customerId", + // schema: { + // tags: ["Connections"], + // description: "This is for cretae New Connection", + // summary: "This is for cretae New Connection.", + // params: { + // required: ["customerId"], + // type: "object", + // properties: { + // customerId: { + // type: "string", + // description: "customerId", + // }, + // }, + // }, + // // body: { + // // type: "object", + // // properties: { + // // tankname: { type: "string" }, + // // inputConnections: { + // // type: "array", + // // maxItems: 2500, + // // items: { + // // type: "object", + // // properties: { + // // inputConnections: { type: "string", default: null }, + // // input_type: { type: "string", default: null }, + // // }, + // // }, + // // }, + // // outputConnections: { + // // type: "array", + // // maxItems: 2500, + // // items: { + // // type: "object", + // // properties: { + // // outputConnections: { type: "string", default: null }, + // // output_type: { type: "string", default: null }, + // // }, + // // }, + // // }, + // // }, + // // }, + // body: { + // type: "object", + // properties: { + // tankname: { type: "string" }, + // inputConnections: { + // type: "array", + // maxItems: 2500, + // items: { + // type: "object", + // properties: { + // inputConnections: { type: "string", default: null }, + // input_type: { type: "string", default: null }, + // inputismotor: { type: "boolean", default: true } + // }, + // }, + // }, + // outputConnections: { + // type: "array", + // maxItems: 2500, + // items: { + // type: "object", + // properties: { + // outputConnections: { type: "string", default: null }, + // output_type: { type: "string", default: null }, + // outputismotor: { type: "boolean", default: true } + // }, + // }, + // }, + // }, + // }, + // security: [ + // { + // basicAuth: [], + // }, + // ], + + // }, + // preHandler: fastify.auth([fastify.authenticate]), + // handler: createConnectionController.createConnections, + // // onResponse: (request, reply) => { + // // validationHandler.sendPhoneVerificationCode(request, reply); + // // }, + // //onResponse: validationHandler.sendPhoneVerificationCode, + // }); + + fastify.route({ - method: "POST", - url: "/api/createConnections/:customerId", - schema: { - tags: ["Connections"], - description: "This is for cretae New Connection", - summary: "This is for cretae New Connection.", - params: { + method: "POST", + url: "/api/createConnections/:customerId", + schema: { + tags: ["Connections"], + description: "This is for cretae New Connection", + summary: "This is for cretae New Connection.", + params: { required: ["customerId"], type: "object", properties: { - customerId: { - type: "string", - description: "customerId", - }, + customerId: { + type: "string", + description: "customerId", + }, }, - }, - body: { - type: "object", - properties: { + }, + body: { + type: "object", + properties: { tankname: { type: "string" }, - inputConnections: { - type: "array", - maxItems: 2500, - items: { - type: "object", - properties: { - inputConnections: { type: "string", default: null }, - input_type: { type: "string", default: null }, - }, - }, - }, - outputConnections: { - type: "array", - maxItems: 2500, - items: { - type: "object", - properties: { - outputConnections: { type: "string", default: null }, - output_type: { type: "string", default: null }, - }, - }, - }, - }, - }, - security: [ - { - basicAuth: [], - }, - ], - - }, - preHandler: fastify.auth([fastify.authenticate]), - handler: createConnectionController.createConnections, - // onResponse: (request, reply) => { - // validationHandler.sendPhoneVerificationCode(request, reply); - // }, - //onResponse: validationHandler.sendPhoneVerificationCode, - }); - - + inputConnections: { + type: "array", + maxItems: 2500, + items: { + type: "object", + properties: { + inputConnections: { type: "string", default: null }, + input_type: { type: "string", default: null }, + inputismotor: { type: "boolean", default: false }, + }, + }, + }, + outputConnections: { + type: "array", + maxItems: 2500, + items: { + type: "object", + properties: { + outputConnections: { type: "string", default: null }, + output_type: { type: "string", default: null }, + outputismotor: { type: "boolean", default: false }, + }, + }, + }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: createConnectionController.createConnections, + }); + fastify.route({ method: "PUT", @@ -95,6 +182,7 @@ module.exports = function (fastify, opts, next) { properties: { inputConnections: { type: "string", default: null }, type: { type: "string", default: null }, + inputismotor: { type: "boolean"}, }, }, }, @@ -106,6 +194,7 @@ module.exports = function (fastify, opts, next) { properties: { outputConnections: { type: "string", default: null }, type: { type: "string", default: null }, + outputismotor: { type: "boolean" }, }, }, },