Skip to content

Commit

Permalink
fixup! bridge: updates for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Apr 30, 2024
1 parent a2f08dd commit 5e257d0
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ CERT_TOOL_SIGN_ALG ?= ECDSA-SHA256
# supported values: P256, P384, P521
CERT_TOOL_ELLIPTIC_CURVE ?= P256
DEVSIM_IMAGE ?= ghcr.io/iotivity/iotivity-lite/cloud-server-discovery-resource-observable-debug:vnext
HUB_TEST_DEVICE_IMAGE = ghcr.io/plgd-dev/hub/test-cloud-server:main
# HUB_TEST_DEVICE_IMAGE = ghcr.io/plgd-dev/hub/test-cloud-server:main
HUB_TEST_DEVICE_IMAGE = ghcr.io/plgd-dev/hub/test-cloud-server:vnext-pr1274

default: build

Expand Down
74 changes: 74 additions & 0 deletions bridge/resources/thingDescription/ocfResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ package thingDescription

import (
_ "embed"
"net/url"

schemaCloud "github.com/plgd-dev/device/v2/schema/cloud"
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 @@ -32,3 +38,71 @@ func GetOCFResourcePropertyElement(resourceHref string) (thingDescription.Proper
}
return prop, true
}

func boolToPtr(v bool) *bool {
if !v {
return nil
}
return &v
}

func stringToPtr(v string) *string {
if v == "" {
return nil
}
return &v
}

func createForm(pe *thingDescription.PropertyElement, href string, contentType string, op []string) error {
uri, err := url.Parse(href)
if err != nil {
return err
}
pe.Forms = []thingDescription.FormElementProperty{
{
ContentType: stringToPtr(contentType),
Op: &thingDescription.FormElementPropertyOp{
StringArray: op,
},
Href: *uri,
},
}
return nil
}

func PatchDeviceResourcePropertyElement(pe *thingDescription.PropertyElement, baseURL, deviceType string, setForm bool) error {
if deviceType != "" {
pe.Type = &thingDescription.TypeDeclaration{
StringArray: []string{schemaDevice.ResourceType, deviceType},
}
}
pe.ReadOnly = boolToPtr(true)
if setForm {
err := createForm(pe, baseURL+schemaDevice.ResourceURI, message.AppCBOR.String(), []string{"readproperty"})
if err != nil {
return err
}
}
return nil
}

func PatchMaintenanceResourcePropertyElement(pe *thingDescription.PropertyElement, baseURL string, setForm bool) error {
if !setForm {
return nil
}
return createForm(pe, baseURL+schemaMaintenance.ResourceURI, message.AppCBOR.String(), []string{"readproperty", "writeproperty"})
}

func PatchCloudResourcePropertyElement(pe *thingDescription.PropertyElement, baseURL string, setForm bool) error {
if !setForm {
return nil
}
return createForm(pe, baseURL+schemaCloud.ResourceURI, message.AppCBOR.String(), []string{"readproperty", "writeproperty"})
}

func PatchCredentialResourcePropertyElement(pe *thingDescription.PropertyElement, baseURL string, setForm bool) error {
if !setForm {
return nil
}
return createForm(pe, baseURL+schemaCredential.ResourceURI, message.AppCBOR.String(), []string{"readproperty", "writeproperty"})
}
18 changes: 15 additions & 3 deletions bridge/resources/thingDescription/ocfResources.jsonld
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"readOnly": true,
"format": "uuid"
}
}
},
"@type": [
"oic.wk.d"
]
},
"/oic/mnt": {
"title": "Maintenance",
Expand All @@ -36,7 +39,10 @@
"title": "Factory Reset",
"type": "boolean"
}
}
},
"@type": [
"oic.wk.mnt"
]
},
"/CoapCloudConfResURI": {
"title": "CoapCloudConfResURI",
Expand Down Expand Up @@ -76,7 +82,10 @@
"title": "Last error code",
"type": "integer"
}
}
},
"@type": [
"oic.r.coapcloudconf"
]
},
"/oic/sec/cred": {
"title": "Credentials",
Expand Down Expand Up @@ -240,6 +249,9 @@
"required": [
"creds",
"rowneruuid"
],
"@type": [
"oic.r.cred"
]
}
}
Expand Down
68 changes: 49 additions & 19 deletions bridge/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,40 +155,70 @@ func NewBridgedDeviceWithThingDescription(t *testing.T, s *service.Service, id s
return NewBridgedDeviceWithConfig(t, s, cfg, opts...)
}

