Files
ssw-mvp/scard/code/connector/state/machine.go
T
admin 3247e9fd15 init: ssw-mvp initial commit
- boot/ (Java/Maven, JNPF 7.0) — 移除旧的 boot/.git,源码直接纳入仓库
- web/ (Vue 3 + Vite + Ant Design Vue, pnpm)
- docs/, scard/, nax/
- refs/INDEX.md + refs/国标/(其余 refs/* 开源子仓库不纳入)
- 已忽略: connector_data/, jweb/, log/, web/{dist,node_modules,.pnpm}, boot/*/target/, .DS_Store, .idea, .vscode
2026-08-05 18:29:12 +08:00

79 lines
1.7 KiB
Go

// State machine for connector startup sequence
package state
import "fmt"
// ConnectorState represents the current state of the connector
type ConnectorState int
const (
StateTPMInit ConnectorState = iota
StateKeyInit
StateAuthRegister
StateEasyTierJoin
StateK3sJoin
StateRunning
)
func (s ConnectorState) String() string {
switch s {
case StateTPMInit:
return "TPM_INIT"
case StateKeyInit:
return "KEY_INIT"
case StateAuthRegister:
return "AUTH_REGISTER"
case StateEasyTierJoin:
return "EASYTIER_JOIN"
case StateK3sJoin:
return "K3S_JOIN"
case StateRunning:
return "RUNNING"
default:
return "UNKNOWN"
}
}
// StateMachine manages connector state transitions
type StateMachine struct {
currentState ConnectorState
deviceID string
onTransition func(from, to ConnectorState)
}
// NewStateMachine creates a new state machine
func NewStateMachine(deviceID string) *StateMachine {
return &StateMachine{
currentState: StateTPMInit,
deviceID: deviceID,
}
}
// TransitionTo moves to a new state
func (sm *StateMachine) TransitionTo(newState ConnectorState) error {
if newState <= sm.currentState {
return fmt.Errorf("invalid state transition from %s to %s", sm.currentState, newState)
}
oldState := sm.currentState
sm.currentState = newState
if sm.onTransition != nil {
sm.onTransition(oldState, newState)
}
return nil
}
// GetState returns the current state
func (sm *StateMachine) GetState() ConnectorState {
return sm.currentState
}
// SetTransitionCallback sets a callback for state transitions
func (sm *StateMachine) SetTransitionCallback(cb func(from, to ConnectorState)) {
sm.onTransition = cb
}
// GetDeviceID returns the device ID
func (sm *StateMachine) GetDeviceID() string {
return sm.deviceID
}