2023-07-09 14:30:30 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
2023-07-28 10:04:31 -06:00
|
|
|
const (
|
|
|
|
StopSignalType = SignalType("STOP")
|
|
|
|
StatusSignalType = SignalType("STATUS")
|
|
|
|
LinkSignalType = SignalType("LINK")
|
|
|
|
LockSignalType = SignalType("LOCK")
|
2023-07-28 11:21:18 -06:00
|
|
|
ReadSignalType = SignalType("READ")
|
2023-07-28 11:59:01 -06:00
|
|
|
ReadResultSignalType = SignalType("READ_RESULT")
|
2023-07-28 12:46:06 -06:00
|
|
|
LinkStartSignalType = SignalType("LINK_START")
|
2023-07-28 10:04:31 -06:00
|
|
|
)
|
|
|
|
|
2023-07-09 14:30:30 -06:00
|
|
|
type SignalDirection int
|
|
|
|
const (
|
|
|
|
Up SignalDirection = iota
|
|
|
|
Down
|
|
|
|
Direct
|
|
|
|
)
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
type SignalType string
|
2023-07-28 10:04:31 -06:00
|
|
|
func (signal_type SignalType) String() string {
|
|
|
|
return string(signal_type)
|
|
|
|
}
|
2023-07-26 15:08:14 -06:00
|
|
|
|
|
|
|
type Signal interface {
|
|
|
|
Serializable[SignalType]
|
2023-07-09 14:30:30 -06:00
|
|
|
Direction() SignalDirection
|
2023-07-28 15:07:38 -06:00
|
|
|
MarshalJSON() ([]byte, error)
|
2023-07-28 00:39:27 -06:00
|
|
|
Permission() Action
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type BaseSignal struct {
|
2023-07-26 15:08:14 -06:00
|
|
|
SignalDirection SignalDirection `json:"direction"`
|
|
|
|
SignalType SignalType `json:"type"`
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func (signal BaseSignal) Type() SignalType {
|
|
|
|
return signal.SignalType
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:39:27 -06:00
|
|
|
func (signal BaseSignal) Permission() Action {
|
2023-07-28 10:04:31 -06:00
|
|
|
return MakeAction(signal.Type())
|
2023-07-28 00:39:27 -06:00
|
|
|
}
|
|
|
|
|
2023-07-09 14:30:30 -06:00
|
|
|
func (signal BaseSignal) Direction() SignalDirection {
|
2023-07-26 15:08:14 -06:00
|
|
|
return signal.SignalDirection
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 15:07:38 -06:00
|
|
|
func (signal BaseSignal) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(signal)
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func (signal BaseSignal) Serialize() ([]byte, error) {
|
2023-07-28 15:07:38 -06:00
|
|
|
return signal.MarshalJSON()
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func NewBaseSignal(signal_type SignalType, direction SignalDirection) BaseSignal {
|
2023-07-09 14:30:30 -06:00
|
|
|
signal := BaseSignal{
|
2023-07-26 15:08:14 -06:00
|
|
|
SignalDirection: direction,
|
|
|
|
SignalType: signal_type,
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
return signal
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func NewDownSignal(signal_type SignalType) BaseSignal {
|
|
|
|
return NewBaseSignal(signal_type, Down)
|
2023-07-22 20:21:17 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func NewUpSignal(signal_type SignalType) BaseSignal {
|
|
|
|
return NewBaseSignal(signal_type, Up)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func NewDirectSignal(signal_type SignalType) BaseSignal {
|
|
|
|
return NewBaseSignal(signal_type, Direct)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 15:27:14 -06:00
|
|
|
var StopSignal = NewDownSignal(StopSignalType)
|
2023-07-22 20:21:17 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func NewIDSignal(signal_type SignalType, direction SignalDirection, id NodeID) IDSignal {
|
2023-07-22 20:21:17 -06:00
|
|
|
return IDSignal{
|
2023-07-26 15:08:14 -06:00
|
|
|
BaseSignal: NewBaseSignal(signal_type, direction),
|
2023-07-22 20:21:17 -06:00
|
|
|
ID: id,
|
|
|
|
}
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-23 21:14:15 -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)
|
2023-07-23 21:14:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewStatusSignal(status string, source NodeID) StatusSignal {
|
|
|
|
return StatusSignal{
|
2023-07-27 18:16:37 -06:00
|
|
|
IDSignal: NewIDSignal(StatusSignalType, Up, source),
|
2023-07-23 21:14:15 -06:00
|
|
|
Status: status,
|
|
|
|
}
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-21 13:59:52 -06:00
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
type StateSignal struct {
|
2023-07-27 18:08:43 -06:00
|
|
|
BaseSignal
|
2023-07-27 16:21:27 -06:00
|
|
|
State string `json:"state"`
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func (signal StateSignal) Serialize() ([]byte, error) {
|
2023-07-27 18:08:43 -06:00
|
|
|
return json.MarshalIndent(signal, "", " ")
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func (signal StateSignal) String() string {
|
2023-07-27 18:08:43 -06:00
|
|
|
ser, _ := signal.Serialize()
|
|
|
|
return string(ser)
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func NewLinkSignal(state string) StateSignal {
|
|
|
|
return StateSignal{
|
2023-07-27 18:08:43 -06:00
|
|
|
BaseSignal: NewDirectSignal(LinkSignalType),
|
2023-07-27 16:21:27 -06:00
|
|
|
State: state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 12:46:06 -06:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:37:06 -06:00
|
|
|
func NewLockSignal(state string) StateSignal {
|
|
|
|
return StateSignal{
|
|
|
|
BaseSignal: NewDirectSignal(LockSignalType),
|
|
|
|
State: state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 10:04:31 -06:00
|
|
|
func (signal StateSignal) Permission() Action {
|
|
|
|
return MakeAction(signal.Type(), signal.State)
|
|
|
|
}
|
|
|
|
|
2023-07-28 11:21:18 -06:00
|
|
|
type ReadSignal struct {
|
|
|
|
BaseSignal
|
|
|
|
Extensions map[ExtType][]string `json:"extensions"`
|
2023-07-21 13:59:52 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 11:21:18 -06:00
|
|
|
func NewReadSignal(exts map[ExtType][]string) ReadSignal {
|
|
|
|
return ReadSignal{
|
|
|
|
BaseSignal: NewDirectSignal(ReadSignalType),
|
|
|
|
Extensions: exts,
|
2023-07-21 13:59:52 -06:00
|
|
|
}
|
|
|
|
}
|
2023-07-28 11:59:01 -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,
|
|
|
|
}
|
|
|
|
}
|
2023-07-28 12:46:06 -06:00
|
|
|
|
2023-07-28 15:07:38 -06:00
|
|
|
|