func ThingDescription(deviceID string, cloudEnabled, credentialEnabled bool) (wotTD.ThingDescription, error) {
td := wotTD.ThingDescription{}
td.Context = &thingDescription.Context
td.Type = &wotTD.TypeDeclaration{StringArray: []string{"Thing"}}
id, err := uri.Parse("urn:uuid:" + deviceID)
if err != nil {
return wotTD.ThingDescription{}, err
}
td.ID = id

td.Properties = make(map[string]wotTD.PropertyElement)
func getProperties(deviceID string, cloudEnabled, credentialEnabled bool) (map[string]wotTD.PropertyElement, error) {
properties := make(map[string]wotTD.PropertyElement)
deviceResource, ok := thingDescriptionResource.GetOCFResourcePropertyElement(schemaDevice.ResourceURI)
if !ok {
return wotTD.ThingDescription{}, errors.New("device resource not found")
return nil, errors.New("device resource not found")
}
baseURL := "/" + deviceID
err := thingDescriptionResource.PatchDeviceResourcePropertyElement(&deviceResource, baseURL, "", true)
if err != nil {
return nil, err
}
td.Properties[schemaDevice.ResourceURI] = deviceResource
properties[schemaDevice.ResourceURI] = deviceResource

maintenanceResource, ok := thingDescriptionResource.GetOCFResourcePropertyElement(schemaMaintenance.ResourceURI)
if !ok {
return wotTD.ThingDescription{}, errors.New("maintenance resource not found")
return nil, errors.New("maintenance resource not found")
}
td.Properties[schemaMaintenance.ResourceURI] = maintenanceResource
properties[schemaMaintenance.ResourceURI] = maintenanceResource
err = thingDescriptionResource.PatchMaintenanceResourcePropertyElement(&maintenanceResource, baseURL, true)
if err != nil {
return nil, err
}
properties[schemaMaintenance.ResourceURI] = maintenanceResource

if cloudEnabled {
cloudResource, ok := thingDescriptionResource.GetOCFResourcePropertyElement(schemaCloud.ResourceURI)
if !ok {
return wotTD.ThingDescription{}, errors.New("cloud resource not found")
return nil, errors.New("cloud resource not found")
}
err = thingDescriptionResource.PatchCloudResourcePropertyElement(&cloudResource, baseURL, true)
if err != nil {
return nil, err
}
td.Properties[schemaCloud.ResourceURI] = cloudResource
properties[schemaCloud.ResourceURI] = cloudResource
}

if credentialEnabled {
credentialResource, ok := thingDescriptionResource.GetOCFResourcePropertyElement(credential.ResourceURI)
if !ok {
return wotTD.ThingDescription{}, errors.New("credential resource not found")
return nil, errors.New("credential resource not found")
}
err = thingDescriptionResource.PatchCredentialResourcePropertyElement(&credentialResource, baseURL, true)
if err != nil {
return nil, err
}
td.Properties[credential.ResourceURI] = credentialResource
properties[credential.ResourceURI] = credentialResource
}
return properties, nil
}

func ThingDescription(deviceID string, cloudEnabled, credentialEnabled bool) (wotTD.ThingDescription, error) {
td := wotTD.ThingDescription{}
td.Context = &thingDescription.Context
td.Type = &wotTD.TypeDeclaration{StringArray: []string{"Thing"}}
id, err := uri.Parse("urn:uuid:" + deviceID)
if err != nil {
return wotTD.ThingDescription{}, err
}
td.ID = id

properties, err := getProperties(deviceID, cloudEnabled, credentialEnabled)
if err != nil {
return wotTD.ThingDescription{}, err
}
td.Properties = properties
return td, nil
}
5 changes: 4 additions & 1 deletion cmd/bridge-device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import (
"github.com/web-of-things-open-source/thingdescription-go/thingDescription"
)

const myPropertyKey = "my-property"
const (
ResourceType = "oic.d.virtual"
myPropertyKey = "my-property"
)

//go:embed bridge-device.jsonld
var bdResourcesData []byte
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 @@ -177,7 +177,7 @@ func main() {
newDevice := func(id uuid.UUID, piid uuid.UUID) (service.Device, error) {
return device.New(device.Config{
Name: fmt.Sprintf("bridged-device-%d", i),
ResourceTypes: []string{"oic.d.virtual"},
ResourceTypes: []string{ResourceType},
ID: id,
ProtocolIndependentID: piid,
MaxMessageSize: cfg.Config.API.CoAP.MaxMessageSize,
Expand Down

0 comments on commit 5e257d0

Please sign in to comment.