Skip to content

Commit

Permalink
set forms for coap additional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed May 10, 2024
1 parent f960580 commit e10f602
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 28 deletions.
50 changes: 39 additions & 11 deletions bridge/device/thingDescription/thingDescription.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package thingDescription

import (
"net/http"
"net/url"

"github.com/fredbi/uri"
"github.com/google/uuid"
"github.com/plgd-dev/device/v2/bridge/resources"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/web-of-things-open-source/thingdescription-go/thingDescription"
)

Expand Down Expand Up @@ -97,7 +99,42 @@ func (p PropertyElementOperations) ToSupportedOperations() resources.SupportedOp
return ops | resources.SupportedOperationRead | resources.SupportedOperationWrite
}

func PatchPropertyElement(prop thingDescription.PropertyElement, types []string, setForm bool, deviceID uuid.UUID, href string, ops resources.SupportedOperation, contentType string) (thingDescription.PropertyElement, error) {
func createForm(hrefUri *url.URL, covMethod string, op thingDescription.StickyDescription, contentType message.MediaType) thingDescription.FormElementProperty {
additionalFields := map[string]interface{}{
"cov:method": covMethod,
"cov:accept": float64(contentType),
}
ops := []string{string(op)}
if op == thingDescription.Observeproperty {
additionalFields["subprotocol"] = "cov:observe"
ops = append(ops, string(thingDescription.Unobserveproperty))
}

return thingDescription.FormElementProperty{
ContentType: StringToPtr(contentType.String()),
Href: *hrefUri,
Op: &thingDescription.FormElementPropertyOp{
StringArray: ops,
},
AdditionalFields: additionalFields,
}
}

func SetForms(hrefUri *url.URL, ops resources.SupportedOperation, contentType message.MediaType) []thingDescription.FormElementProperty {
forms := make([]thingDescription.FormElementProperty, 0, 3)
if ops.HasOperation(resources.SupportedOperationWrite) {
forms = append(forms, createForm(hrefUri, http.MethodPost, thingDescription.Writeproperty, contentType))
}
if ops.HasOperation(resources.SupportedOperationRead) {
forms = append(forms, createForm(hrefUri, http.MethodGet, thingDescription.Readproperty, contentType))
}
if ops.HasOperation(resources.SupportedOperationObserve) {
forms = append(forms, createForm(hrefUri, http.MethodGet, thingDescription.Observeproperty, contentType))
}
return forms
}

func PatchPropertyElement(prop thingDescription.PropertyElement, types []string, setForm bool, deviceID uuid.UUID, href string, ops resources.SupportedOperation, contentType message.MediaType) (thingDescription.PropertyElement, error) {
if len(types) > 0 {
prop.Type = &thingDescription.TypeDeclaration{
StringArray: types,
Expand Down Expand Up @@ -126,16 +163,7 @@ func PatchPropertyElement(prop thingDescription.PropertyElement, types []string,
return thingDescription.PropertyElement{}, err
}
}
form := thingDescription.FormElementProperty{
ContentType: StringToPtr(contentType),
Op: &thingDescription.FormElementPropertyOp{
StringArray: opsStrs,
},
}
if hrefUri != nil {
form.Href = *hrefUri
}
prop.Forms = []thingDescription.FormElementProperty{form}
prop.Forms = SetForms(hrefUri, ops, contentType)
return prop, nil
}

Expand Down
11 changes: 6 additions & 5 deletions bridge/resources/thingDescription/ocfResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
schemaCredential "github.com/plgd-dev/device/v2/schema/credential"
schemaDevice "github.com/plgd-dev/device/v2/schema/device"
schemaMaintenance "github.com/plgd-dev/device/v2/schema/maintenance"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/web-of-things-open-source/thingdescription-go/thingDescription"
)

Expand Down Expand Up @@ -39,27 +40,27 @@ func GetOCFResourcePropertyElement(resourceHref string) (thingDescription.Proper
return prop, true
}

func patchResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, resourceTypes []string, resourceHref, contentType string) (thingDescription.PropertyElement, error) {
func patchResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, resourceTypes []string, resourceHref string, contentType message.MediaType) (thingDescription.PropertyElement, error) {
propOps := bridgeTD.GetPropertyElementOperations(pe)
return bridgeTD.PatchPropertyElement(pe, resourceTypes, true, deviceID, resourceHref, propOps.ToSupportedOperations(), contentType)
}

func PatchDeviceResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL, contentType string, deviceType string) (thingDescription.PropertyElement, error) {
func PatchDeviceResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL string, contentType message.MediaType, deviceType string) (thingDescription.PropertyElement, error) {
var types []string
if deviceType != "" {
types = []string{schemaDevice.ResourceType, deviceType}
}
return patchResourcePropertyElement(pe, deviceID, types, baseURL+schemaDevice.ResourceURI, contentType)
}

func PatchMaintenanceResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL, contentType string) (thingDescription.PropertyElement, error) {
func PatchMaintenanceResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL string, contentType message.MediaType) (thingDescription.PropertyElement, error) {
return patchResourcePropertyElement(pe, deviceID, []string{schemaMaintenance.ResourceType}, baseURL+schemaMaintenance.ResourceURI, contentType)
}

func PatchCloudResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL, contentType string) (thingDescription.PropertyElement, error) {
func PatchCloudResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL string, contentType message.MediaType) (thingDescription.PropertyElement, error) {
return patchResourcePropertyElement(pe, deviceID, []string{schemaCloud.ResourceType}, baseURL+schemaCloud.ResourceURI, contentType)
}

func PatchCredentialResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL, contentType string) (thingDescription.PropertyElement, error) {
func PatchCredentialResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL string, contentType message.MediaType) (thingDescription.PropertyElement, error) {
return patchResourcePropertyElement(pe, deviceID, []string{schemaCredential.ResourceType}, baseURL+schemaCredential.ResourceURI, contentType)
}
2 changes: 1 addition & 1 deletion bridge/resources/thingDescription/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func getEndpoint(t *testing.T, c *client.Client, deviceID string) string {

func getPatchedTD(td wotTD.ThingDescription, d service.Device, epURI string) wotTD.ThingDescription {
return bridgeDeviceTD.PatchThingDescription(td, d, epURI, func(resourceHref string, resource bridgeDeviceTD.Resource) (wotTD.PropertyElement, bool) {
return bridgeTest.GetPropertyElement(td, d, epURI, resourceHref, resource, message.AppCBOR.String())
return bridgeTest.GetPropertyElement(td, d, epURI, resourceHref, resource, message.AppCBOR)
})
}

Expand Down
12 changes: 6 additions & 6 deletions bridge/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func makeDeviceConfig(id uuid.UUID, cloudEnabled bool, credentialEnabled bool) d
return cfg
}

