New events api implementation

This commit is contained in:
2023-07-07 04:28:46 +03:00
parent 8c81bc8e24
commit 00762d173b
8 changed files with 428 additions and 566 deletions
-43
View File
@@ -1,43 +0,0 @@
package ui
import "cli-mon/yabl"
var fNewCIS = func(packet *yabl.ContactorInternalState) *contactorType {
return &contactorType{state: packet}
}
var fGetCIS = func(c *contactorType) *yabl.ContactorInternalState {
return c.state
}
var fSetCIS = func(c *contactorType, packet *yabl.ContactorInternalState) {
c.state = packet
}
var fNewCES = func(packet *yabl.ContactorInternalErrors) *contactorType {
return &contactorType{errors: packet}
}
var fGetCES = func(c *contactorType) *yabl.ContactorInternalErrors {
return c.errors
}
var fSetCES = func(c *contactorType, packet *yabl.ContactorInternalErrors) {
c.errors = packet
}
var fNewPPE = func(packet *yabl.PuPresentEnergy) *connectorType {
return &connectorType{energy: packet}
}
var fGetPPE = func(c *connectorType) *yabl.PuPresentEnergy {
return c.energy
}
var fSetPPE = func(c *connectorType, packet *yabl.PuPresentEnergy) {
c.energy = packet
}
var fNewPP = func(packet *yabl.PuPeriphery) *connectorType {
return &connectorType{state: packet}
}
var fGetPP = func(c *connectorType) *yabl.PuPeriphery {
return c.state
}
var fSetPP = func(c *connectorType, packet *yabl.PuPeriphery) {
c.state = packet
}
-81
View File
@@ -1,81 +0,0 @@
package ui
import (
"cli-mon/yabl"
"github.com/gizak/termui/v3/widgets"
"reflect"
)
type connectorType struct {
energy *yabl.PuPresentEnergy
state *yabl.PuPeriphery
}
type contactorType struct {
state *yabl.ContactorInternalState
errors *yabl.ContactorInternalErrors
}
type modelTypes interface {
*connectorType | *contactorType
}
type dataTypes interface {
*yabl.PuPresentEnergy | *yabl.PuPeriphery | *yabl.ContactorInternalState | *yabl.ContactorInternalErrors
}
type fNewType[T modelTypes, V dataTypes] func(e V) T
type fGetType[T modelTypes, V dataTypes] func(m T) V
type fSetType[T modelTypes, V dataTypes] func(m T, e V)
var connectors = make([]*connectorType, connectorsCount+1)
var contactors = make([]*contactorType, contactorsCount+1)
var connUI [connectorsCount + 1]*widgets.Paragraph
var contUI [contactorsCount + 1]*widgets.Paragraph
var contactorsErr *widgets.Paragraph
func process(message yabl.Packet) {
if message == nil {
return
}
unitId := message.GetUnitId()
switch msg := message.(type) {
case *yabl.ContactorInternalState:
if updateOrNew(contactors, unitId, msg, fNewCIS, fGetCIS, fSetCIS) {
updateContactorsView(unitId)
}
case *yabl.ContactorInternalErrors:
if updateOrNew(contactors, unitId, msg, fNewCES, fGetCES, fSetCES) {
updateContactorsStateView()
}
case *yabl.PuPeriphery:
if updateOrNew(connectors, unitId, msg, fNewPP, fGetPP, fSetPP) {
updateConnectorsView(unitId)
}
case *yabl.PuPresentEnergy:
if updateOrNew(connectors, unitId, msg, fNewPPE, fGetPPE, fSetPPE) {
updateConnectorsView(unitId)
}
}
}
func updateOrNew[T modelTypes, V dataTypes](array []T, id uint, msg V, fNew fNewType[T, V], fGet fGetType[T, V], fSet fSetType[T, V]) bool {
if model := array[id]; model != nil {
if element := fGet(model); !reflect.DeepEqual(element, msg) {
fSet(model, msg)
return true
}
} else {
model = fNew(msg)
array[id] = model
return true
}
return false
}
+22 -1
View File
@@ -12,7 +12,11 @@ const headerHeight int = 4
const contactorsCount = 18
const connectorsCount = 6
func InitCliApp(events <-chan yabl.Packet) {
var connUI [connectorsCount + 1]*widgets.Paragraph
var contUI [contactorsCount + 1]*widgets.Paragraph
var contactorsErr *widgets.Paragraph
func InitCliApp(events <-chan *yabl.Event) {
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
@@ -86,3 +90,20 @@ func InitCliApp(events <-chan yabl.Packet) {
}
}
}
func process(message *yabl.Event) {
if message == nil || message.Object == nil {
return
}
switch msg := message.Object.(type) {
case *yabl.ContactorInternalState:
updateContactorsView(msg)
case *yabl.PuPeriphery:
updateConnectorsView(msg)
case *yabl.PuPresentEnergy:
updateConnectorsView(msg)
case *yabl.ContactorInternalErrors:
updateContactorsStateView(msg)
}
}
+24 -13
View File
@@ -125,9 +125,10 @@ func newContactorsErrView() *widgets.Paragraph {
return p
}
func updateContactorsView(unitId uint) {
p := contUI[unitId]
s := contactors[unitId].state
func updateContactorsView(msg *yabl.ContactorInternalState) {
p := contUI[msg.GetUnitId()]
s := msg
if s == nil || p == nil {
return
@@ -154,10 +155,26 @@ func updateContactorsView(unitId uint) {
}
}
func updateConnectorsView(unitId uint) {
var connectorsState [connectorsCount + 1]*yabl.PuPeriphery
var connectorsEnergy [connectorsCount + 1]*yabl.PuPresentEnergy
func updateConnectorsView(action yabl.Action) {
if action == nil {
return
}
var unitId = action.GetUnitId()
switch msg := action.(type) {
case *yabl.PuPeriphery:
connectorsState[unitId] = msg
case *yabl.PuPresentEnergy:
connectorsEnergy[unitId] = msg
}
p := connUI[unitId]
s := connectors[unitId].state
e := connectors[unitId].energy
s := connectorsState[unitId]
e := connectorsEnergy[unitId]
if s == nil || e == nil {
return
@@ -192,15 +209,9 @@ func updateConnectorsView(unitId uint) {
e.PresentCurrent)
}
func updateContactorsStateView() {
func updateContactorsStateView(e *yabl.ContactorInternalErrors) {
p := contactorsErr
if contactors[1] == nil {
return
}
e := contactors[1].errors
if e == nil {
return
}