chenyc
2026-03-22 7885cede659f3255be56f77c1eef2ada7387d6f1
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
139
140
141
142
143
144
145
146
147
148
149
150
151
const express = require("express");
const config = require("./config");
const logger = require("./logger");
 
function createCorsMiddleware() {
  return (req, res, next) => {
    if (!config.http.cors || !config.http.cors.enabled) {
      return next();
    }
    const origin = req.headers.origin || "*";
    const allowed =
      config.http.cors.origins.includes("*") ||
      config.http.cors.origins.includes(origin);
 
    if (allowed) {
      res.header("Access-Control-Allow-Origin", origin);
      res.header("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
      res.header("Access-Control-Allow-Headers", "Content-Type");
    }
 
    if (req.method === "OPTIONS") {
      return res.sendStatus(204);
    }
 
    next();
  };
}
 
function createHttpServer({ dataCache, rateLimiter, propertyMapper, deviceManager }) {
  if (!config.http || !config.http.enabled) {
    logger.info("HTTP server is disabled by config");
    return null;
  }
 
  const app = express();
  app.use(express.json({ limit: "1mb" }));
  app.use(createCorsMiddleware());
 
  app.get("/api/device/data", (req, res) => {
    const deviceNumber = req.query.deviceNumber;
    const mapped = String(req.query.mapped || "false").toLowerCase() === "true";
 
    if (!deviceNumber) {
      return res.status(400).json({ error: "deviceNumber is required" });
    }
 
    const limit = rateLimiter.checkLimit(
      `device:${deviceNumber}`,
      config.http.rateLimit.singleDeviceMs
    );
    if (!limit.allowed) {
      return res.status(429).json({
        error: "too many requests",
        waitMs: limit.waitMs
      });
    }
 
    const entry = dataCache.getDeviceData(deviceNumber);
    if (!entry) {
      return res.status(404).json({ error: "device not found" });
    }
 
    const base = mapped
      ? propertyMapper.transformForHTTP(entry.data, deviceNumber)
      : { deviceNumber, data: entry.data };
 
    return res.json({ ...base, updatedAt: entry.updatedAt });
  });
 
  app.get("/api/device/all", (req, res) => {
    const mapped = String(req.query.mapped || "false").toLowerCase() === "true";
 
    const limit = rateLimiter.checkLimit(
      "__all_devices__",
      config.http.rateLimit.allDevicesMs
    );
    if (!limit.allowed) {
      return res.status(429).json({
        error: "too many requests",
        waitMs: limit.waitMs
      });
    }
 
    const all = dataCache.getAllDeviceData();
    const result = {};
 
    for (const [deviceNumber, entry] of Object.entries(all)) {
      if (mapped) {
        result[deviceNumber] = {
          ...propertyMapper.transformForHTTP(entry.data, deviceNumber),
          updatedAt: entry.updatedAt
        };
      } else {
        result[deviceNumber] = {
          deviceNumber,
          data: entry.data,
          updatedAt: entry.updatedAt
        };
      }
    }
 
    return res.json(result);
  });
 
  app.get("/api/device/list", (req, res) => {
    return res.json(dataCache.getDeviceList());
  });
 
  app.get("/api/cache/stats", (req, res) => {
    return res.json(dataCache.getStats());
  });
 
  app.post("/api/cache/clear", (req, res) => {
    dataCache.clear();
    return res.json({ ok: true });
  });
 
  app.get("/api/device/idle", (req, res) => {
    const timeout = parseInt(req.query.timeout, 10);
    const timeoutMs = Number.isFinite(timeout) && timeout > 0 ? timeout : 5 * 60 * 1000;
    return res.json(dataCache.getIdleDevices(timeoutMs));
  });
 
  app.get("/api/ratelimit/stats", (req, res) => {
    return res.json(rateLimiter.getStats());
  });
 
  app.post("/api/ratelimit/clear", (req, res) => {
    rateLimiter.clear();
    return res.json({ ok: true });
  });
 
  app.get("/api/health", (req, res) => {
    res.json({
      status: "ok",
      tcp: "listening",
      devices: deviceManager ? deviceManager.getStats() : []
    });
  });
 
  const server = app.listen(config.http.port, config.http.host, () => {
    logger.info("HTTP server listening", {
      host: config.http.host,
      port: config.http.port
    });
  });
 
  return server;
}
 
module.exports = createHttpServer;