docs(iot): 存量英文注释中文化(仅注释,代码零字节改动)

- 5 个文件:123 行英文注释 → 99 行中文注释(中文更紧凑,内容无删减)。
- 机械校验(git diff -U0 去掉 // 行与空行)输出为空;gofmt -l 干净(改动前亦干净)。
- 验收:go build/vet/test ./internal/iot/... 全绿。
This commit is contained in:
AskaEth committed 2026-10-04 16:58:00 +08:00
1 parent 127c32b440
commit 2593765add
5 files changed
+99 -123

No files matched your search

+27 -32
View File
@@ -13,35 +13,32 @@ import (
"git.yeij.top/AskaEth/Cyrene_v2/internal/logger"
)
// Manager is the IoT subsystem facade. It selects a Provider from
// configuration, serves List/Control/Status, and publishes device state
// transitions on the in-process bus as DeviceUpdate events.
// Manager 是 IoT 子系统的门面:依据配置选定 Provider,对外提供
// List/Control/Status,并把设备状态跃迁以 DeviceUpdate 事件的形式发布到进程内 bus。
type Manager struct {
providers []Provider
bus bus.Bus
log *slog.Logger
}
// Option customizes a Manager built by New.
// Option 用于定制由 New 构建的 Manager。
type Option func(*Manager)
// WithBus injects an existing bus instead of letting the Manager create one. It
// mirrors the other subsystems so iot.device_update flows on the shared bus and
// reaches the gateway hub / background-trigger subscribers rather than staying
// on an isolated manager-owned bus.
// WithBus 注入现成的 bus,而不让 Manager 自建一个。此设计与其余子系统保持一致:
// iot.device_update 由此流经共享 bus,能到达 gateway hub / 后台触发订阅者,
// 而不是滞留在一个 Manager 私有的隔离 bus 上。
func WithBus(b bus.Bus) Option {
return func(m *Manager) { m.bus = b }
}
// New builds a Manager from cfg, selecting the device Provider by cfg.Provider.
// New 依据 cfg 构建 Manager,并以 cfg.Provider 选定设备 Provider。
//
// Supported providers: "" or "mock" registers the built-in mock provider with
// eight simulated devices; "none" disables the subsystem (no provider is
// registered, List returns an empty slice and Control/Status fail with
// ErrDeviceNotFound). Any other value returns ErrUnknownProvider.
// 支持的 Provider 取值:"" 或 "mock" 注册内置 mock Provider(八台模拟设备);
// "none" 关闭该子系统(不注册任何 Provider,List 返回空切片,Control/Status 以
// ErrDeviceNotFound 失败)。其他取值返回 ErrUnknownProvider。
//
// log may be nil; it is replaced with a discard logger. The Manager owns a
// fresh, isolated bus exposed via Bus for subscribers to attach to.
// log 可为 nil,此时会替换为丢弃输出的 logger。Manager 自持一个全新且隔离的 bus,
// 经 Bus 暴露给订阅方挂接。
func New(cfg config.IoTConfig, log *slog.Logger, opts ...Option) (*Manager, error) {
m := &Manager{
bus: bus.New(),
@@ -58,7 +55,7 @@ func New(cfg config.IoTConfig, log *slog.Logger, opts ...Option) (*Manager, erro
}
m.providers = append(m.providers, mp)
case "none":
// No provider is registered: the subsystem is disabled.
// 不注册任何 Provider:子系统处于关闭状态。
default:
return nil, fmt.Errorf("iot: new: provider %q: %w", cfg.Provider, ErrUnknownProvider)
}
@@ -66,8 +63,8 @@ func New(cfg config.IoTConfig, log *slog.Logger, opts ...Option) (*Manager, erro
return m, nil
}
// Register adds p as a device source. Registering a provider whose Name already
// exists returns ErrDuplicate; a nil provider returns ErrInvalidArgument.
// Register 把 p 注册为设备来源。注册的 Provider 若 Name 已存在,返回 ErrDuplicate;
// p 为 nil 则返回 ErrInvalidArgument。
func (m *Manager) Register(p Provider) error {
if p == nil {
return fmt.Errorf("iot: register: %w", ErrInvalidArgument)
@@ -82,13 +79,13 @@ func (m *Manager) Register(p Provider) error {
return nil
}
// Bus returns the Manager's in-process event bus. Subscribers attach to
// DeviceUpdateSubject (or "iot.>") to observe device state transitions.
// Bus 返回 Manager 的进程内事件总线。订阅方挂接 DeviceUpdateSubject
// (或 "iot.>")以观察设备状态跃迁。
func (m *Manager) Bus() bus.Bus {
return m.bus
}
// List returns every device from every registered provider.
// List 返回全部已注册 Provider 的所有设备。
func (m *Manager) List(ctx context.Context) ([]Device, error) {
if err := ctx.Err(); err != nil {
return nil, err
@@ -105,9 +102,8 @@ func (m *Manager) List(ctx context.Context) ([]Device, error) {
return out, nil
}
// Control applies action to the owning provider's device and, on success,
// publishes a DeviceUpdate on the bus. It returns ErrDeviceNotFound when no
// provider owns the device.
// Control 把 action 施加到拥有该设备的 Provider 上,成功后向 bus 发布一条
// DeviceUpdate。若没有任何 Provider 拥有该设备,返回 ErrDeviceNotFound。
func (m *Manager) Control(ctx context.Context, deviceID, action string, params map[string]any) (*Device, error) {
if err := ctx.Err(); err != nil {
return nil, err
@@ -129,8 +125,8 @@ func (m *Manager) Control(ctx context.Context, deviceID, action string, params m
return nil, fmt.Errorf("iot: control %q: %w", deviceID, ErrDeviceNotFound)
}
// Status returns the current state of the device owned by the matching provider.
// It returns ErrDeviceNotFound when no provider owns the device.
// Status 返回由匹配 Provider 所持有设备的当前状态。若没有任何 Provider 拥有该
// 设备,返回 ErrDeviceNotFound。
func (m *Manager) Status(ctx context.Context, deviceID string) (*Device, error) {
if err := ctx.Err(); err != nil {
return nil, err
@@ -151,10 +147,9 @@ func (m *Manager) Status(ctx context.Context, deviceID string) (*Device, error)
return nil, fmt.Errorf("iot: status %q: %w", deviceID, ErrDeviceNotFound)
}
// publishUpdate fans a DeviceUpdate out on the bus. A publish failure (for
// example a canceled context) is logged, not returned: the device state already
// changed, and control should not be reported as failed because an observer did
// not receive the event.
// publishUpdate 把 DeviceUpdate 扇出到 bus。发布失败(例如 ctx 被取消)只记日志、
// 不作为错误返回:设备状态已经改变,不应因为某个观察者没收到事件就把本次控制判为
// 失败。
func (m *Manager) publishUpdate(ctx context.Context, provider string, d Device, action string, params map[string]any) {
msg := fmt.Sprintf("设备 %s · %s(%s)", d.Name, action, provider)
if err := m.bus.Publish(ctx, DeviceUpdateSubject, DeviceUpdate{
@@ -170,8 +165,8 @@ func (m *Manager) publishUpdate(ctx context.Context, provider string, d Device,
m.log.Debug("iot device update published", "device", d.ID, "action", action, "provider", provider)
}
// discardLogger returns log if non-nil, otherwise a logger that writes nowhere,
// so New always has a safe logger to use.
// discardLogger 在 log 非 nil 时原样返回,否则返回一个不写任何输出的 logger,
// 保证 New 始终有可用的 logger。
func discardLogger(log *slog.Logger) *slog.Logger {
if log != nil {
return log