|
| 1 | +import * as RED from "node-red"; |
| 2 | +import { Red } from "node-red"; |
| 3 | +import { NoderedUtil } from "./NoderedUtil"; |
| 4 | +import { Logger } from "../../Logger"; |
| 5 | +import { amqp_consumer } from "../../amqp_consumer"; |
| 6 | +import { amqp_publisher } from "../../amqp_publisher"; |
| 7 | +import { Config } from "../../Config"; |
| 8 | + |
| 9 | +export interface Irpa_detector_node { |
| 10 | + queue: string; |
| 11 | + noack: boolean; |
| 12 | +} |
| 13 | +export class rpa_detector_node { |
| 14 | + public node: Red = null; |
| 15 | + public name: string = ""; |
| 16 | + public con: amqp_consumer; |
| 17 | + public host: string = null; |
| 18 | + constructor(public config: Irpa_detector_node) { |
| 19 | + RED.nodes.createNode(this, config); |
| 20 | + try { |
| 21 | + this.node = this; |
| 22 | + this.node.on("close", this.onclose); |
| 23 | + this.host = Config.amqp_url; |
| 24 | + this.connect(); |
| 25 | + } catch (error) { |
| 26 | + NoderedUtil.HandleError(this, error); |
| 27 | + } |
| 28 | + } |
| 29 | + async connect() { |
| 30 | + try { |
| 31 | + this.node.status({ fill: "blue", shape: "dot", text: "Connecting..." }); |
| 32 | + this.con = new amqp_consumer(Logger.instanse, this.host, this.config.queue); |
| 33 | + this.con.OnMessage = this.OnMessage.bind(this); |
| 34 | + await this.con.connect(this.config.noack); |
| 35 | + this.node.status({ fill: "green", shape: "dot", text: "Connected" }); |
| 36 | + } catch (error) { |
| 37 | + NoderedUtil.HandleError(this, error); |
| 38 | + } |
| 39 | + } |
| 40 | + async OnMessage(msg: any, ack: any) { |
| 41 | + try { |
| 42 | + var result: any = {}; |
| 43 | + result.amqpacknowledgment = ack; |
| 44 | + |
| 45 | + var data = JSON.parse(msg.content.toString()); |
| 46 | + try { |
| 47 | + data.payload = JSON.parse(data.payload); |
| 48 | + } catch (error) { |
| 49 | + } |
| 50 | + result.payload = data.payload; |
| 51 | + result.jwt = data.jwt; |
| 52 | + this.node.send(result); |
| 53 | + } catch (error) { |
| 54 | + NoderedUtil.HandleError(this, error); |
| 55 | + } |
| 56 | + } |
| 57 | + onclose() { |
| 58 | + if (!NoderedUtil.IsNullUndefinded(this.con)) { |
| 59 | + this.con.close(); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +export interface Irpa_workflow_node { |
| 67 | + queue: string; |
| 68 | + workflow: string; |
| 69 | + localqueue: string; |
| 70 | +} |
| 71 | +export class rpa_workflow_node { |
| 72 | + public node: Red = null; |
| 73 | + public name: string = ""; |
| 74 | + public con: amqp_publisher; |
| 75 | + public host: string = null; |
| 76 | + constructor(public config: Irpa_workflow_node) { |
| 77 | + RED.nodes.createNode(this, config); |
| 78 | + try { |
| 79 | + this.node = this; |
| 80 | + this.node.on("input", this.oninput); |
| 81 | + this.node.on("close", this.onclose); |
| 82 | + this.host = Config.amqp_url; |
| 83 | + this.connect(); |
| 84 | + } catch (error) { |
| 85 | + NoderedUtil.HandleError(this, error); |
| 86 | + } |
| 87 | + } |
| 88 | + async connect() { |
| 89 | + try { |
| 90 | + this.node.status({ fill: "blue", shape: "dot", text: "Connecting..." }); |
| 91 | + this.con = new amqp_publisher(Logger.instanse, this.host, this.config.localqueue); |
| 92 | + this.con.OnMessage = this.OnMessage.bind(this); |
| 93 | + await this.con.connect(); |
| 94 | + this.node.status({ fill: "green", shape: "dot", text: "Connected" }); |
| 95 | + } catch (error) { |
| 96 | + NoderedUtil.HandleError(this, error); |
| 97 | + } |
| 98 | + } |
| 99 | + async OnMessage(msg: any, ack: any) { |
| 100 | + try { |
| 101 | + var result: any = {}; |
| 102 | + result.amqpacknowledgment = ack; |
| 103 | + var json: string = msg.content.toString(); |
| 104 | + var data = JSON.parse(json); |
| 105 | + result.jwt = data.jwt; |
| 106 | + |
| 107 | + if (data.payload.command == "invokecompleted") { |
| 108 | + result.payload = data.payload.data; |
| 109 | + this.node.send(result); |
| 110 | + } |
| 111 | + else if (data.payload.command == "invokefailed" || data.payload.command == "invokeaborted") { |
| 112 | + result.payload = data.payload; |
| 113 | + this.node.send([null, null, result]); |
| 114 | + } |
| 115 | + else { |
| 116 | + result.payload = data.payload; |
| 117 | + this.node.send([null, result]); |
| 118 | + } |
| 119 | + // this.node.send(result); |
| 120 | + } catch (error) { |
| 121 | + NoderedUtil.HandleError(this, error); |
| 122 | + } |
| 123 | + } |
| 124 | + async oninput(msg: any) { |
| 125 | + try { |
| 126 | + this.node.status({}); |
| 127 | + var rpacommand = { |
| 128 | + command: "invoke", |
| 129 | + workflowid: this.config.workflow, |
| 130 | + data: msg.payload |
| 131 | + } |
| 132 | + var data = { |
| 133 | + jwt: msg.jwt, |
| 134 | + payload: rpacommand |
| 135 | + } |
| 136 | + this.con.SendMessage(JSON.stringify(data), this.config.queue); |
| 137 | + // var data: any = {}; |
| 138 | + // data.payload = msg.payload; |
| 139 | + // data.jwt = msg.jwt; |
| 140 | + // this.con.SendMessage(JSON.stringify(data), this.config.queue); |
| 141 | + this.node.status({}); |
| 142 | + } catch (error) { |
| 143 | + NoderedUtil.HandleError(this, error); |
| 144 | + } |
| 145 | + } |
| 146 | + onclose() { |
| 147 | + if (!NoderedUtil.IsNullUndefinded(this.con)) { |
| 148 | + this.con.close(); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +export async function get_rpa_detectors(req, res) { |
| 154 | + var token = await NoderedUtil.GetToken(null, null); |
| 155 | + var result: any[] = await NoderedUtil.Query('openrpa', { _type: "detector" }, |
| 156 | + { name: 1 }, { name: -1 }, 1000, 0, token.jwt) |
| 157 | + res.json(result); |
| 158 | +} |
| 159 | +export async function get_rpa_robots(req, res) { |
| 160 | + var token = await NoderedUtil.GetToken(null, null); |
| 161 | + var result: any[] = await NoderedUtil.Query('users', { _type: "user" }, |
| 162 | + { name: 1 }, { name: -1 }, 1000, 0, token.jwt) |
| 163 | + res.json(result); |
| 164 | +} |
| 165 | +export async function get_rpa_workflows(req, res) { |
| 166 | + var token = await NoderedUtil.GetToken(null, null); |
| 167 | + var q: any = { _type: "workflow" }; |
| 168 | + if (req.query.queue != null && req.query.queue != undefined) { |
| 169 | + q = { _type: "workflow", $or: [{ _createdbyid: req.query.queue }, { _modifiedbyid: req.query.queue }] }; |
| 170 | + } |
| 171 | + var result: any[] = await NoderedUtil.Query('openrpa', q, |
| 172 | + { name: 1 }, { name: -1 }, 1000, 0, token.jwt) |
| 173 | + res.json(result); |
| 174 | +} |
0 commit comments