From 3537906a6e250fd438358d23cedca36cafd8ad2c Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 28 Sep 2023 13:14:17 -0700 Subject: clean up mqtt publish --- lib/mqtt.js | 4 ++ lib/payments.js | 114 +++----------------------------------------------------- 2 files changed, 9 insertions(+), 109 deletions(-) create mode 100644 lib/mqtt.js (limited to 'lib') diff --git a/lib/mqtt.js b/lib/mqtt.js new file mode 100644 index 00000000..df87f42e --- /dev/null +++ b/lib/mqtt.js @@ -0,0 +1,4 @@ +import mqtt from "mqtt"; +import config from "$config"; + +export default mqtt.connect("mqtt://ln.coinos.io", config.mqtt); diff --git a/lib/payments.js b/lib/payments.js index b17dfde6..d3bb275c 100644 --- a/lib/payments.js +++ b/lib/payments.js @@ -9,12 +9,12 @@ import { fail, wait } from "$lib/utils"; import { callWebhook } from "$lib/webhooks"; import dollarsToWords from "dollars-to-words"; import got from "got"; -import mqtt from "mqtt"; import { spawn } from "child_process"; import ffmpeg from "fluent-ffmpeg"; import fs from "fs"; import EventEmitter from "events"; import crc32 from "buffer-crc32"; +import mqtt from "$lib/mqtt"; export const types = { internal: "internal", @@ -180,114 +180,10 @@ export let credit = async (hash, amount, memo, ref, type = types.internal) => { notify(user, `Received ${amount} sats`); callWebhook(invoice, p); - if (user.hasprinter) { - - let client = mqtt.connect("mqtt://ln.coinos.io", config.mqtt); - client.subscribe(username); - - let connected; - - if (user.hasspeaker) { - let fiat = parseFloat(((amount * p.rate) / 100000000).toFixed(2)); - let words = dollarsToWords(fiat); - - if (fiat === 0.02) { - words = "there is no second best"; - client.publish(username, "easter"); - } - - if (fiat === 0.21) { - words = "tick tock, next block"; - client.publish(username, "easter"); - } - - if (fiat === 0.69) { - words = "have fun staying poor"; - client.publish(username, "easter"); - } - - let raw = `/app/data/uploads/${p.id}-raw.mp3`; - let out = `/app/data/uploads/${p.id}.mp3`; - - let mimic = spawn("/mimic", [ - "-t", - words, - "-voice", - "/cmu_us_axb.flitevox", - "-o", - raw - ]); - - mimic.stdout.on("end", async () => { - await wait(() => connected); - client.publish(username, "start"); - - let n = 0; - let t = 0; - let sz = 480; - let chunks = []; - let buf = Buffer.alloc(0); - - let send = n => { - let chunk = chunks[n]; - if (!chunk) return; - - let num = Buffer.alloc(2); - num.writeUInt16BE(n, 0); - - let flag = Buffer.from("dddddddd", "hex"); - let checksum = crc32(chunk).reverse(); - - let payload = Buffer.concat([flag, num, checksum, chunk]); - client.publish(username, payload); - }; - - client.on("message", (topic, message) => { - if (topic !== username) return; - let m = message.toString(); - if (m.startsWith("missing:")) { - let missing = m.replace("missing:", "").split(","); - - for (let n of missing) send(chunk); - client.publish(username, `end:${n}`); - } - }); - - ffmpeg(raw) - .audioBitrate(128) - .audioFrequency(44100) - .toFormat("mp3") - .on("error", err => { - console.log("Error:", err); - }) - .stream() - .on("data", data => { - t += data.length; - let i = 0; - buf = Buffer.concat([buf, data]); - - while (i < data.length) { - chunks[n] = data.slice(i, i + sz); - send(n); - - n++; - i += sz; - } - }) - .on("end", () => { - client.publish(username, `end:${chunks.length}`); - }); - }); - } - - client.on("connect", function() { - client.publish( - username, - `pay:${p.amount}:${p.tip}:${p.rate}:${p.created}:${p.id}` - ); - setTimeout(() => (connected = true), 50); - }); - } + mqtt.publish( + username, + `pay:${p.amount}:${p.tip}:${p.rate}:${p.created}:${p.id}` + ); return p; }; -- cgit v1.2.3