graphvent/signal.go

192 lines
4.0 KiB
Go

2023-07-09 14:30:30 -06:00
package graphvent
import (
"encoding/json"
)
const (
StopSignalType = SignalType("STOP")
StatusSignalType = SignalType("STATUS")
LinkSignalType = SignalType("LINK")
LockSignalType = SignalType("LOCK")
ReadSignalType = SignalType("READ")
ReadResultSignalType = SignalType("READ_RESULT")
LinkStartSignalType = SignalType("LINK_START")
)
2023-07-09 14:30:30 -06:00
type SignalDirection int
const (
Up SignalDirection = iota
Down
Direct
)
type SignalType string
func (signal_type SignalType) String() string {
return string(signal_type)
}
type Signal interface {
Serializable[SignalType]
2023-07-09 14:30:30 -06:00
Direction() SignalDirection
MarshalJSON() ([]byte, error)
Permission() Action
2023-07-09 14:30:30 -06:00
}
type BaseSignal struct {
SignalDirection SignalDirection `json:"direction"`
SignalType SignalType `json:"type"`
2023-07-09 14:30:30 -06:00
}
func (signal BaseSignal) Type() SignalType {
return signal.SignalType
2023-07-09 14:30:30 -06:00
}
func (signal BaseSignal) Permission() Action {
return MakeAction(signal.Type())
}
2023-07-09 14:30:30 -06:00
func (signal BaseSignal) Direction() SignalDirection {
return signal.SignalDirection
2023-07-09 14:30:30 -06:00
}
func (signal BaseSignal) MarshalJSON() ([]byte, error) {
return json.Marshal(signal)
}
func (signal BaseSignal) Serialize() ([]byte, error) {
return signal.MarshalJSON()
2023-07-09 14:30:30 -06:00
}
func NewBaseSignal(signal_type SignalType, direction SignalDirection) BaseSignal {
2023-07-09 14:30:30 -06:00
signal := BaseSignal{
SignalDirection: direction,
SignalType: signal_type,
2023-07-09 14:30:30 -06:00
}
return signal
}
func NewDownSignal(signal_type SignalType) BaseSignal {
return NewBaseSignal(signal_type, Down)
}
func NewUpSignal(signal_type SignalType) BaseSignal {
return NewBaseSignal(signal_type, Up)
2023-07-09 14:30:30 -06:00
}
func NewDirectSignal(signal_type SignalType) BaseSignal {
return NewBaseSignal(signal_type, Direct)
2023-07-09 14:30:30 -06:00
}
var StopSignal = NewDownSignal(StopSignalType)
type IDSignal struct {
BaseSignal
ID NodeID `json:"id"`
2023-07-09 14:30:30 -06:00
}
2023-07-24 17:07:27 -06:00
func (signal IDSignal) String() string {
ser, err := json.Marshal(signal)
if err != nil {
return "STATE_SER_ERR"
}
return string(ser)
}
func NewIDSignal(signal_type SignalType, direction SignalDirection, id NodeID) IDSignal {
return IDSignal{
BaseSignal: NewBaseSignal(signal_type, direction),
ID: id,
}
2023-07-09 14:30:30 -06:00
}
type StatusSignal struct {
IDSignal
2023-07-24 17:07:27 -06:00
Status string `json:"status"`
}
func (signal StatusSignal) String() string {
ser, err := json.Marshal(signal)
if err != nil {
return "STATE_SER_ERR"
}
return string(ser)
}
func NewStatusSignal(status string, source NodeID) StatusSignal {
return StatusSignal{
2023-07-27 18:16:37 -06:00
IDSignal: NewIDSignal(StatusSignalType, Up, source),
Status: status,
}
2023-07-09 14:30:30 -06:00
}
2023-07-21 13:59:52 -06:00
type StateSignal struct {
BaseSignal
2023-07-27 16:21:27 -06:00
State string `json:"state"`
}
func (signal StateSignal) Serialize() ([]byte, error) {
return json.MarshalIndent(signal, "", " ")
}
func (signal StateSignal) String() string {
ser, _ := signal.Serialize()
return string(ser)
}
func NewLinkSignal(state string) StateSignal {
return StateSignal{
BaseSignal: NewDirectSignal(LinkSignalType),
2023-07-27 16:21:27 -06:00
State: state,
}
}
type LinkStartSignal struct {
IDSignal
LinkType string `json:"link_type"`
}
func NewLinkStartSignal(link_type string, target NodeID) LinkStartSignal {
return LinkStartSignal{
IDSignal: NewIDSignal(LinkStartSignalType, Direct, target),
LinkType: link_type,
}
}
func NewLockSignal(state string) StateSignal {
return StateSignal{
BaseSignal: NewDirectSignal(LockSignalType),
State: state,
}
}
func (signal StateSignal) Permission() Action {
return MakeAction(signal.Type(), signal.State)
}
type ReadSignal struct {
BaseSignal
Extensions map[ExtType][]string `json:"extensions"`
2023-07-21 13:59:52 -06:00
}
func NewReadSignal(exts map[ExtType][]string) ReadSignal {
return ReadSignal{
BaseSignal: NewDirectSignal(ReadSignalType),
Extensions: exts,
2023-07-21 13:59:52 -06:00
}
}
type ReadResultSignal struct {
BaseSignal
Extensions map[ExtType]map[string]interface{} `json:"extensions"`
}
func NewReadResultSignal(exts map[ExtType]map[string]interface{}) ReadResultSignal {
return ReadResultSignal{
BaseSignal: NewDirectSignal(ReadResultSignalType),
Extensions: exts,
}
}