diff --git a/matrix.go b/matrix.go index c1475e9..c4f4915 100644 --- a/matrix.go +++ b/matrix.go @@ -48,15 +48,25 @@ func (matrix *Matrix) GetDestination(dst uint16) *Destination { return matrix.destinations[dst] } +func (matrix *Matrix) GetSource(src uint16) *Source { + matrix.mux.Lock() + defer matrix.mux.Unlock() + return matrix.sources[src] +} + func (dst *Destination) GetID() uint16 { return dst.Id } +func (dst *Destination) GetIDInt() int { + return int(dst.Id) +} + func (dst *Destination) GetLabel() string { return dst.Label } -func (dst *Destination) Setlabel(lbl string) { +func (dst *Destination) SetLabel(lbl string) { dst.Label = lbl } @@ -68,10 +78,14 @@ func (src *Source) GetID() uint16 { return src.Id } +func (src *Source) GetIDInt() int { + return int(src.Id) +} + func (src *Source) GetLabel() string { return src.Label } -func (src *Source) Setlabel(lbl string) { +func (src *Source) SetLabel(lbl string) { src.Label = lbl } diff --git a/model.go b/model.go index f1bfc8f..ff11713 100644 --- a/model.go +++ b/model.go @@ -21,7 +21,7 @@ type Router struct { Level Level Matrix Matrix Conn net.Conn - onUpdate func(*Destination) + onUpdate func(*Update) } type Destination struct { @@ -64,3 +64,8 @@ type Matrix struct { sources map[uint16]*Source mux sync.Mutex } + +type Update struct { + Type string + Data interface{} +} diff --git a/nk.go b/nk.go index 344bad9..71bfa73 100644 --- a/nk.go +++ b/nk.go @@ -74,7 +74,7 @@ func New(IP string, RTRAddress uint8, model Model) *Router { } } - rtr.Matrix.sources[0].Setlabel("DISCONNECTED") + rtr.Matrix.sources[0].SetLabel("DISCONNECTED") for i := 0; i < int(rtr.Destinations)+1; i++ { rtr.Matrix.destinations[uint16(i)] = &Destination{ @@ -96,10 +96,10 @@ func (rtr *Router) LoadLabels(labels string) { log.Printf("%+v", columns) if _, ok := rtr.Matrix.destinations[uint16(i+1)]; ok { - rtr.Matrix.destinations[uint16(i+1)].Setlabel(columns[1]) + rtr.Matrix.destinations[uint16(i+1)].SetLabel(columns[1]) } if _, ok := rtr.Matrix.sources[uint16(i+1)]; ok { - rtr.Matrix.sources[uint16(i+1)].Setlabel(columns[3]) + rtr.Matrix.sources[uint16(i+1)].SetLabel(columns[3]) } } } @@ -193,11 +193,48 @@ func (rtr *Router) updateMatrix(lvl Level, dst uint16, src uint16) { rtr.Matrix.SetCrosspoint(dst, src) if rtr.onUpdate != nil { - go rtr.onUpdate(rtr.Matrix.GetDestination(dst)) + go rtr.onUpdate(&Update{ + Type: "destination", + Data: rtr.Matrix.GetDestination(dst), + }) } } } -func (rtr *Router) SetOnUpdate(notify func(*Destination)) { +func (rtr *Router) UpdateSourceLabel(src int, label string) { + if src <= int(rtr.Sources) { + rtr.Matrix.GetSource(uint16(src)).SetLabel(label) + go rtr.onUpdate(&Update{ + Type: "source", + Data: rtr.Matrix.GetSource(uint16(src)), + }) + + for _, dst := range rtr.Matrix.destinations { + if dst.Source != nil && dst.Source.GetID() == uint16(src) { + if rtr.onUpdate != nil { + go rtr.onUpdate(&Update{ + Type: "destination", + Data: dst, + }) + } + } + } + } +} + +func (rtr *Router) UpdateDestinationLabel(dst int, label string) { + if dst <= int(rtr.Destinations) { + rtr.Matrix.destinations[uint16(dst)].SetLabel(label) + + if rtr.onUpdate != nil { + go rtr.onUpdate(&Update{ + Type: "destination", + Data: rtr.Matrix.GetDestination(uint16(dst)), + }) + } + } +} + +func (rtr *Router) SetOnUpdate(notify func(*Update)) { rtr.onUpdate = notify }