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

+20 -25
View File
@@ -11,10 +11,8 @@ import (
"git.yeij.top/AskaEth/Cyrene_v2/internal/logger"
)
// mockProvider is the built-in deterministic Provider with eight simulated
// devices spread across every supported device type. It enforces the
// control_rules (capability constraints) that bound what each device type
// supports.
// mockProvider 是内置的确定性 Provider,八台模拟设备覆盖全部受支持的设备类型。
// 它施加 control_rules(capability 约束),界定每种设备类型的支持范围。
type mockProvider struct {
mu sync.RWMutex
log *slog.Logger
@@ -22,19 +20,18 @@ type mockProvider struct {
rules map[string]typeRules
}
// typeRules holds the control_rules for one device type: the set of supported
// actions and how each mutates the device state.
// typeRules 保存单一设备类型的 control_rules:受支持的动作集合,以及每个动作
// 如何改动设备状态。
type typeRules struct {
supported map[string]applyFunc
}
// applyFunc mutates d.State according to the action. It returns an error when
// the parameters are invalid.
// applyFunc 依据动作修改 d.State;参数非法时返回错误。
type applyFunc func(d *Device, params map[string]any) error
const mockProviderName = "mock"
// Name returns the provider identifier.
// Name 返回 Provider 标识。
func (m *mockProvider) Name() string { return mockProviderName }
func newMockProvider(log *slog.Logger) (*mockProvider, error) {
@@ -49,7 +46,7 @@ func newMockProvider(log *slog.Logger) (*mockProvider, error) {
return m, nil
}
// List returns every device, sorted by ID for deterministic output.
// List 返回全部设备,按 ID 排序以保证输出确定。
func (m *mockProvider) List(ctx context.Context) ([]Device, error) {
if err := ctx.Err(); err != nil {
return nil, err
@@ -64,7 +61,7 @@ func (m *mockProvider) List(ctx context.Context) ([]Device, error) {
return out, nil
}
// Status returns a clone of the named device.
// Status 返回指定设备的副本。
func (m *mockProvider) Status(ctx context.Context, deviceID string) (*Device, error) {
if err := ctx.Err(); err != nil {
return nil, err
@@ -79,7 +76,7 @@ func (m *mockProvider) Status(ctx context.Context, deviceID string) (*Device, er
return &cp, nil
}
// Control applies action to the named device, enforcing the type's control_rules.
// Control 对指定设备施加 action,并强制该类型的 control_rules。
func (m *mockProvider) Control(ctx context.Context, deviceID, action string, params map[string]any) (*Device, error) {
if err := ctx.Err(); err != nil {
return nil, err
@@ -109,7 +106,7 @@ func (m *mockProvider) Control(ctx context.Context, deviceID, action string, par
return &cp, nil
}
// buildRules returns the control_rules for every supported device type.
// buildRules 返回全部受支持设备类型的 control_rules。
func buildRules() map[string]typeRules {
return map[string]typeRules{
"light": lightRules(),
@@ -120,7 +117,7 @@ func buildRules() map[string]typeRules {
}
}
// lightRules returns the control_rules for light devices.
// lightRules 返回灯类设备的 control_rules。
func lightRules() typeRules {
return typeRules{supported: map[string]applyFunc{
"on": func(d *Device, _ map[string]any) error {
@@ -150,7 +147,7 @@ func lightRules() typeRules {
}}
}
// acRules returns the control_rules for AC devices.
// acRules 返回空调设备的 control_rules。
func acRules() typeRules {
return typeRules{supported: map[string]applyFunc{
"on": func(d *Device, _ map[string]any) error {
@@ -185,7 +182,7 @@ func acRules() typeRules {
}}
}
// curtainRules returns the control_rules for curtain devices.
// curtainRules 返回窗帘设备的 control_rules。
func curtainRules() typeRules {
return typeRules{supported: map[string]applyFunc{
"open": func(d *Device, _ map[string]any) error {
@@ -208,14 +205,13 @@ func curtainRules() typeRules {
}}
}
// sensorRules returns the control_rules for sensor devices. Sensors are
// read-only: no action is supported, so any Control attempt returns
// ErrUnsupported.
// sensorRules 返回传感器设备的 control_rules。传感器只读:不支持任何动作,
// 因此任何 Control 尝试都会返回 ErrUnsupported。
func sensorRules() typeRules {
return typeRules{supported: map[string]applyFunc{}}
}
// lockRules returns the control_rules for lock devices.
// lockRules 返回门锁设备的 control_rules。
func lockRules() typeRules {
return typeRules{supported: map[string]applyFunc{
"lock": func(d *Device, _ map[string]any) error {
@@ -233,9 +229,8 @@ func lockRules() typeRules {
}}
}
// mockDevices returns the eight seeded mock devices. It covers all five types;
// ac.bedroom is seeded offline to exercise the Online field and the offline
// control guard.
// mockDevices 返回八台预置的模拟设备,覆盖全部五种类型;ac.bedroom 预置为离线,
// 用于检验 Online 字段与离线控制保护逻辑。
func mockDevices() []*Device {
return []*Device{
{ID: "ac.bedroom", Name: "卧室空调", Type: "ac", Online: false, State: map[string]any{"on": false, "temperature": 26, "mode": "cool"}},
@@ -274,8 +269,8 @@ func clamp(n, lo, hi int) int {
return n
}
// intParam reads an integer action parameter, accepting int/int64/float64/string
// shapes so JSON-decoded params work the same as literal ones.
// intParam 读取整型动作参数,可接受 int/int64/float64/string 等多种形态,
// 使 JSON 解码得到的参数与字面量参数行为一致。
func intParam(p map[string]any, key string) (int, error) {
v, ok := p[key]
if !ok {