You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
250 lines
5.2 KiB
250 lines
5.2 KiB
package yabl
|
|
|
|
import (
|
|
can "cli-mon/can"
|
|
"encoding/binary"
|
|
time "time"
|
|
)
|
|
|
|
type Action interface {
|
|
GetUnitId() uint
|
|
}
|
|
|
|
type SignedAirTemp8BitType int
|
|
type ConnectedOut8bitType uint
|
|
type ForceModeType uint
|
|
type SignedVoltage12bitType int
|
|
type DebugSwitchModeType uint
|
|
type OtherError4BitType uint
|
|
type ContactorModeType uint
|
|
type Power14BitType uint
|
|
type AuthStateType uint
|
|
type AuthModeType uint
|
|
type BoardReadyType uint
|
|
type BooleanType uint
|
|
type ErrorType uint
|
|
type ContactorInternalOtherErrorType uint
|
|
type ContactorGroupChangedType uint
|
|
type V2GModeType uint
|
|
type Voltage11BitType uint
|
|
type Current10BitType uint
|
|
type CpLineLevelType uint
|
|
type IsolationStateType uint
|
|
type Voltage9BitType float32
|
|
|
|
type AName string
|
|
type FName string
|
|
|
|
type Event struct {
|
|
UnitId uint
|
|
ActionName AName
|
|
Field FName
|
|
Object Action
|
|
Updated *time.Time
|
|
Value any
|
|
}
|
|
|
|
type action struct {
|
|
object Action
|
|
fields []*field
|
|
interval uint
|
|
name AName
|
|
}
|
|
|
|
type field struct {
|
|
length uint8
|
|
setter func(uint64 uint64) any
|
|
value uint64
|
|
last *time.Time
|
|
name FName
|
|
}
|
|
|
|
type key struct {
|
|
canId uint32
|
|
unitId uint
|
|
}
|
|
|
|
var protocolMap map[key]action = make(map[key]action)
|
|
var cpuPresentEnergyArray [CONNECTOR_MAX]*CpuPresentEnergy
|
|
var cpuPeripheryInstance *CpuPeriphery
|
|
var cpuEnergySettingsArray [CONNECTOR_MAX]*CpuEnergySettings
|
|
var cpuErrorsInstance *CpuErrors
|
|
var cpuDebugInstance *CpuDebug
|
|
var puPresentEnergyArray [CONNECTOR_MAX]*PuPresentEnergy
|
|
var puPeripheryArray [CONNECTOR_MAX]*PuPeriphery
|
|
var puErrorsArray [CONNECTOR_MAX]*PuErrors
|
|
var puDebugArray [CONNECTOR_MAX]*PuDebug
|
|
var seccTargetEnergyArray [CONNECTOR_MAX]*SeccTargetEnergy
|
|
var seccErrorsArray [CONNECTOR_MAX]*SeccErrors
|
|
var logicAuthArray [CONNECTOR_MAX]*LogicAuth
|
|
var logicEnergyMode [CONNECTOR_MAX]*LogicEnergyMode
|
|
var logicErrorsInstance *LogicErrors
|
|
var logicWorkingMode *LogicWorkingMode
|
|
var contactorInternalStateArray [CONTACTOR_MAX]*ContactorInternalState
|
|
var contactorInternalErrorsInstance *ContactorInternalErrors
|
|
var contactorsInternalForce *ContactorsInternalForce
|
|
var contactorInternalDebugArray [CONTACTOR_MAX]*ContactorInternalDebug
|
|
var peripheryStateArray [CONNECTOR_MAX]*PeripheryState
|
|
var peripheryInfoArray [CONNECTOR_MAX]*PeripheryInfo
|
|
var peripheryDebugArray [CONNECTOR_MAX]*PeripheryDebug
|
|
var converterPresentEnergyArray [CONVERTERS_MAX]*ConverterPresentEnergy
|
|
var converterErrorsArray [CONVERTERS_MAX]*ConverterErrors
|
|
var converterDebugArray [CONVERTERS_MAX]*ConverterDebug
|
|
|
|
func StartProtocolParsing(frames <-chan *can.CanFrame) <-chan *Event {
|
|
result := make(chan *Event)
|
|
initialize()
|
|
|
|
go func() {
|
|
defer close(result)
|
|
|
|
for frame := range frames {
|
|
if frames == nil {
|
|
continue
|
|
}
|
|
|
|
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}
|
|
|
|
param := protocolMap[k]
|
|
|
|
var start uint8 = 0
|
|
for _, f := range param.fields {
|
|
if val, found := get(start, f.length, frame.Payload); found {
|
|
f.last = frame.Date
|
|
if f.value != val {
|
|
f.value = val
|
|
obj := f.setter(val)
|
|
result <- &Event{
|
|
Field: f.name,
|
|
ActionName: param.name,
|
|
Object: param.object,
|
|
UnitId: unitId,
|
|
Updated: frame.Date,
|
|
Value: obj,
|
|
}
|
|
}
|
|
}
|
|
start += f.length
|
|
}
|
|
|
|
}
|
|
}()
|
|
|
|
return result
|
|
}
|
|
|
|
func get(from uint8, size uint8, buffer []byte) (uint64, bool) {
|
|
value := binary.LittleEndian.Uint64(buffer)
|
|
|
|
var mask uint64 = ^(ALL_BITS << size)
|
|
mask = mask << from
|
|
selected := mask & value
|
|
|
|
if selected == mask {
|
|
return 0, false
|
|
} else {
|
|
return selected >> from, true
|
|
}
|
|
}
|
|
|
|
func (t *ContactorInternalState) GetUnitId() uint {
|
|
return t.UnitId
|
|
}
|
|
|
|
func (t *ContactorInternalErrors) GetUnitId() uint {
|
|
return t.UnitId
|
|
}
|
|
|
|
func (t *PuPresentEnergy) GetUnitId() uint {
|
|
return t.UnitId
|
|
}
|
|
|
|
func (t *PuPeriphery) GetUnitId() uint {
|
|
return t.UnitId
|
|
}
|
|
|
|
func (a *LogicAuth) GetUnitId() uint {
|
|
return a.UnitId
|
|
}
|
|
|
|
func (l *LogicEnergyMode) GetUnitId() uint {
|
|
return l.UnitId
|
|
}
|
|
|
|
func (l *LogicErrors) GetUnitId() uint {
|
|
return l.UnitId
|
|
}
|
|
|
|
func (l *LogicWorkingMode) GetUnitId() uint {
|
|
return l.UnitId
|
|
}
|
|
|
|
func (c *CpuPresentEnergy) GetUnitId() uint {
|
|
return c.UnitId
|
|
}
|
|
|
|
func (c *CpuPeriphery) GetUnitId() uint {
|
|
return c.UnitId
|
|
}
|
|
|
|
func (c *CpuEnergySettings) GetUnitId() uint {
|
|
return c.UnitId
|
|
}
|
|
|
|
func (c *CpuErrors) GetUnitId() uint {
|
|
return c.UnitId
|
|
}
|
|
|
|
func (d *CpuDebug) GetUnitId() uint {
|
|
return d.UnitId
|
|
}
|
|
|
|
func (p *PuErrors) GetUnitId() uint {
|
|
return p.UnitId
|
|
}
|
|
|
|
func (d *PuDebug) GetUnitId() uint {
|
|
return d.UnitId
|
|
}
|
|
|
|
func (s *SeccTargetEnergy) GetUnitId() uint {
|
|
return s.UnitId
|
|
}
|
|
|
|
func (e *SeccErrors) GetUnitId() uint {
|
|
return e.UnitId
|
|
}
|
|
|
|
func (c *ContactorsInternalForce) GetUnitId() uint {
|
|
return c.UnitId
|
|
}
|
|
|
|
func (c *ContactorInternalDebug) GetUnitId() uint {
|
|
return c.UnitId
|
|
}
|
|
|
|
func (p *PeripheryState) GetUnitId() uint {
|
|
return p.UnitId
|
|
}
|
|
|
|
func (p *PeripheryInfo) GetUnitId() uint {
|
|
return p.UnitId
|
|
}
|
|
|
|
func (p *PeripheryDebug) GetUnitId() uint {
|
|
return p.UnitId
|
|
}
|
|
|
|
func (c *ConverterPresentEnergy) GetUnitId() uint {
|
|
return c.UnitId
|
|
}
|
|
|
|
func (c *ConverterErrors) GetUnitId() uint {
|
|
return c.UnitId
|
|
}
|
|
|
|
func (c *ConverterDebug) GetUnitId() uint {
|
|
return c.UnitId
|
|
}
|
|
|