Commit Graph

25 Commits

Author SHA1 Message Date
caoqianming a7f5c85032 Persist event subject_type/subject_id and add ops event timeline API
EventInsert + EventRecord + record_event now carry the subject_type /
subject_id columns added by the P1 migration. Ops events populate
"segment" / "station" subjects so the timeline can be filtered without
parsing event_type strings. Platform SourceCreated / Updated / Deleted
attribute themselves to subject_type="source". Adds get_events_*_filtered
in core and exposes GET /api/event on ops with event_type /
event_type_prefix / subject_type / subject_id query params, closing
design doc §14 "event 表能按 ops.* 和 subject_type/subject_id 查到全链路事件".

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-19 08:57:40 +08:00
caoqianming 19ace9c2be Move feeder unit model out of platform core
Lifts ControlUnit, UnitRuntime/State, ControlRuntimeStore and unit CRUD
into app_feeder_distributor; their feeder-specific semantics
(DistributorRunning state, run_time/stop_time/acc_time/bl_time pacing)
violated the shared-core invariant.

WsMessage drops the UnitRuntimeChanged variant in favor of a generic
AppEvent(AppWsEvent) carrying {app, event_type, data}. Feeder emits
"feeder"/"unit_runtime_changed"; the web client dispatches by app
namespace, leaving core free of business types. The equipment handler
keeps its friendly unit-exists check by issuing an inline EXISTS query
instead of pulling the unit service.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 21:38:52 +08:00
caoqianming 2a6dde9e0e Restore SIMULATE_PLC chaos task and run feedback
Bring back crates/app_feeder_distributor/src/control/simulate.rs which
was dropped when feeder's local connection/telemetry/websocket modules
moved into core; the restored module reuses core's equivalents via
plc_platform_core::{connection, service, telemetry, websocket}.

Also restore the call sites that were lost alongside it:
- app.rs starts the chaos task when SIMULATE_PLC=true
- engine.rs fires simulate_run_feedback after each pulse command so
  the auto-control state machine sees the RUN bit transition it
  would get from a real PLC
- handler/control.rs does the same after manual start/stop commands

The SIMULATE_PLC flag is now read via simulate::enabled() from the
environment rather than state.config.simulate_plc (the old config
struct was removed with the module migration). To expose equipment
ids by kind (used for run feedback), build_equipment_maps now also
returns HashMap<kind, Uuid>.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-22 10:33:51 +08:00
caoqianming 3b92c0028a Merge handle_control_event and persist_event_if_needed
The split existed only so handle_control_event could log
UnitStateChanged and then delegate; that's no longer worth its own
function. Co-locating the UnitStateChanged special case with the other
match arms makes its 'log-only, no persist' treatment self-evident,
and the call chain drops from handle → persist → record to handle →
record.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-22 08:39:17 +08:00
caoqianming 6c8e5561dc Cache unit/equipment codes for event messages
Add MetadataCache to PlatformContext — a lazy-loaded, cross-app cache
of code fields used when formatting event messages. Each persisted
AppEvent previously did 1-2 extra SELECTs to look up the code for its
human-readable message; after this change the same id hits the cache
on all subsequent events.

Invalidation: the platform-owned equipment handler invalidates its
entry on update/delete; feeder's unit handler does the same for
units. Deletes are invalidated for hygiene only — no further events
should target a deleted id.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-22 08:21:46 +08:00
caoqianming 3e0d4c242b Emit tracing inside record_event and drop duplicate feeder match
record_event now logs at the matching tracing level (error/warn/info)
using the persisted message — giving every app uniform event logs for
free. Feeder's handle_control_event collapses from a 60-line match
(which just duplicated the persisted message with less-readable UUIDs)
to a single if-let for UnitStateChanged, which is the only AppEvent
that is intentionally not persisted.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-21 20:11:59 +08:00
caoqianming 1c646dfaa7 Extract event persistence primitive to platform core
Move the INSERT + WebSocket broadcast mechanism out of the feeder app
and into plc_platform_core as pub record_event(pool, ws, EventInsert).
The event table schema is owned by core, so writing to it is a platform
capability — apps (feeder, future ops) should only decide what to emit,
not how to persist it. Also replaces the 7-tuple in core's
persist_and_broadcast with the named EventInsert struct for readability.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-21 20:04:25 +08:00
caoqianming 58fdb9f58e Clean up clippy warnings and improve code organization
- engine.rs: use is_none_or instead of map_or, remove redundant closures
- service/control.rs: move get_equipment_role_points before test module
- event.rs: replace complex 7-element tuple with PersistableEvent struct

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-21 19:49:27 +08:00
caoqianming 706fb4f72a Simplify imports in feeder control handler
Replace verbose fully-qualified paths with top-level use imports
for plc_platform_core types and services throughout control.rs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-21 19:45:46 +08:00
caoqianming 087f016f01 Remove dead handler re-exports from feeder
The feeder router uses platform_routes() directly from core,
making these re-export modules unused. The only actual dependency
(SignalRolePoint) now references plc_platform_core directly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-21 19:32:20 +08:00
caoqianming dabcde1fca Remove unit change platform event plumbing 2026-04-21 19:12:09 +08:00
caoqianming a49f6adf9b Format feeder event module 2026-04-21 16:24:26 +08:00
caoqianming f8ba864a65 Clean feeder core dependency boundaries 2026-04-21 16:22:11 +08:00
caoqianming 24b1d3546b Move shared feeder plumbing into core 2026-04-21 16:04:03 +08:00
caoqianming 1317271e16 refactor(core): centralize telemetry, connection management, and event sink in platform
Move TelemetryProcessor (PointNewValue batching/dedup/broadcast) from feeder to
plc_platform_core. PlatformBuilder.build() now auto-wires telemetry processing and
reconnect task. PlatformContext.emit_event() handles connection management side effects
(connect/reconnect/disconnect/subscribe/unsubscribe) directly. Simplify PlatformEventSink
trait from 6 methods to single on_event(). Feeder's AppEvent now only contains business
events; FeederPlatformEventSink only handles UnitsChanged for control runtime.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-21 15:30:45 +08:00
caoqianming 6814e9eae9 refactor: migrate platform handlers to core, centralize routes and event persistence
- Move source/point/equipment/tag/page handlers from feeder to plc_platform_core
  using State<PlatformContext>; feeder re-exports via handler modules
- Keep batch_set_point_value in feeder (requires app-specific write key auth)
- Add PlatformEvent enum and persist_and_broadcast() in core for platform event
  persistence to DB + WebSocket broadcast
- Add PlatformContext::emit_event() that handles both sink notification and
  async persistence in one call
- Add platform_routes<S>() in core for centralized route registration;
  both feeder and ops merge it instead of duplicating route definitions
- Implement FromRef<AppState> for PlatformContext in both apps
- Add FeederPlatformEventSink adapter bridging core events to feeder's
  EventManager + ControlRuntimeStore
- Add event namespace prefixes: platform.source.created, feeder.unit.fault_locked, etc.
- Register full platform CRUD routes in ops app

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-21 13:45:02 +08:00
caoqianming 429c2d0b17 refactor(core): fill PlatformContext with pool/connection/websocket
- PlatformContext now holds pool, connection_manager, ws_manager
- bootstrap_platform returns PlatformBuilder for pre-Arc setup
- Feeder AppState embeds PlatformContext (state.platform.pool etc.)
- Ops AppState embeds PlatformContext with real DB connection
- Remove WebSocket type duplication: feeder re-exports from core
- Add subscribe_room/send_to_room/remove_room_if_empty to WebSocketManager

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-21 09:11:03 +08:00
caoqianming b651b6af66 refactor(core): move doc handler to core and split API.md per app
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-20 08:18:51 +08:00
caoqianming 4761e88c81 refactor(core): move log handler to shared platform core
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-17 13:36:26 +08:00
caoqianming f9f9915012 refactor(feeder): update static file serving for split web dirs
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-17 08:21:17 +08:00
caoqianming c3037e52cf docs(code): clean garbled rust comments 2026-04-16 15:40:28 +08:00
caoqianming c562bcc10b feat(feeder): create dedicated feeder distributor app crate 2026-04-16 12:38:58 +08:00
caoqianming 9a3d1f5ebb refactor(core): move websocket runtime and command infrastructure 2026-04-16 11:16:56 +08:00
caoqianming 7d83cf27dd fix(feeder): restore downstream wiring after shared-core extraction 2026-04-16 08:38:47 +08:00
caoqianming cf26a1f319 feat(core): add shared platform skeleton 2026-04-15 12:55:52 +08:00