| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import { NextResponse } from "next/server";
- import automation from "data/automation.json";
- import { pick } from "lib/utils";
- const get = (url) => {
- return fetch(url, {
- headers: { "Content-Type": "application/json" },
- redirect: "follow",
- cache: "no-store",
- mode: "cors"
- })
- .then((resp) => resp && resp.json())
- .then((result) => result);
- };
- const status = ({ client }) => {
- let url = `http://${client}/json/state`;
- return get(url)
- .then((resp) => resp?.state || resp || {})
- .then((state) => pick(state, ["on", "bri", "ps", "pl", "udpn"]))
- .then((state) => ({ ...state, client }))
- .catch((err) => ({ error: err.message }));
- };
- export async function GET(req, { params }) {
- let promises = [];
- let id = 1;
- try {
- if (!id) throw new Error("No id specified");
- let clients = (automation?.[id] && Object.keys(automation?.[id])) || [];
- if (!clients) throw new Error("No clients for id", id);
- for (let client of clients) {
- promises.push(status({ id, client, ...automation?.[id]?.[client] }));
- }
- } catch (err) {
- return NextResponse.json({ error: err?.message }, { status: 500 });
- }
- return Promise.allSettled(promises)
- .then((results) => {
- return NextResponse.json(results?.map((o) => o?.value));
- })
- .catch((err) => {
- return NextResponse.json({ error: err?.message }, { status: 500 });
- });
- }
|