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
+45 -141
View File
@@ -3,10 +3,10 @@ package yabl
import (
"cli-mon/can"
"encoding/binary"
"time"
time "time"
)
type Packet interface {
type Action interface {
GetUnitId() uint
}
@@ -32,13 +32,22 @@ type CpLineLevelType uint
type IsolationStateType uint
type Voltage9BitType float32
type PName string
type AName string
type FName string
type parameters struct {
type Event struct {
UnitId uint
ActionName AName
Field FName
Object Action
Updated time.Time
}
type action struct {
object Action
fields []field
interval uint
name PName
name AName
}
type field struct {
@@ -50,10 +59,11 @@ type field struct {
}
type key struct {
canId, unitId uint
canId uint32
unitId uint
}
var protocolMap map[key]parameters
var protocolMap map[key]action = make(map[key]action)
var cpuPresentEnergyArray [CONNECTOR_MAX]*CpuPresentEnergy
var cpuPeripheryInstance *CpuPeriphery
var cpuEnergySettingsArray [CONNECTOR_MAX]*CpuEnergySettings
@@ -69,10 +79,10 @@ var logicAuthArray [CONNECTOR_MAX]*LogicAuth
var logicEnergyMode [CONNECTOR_MAX]*LogicEnergyMode
var logicErrorsInstance *LogicErrors
var logicWorkingMode *LogicWorkingMode
var contactorInternalStateArray [CONNECTOR_MAX]*ContactorInternalState
var contactorInternalStateArray [CONTACTOR_MAX]*ContactorInternalState
var contactorInternalErrorsInstance *ContactorInternalErrors
var contactorsInternalForce *ContactorsInternalForce
var contactorInternalDebugArray [CONNECTOR_MAX]*ContactorInternalDebug
var contactorInternalDebugArray [CONTACTOR_MAX]*ContactorInternalDebug
var peripheryStateArray [CONNECTOR_MAX]*PeripheryState
var peripheryInfoArray [CONNECTOR_MAX]*PeripheryInfo
var peripheryDebugArray [CONNECTOR_MAX]*PeripheryDebug
@@ -80,15 +90,12 @@ var converterPresentEnergyArray [CONVERTERS_MAX]*ConverterPresentEnergy
var converterErrorsArray [CONVERTERS_MAX]*ConverterErrors
var converterDebugArray [CONVERTERS_MAX]*ConverterDebug
func StartProtocolParsing(frames <-chan *can.CanFrame) <-chan Packet {
result := make(chan Packet)
func StartProtocolParsing(frames <-chan *can.CanFrame) <-chan *Event {
result := make(chan *Event)
initialize()
go func() {
defer close(result)
var contactorsFirstState [CONTACTOR_MAX]bool
var contactorsFirstErr [CONTACTOR_MAX]bool
var puFirst [CONNECTOR_MAX]bool
var puPeripheryFirst [CONNECTOR_MAX]bool
for frame := range frames {
if frames == nil {
@@ -96,141 +103,38 @@ func StartProtocolParsing(frames <-chan *can.CanFrame) <-chan Packet {
}
var unitId = uint((frame.CanId & UNIT_ID_MASK) >> UNIT_ID_OFFSET)
var canId = frame.CanId & ACTION_ID_MASK
var k = key{canId: canId, unitId: unitId}
var now = time.Now()
switch {
case frame.CanId&ACTION_ID_MASK == can.CAN_ID_071:
changed := false
var model = ContactorInternalState{UnitId: unitId}
if val, ok := get(0, 3, frame.Payload); ok && model.ContactorReady != BoardReadyType(val) {
model.ContactorReady = BoardReadyType(val)
changed = true
}
if val, ok := get(3, 2, frame.Payload); ok && model.ContactorOn != BooleanType(val) {
model.ContactorOn = BooleanType(val)
changed = true
}
if val, ok := get(5, 2, frame.Payload); ok && model.UnexpectedState != ErrorType(val) {
model.UnexpectedState = ErrorType(val)
changed = true
}
if val, ok := get(7, 2, frame.Payload); ok && model.Isolated != ErrorType(val) {
model.Isolated = ErrorType(val)
changed = true
}
if val, ok := get(9, 2, frame.Payload); ok && model.DebugEnabled != BooleanType(val) {
model.DebugEnabled = BooleanType(val)
changed = true
}
if changed || !contactorsFirstState[unitId] {
contactorsFirstState[unitId] = true
result <- &model
}
case frame.CanId&ACTION_ID_MASK == can.CAN_ID_073:
changed := false
model := ContactorInternalErrors{UnitId: unitId}
if val, ok := get(0, 3, frame.Payload); ok && model.BoardReady != BoardReadyType(val) {
model.BoardReady = BoardReadyType(val)
changed = true
}
if val, ok := get(3, 4, frame.Payload); ok && model.OtherError != ContactorInternalOtherErrorType(val) {
model.OtherError = ContactorInternalOtherErrorType(val)
changed = true
}
if val, ok := get(7, 3, frame.Payload); ok && model.ContactorGroupChanged != ContactorGroupChangedType(val) {
model.ContactorGroupChanged = ContactorGroupChangedType(val)
changed = true
}
if val, ok := get(10, 4, frame.Payload); ok && model.UnexpectedFormation != ContactorInternalOtherErrorType(val) {
model.UnexpectedFormation = ContactorInternalOtherErrorType(val)
changed = true
}
if val, ok := get(14, 2, frame.Payload); ok && model.CpuNotReady != ErrorType(val) {
model.CpuNotReady = ErrorType(val)
changed = true
}
if val, ok := get(16, 2, frame.Payload); ok && model.PuNotReady != ErrorType(val) {
model.PuNotReady = ErrorType(val)
changed = true
}
if val, ok := get(18, 2, frame.Payload); ok && model.Debug != BooleanType(val) {
model.Debug = BooleanType(val)
changed = true
}
if changed || !contactorsFirstErr[unitId] {
contactorsFirstErr[unitId] = true
result <- &model
}
case frame.CanId&ACTION_ID_MASK == can.CAN_ID_021:
changed := false
model := PuPresentEnergy{UnitId: unitId}
if val, ok := get(0, 2, frame.Payload); ok && model.V2GMode != V2GModeType(val) {
model.V2GMode = V2GModeType(val)
changed = true
}
if val, ok := get(2, 11, frame.Payload); ok && model.VoltageBefore != Voltage11BitType(val) {
model.VoltageBefore = Voltage11BitType(val)
changed = true
}
if val, ok := get(13, 11, frame.Payload); ok && model.VoltageAfter != Voltage11BitType(val) {
model.VoltageAfter = Voltage11BitType(val)
changed = true
}
if val, ok := get(24, 10, frame.Payload); ok && model.PresentCurrent != Current10BitType(val) {
model.PresentCurrent = Current10BitType(val)
changed = true
}
if changed || !puFirst[unitId] {
puFirst[unitId] = true
result <- &model
}
case frame.CanId&ACTION_ID_MASK == can.CAN_ID_022:
changed := false
model := PuPeriphery{UnitId: unitId}
if val, ok := get(0, 2, frame.Payload); ok && model.ConnectorInsert != BooleanType(val) {
model.ConnectorInsert = BooleanType(val)
changed = true
}
if val, ok := get(2, 2, frame.Payload); ok && model.ContactorOn != BooleanType(val) {
model.ContactorOn = BooleanType(val)
changed = true
}
if val, ok := get(4, 2, frame.Payload); ok && model.ConnectorLocked != BooleanType(val) {
model.ConnectorLocked = BooleanType(val)
changed = true
}
if val, ok := get(6, 3, frame.Payload); ok && model.CpLineLevel != CpLineLevelType(val) {
model.CpLineLevel = CpLineLevelType(val)
changed = true
}
if val, ok := get(9, 3, frame.Payload); ok && model.IsolationState != IsolationStateType(val) {
model.IsolationState = IsolationStateType(val)
changed = true
}
if val, ok := get(12, 2, frame.Payload); ok && model.ChargingAllowed != BooleanType(val) {
model.ChargingAllowed = BooleanType(val)
changed = true
}
if val, ok := get(14, 2, frame.Payload); ok && model.PwmEnabled != BooleanType(val) {
model.PwmEnabled = BooleanType(val)
changed = true
}
if val, ok := get(16, 9, frame.Payload); ok && model.CpLineVoltage != (Voltage9BitType(val)*0.1-15) {
model.CpLineVoltage = Voltage9BitType(val)*0.1 - 15
changed = true
}
if changed || !puPeripheryFirst[unitId] {
puPeripheryFirst[unitId] = true
result <- &model
}
param := protocolMap[k]
var start uint8 = 0
for _, f := range param.fields {
if val, found := get(start, f.length, frame.Payload); found {
start += f.length
f.last = now
if f.value != val {
f.value = val
f.setter(val)
result <- &Event{
Field: f.name,
ActionName: param.name,
Object: param.object,
UnitId: unitId,
Updated: now,
}
}
}
}
}
}()
return result
}
func get(from uint, size uint, buffer []byte) (uint64, bool) {
func get(from uint8, size uint8, buffer []byte) (uint64, bool) {
value := binary.LittleEndian.Uint64(buffer)
var mask uint64 = ^(ALL_BITS << size)