83 lines
1.8 KiB
Go
83 lines
1.8 KiB
Go
package mqttbridge
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"git.i3omb.com/gronod/ha-n95-local-control/internal/ctl"
|
|
"git.i3omb.com/gronod/ha-n95-local-control/internal/session"
|
|
)
|
|
|
|
type diagnosticJSON struct {
|
|
Direction string `json:"direction"`
|
|
Kind string `json:"kind"`
|
|
Timestamp string `json:"timestamp"`
|
|
Session uint64 `json:"session"`
|
|
XML string `json:"xml"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
|
|
func (b *Bridge) DiagnosticSink() session.DiagnosticSink {
|
|
return func(d session.Diagnostic) {
|
|
s := b.slotFor(d.Serial)
|
|
if s == nil {
|
|
return
|
|
}
|
|
|
|
dir := ""
|
|
if d.Direction == session.DirectionIn {
|
|
dir = "robot_to_bridge"
|
|
} else if d.Direction == session.DirectionOut {
|
|
dir = "bridge_to_robot"
|
|
}
|
|
|
|
payload := diagnosticJSON{
|
|
Direction: dir,
|
|
Kind: d.Kind,
|
|
Timestamp: time.Now().UTC().Format(time.RFC3339),
|
|
Session: d.Generation,
|
|
XML: string(d.XML),
|
|
Reason: d.Reason,
|
|
}
|
|
|
|
data, err := json.Marshal(payload)
|
|
if err == nil {
|
|
_ = b.Publish(d.Serial, "raw", false, data)
|
|
}
|
|
}
|
|
}
|
|
|
|
type traceJSON struct {
|
|
SID string `json:"sid"`
|
|
CID string `json:"cid"`
|
|
Command string `json:"command"`
|
|
Phase string `json:"phase"`
|
|
Ret string `json:"ret"`
|
|
Errno *string `json:"errno"`
|
|
Timestamp string `json:"timestamp"`
|
|
}
|
|
|
|
func (b *Bridge) TraceSink() ctl.TraceSink {
|
|
return func(serial string, tr ctl.Trace) {
|
|
s := b.slotFor(serial)
|
|
if s == nil {
|
|
return
|
|
}
|
|
|
|
payload := traceJSON{
|
|
SID: tr.SID,
|
|
CID: tr.CID,
|
|
Command: tr.Command,
|
|
Phase: tr.Phase,
|
|
Ret: tr.Ret,
|
|
Errno: tr.Errno,
|
|
Timestamp: time.Now().UTC().Format(time.RFC3339),
|
|
}
|
|
|
|
data, err := json.Marshal(payload)
|
|
if err == nil {
|
|
_ = b.Publish(serial, "command_result", false, data)
|
|
}
|
|
}
|
|
}
|