diff --git a/main.go b/main.go index 2071691..7cc3081 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "io" "log" "os" + "reflect" "regexp" "strconv" "sync" @@ -29,20 +30,21 @@ var ( // decode/encode only a minimal set of messages. // other messages change too frequently and cannot be integrated into a static tool. func generateDialect(hbDisable bool, streamreqDisable bool) *dialect.Dialect { - msgs := []message.Message{ - &common.MessageCommandLong{}, - &common.MessageCommandAck{}, - &common.MessageCommandInt{}, + msgs := []message.Message{} + + // add all messages with the TargetSystem and TargetComponent fields + var zero reflect.Value + for _, msg := range common.Dialect.Messages { + rv := reflect.ValueOf(msg).Elem() + if rv.FieldByName("TargetSystem") != zero && rv.FieldByName("TargetComponent") != zero { + msgs = append(msgs, msg) + } } if !hbDisable || !streamreqDisable { msgs = append(msgs, &common.MessageHeartbeat{}) } - if !streamreqDisable { - msgs = append(msgs, &common.MessageRequestDataStream{}) - } - return &dialect.Dialect{Version: 3, Messages: msgs} } diff --git a/message_handler.go b/message_handler.go index eb0c527..5cc8a66 100644 --- a/message_handler.go +++ b/message_handler.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "reflect" "sync" "time" @@ -16,16 +17,15 @@ const ( nodeInactiveAfter = 30 * time.Second ) -func getTarget(msg message.Message) (byte, byte, bool) { - switch msg := msg.(type) { - case *common.MessageCommandLong: - return msg.TargetSystem, msg.TargetComponent, true +var zero reflect.Value - case *common.MessageCommandAck: - return msg.TargetSystem, msg.TargetComponent, true +func getTarget(msg message.Message) (byte, byte, bool) { + rv := reflect.ValueOf(msg).Elem() + ts := rv.FieldByName("TargetSystem") + tc := rv.FieldByName("TargetComponent") - case *common.MessageCommandInt: - return msg.TargetSystem, msg.TargetComponent, true + if ts != zero && tc != zero { + return byte(ts.Uint()), byte(tc.Uint()), true } return 0, 0, false