UNPKG

grunig-nodes

Version:

Grünig Nodes Library

309 lines (287 loc) 14.6 kB
module.exports = function(RED) { function G_FrameManager(config) { RED.nodes.createNode(this, config); var node = this; var mysql = require('mysql'); var mssql = require('mssql'); node.databaseConfig = RED.nodes.getNode(config.database); if (this.databaseConfig) { } else { // Keine Konfigurationsnode konfiguriert node.error("Keine Konfigurationsnode konfiguriert"); } // MySQL-Verbindungsinformationen var host = node.databaseConfig.host; var port = node.databaseConfig.port; var user = node.databaseConfig.user; var password = node.databaseConfig.password; var database = node.databaseConfig.database; var mssqlSelected = node.databaseConfig.mssql; node.status({fill:"red",shape:"ring",text:"disconnected"}); var connection; var connected = false; var sqlResult = null; var msgData = null; connect(); // Verbindung zur MySQL-Datenbank herstellen, wenn gültige Verbindungsinformationen vorhanden sind function connect() { node.status({ fill: "green", shape: "ring", text: "connecting..." }); if (host && port && user && password && database) { if (mssqlSelected) { // MSSQL-Konfiguration const sqlConfig = { user: user, password: password, server: host, port: parseInt(port, 10), // Port in Zahl umwandeln database: database, options: { encrypt: true, enableArithAbort: true, trustServerCertificate: true // Akzeptiert selbstsignierte Zertifikate } }; connection = new mssql.ConnectionPool(sqlConfig); connection.connect().then(() => { node.status({ fill: "green", shape: "dot", text: "connected to MSSQL" }); connected = true; }).catch(err => { node.status({ fill: "red", shape: "ring", text: "ERROR: " + err.message }); node.error("Error connecting to MSSQL: " + err.message); }); } else { // MySQL-Verbindung connection = mysql.createConnection({ host: host, port: port, user: user, password: password, database: database }); connection.connect(function (err) { if (err) { node.status({ fill: "red", shape: "ring", text: "ERROR: " + err.message }); node.error("Error connecting to MySQL: " + err.message); } else { node.status({ fill: "green", shape: "dot", text: "connected to MySQL" }); connected = true; } }); } } else { node.error("Missing connection information"); } } // Function to close the database connection function disconnect() { if (mssqlSelected && connection) { connection.close().then(() => { node.status({ fill: "red", shape: "ring", text: "disconnected from MSSQL" }); connected = false; }).catch(err => { node.error("Error disconnecting from MSSQL: " + err.message); }); } else if (connection) { connection.end(err => { if (err) { node.error("Error disconnecting from MySQL: " + err.message); } else { node.status({ fill: "red", shape: "ring", text: "disconnected from MySQL" }); connected = false; } }); } } function getSQLresult(topic, callback) { try { if (connection && topic) { connection.query(topic, function(error, results, fields) { if (error) { node.error("Error executing MySQL query: " + error.message + " | " + topic); return callback(error, null); } // Ergebnis zur Nachricht hinzufügen und senden callback(null, results); }); } } catch (error) { node.error("Error executing MySQL query: " + error.message + " | " + topic); callback(error, null); } } // Nachrichtenverarbeitungsfunktion node.on('input', function(msg) { if (!connection || connected === false) { host = node.databaseConfig.host; port = node.databaseConfig.port; user = node.databaseConfig.user; password = node.databaseConfig.password; database = node.databaseConfig.database; connect(); } else { // Einstellungen speichern var inputFrameId = config.frameId; var inputFunct = config.function; var inputProcess = config.process; var sqlDataRecieved = false; var frameId, funct, process, processPN, processPC; if (inputFrameId === ""){ frameId = msg.payload.FrameID; } else { frameId = inputFrameId } if (inputProcess === "none"){ process = msg.payload.Process; } else { process = inputProcess; } processPN = process + "PN"; processPC = process + "PC"; if (inputFunct === "none") { funct = msg.payload.Function; } else { funct = inputFunct; } msg.payload = {} msg.payload.frameID = frameId; if (frameId != 0) { // MySQL-Verbindung verwenden, um die Abfrage auszuführen if (funct == "ReadAllData") { topic = ` SELECT FrameId.FrameId, Frame.*, FrameData.ManufacturingDate, ProfileType.Profile, Material.Material, Mesh.*, MeshData.*, MeshAngle.Angle, Program.*, Customized.* FROM FrameId LEFT JOIN Frame ON FrameId.FKey_Frame = Frame.PKey_Frame LEFT JOIN FrameData ON FrameId.FKey_FrameData = FrameData.PKey_FrameData LEFT JOIN ProfileType ON Frame.FKey_ProfileType = ProfileType.PKey_ProfileType LEFT JOIN Material ON Frame.FKey_Material = Material.PKey_Material LEFT JOIN Mesh ON FrameId.FKey_Mesh = Mesh.PKey_Mesh LEFT JOIN MeshData ON FrameId.FKey_MeshData = MeshData.PKey_MeshData LEFT JOIN MeshAngle ON MeshData.FKey_MeshAngle = MeshAngle.PKey_MeshAngle LEFT JOIN Program ON FrameId.FKey_Program = Program.PKey_Program LEFT JOIN Customized ON FrameId.FKey_Customized = Customized.PKey_Customized WHERE FrameId.FrameId = '` + frameId + `'`; } else { topic = ` SELECT FrameId.PKey_FrameId, Program.*, Frame.TypNo FROM FrameId LEFT JOIN Program ON FrameId.FKey_Program = Program.PKey_Program LEFT JOIN Frame ON FrameId.FKey_Frame = Frame.PKey_Frame WHERE FrameId.FrameId = '` + frameId + `'`; } // MySQL-Verbindung verwenden, um die Abfrage auszuführen getSQLresult(topic, function(error, result) { if (error) { node.error("Error executing MySQL query: " + error.message); sqlDataRecieved = false; } else { if (mssqlSelected) { result = result.recordset; } sqlResult = result; var programNumber, frameNumber; if (Array.isArray(sqlResult) && sqlResult.length === 0) { msgData = {payload: "Error01"}; node.send(msgData); } else if (funct === "ReadProgram") { if (sqlResult[0]["TypNo"]) { frameNumber = sqlResult[0]["TypNo"]; } if (sqlResult[0][processPN]) { programNumber = sqlResult[0][processPN]; } msgData = { payload: { "ProgramNumber": programNumber, "FrameNumber": frameNumber } } node.send(msgData); } else if (funct === "ReadAllData") { msgData = {payload: sqlResult}; node.send(msgData); } if (funct === "CounterIncrease") { var programId = sqlResult[0].PKey_Program; // Hinzufügen der Endung zum CounterValue var counterValue = parseInt(sqlResult[0][processPC]) + 1; if (mssqlSelected) { var topic = ` UPDATE Program SET [${processPC}] = '${counterValue}' WHERE PKey_Program = '${programId}'`; } else { var topic = ` UPDATE Program SET \`${processPC}\` = '${counterValue}' WHERE PKey_Program = '${programId}'`; } msgData = { payload: { programId: programId, counterValue: counterValue }, }; if (processPC && counterValue && programId) { getSQLresult(topic, function(error, result) { if (error) { node.error("Error executing MySQL query: " + error.message); sqlDataRecieved = false; } else { sqlResult = result; node.send(msgData); } }); } else { node.error("no valid input | " + "processPC: " + processPC + "counterValue: " + counterValue + "programId: " + programId); } } } }); } else { node.error("FrameID is not allowed to be 0"); } } }); // Node wird geschlossen node.on('close', function() { disconnect(); }); } // Node-Typ registrieren RED.nodes.registerType("G-FrameManager", G_FrameManager); };