Small CLI monitoring tool to decode proprietary [...] protocol and display info in human readable representation
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.
ycli-mon/yabl/init.go

295 lines
8.9 KiB

package yabl
import (
"fmt"
"reflect"
)
func entityToFields(entity Action) []*field {
t := reflect.TypeOf(entity).Elem()
count := t.NumField()
var result = make([]*field, count-1)
//var testMap = make(map[reflect.Type]uint8)
//var offset uint8 = 0
for i := 1; i <= count-1; i++ {
f := t.Field(i)
//var a = V2GModeType(1)
//reflA := reflect.ValueOf(&a)
//val := reflect.ValueOf(entity).Elem()
//val.Field(i).Set(reflA)
//typeOf := reflect.TypeOf((*V2GModeType)(nil))
//implements := f.Type.AssignableTo(typeOf)
//testMap[typeOf] = uint8(i)
var helper fHelper
var ok bool
if helper, ok = fieldHelperPrototypeMap[f.Type]; !ok {
panic(fmt.Sprintf("Helper not found for type %v", f.Type))
}
// This variable is value wrapper for assigning struct fields via reflection
value := reflect.ValueOf(entity).Elem().Field(i)
// Create copy of TypeHelper prototype
var copyHelper = reflect.New(reflect.ValueOf(helper).Elem().Type()).Interface().(fHelper)
reflect.ValueOf(copyHelper).Elem().Set(reflect.ValueOf(helper).Elem())
// Set entity properties
copyHelper.setReflectedValue(&value)
result[i-1] = &field{
length: copyHelper.getSize(),
fromBus: copyHelper.fromBus,
toBus: copyHelper.toBus,
name: FName(f.Name),
}
//offset += helper.getSize()
}
return result
}
func (c *converter) initialize() {
c.protocolMap = make(map[key]*action)
for i := uint(1); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdCpuPresentEnergy, i}
cpe := &CpuPresentEnergy{unit: 1}
c.cpuPresentEnergyArray[i] = cpe
fields := entityToFields(cpe)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PCpuPresentEnergy, object: cpe}
}
c.cpuPeripheryInstance = &CpuPeriphery{unit: 0}
fields := entityToFields(c.cpuPeripheryInstance)
c.protocolMap[key{CanIdCpuPeriphery, 0}] = &action{
fields: fields,
interval: 1000,
name: PCpuPeriphery,
object: c.cpuPeripheryInstance,
}
for i := uint(0); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdCpuEnergySettings, i}
ces := &CpuEnergySettings{unit: unit(i)}
c.cpuEnergySettingsArray[i] = ces
fields = entityToFields(ces)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PCpuEnergySettings, object: ces}
}
c.cpuErrorsInstance = &CpuErrors{unit: 0}
fields = entityToFields(c.cpuErrorsInstance)
c.protocolMap[key{CanIdCpuErrors, 0}] = &action{
fields: fields,
interval: 1000,
name: PCpuErrors,
object: c.cpuErrorsInstance,
}
c.cpuDebugInstance = &CpuDebug{unit: 0}
fields = entityToFields(c.cpuDebugInstance)
c.protocolMap[key{CanIdCpuDebug, 0}] = &action{
fields: fields,
interval: 1000,
name: PCpuDebug,
object: c.cpuDebugInstance,
}
for i := uint(1); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdPuPresentEnergy, i}
ppe := &PuPresentEnergy{unit: unit(i)}
c.puPresentEnergyArray[i] = ppe
fields = entityToFields(ppe)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PPuPresentEnergy, object: ppe}
}
for i := uint(1); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdPuPeriphery, i}
pp := &PuPeriphery{unit: unit(i)}
c.puPeripheryArray[i] = pp
fields = entityToFields(pp)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PPuPeriphery, object: pp}
}
for i := uint(1); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdPuErrors, i}
pe := &PuErrors{unit: 1}
c.puErrorsArray[i] = pe
fields := entityToFields(pe)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PPuErrors, object: pe}
}
for i := uint(0); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdPuDebug, i}
pd := &PuDebug{unit: unit(i)}
c.puDebugArray[i] = pd
fields = entityToFields(pd)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PPuDebug, object: pd}
}
for i := uint(1); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdSeccTargetEnergy, i}
ste := &SeccTargetEnergy{unit: unit(i)}
c.seccTargetEnergyArray[i] = ste
fields = entityToFields(ste)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PSeccTargetEnergy, object: ste}
}
for i := uint(1); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdSeccErrors, i}
se := &SeccErrors{unit: unit(i)}
c.seccErrorsArray[i] = se
fields = entityToFields(se)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PSeccErrors, object: se}
}
for i := uint(0); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdLogicAuth, i}
la := &LogicAuth{unit: unit(i)}
c.logicAuthArray[i] = la
fields = entityToFields(la)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PLogicAuth, object: la}
}
for i := uint(0); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdLogicEnergyMode, i}
lem := &LogicEnergyMode{unit: unit(i)}
c.logicEnergyMode[i] = lem
fields = entityToFields(lem)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PLogicEnergyMode, object: lem}
}
c.logicErrorsInstance = &LogicErrors{unit: 0}
fields = entityToFields(c.logicErrorsInstance)
c.protocolMap[key{CanIdLogicErrors, 0}] = &action{
fields: fields,
interval: 1000,
name: PLogicErrors,
object: c.logicErrorsInstance,
}
for i := uint(0); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdLogicWorkingMode, i}
lwm := &LogicWorkingMode{unit: unit(i)}
c.logicWorkingMode[i] = lwm
fields = entityToFields(lwm)
c.protocolMap[key] = &action{
fields: fields,
interval: 1000,
name: PLogicWorkingMode,
object: lwm,
}
}
for i := uint(0); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdLogicCommunicationMode, i}
lcm := &LogicCommunicationMode{unit: unit(i)}
c.LogicCommunicationMode[i] = lcm
fields = entityToFields(lcm)
c.protocolMap[key] = &action{
fields: fields,
interval: 1000,
name: PLogicCommunicationMode,
object: lcm,
}
}
for i := uint(0); i < CONTACTOR_MAX; i++ {
key := key{CanIdContIntState, i}
cis := &ContactorInternalState{unit: unit(i)}
c.contactorInternalStateArray[i] = cis
fields = entityToFields(cis)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PContactorInternalState, object: cis}
}
c.contactorInternalErrorsInstance = &ContactorInternalErrors{unit: 0}
fields = entityToFields(c.contactorInternalErrorsInstance)
c.protocolMap[key{CanIdContIntErr, 0}] = &action{
fields: fields,
interval: 1000,
name: PContactorInternalErrors,
object: c.contactorInternalErrorsInstance,
}
c.contactorsInternalForce = &ContactorsInternalForce{unit: 0}
fields = entityToFields(c.contactorsInternalForce)
c.protocolMap[key{CanIdContIntForce, 0}] = &action{
fields: fields,
interval: 1000,
name: PContactorsInternalForce,
object: c.contactorsInternalForce,
}
for i := uint(0); i < CONTACTOR_MAX; i++ {
key := key{CanIdContIntDebug, i}
cid := &ContactorInternalDebug{unit: unit(i)}
c.contactorInternalDebugArray[i] = cid
fields = entityToFields(cid)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PContactorInternalDebug, object: cid}
}
for i := uint(0); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdPeripheryState, i}
ps := &PeripheryState{unit: unit(i)}
c.peripheryStateArray[i] = ps
fields = entityToFields(ps)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PPeripheryState, object: ps}
}
for i := uint(0); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdPeripheryInfo, i}
pi := &PeripheryInfo{unit: unit(i)}
c.peripheryInfoArray[i] = pi
fields = entityToFields(pi)
c.protocolMap[key] = &action{fields: fields, interval: 5000, name: PPeripheryInfo, object: pi}
}
for i := uint(0); i <= CONNECTOR_COUNT; i++ {
key := key{CanIdPeripheryDebug, i}
pd := &PeripheryDebug{unit: unit(i)}
c.peripheryDebugArray[i] = pd
fields = entityToFields(pd)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PPeripheryDebug, object: pd}
}
for i := uint(1); i < CONVERTERS_MAX; i++ {
key := key{CanIdConverterPresentEnergy, i}
cpe := &ConverterPresentEnergy{unit: unit(i)}
c.converterPresentEnergyArray[i] = cpe
fields = entityToFields(cpe)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PConverterPresentEnergy, object: cpe}
}
for i := uint(1); i < CONVERTERS_MAX; i++ {
key := key{CanIdConverterErrors, i}
ce := &ConverterErrors{unit: unit(i)}
c.converterErrorsArray[i] = ce
fields = entityToFields(ce)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PConverterErrors, object: ce}
}
for i := uint(0); i < CONVERTERS_MAX; i++ {
key := key{CanIdConverterDebug, i}
cd := &ConverterDebug{unit: unit(i)}
c.converterDebugArray[i] = cd
fields = entityToFields(cd)
c.protocolMap[key] = &action{fields: fields, interval: 1000, name: PConverterDebug, object: cd}
}
// set "null" values
for _, element := range c.protocolMap {
var from uint8 = 0
for _, f := range element.fields {
var mask uint64 = ^(ALL_BITS << f.length)
mask = mask << from
f.value = mask
from += f.length
}
}
}