func GetPropertyElement(td wotTD.ThingDescription, device bridgeDeviceTD.Device, endpoint string, resourceHref string, resource bridgeDeviceTD.Resource, contentType string) (wotTD.PropertyElement, bool) {
func GetPropertyElement(td wotTD.ThingDescription, device bridgeDeviceTD.Device, endpoint string, resourceHref string, resource bridgeDeviceTD.Resource, contentType message.MediaType) (wotTD.PropertyElement, bool) {
propElement, ok := td.Properties[resourceHref]
if !ok {
return wotTD.PropertyElement{}, false
Expand Down Expand Up @@ -149,7 +149,7 @@ func NewBridgedDeviceWithThingDescription(t *testing.T, s *service.Service, id s
}
newTD := bridgeDeviceTD.PatchThingDescription(*td, device, endpoint,
func(resourceHref string, resource bridgeDeviceTD.Resource) (wotTD.PropertyElement, bool) {
return GetPropertyElement(*td, device, endpoint, resourceHref, resource, message.AppCBOR.String())
return GetPropertyElement(*td, device, endpoint, resourceHref, resource, message.AppCBOR)
})
return &newTD
}))
Expand All @@ -163,7 +163,7 @@ func getOCFResourcesProperties(deviceID uuid.UUID, baseURL string, cloudEnabled,
if !ok {
return nil, errors.New("device resource not found")
}
deviceResource, err := thingDescriptionResource.PatchDeviceResourcePropertyElement(deviceResource, deviceID, baseURL, message.AppCBOR.String(), "")
deviceResource, err := thingDescriptionResource.PatchDeviceResourcePropertyElement(deviceResource, deviceID, baseURL, message.AppCBOR, "")
if err != nil {
return nil, err
}
Expand All @@ -174,7 +174,7 @@ func getOCFResourcesProperties(deviceID uuid.UUID, baseURL string, cloudEnabled,
return nil, errors.New("maintenance resource not found")
}
properties[schemaMaintenance.ResourceURI] = maintenanceResource
maintenanceResource, err = thingDescriptionResource.PatchMaintenanceResourcePropertyElement(maintenanceResource, deviceID, baseURL, message.AppCBOR.String())
maintenanceResource, err = thingDescriptionResource.PatchMaintenanceResourcePropertyElement(maintenanceResource, deviceID, baseURL, message.AppCBOR)
if err != nil {
return nil, err
}
Expand All @@ -185,7 +185,7 @@ func getOCFResourcesProperties(deviceID uuid.UUID, baseURL string, cloudEnabled,
if !ok {
return nil, errors.New("cloud resource not found")
}
cloudResource, err = thingDescriptionResource.PatchCloudResourcePropertyElement(cloudResource, deviceID, baseURL, message.AppCBOR.String())
cloudResource, err = thingDescriptionResource.PatchCloudResourcePropertyElement(cloudResource, deviceID, baseURL, message.AppCBOR)
if err != nil {
return nil, err
}
Expand All @@ -197,7 +197,7 @@ func getOCFResourcesProperties(deviceID uuid.UUID, baseURL string, cloudEnabled,
if !ok {
return nil, errors.New("credential resource not found")
}
credentialResource, err = thingDescriptionResource.PatchCredentialResourcePropertyElement(credentialResource, deviceID, baseURL, message.AppCBOR.String())
credentialResource, err = thingDescriptionResource.PatchCredentialResourcePropertyElement(credentialResource, deviceID, baseURL, message.AppCBOR)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/bridge-device/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/google/uuid"
bridgeTD "github.com/plgd-dev/device/v2/bridge/device/thingDescription"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/web-of-things-open-source/thingdescription-go/thingDescription"
)

Expand Down Expand Up @@ -41,7 +42,7 @@ func GetPropertyDescriptionForTestResource() thingDescription.PropertyElement {
}
}

func PatchTestResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, href, contentType string) (thingDescription.PropertyElement, error) {
func PatchTestResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, href string, contentType message.MediaType) (thingDescription.PropertyElement, error) {
propOps := bridgeTD.GetPropertyElementOperations(pe)
return bridgeTD.PatchPropertyElement(pe, []string{TestResourceType}, true, deviceID, href,
propOps.ToSupportedOperations(), contentType)
Expand Down
2 changes: 1 addition & 1 deletion cmd/bridge-device/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func patchPropertyElement(td wotTD.ThingDescription, dev *device.Device, endpoin
if !ok {
return wotTD.PropertyElement{}, false
}
propElement, err := thingDescription.PatchPropertyElement(propElement, resource.GetResourceTypes(), endpoint != "", dev.GetID(), resource.GetHref(), resource.SupportsOperations(), message.AppCBOR.String())
propElement, err := thingDescription.PatchPropertyElement(propElement, resource.GetResourceTypes(), endpoint != "", dev.GetID(), resource.GetHref(), resource.SupportsOperations(), message.AppCBOR)
return propElement, err == nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/plgd-dev/kit/v2 v2.0.0-20211006190727-057b33161b90
github.com/stretchr/testify v1.9.0
github.com/ugorji/go/codec v1.2.12
github.com/web-of-things-open-source/thingdescription-go v0.0.0-20240510090525-772cd4ad3459
github.com/web-of-things-open-source/thingdescription-go v0.0.0-20240510130416-741fef736e1e
go.uber.org/atomic v1.11.0
golang.org/x/sync v0.7.0
google.golang.org/grpc v1.63.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.12.0/go.mod h1:229t1eWu9UXTPmoUkbpN/fctKPBY4IJoFXQnxHGXy6E=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/web-of-things-open-source/thingdescription-go v0.0.0-20240510090525-772cd4ad3459 h1:5JIsujthXmSrKC318Y6UJRyQ2yysV0cuen70fL9LW3U=
github.com/web-of-things-open-source/thingdescription-go v0.0.0-20240510090525-772cd4ad3459/go.mod h1:L/jWuWf+v7rmuFykpUP/runRXTnnA0QdGGgou8vzPrw=
github.com/web-of-things-open-source/thingdescription-go v0.0.0-20240510130416-741fef736e1e h1:blQyU8WqqyRcBmaAPLiU5cTg9BSQu04CJZ/ffEzgI1s=
github.com/web-of-things-open-source/thingdescription-go v0.0.0-20240510130416-741fef736e1e/go.mod h1:L/jWuWf+v7rmuFykpUP/runRXTnnA0QdGGgou8vzPrw=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down

0 comments on commit e10f602

Please sign in to comment.