@@ -28,13 +28,15 @@ import (
2828 "github.com/google/uuid"
2929 "github.com/plgd-dev/device/v2/bridge/device/cloud"
3030 "github.com/plgd-dev/device/v2/bridge/device/credential"
31+ "github.com/plgd-dev/device/v2/bridge/device/thingDescription"
3132 "github.com/plgd-dev/device/v2/bridge/net"
3233 "github.com/plgd-dev/device/v2/bridge/resources"
3334 cloudResource "github.com/plgd-dev/device/v2/bridge/resources/cloud"
3435 resourcesDevice "github.com/plgd-dev/device/v2/bridge/resources/device"
3536 "github.com/plgd-dev/device/v2/bridge/resources/discovery"
3637 "github.com/plgd-dev/device/v2/bridge/resources/maintenance"
3738 credentialResource "github.com/plgd-dev/device/v2/bridge/resources/secure/credential"
39+ thingDescriptionResource "github.com/plgd-dev/device/v2/bridge/resources/thingDescription"
3840 "github.com/plgd-dev/device/v2/pkg/eventloop"
3941 pkgLog "github.com/plgd-dev/device/v2/pkg/log"
4042 "github.com/plgd-dev/device/v2/schema"
@@ -47,9 +49,10 @@ import (
4749 "github.com/plgd-dev/go-coap/v3/message/codes"
4850 "github.com/plgd-dev/go-coap/v3/message/pool"
4951 "github.com/plgd-dev/go-coap/v3/pkg/sync"
52+ wotTD "github.com/web-of-things-open-source/thingdescription-go/thingDescription"
5053)
5154
52- type Resource interface {
55+ type Resource = interface {
5356 Close ()
5457 ETag () []byte
5558 GetHref () string
@@ -59,18 +62,20 @@ type Resource interface {
5962 GetPolicyBitMask () schema.BitMask
6063 SetObserveHandler (loop * eventloop.Loop , createSubscription resources.CreateSubscriptionFunc )
6164 UpdateETag ()
65+ SupportsOperations () resources.SupportedOperation
6266}
6367
6468type Device struct {
65- cfg Config
66- resources * sync.Map [string , Resource ]
67- cloudManager * cloud.Manager
68- credentialManager * credential.Manager
69- onDeviceUpdated func (d * Device )
70- loop * eventloop.Loop
71- runLoop bool
72- done chan struct {}
73- stopped atomic.Bool
69+ cfg Config
70+ resources * sync.Map [string , Resource ]
71+ cloudManager * cloud.Manager
72+ credentialManager * credential.Manager
73+ thingDescriptionManager * thingDescription.Manager
74+ onDeviceUpdated func (d * Device )
75+ loop * eventloop.Loop
76+ runLoop bool
77+ done chan struct {}
78+ stopped atomic.Bool
7479}
7580
7681func NewLogger (id uuid.UUID , level pkgLog.Level ) pkgLog.Logger {
@@ -170,6 +175,15 @@ func New(cfg Config, opts ...Option) (*Device, error) {
170175 d .cloudManager = cm
171176 d .AddResources (cloudResource .New (cloudSchema .ResourceURI , d .cloudManager ))
172177 }
178+ if o .getThingDescription != nil {
179+ td := thingDescription .New (d , o .loop )
180+ tdRes := thingDescriptionResource .New (thingDescriptionResource .ResourceURI , func (ctx context.Context , endpoints schema.Endpoints ) * wotTD.ThingDescription {
181+ return o .getThingDescription (ctx , d , endpoints )
182+ }, td .RegisterSubscription )
183+ tdRes .SetObserveHandler (o .loop , tdRes .CreateSubscription )
184+ d .AddResources (tdRes )
185+ d .thingDescriptionManager = td
186+ }
173187
174188 d .AddResources (resourcesDevice .New (plgdDevice .ResourceURI , d , o .getAdditionalProperties ))
175189 // oic/res is not discoverable
@@ -212,6 +226,11 @@ func (d *Device) GetCloudManager() *cloud.Manager {
212226 return d .cloudManager
213227}
214228
229+ // GetThingDescriptionManager returns thing description manager of the device.
230+ func (d * Device ) GetThingDescriptionManager () * thingDescription.Manager {
231+ return d .thingDescriptionManager
232+ }
233+
215234func (d * Device ) Range (f func (resourceHref string , resource Resource ) bool ) {
216235 d .resources .Range (f )
217236}
@@ -308,6 +327,9 @@ func (d *Device) Close() {
308327 if d .credentialManager != nil {
309328 d .credentialManager .Close ()
310329 }
330+ if d .thingDescriptionManager != nil {
331+ d .thingDescriptionManager .Close ()
332+ }
311333 for _ , resource := range d .resources .LoadAndDeleteAll () {
312334 resource .Close ()
313335 }
0 commit comments