Skip to content

Instantly share code, notes, and snippets.

@masawada
Created January 5, 2025 10:51
Show Gist options
  • Save masawada/a84126cea236394f0309d41ba50415c6 to your computer and use it in GitHub Desktop.
Save masawada/a84126cea236394f0309d41ba50415c6 to your computer and use it in GitHub Desktop.
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { cors } from "hono/cors";
import { SwitchBotOpenAPI } from "node-switchbot";
// fetch RoomInfo
const roomInfo = {
temperature: 0,
humidity: 0,
co2: 0,
};
const apiClient = new SwitchBotOpenAPI(
process.env.SWITCHBOT_API_TOKEN,
process.env.SWITCHBOT_API_SECRET,
);
const refreshRoomInfo = async (): Promise<void> => {
const deskStatus = await apiClient.getDeviceStatus(
process.env.DESK_DEVICE_ID,
);
const co2Status = await apiClient.getDeviceStatus(process.env.CO2_DEVICE_ID);
roomInfo.temperature = deskStatus.response.body.temperature;
roomInfo.humidity = deskStatus.response.body.humidity;
roomInfo.co2 = co2Status.response.body.CO2;
console.log(new Date().toString(), JSON.stringify(roomInfo));
};
setInterval(refreshRoomInfo, process.env.REFRESH_INTERVAL);
refreshRoomInfo();
// server
const app = new Hono();
app.use(cors());
app.get("/roomInfo", (c) => {
return c.json({
html: `<p>${roomInfo.temperature}°C / ${roomInfo.humidity}%</p><p>CO2: ${roomInfo.co2}ppm</p>`,
});
});
const port = 3000;
console.log(`Server is running on http://localhost:${port}`);
serve({
fetch: app.fetch,
port,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment