Files
magistrala/re/outputs/channel.go
T
Dušan Borovčanin 49488738df
Continuous Delivery / lint-and-build (push) Has been cancelled
Deploy GitHub Pages / swagger-ui (push) Has been cancelled
CI Pipeline / Lint Proto (push) Has been cancelled
CI Pipeline / Detect Changes (push) Has been cancelled
Continuous Delivery / Build and Push Docker Images (push) Has been cancelled
CI Pipeline / lint-and-build (push) Has been cancelled
CI Pipeline / Test ${{ matrix.module }} (push) Has been cancelled
CI Pipeline / Upload Coverage (push) Has been cancelled
NOISSUE - Fix queue subscriptions (#3522)
Signed-off-by: dusan <borovcanindusan1@gmail.com>
2026-05-27 00:50:36 +02:00

51 lines
1.1 KiB
Go

// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package outputs
import (
"context"
"encoding/json"
"github.com/absmach/magistrala/pkg/messaging"
)
type ChannelPublisher struct {
RePubSub messaging.PubSub `json:"-"`
Channel string `json:"channel"`
Topic string `json:"topic"`
}
func (p *ChannelPublisher) Run(ctx context.Context, msg *messaging.Message, val any) error {
data, err := json.Marshal(val)
if err != nil {
return err
}
m := &messaging.Message{
Domain: msg.Domain,
Publisher: msg.Publisher,
ClientId: msg.ClientIdentity(),
Created: msg.Created,
Channel: p.Channel,
Subtopic: p.Topic,
Protocol: msg.Protocol,
Payload: data,
}
topic := messaging.EncodeTopicSuffix(msg.Domain, p.Channel, p.Topic)
if err := p.RePubSub.Publish(ctx, topic, m); err != nil {
return err
}
return nil
}
func (cp *ChannelPublisher) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{
"type": ChannelsType.String(),
"channel": cp.Channel,
"topic": cp.Topic,
})
}