สถาปัตยกรรมระบบ SafeQuakeSafeQuake System Architecture
โค้ดชุดเดียว ทำงาน 2 บทบาท: เครื่องอาคาร (Client) บน Windows ที่อ่านเซนเซอร์ในตึก และ SafeQuake Server ส่วนกลางใน Docker ที่เก็บบัญชี ข้อมูลหลัก และรับข้อมูลจากทุกอาคาร คลิกกล่องในแผนผังเพื่อดูรายละเอียด หรือกดเล่นลำดับงานเพื่อดูข้อมูลวิ่งOne code base with two roles: the building Client on Windows reads the sensors in the building, and the central SafeQuake Server in Docker keeps accounts and master data and collects data from every building. Click a box on the map for details, or play a flow to watch data move.
1. แผนผังระบบแบบโต้ตอบ1. Interactive system map
2. ลำดับงานแบบเคลื่อนไหว2. Animated flows
เลือกสถานการณ์ แล้วกดเล่น แผนผังด้านบนจะไฮไลต์กล่องและเส้นที่ทำงานในแต่ละขั้นPick a scenario and press play. The map above highlights the boxes and links used at each step.
3. ภายใน Gateway ของเครื่องอาคาร3. Inside the building gateway
Gateway เป็นโปรเซส Node แยกจากเว็บ เขียน SQLite ได้ตัวเดียว ทำงานแบบ live (ต่อเครื่องจริง) หรือ demo (เล่นข้อมูลบันทึก) ทีละโหมดThe gateway is a Node process separate from the web, the only writer of SQLite, running either live (real devices) or demo (replay), never both.
poller.mjsring.mjs · stream.mjsengine.mjs · intensity.mjsdb.mjsws.mjs · uplink.mjs · notify.mjs| โมดูลModule | หน้าที่Role | จังหวะ / ค่าTiming / values |
|---|---|---|
cube.mjs | อ่าน CUBE ผ่าน Modbus แบบอ่านอย่างเดียว: กฎโหวต สถานะ DIO เวลาreads CUBE over Modbus, read-only: vote rule, DIO, clock | ทุก 1 นาทีevery minute |
cube-hazus.mjs | เข้าเว็บของ CUBE ดึงผล HAZUS และไฟล์คลื่น 100 Hzlogs in to CUBE's web, fetches HAZUS results and 100 Hz files | ทุก 20 วิ หลังเหตุการณ์every 20 s after events |
hazus-config.mjs | อ่าน/เขียนความสูงใน hazus.cfg ผ่าน SFTPreads/writes heights in hazus.cfg over SFTP | เมื่อทีมสั่งon demand |
px01.mjs | อ่านสถานะรีเลย์ PX-01 ไม่สั่งการreads PX-01 relay status, never drives it | เมื่อเปิดหน้าwhen the page asks |
health.mjs | สุขภาพเครื่อง แรงดัน NTPdevice health, voltage, NTP | 60 s |
backup.mjs · retention.mjs | สำรอง VACUUM INTO เก็บ 7 ชุด · ลบข้อมูลเก่า (เหตุการณ์ไม่ลบ)VACUUM INTO backup, keep 7 · prune old rows (events kept) | 02:00 ทุกคืน · 1s 14 วัน · 1m 730 วัน · health 365 วัน02:00 nightly · 1s 14 d · 1m 730 d · health 365 d |
notify.mjs · notify-email.mjs | LINE · Telegram · Webhook · SMTP | ปิดเป็นค่าเริ่มต้น และปิดในโหมด mockoff by default, off in mock |
update-agent.mjs | ตรวจและดาวน์โหลดรุ่นใหม่ (Client เท่านั้น)checks and downloads releases (Client only) | ตอนเริ่ม ทุก 6 ชม. เมื่อ Server แจ้งat start, every 6 h, on notice |
network-warnings.mjs | ตรวจลายเซ็นคำเตือนจากเครือข่ายแล้วเก็บไว้verifies and keeps network warnings | เก็บ 1 วันkept 1 day |
4. การเชื่อมต่อเครื่องอาคารกับ Server4. Client ↔ Server link
เครื่องอาคารเป็นฝ่ายเชื่อมออกเสมอ Server ไม่ต่อเข้าอาคาร ทุกคำขอลงลายเซ็นด้วยกุญแจ Ed25519 ของเครื่องที่สร้างตอนจับคู่The building always connects out; the Server never connects in. Every request is signed with the machine's Ed25519 key made at pairing.
ช่องที่ 1: ส่งข้อมูลถาวร (Uplink)Channel 1: durable uplink
POST /sync/v1/ingest/<stream>· gzip · 2000 แถวต่อชุดrows per batch- ทุก 15 วิ: events, peaks, ไฟล์คลื่นEvery 15 s: events, peaks, waveform files
- ทุก 5 นาที: readings_1m, sensor_healthEvery 5 min: readings_1m, sensor_health
- cursor ขยับเมื่อ Server ตอบรับเท่านั้น ออฟไลน์แล้วส่งย้อนหลังได้ 60 วันcursor moves only on ack; backlog up to 60 days
ช่องที่ 2: ช่องสด (sq-link.v1)Channel 2: live link (sq-link.v1)
wss://<server>/ws→ Sync Hub- ขึ้น: hello, stat ทุกวินาที, flag เมื่อเกิดเหตุ, healthUp: hello, stat every second, flag on events, health
- ลง: ping, notice (update / check), warningDown: ping, notice (update / check), warning
- หลุดแล้วต่อใหม่ 1, 2, 5, 10, 30 วิreconnect 1, 2, 5, 10, 30 s
ช่องที่ 3: เอกสารและคำสั่งChannel 3: documents & commands
GET /sync/v1/docs?since=ทุก 15 วิevery 15 s: buildings, sensors, threshold-sets- คำสั่งแก้ค่า sq-change.v1 (ย้อนค่าเองถ้าอุปกรณ์ไม่ตอบ) · ทดสอบ sq-check.v1 · คำเตือน sq-warning.v1sq-change.v1 edits (self-rollback) · sq-check.v1 tests · sq-warning.v1 warnings
/sync/v1/config(MapTiler) ·/models(3D)
ช่องที่ 4: เข้าระบบด้วยการอนุมัติChannel 4: sign-in by approval
POST /sync/v1/auth/request→ รหัส 8 ตัว อายุ 5 นาที8-char code, 5 min- ผู้ใช้อนุมัติที่
/approveบน Server ด้วยโทรศัพท์user approves at/approveon the Server from a phone - เครื่องอาคารรับ grant sq-grant.v1 ตรวจลายเซ็น แล้วเข้าระบบClient fetches sq-grant.v1, verifies, signs in
- เครื่องอาคารไม่เก็บบัญชีและรหัสผ่านno accounts or passwords on the Client
5. เว็บแอปและสิทธิ์5. Web app & roles
Next.js 15 ผ่าน custom server (server/index.mjs) ที่ส่งต่อ /ws ไปยัง gateway ข้อมูลหลักเป็นไฟล์ JSON ใน data/ ข้อมูลอนุกรมเวลาอ่านจาก SQLite ของ gateway แบบอ่านอย่างเดียว กฎสิทธิ์อยู่ที่ src/shared/lib/scope.ts ใช้ร่วมกันทั้งหน้าเว็บ API และ WebSocketNext.js 15 behind a custom server (server/index.mjs) that forwards /ws to the gateway. Master data is JSON in data/; time series are read from the gateway's SQLite, read-only. One scope rule (src/shared/lib/scope.ts) serves pages, API and WebSocket.
| บัญชีAccount | เห็นSees | เข้าระบบที่Signs in at |
|---|---|---|
| ทีม SafeQuakeSafeQuake team platform | ทุกองค์กร ทุกตึกevery workspace and building | Server |
| ผู้ดูแลหลักWorkspace admin allBuildings | ทุกตึกขององค์กรตน และเชิญผู้ดูแลตึกได้ (รวม 20 บัญชี)all buildings of its workspace; invites building admins (20 accounts) | Server · เครื่องอาคารผ่านการอนุมัติServer · Client by approval |
| ผู้ดูแลตึกBuilding admin buildingIds | เฉพาะตึกที่กำหนดlisted buildings only | Server · เครื่องอาคารผ่านการอนุมัติServer · Client by approval |
หน้าหลักMain pages
/dashboard · /center · /buildings · /monitoring/[ws]/[b] · /station/… · /reports · /report/… · /report-document/[eventId] · /raw-data · /event-log
จัดการระบบAdmin
/admin/master/{accounts, buildings, thresholds, workspaces} · devices · connection · system · notifications · mock-test-data · forecast · sites
เข้าระบบAuth
next-auth JWT 8 ชม. · Server: ชื่อผู้ใช้ + รหัสผ่าน (bcrypt, จำกัด 5 ครั้ง/15 นาทีต่อชื่อ, 30 ต่อ IP) · Client: grant จาก Server เท่านั้นnext-auth JWT 8 h · Server: username + password (bcrypt, 5 per 15 min per name, 30 per IP) · Client: Server grant only
ข้อมูลภายนอกExternal data
กรมอุตุฯ (TMD) ทุก 300 วิ ผ่านโปรเซส tmd/ · แผนที่ MapTiler (กุญแจจาก Server) และแผนที่ OFFLINE ในตัวTMD every 300 s via the tmd/ process · MapTiler (key from the Server) and a built-in OFFLINE map
6. การอัปเดตเครื่องอาคารและการ deploy Server6. Client updates & Server deploy
Update Now
client-release.mjsสร้าง app.pack + runtime.pack + manifest ลงลายเซ็น Ed25519 (ช่อง dev)client-release.mjsbuilds app.pack + runtime.pack + a manifest signed with Ed25519 (dev channel)- อัปโหลดเข้า
/sync/v1/updates/…แล้ว Server ส่ง notice ผ่านช่องสดuploaded to/sync/v1/updates/…; the Server sends a notice over the live link - update-agent ตรวจลายเซ็นกับ release-keys.json แล้วดาวน์โหลดเบื้องหลัง (หยุดระหว่างเหตุการณ์และ 10 นาทีหลัง)update-agent checks against release-keys.json and downloads in the background (paused during events and 10 min after)
- ผู้ใช้กด Update Now → ทดสอบรุ่นใหม่ที่พอร์ต 4520–4522 → สำรองฐานข้อมูล → SafeQuakeUpdater สลับ current.jsonuser clicks Update Now → self-test on 4520–4522 → DB backup → SafeQuakeUpdater switches current.json
- ย้อนรุ่นเองถ้าไม่ขึ้นใน 45 วิ เซนเซอร์ไม่กลับใน 20 วิ หรือเว็บไม่รายงานรุ่นใน 60 วิauto-rollback if not up in 45 s, sensors not back in 20 s, or web not on the new version in 60 s
Deploy Server ทดสอบTest Server deploy
- push เข้า
release/1.0-w-mock= deploy ทันทีa push torelease/1.0-w-mockdeploys at once deploy-demo.yml: CI (typecheck, lint, test, build) แล้ว runner บน VM .10 รันsafequake-ci-deploy-demo.shdeploy-demo.yml: CI (typecheck, lint, tests, build), then the VM .10 runner runssafequake-ci-deploy-demo.sh- สร้างอิมเมจ
Dockerfile.live(node:22-slim) เป็นรุ่น server เริ่มด้วย--server --legacy-gatewaybuildsDockerfile.live(node:22-slim) as the server edition, started with--server --legacy-gateway - ตรวจ
/api/healthและประทับรุ่น2.0.0-dev.<run>+<commit>smoke-checks/api/healthand stamps2.0.0-dev.<run>+<commit>
- Server ทดสอบยังรัน gateway เต็มตัวที่มี Sync Hub อยู่ข้างใน (
--legacy-gateway) ไม่ใช่ Sync Hub แยกThe test Server still runs the full gateway with the Sync Hub inside (--legacy-gateway), not the standalone hub. - โปรเซส TMD ไม่ได้เริ่มจาก launch.mjs หรือสคริปต์ Docker ต้องรันแยก (pm2
safequake-tmd)The TMD process is not started by launch.mjs or the Docker script; it runs separately (pm2safequake-tmd). - ไฟล์
deploy/delivery-seedของสาขานี้ใส่ IP ของ S-04/S-05 King Square สลับกันdeploy/delivery-seedon this branch has King Square's S-04/S-05 IPs swapped. - เอกสารใน
docs/บางฉบับเก่ากว่าโค้ด (เช่น SYSTEM-OVERVIEW ยังเขียนก่อนมี Server/Client)Some files indocs/are older than the code (SYSTEM-OVERVIEW predates Server/Client).
7. ตารางอ้างอิง7. Reference tables
พอร์ตและโปรโตคอลPorts & protocols
| ที่Where | พอร์ตPort | อะไรWhat |
|---|---|---|
| เครื่องอาคารClient | 127.0.0.1:4510 / 4511 / 4512 | เว็บ / WebSocket ของ gateway / control (เปิดได้เฉพาะในเครื่อง)web / gateway WebSocket / control (local only) |
| เครื่องอาคารClient | 4520–4522 | ทดสอบรุ่นใหม่ก่อนสลับupdate self-test |
| เครื่องอาคารClient | UDP 18000 | สตรีมคลื่น 100 Hz จาก pALERTpALERT 100 Hz stream |
| เครือข่ายอาคารBuilding LAN | TCP 502 · HTTP · SFTP · UDP 123 | Modbus ของ S303/CUBE/PX-01 · เว็บของ CUBE · hazus.cfg · NTP ที่ CUBEModbus to S303/CUBE/PX-01 · CUBE web · hazus.cfg · NTP on CUBE |
| Server (Docker) | 127.0.0.1:3000 / 3001 | เว็บและ API / WebSocket + Sync Hub หลัง nginxweb & API / WebSocket + Sync Hub behind nginx |
| VM .10 nginx | 80 (default_server) | / → 3000, /ws → 3001 · เข้าจากภายนอกผ่าน Cloudflare Quick Tunnel/ → 3000, /ws → 3001 · public via Cloudflare Quick Tunnel |
ที่เก็บข้อมูลData stores
| ไฟล์File | ที่Where | เก็บอะไรHolds |
|---|---|---|
data/*.json | ทั้งสองboth | users, workspaces, buildings, sensors, threshold-sets, events, recommendations |
data/safequake.sqlite | gatewaygateway | readings_1s, readings_1m, events, event_sensor_peaks, sensor_health, cube_status, uplink_cursor |
data/events/<id>/ | gatewaygateway | ไฟล์คลื่น CSV และไฟล์ HAZUS จาก CUBEwaveform CSVs and HAZUS files from CUBE |
data/central.sqlite | Server | central_rows, central_batches, central_waveforms, central_docs, central_changes, central_checks |
data/sync-identity.json · sync-sites.json · sync-live.json | Client · Server · ServerClient · Server · Server | ตัวตนและกุญแจเครื่อง · รายชื่อเครื่องที่จับคู่ · สถานะช่องสดmachine identity · paired sites · live link state |
data/tmd.sqlite | tmd | tmd_events, tmd_stations, tmd_bulletins |
C:\ProgramData\SafeQuake\ | Client WindowsWindows Client | data · backups · logs · keys · updates |
โหมดข้อมูลData modes
| โหมดMode | ทำอะไรWhat |
|---|---|
| live | อ่านเครื่องจริงreal devices |
| mock | อุปกรณ์จำลองที่ 127.0.0.1:15020+ ฐานข้อมูลแยก data/mock/ ไม่แจ้งเตือน ไม่ส่งขึ้น Server ปิดเองใน 8 ชม. (สูงสุด 24)simulated devices on 127.0.0.1:15020+, separate data/mock/, no alerts or upload, ends in 8 h (max 24) |
| demo / replay | เล่นเหตุการณ์ที่บันทึกไว้ สำหรับสาธิตขายreplays a recorded event for sales demos |