-
Notifications
You must be signed in to change notification settings - Fork 13
/
adapter.go
98 lines (81 loc) · 2.77 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
// Package coap contains the domain concept definitions needed to support
// Mainflux CoAP adapter service functionality. All constant values are taken
// from RFC, and could be adjusted based on specific use case.
package coap
import (
"context"
"fmt"
"sync"
"github.com/MainfluxLabs/mainflux/pkg/errors"
"github.com/MainfluxLabs/mainflux"
"github.com/MainfluxLabs/mainflux/pkg/messaging"
)
const chansPrefix = "channels"
// ErrUnsubscribe indicates an error to unsubscribe
var ErrUnsubscribe = errors.New("unable to unsubscribe")
// Service specifies CoAP service API.
type Service interface {
// Publish Messssage
Publish(ctx context.Context, key string, msg messaging.Message) error
// Subscribe subscribes to channel with specified id, subtopic and adds subscription to
// service map of subscriptions under given ID.
Subscribe(ctx context.Context, key, chanID, subtopic string, c Client) error
// Unsubscribe method is used to stop observing resource.
Unsubscribe(ctx context.Context, key, chanID, subptopic, token string) error
}
var _ Service = (*adapterService)(nil)
// Observers is a map of maps,
type adapterService struct {
things mainflux.ThingsServiceClient
pubsub messaging.PubSub
obsLock sync.Mutex
}
// New instantiates the CoAP adapter implementation.
func New(things mainflux.ThingsServiceClient, pubsub messaging.PubSub) Service {
as := &adapterService{
things: things,
pubsub: pubsub,
obsLock: sync.Mutex{},
}
return as
}
func (svc *adapterService) Publish(ctx context.Context, key string, msg messaging.Message) error {
cr := &mainflux.ConnByKeyReq{
Key: key,
}
conn, err := svc.things.GetConnByKey(ctx, cr)
if err != nil {
return errors.Wrap(errors.ErrAuthorization, err)
}
m := messaging.CreateMessage(conn, msg.Protocol, msg.Subtopic, &msg.Payload)
return svc.pubsub.Publish(m)
}
func (svc *adapterService) Subscribe(ctx context.Context, key, chanID, subtopic string, c Client) error {
cr := &mainflux.ConnByKeyReq{
Key: key,
}
if _, err := svc.things.GetConnByKey(ctx, cr); err != nil {
return errors.Wrap(errors.ErrAuthorization, err)
}
subject := fmt.Sprintf("%s.%s", chansPrefix, chanID)
if subtopic != "" {
subject = fmt.Sprintf("%s.%s", subject, subtopic)
}
return svc.pubsub.Subscribe(c.Token(), subject, c)
}
func (svc *adapterService) Unsubscribe(ctx context.Context, key, chanID, subtopic, token string) error {
cr := &mainflux.ConnByKeyReq{
Key: key,
}
conn, err := svc.things.GetConnByKey(ctx, cr)
if err != nil {
return errors.Wrap(errors.ErrAuthorization, err)
}
subject := fmt.Sprintf("%s.%s", chansPrefix, conn.ChannelID)
if subtopic != "" {
subject = fmt.Sprintf("%s.%s", subject, subtopic)
}
return svc.pubsub.Unsubscribe(token, subject)
}