feat(go): publish realtime fields stream

This commit is contained in:
lingniu
2026-07-03 14:08:32 +08:00
parent e1bf2d72e1
commit 535acdb2a6
23 changed files with 284 additions and 15 deletions

View File

@@ -16,6 +16,7 @@ type NATSConfig struct {
URL string
Name string
RawSubjects map[envelope.Protocol]string
FieldsSubjects map[envelope.Protocol]string
UnifiedSubject string
}
@@ -23,6 +24,7 @@ type NATSSink struct {
conn *nats.Conn
publisher natsPublisher
rawSubjects map[envelope.Protocol]string
fieldsSubjects map[envelope.Protocol]string
unifiedSubject string
}
@@ -65,6 +67,16 @@ func newNATSSinkWithPublisher(publisher natsPublisher, cfg NATSConfig) *NATSSink
rawSubjects[protocol] = subject
}
}
fieldsSubjects := map[envelope.Protocol]string{
envelope.ProtocolGB32960: topics.FieldsGB32960,
envelope.ProtocolJT808: topics.FieldsJT808,
envelope.ProtocolYutongMQTT: topics.FieldsYutongMQTT,
}
for protocol, subject := range cfg.FieldsSubjects {
if subject != "" {
fieldsSubjects[protocol] = subject
}
}
unifiedSubject := cfg.UnifiedSubject
if unifiedSubject == "" {
unifiedSubject = topics.Unified
@@ -72,6 +84,7 @@ func newNATSSinkWithPublisher(publisher natsPublisher, cfg NATSConfig) *NATSSink
return &NATSSink{
publisher: publisher,
rawSubjects: rawSubjects,
fieldsSubjects: fieldsSubjects,
unifiedSubject: unifiedSubject,
}
}
@@ -91,6 +104,14 @@ func (s *NATSSink) PublishUnified(ctx context.Context, env envelope.FrameEnvelop
return s.publish(ctx, s.unifiedSubject, env)
}
func (s *NATSSink) PublishFields(ctx context.Context, env envelope.FrameEnvelope) error {
subject, ok := s.fieldsSubjects[env.Protocol]
if !ok || subject == "" {
return fmt.Errorf("fields subject not configured for protocol %s", env.Protocol)
}
return s.publish(ctx, subject, env)
}
func (s *NATSSink) Close() error {
if s == nil || s.conn == nil {
return nil