mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-23 04:10:28 +00:00
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
Signed-off-by: dusan <borovcanindusan1@gmail.com>
51 lines
1.1 KiB
Go
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,
|
|
})
|
|
}
|