-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipedream.mjs
More file actions
138 lines (119 loc) · 4.07 KB
/
pipedream.mjs
File metadata and controls
138 lines (119 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Scribe Evolution Printer - Pipedream MQTT Bridge
*
* A Pipedream component for sending messages to Scribe Evolution thermal printers via MQTT.
* This component receives HTTP requests and forwards them as MQTT messages to
* specified remote printers.
*
* Author: Adam Knowles
* License: Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)
*
* You are free to:
* - Share — copy and redistribute the material in any medium or format
* - Adapt — remix, transform, and build upon the material
*
* Under the following terms:
* - Attribution — You must give appropriate credit, provide a link to the license,
* and indicate if changes were made.
* - NonCommercial — You may not use the material for commercial purposes.
* - ShareAlike — If you remix, transform, or build upon the material, you must
* distribute your contributions under the same license as the original.
*
* For full license text, see: https://creativecommons.org/licenses/by-nc-sa/4.0/
*/
import mqtt from "mqtt";
function mqttPublishAsync(client, topic, payload) {
return new Promise((resolve, reject) => {
client.publish(topic, payload, { qos: 1 }, (err) => {
if (err) reject(err);
else resolve();
});
});
}
function mqttConnectAsync(opts) {
return new Promise((resolve, reject) => {
const client = mqtt.connect(opts);
client.once("connect", () => resolve(client));
client.once("error", (err) => reject(err));
});
}
export default defineComponent({
props: {},
async run({ steps, $ }) {
let host = process.env.MQTT_host;
const port = process.env.MQTT_port;
const username = process.env.MQTT_username;
const password = process.env.MQTT_password;
if (!host || !port || !username || !password) {
const missingEnvVars = [];
if (!host) missingEnvVars.push("MQTT_host");
if (!port) missingEnvVars.push("MQTT_port");
if (!username) missingEnvVars.push("MQTT_username");
if (!password) missingEnvVars.push("MQTT_password");
await $.respond({
status: 500,
body: {
error: `Missing required environment variables: ${missingEnvVars.join(", ")}`,
},
});
return;
}
if (host.includes(":")) {
host = host.split(":")[0];
}
// Extract data from HTTP POST body (steps.trigger.event is already an object)
const data = steps.trigger.event || {};
const remote_printer = data.remote_printer;
const header = data.header;
const body = data.body;
const sender = data.sender;
const timestamp = data.timestamp;
// Validate required fields
const missingFields = [];
if (!remote_printer) missingFields.push("remote_printer");
if (!header) missingFields.push("header");
if (!body) missingFields.push("body");
if (!sender) missingFields.push("sender");
if (missingFields.length > 0) {
await $.respond({
status: 400,
body: { error: `Missing required fields: ${missingFields.join(", ")}` },
});
return;
}
const topic = `scribe/${remote_printer}/print`;
// Build payload with header, body, sender, and optional timestamp
const payloadData = { header, body, sender };
if (timestamp) payloadData.timestamp = timestamp;
const payload = JSON.stringify(payloadData);
try {
const client = await mqttConnectAsync({
host,
port: Number(port),
protocol: "mqtts",
username,
password,
rejectUnauthorized: false,
});
await mqttPublishAsync(client, topic, payload);
console.log(
`MQTT message published with broker confirmation to topic: ${topic} (${payload.length} bytes)`,
);
await new Promise((resolve) => {
client.end(true, () => {
client.removeAllListeners();
resolve();
});
});
await $.respond({
status: 200,
body: { status: "Published", topic, payload },
});
} catch (err) {
await $.respond({
status: 500,
body: { error: `MQTT Error: ${err.message}` },
});
}
},
});