|
1 | 1 | const express = require("express"); |
2 | 2 | const morgan = require("morgan"); |
3 | | -require("dotenv").config(); |
4 | 3 | require("dotenv").config({ path: __dirname + "/.env" }); |
5 | 4 |
|
6 | | -const { Client } = require("openrgb-sdk"); |
| 5 | +const { Client, utils } = require("openrgb-sdk"); |
7 | 6 |
|
8 | | -const app = express(); |
| 7 | +const app = express({}); |
9 | 8 | app.use(express.json()); |
10 | 9 | app.use(morgan("dev")); |
11 | 10 |
|
12 | | -const client = new Client("OpenRGB-REST-API", Number(process.env.OPENRGB_PORT), process.env.OPENRGB_HOST); |
| 11 | +const client = new Client("OpenRGB-REST-API", Number(process.env.OPENRGB_PORT) || 6742, process.env.OPENRGB_HOST || "127.0.0.1"); |
13 | 12 |
|
14 | | -app.get("/devices", async (req, res) => { |
| 13 | +app.use(async (req, res, next) => { |
15 | 14 | try { |
16 | 15 | await client.connect(); |
17 | | - const devices = await client.getAllControllerData(); |
18 | | - res.status(200).json(devices); |
19 | 16 | } catch (err) { |
20 | | - res.status(500).json({ error: err.message || "Failed to fetch devices" }); |
| 17 | + return res.status(500).json({ error: "Failed to connect to OpenRGB: " + err.message }); |
| 18 | + } |
| 19 | + |
| 20 | + res.on("finish", async () => { |
| 21 | + try { |
| 22 | + await client.disconnect(); |
| 23 | + } catch { |
| 24 | + // ignore disconnect errors |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + return next(); |
| 29 | +}); |
| 30 | + |
| 31 | +// GET /devices – list all devices |
| 32 | +app.get("/devices", async (req, res) => { |
| 33 | + res.status(200).json(await client.getAllControllerData()); |
| 34 | +}); |
| 35 | + |
| 36 | +// GET /devices/:id – get single device info |
| 37 | +app.get("/devices/:id", async (req, res) => { |
| 38 | + const id = parseInt(req.params.id); |
| 39 | + res.status(200).json(await client.getControllerData(id)); |
| 40 | +}); |
| 41 | + |
| 42 | +// TODO |
| 43 | +// POST /devices/:id/mode – set native mode |
| 44 | +// Body: { mode: <id or name>, save?: boolean } |
| 45 | +app.post("/devices/:id/mode", async (req, res) => { |
| 46 | + const id = parseInt(req.params.id); |
| 47 | + const { mode, save = false } = req.body; |
| 48 | + await client.updateMode(id, mode); |
| 49 | + if (save) await client.saveMode(id); |
| 50 | + res.status(200).json({ mode, saved: save }); |
| 51 | +}); |
| 52 | + |
| 53 | +// TODO |
| 54 | +// POST /devices/:id/leds – set all LEDs |
| 55 | +// Body: { colors: [{r,g,b}, ...] } or { color: "#ff00aa" } |
| 56 | +app.post("/devices/:id/leds", async (req, res) => { |
| 57 | + const id = parseInt(req.params.id); |
| 58 | + let color; |
| 59 | + let colors; |
| 60 | + if (req.body.color) color = req.body.color; |
| 61 | + if (req.body.colors) colors = req.body.colors; |
| 62 | + if (!colors && color) { |
| 63 | + colors = Array(req.body.count || 1).fill(utils.hexColor(color)); |
| 64 | + } |
| 65 | + await client.updateLeds(id, colors); |
| 66 | + res.status(200).json({ leds: colors.length }); |
| 67 | +}); |
| 68 | + |
| 69 | +// TODO |
| 70 | +// POST /devices/:id/color-all – set same color everywhere |
| 71 | +// Body: { color: "#00ff00" or {r,g,b} } |
| 72 | +app.post("/devices/:id/color-all", async (req, res) => { |
| 73 | + const id = parseInt(req.params.id); |
| 74 | + const c = req.body.color; |
| 75 | + const color = typeof c === "string" ? utils.hexColor(c) : utils.color(c.red, c.green, c.blue); |
| 76 | + await client.setColor(color, id); |
| 77 | + res.status(200).json({ color }); |
| 78 | +}); |
| 79 | + |
| 80 | +// POST /devices/:id/zone/:zoneId – set zone color(s) |
| 81 | +// Body: { colors: [...], fast?: boolean } |
| 82 | +app.post("/devices/:id/zone/:zoneId", async (req, res) => { |
| 83 | + const id = parseInt(req.params.id); |
| 84 | + const zoneId = parseInt(req.params.zoneId); |
| 85 | + const { colors, fast = false } = req.body; |
| 86 | + const device = (await client.getAllControllerData())[id]; |
| 87 | + const zone = device.zones.find((z) => z.id === zoneId); |
| 88 | + if (!zone) throw new Error("Zone not found"); |
| 89 | + await client.updateZoneColors(id, zoneId, colors, fast); |
| 90 | + res.status(200).json({ zone: zoneId }); |
| 91 | +}); |
| 92 | + |
| 93 | +// POST /devices/:id/profile – load or save profile |
| 94 | +// Body: { profile: name_or_index, save?: boolean } |
| 95 | +app.post("/devices/:id/profile", async (req, res) => { |
| 96 | + const id = parseInt(req.params.id); |
| 97 | + const { profile, save = false } = req.body; |
| 98 | + if (save) { |
| 99 | + await client.saveProfile(profile, false); |
| 100 | + res.status(200).json({ saved: profile }); |
| 101 | + } else { |
| 102 | + await client.loadProfile(profile, false); |
| 103 | + res.status(200).json({ loaded: profile }); |
21 | 104 | } |
22 | 105 | }); |
23 | 106 |
|
|
0 commit comments