/** * Copyright JS Foundation and other contributors, http://js.foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ const clone = require("clone"); const Flow = require('./Flow').Flow; const context = require('../context'); const util = require("util"); const redUtil = require("@node-red/util").util; const flowUtil = require("./util"); const credentials = require("../credentials"); var Log; /** * Create deep copy of object */ function deepCopy(obj) { return JSON.parse(JSON.stringify(obj)); } /** * Evaluate Input Value */ function evaluateInputValue(value, type, node) { if (type === "bool") { return (value === "true") || (value === true); } if (type === "cred") { return value; } return redUtil.evaluateNodeProperty(value, type, node, null, null); } /** * Compose information object for env var */ function composeInfo(info, val) { var result = { name: info.name, type: info.type, value: val, }; if (info.ui) { var ui = info.ui; result.ui = { hasUI: ui.hasUI, icon: ui.icon, labels: ui.labels, type: ui.type }; var retUI = result.ui; if (ui.type === "input") { retUI.inputTypes = ui.inputTypes; } if (ui.type === "select") { retUI.menu = ui.menu; } if (ui.type === "spinner") { retUI.spinner = ui.spinner; } } return result; } /** * This class represents a subflow - which is handled as a special type of Flow */ class Subflow extends Flow { /** * Create a Subflow object. * This takes a subflow definition and instance node, creates a clone of the * definition with unique ids applied and passes to the super class. * @param {[type]} parent [description] * @param {[type]} globalFlow [description] * @param {[type]} subflowDef [description] * @param {[type]} subflowInstance [description] */ constructor(parent,globalFlow,subflowDef,subflowInstance) { // console.log("CREATE SUBFLOW",subflowDef.id,subflowInstance.id,"alias?",subflowInstance._alias); // console.log("SubflowInstance\n"+JSON.stringify(subflowInstance," ",2)); // console.log("SubflowDef\n"+JSON.stringify(subflowDef," ",2)); var subflows = parent.flow.subflows; var globalSubflows = parent.global.subflows; var node_map = {}; var node; var wires; var i; var subflowInternalFlowConfig = { id: subflowInstance.id, configs: {}, nodes: {}, subflows: {} } if (subflowDef.configs) { // Clone all of the subflow config node definitions and give them new IDs for (i in subflowDef.configs) { if (subflowDef.configs.hasOwnProperty(i)) { node = createNodeInSubflow(subflowInstance.id,subflowDef.configs[i]); node_map[node._alias] = node; subflowInternalFlowConfig.configs[node.id] = node; } } } if (subflowDef.nodes) { // Clone all of the subflow node definitions and give them new IDs for (i in subflowDef.nodes) { if (subflowDef.nodes.hasOwnProperty(i)) { node = createNodeInSubflow(subflowInstance.id,subflowDef.nodes[i]); node_map[node._alias] = node; subflowInternalFlowConfig.nodes[node.id] = node; } } } subflowInternalFlowConfig.subflows = clone(subflowDef.subflows || {}); remapSubflowNodes(subflowInternalFlowConfig.configs,node_map); remapSubflowNodes(subflowInternalFlowConfig.nodes,node_map); // console.log("Instance config\n",JSON.stringify(subflowInternalFlowConfig,"",2)); super(parent,globalFlow,subflowInternalFlowConfig); this.TYPE = 'subflow'; this.subflowDef = subflowDef; this.subflowInstance = subflowInstance; this.node_map = node_map; this.path = parent.path+"/"+(subflowInstance._alias||subflowInstance.id); this.templateCredentials = credentials.get(subflowDef.id); this.instanceCredentials = credentials.get(this.id); var env = []; if (this.subflowDef.env) { this.subflowDef.env.forEach(e => { env[e.name] = e; if (e.type === "cred") { e.value = this.templateCredentials[e.name]; } }); } if (this.subflowInstance.env) { this.subflowInstance.env.forEach(e => { var old = env[e.name]; var ui = old ? old.ui : null; env[e.name] = e; if (ui) { env[e.name].ui = ui; } if (e.type === "cred") { if (!old || this.instanceCredentials.hasOwnProperty(e.name) ) { e.value = this.instanceCredentials[e.name]; } else if (old) { e.value = this.templateCredentials[e.name]; } } }); } this.env = env; } /** * Start the subflow. * This creates a subflow instance node to handle the inbound messages. It also * rewires an subflow internal node that is connected to an output so it is connected * to the parent flow nodes the subflow instance is wired to. * @param {[type]} diff [description] * @return {[type]} [description] */ start(diff) { var self = this; // Create a subflow node to accept inbound messages and route appropriately var Node = require("../Node"); if (this.subflowDef.status) { var subflowStatusConfig = { id: this.subflowInstance.id+":status", type: "subflow-status", z: this.subflowInstance.id, _flow: this.parent } this.statusNode = new Node(subflowStatusConfig); this.statusNode.on("input", function(msg) { if (msg.payload !== undefined) { if (typeof msg.payload === "string") { // if msg.payload is a String, use it as status text self.node.status({text:msg.payload}) return; } else if (Object.prototype.toString.call(msg.payload) === "[object Object]") { if (msg.payload.hasOwnProperty('text') || msg.payload.hasOwnProperty('fill') || msg.payload.hasOwnProperty('shape') || Object.keys(msg.payload).length === 0) { // msg.payload is an object that looks like a status object self.node.status(msg.payload); return; } } // Anything else - inspect it and use as status text var text = util.inspect(msg.payload); if (text.length > 32) { text = text.substr(0,32) + "..."; } self.node.status({text:text}); } else if (msg.status !== undefined) { // if msg.status exists if (msg.status.hasOwnProperty("text") && msg.status.text.indexOf("common.") === 0) { msg.status.text = "node-red:"+msg.status.text; } self.node.status(msg.status) } }) } var subflowInstanceConfig = { id: this.subflowInstance.id, type: this.subflowInstance.type, z: this.subflowInstance.z, name: this.subflowInstance.name, wires: [], _flow: this } if (this.subflowDef.in) { subflowInstanceConfig.wires = this.subflowDef.in.map(function(n) { return n.wires.map(function(w) { return self.node_map[w.id].id;})}) subflowInstanceConfig._originalWires = clone(subflowInstanceConfig.wires); } this.node = new Node(subflowInstanceConfig); this.node.on("input", function(msg) { this.send(msg);}); this.node.on("close", function() { this.status({}); }) this.node.status = status => this.parent.handleStatus(this.node,status); // Create a context instance console.log("Node.context",this.type,"id:",this._alias||this.id,"z:",this.z) this._context = context.get(this._alias||this.id,this.z); this.node._updateWires = this.node.updateWires; this.node.updateWires = function(newWires) { // Wire the subflow outputs if (self.subflowDef.out) { var node,wires,i,j; // Restore the original wiring to the internal nodes subflowInstanceConfig.wires = clone(subflowInstanceConfig._originalWires); for (i=0;i",nid, "(",node.type,")") // node_map[node.id] = node; node._alias = node.id; node.id = nid; node.z = subflowInstanceId; return node; } /** * Given an object of {id:nodes} and a map of {old-id:node}, modifiy all * properties in the nodes object to reference the new node ids. * This handles: * - node.wires, * - node.scope of Complete, Catch and Status nodes, * - node.XYZ for any property where XYZ is recognised as an old property * @param {[type]} nodes [description] * @param {[type]} nodeMap [description] * @return {[type]} [description] */ function remapSubflowNodes(nodes,nodeMap) { for (var id in nodes) { if (nodes.hasOwnProperty(id)) { var node = nodes[id]; if (node.wires) { var outputs = node.wires; for (j=0;j