From 9604b2607b7f560aef8bef9b62c3bc25b8540e99 Mon Sep 17 00:00:00 2001 From: Jozef Kralik Date: Fri, 10 May 2024 13:42:26 +0000 Subject: [PATCH] allow to custom form properties --- .../thingDescription/thingDescription.go | 43 ++++++++++++++----- .../thingDescription/ocfResources.go | 20 ++++----- bridge/test/test.go | 16 ++++--- cmd/bridge-device/device/device.go | 6 +-- cmd/bridge-device/main.go | 6 ++- 5 files changed, 61 insertions(+), 30 deletions(-) diff --git a/bridge/device/thingDescription/thingDescription.go b/bridge/device/thingDescription/thingDescription.go index f770f185..30f46acc 100644 --- a/bridge/device/thingDescription/thingDescription.go +++ b/bridge/device/thingDescription/thingDescription.go @@ -99,9 +99,20 @@ func (p PropertyElementOperations) ToSupportedOperations() resources.SupportedOp return ops | resources.SupportedOperationRead | resources.SupportedOperationWrite } -func createForm(hrefUri *url.URL, covMethod string, op thingDescription.StickyDescription, contentType message.MediaType) thingDescription.FormElementProperty { +type CreateFormFunc func(hrefUri *url.URL, op thingDescription.StickyDescription, contentType message.MediaType) (thingDescription.FormElementProperty, bool) + +func CreateCOAPForm(hrefUri *url.URL, op thingDescription.StickyDescription, contentType message.MediaType) (thingDescription.FormElementProperty, bool) { + methods := map[thingDescription.StickyDescription]string{ + thingDescription.Readproperty: http.MethodGet, + thingDescription.Writeproperty: http.MethodPost, + thingDescription.Observeproperty: http.MethodGet, + } + method, ok := methods[op] + if !ok { + return thingDescription.FormElementProperty{}, false + } additionalFields := map[string]interface{}{ - "cov:method": covMethod, + "cov:method": method, "cov:accept": float64(contentType), } ops := []string{string(op)} @@ -117,24 +128,36 @@ func createForm(hrefUri *url.URL, covMethod string, op thingDescription.StickyDe StringArray: ops, }, AdditionalFields: additionalFields, - } + }, true } -func SetForms(hrefUri *url.URL, ops resources.SupportedOperation, contentType message.MediaType) []thingDescription.FormElementProperty { +func SetForms(hrefUri *url.URL, ops resources.SupportedOperation, contentType message.MediaType, createFormFunc CreateFormFunc) []thingDescription.FormElementProperty { + if createFormFunc == nil { + return nil + } forms := make([]thingDescription.FormElementProperty, 0, 3) if ops.HasOperation(resources.SupportedOperationWrite) { - forms = append(forms, createForm(hrefUri, http.MethodPost, thingDescription.Writeproperty, contentType)) + form, ok := createFormFunc(hrefUri, thingDescription.Writeproperty, contentType) + if ok { + forms = append(forms, form) + } } if ops.HasOperation(resources.SupportedOperationRead) { - forms = append(forms, createForm(hrefUri, http.MethodGet, thingDescription.Readproperty, contentType)) + form, ok := createFormFunc(hrefUri, thingDescription.Readproperty, contentType) + if ok { + forms = append(forms, form) + } } if ops.HasOperation(resources.SupportedOperationObserve) { - forms = append(forms, createForm(hrefUri, http.MethodGet, thingDescription.Observeproperty, contentType)) + form, ok := createFormFunc(hrefUri, thingDescription.Observeproperty, contentType) + if ok { + forms = append(forms, form) + } } 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) { +func PatchPropertyElement(prop thingDescription.PropertyElement, types []string, deviceID uuid.UUID, href string, ops resources.SupportedOperation, contentType message.MediaType, createFormFunc CreateFormFunc) (thingDescription.PropertyElement, error) { if len(types) > 0 { prop.Type = &thingDescription.TypeDeclaration{ StringArray: types, @@ -144,7 +167,7 @@ func PatchPropertyElement(prop thingDescription.PropertyElement, types []string, prop.Observable = BoolToPtr(propOps.Observable) prop.ReadOnly = BoolToPtr(propOps.ReadOnly) prop.WriteOnly = BoolToPtr(propOps.WriteOnly) - if !setForm { + if createFormFunc == nil { return prop, nil } opsStrs := SupportedOperationToTDOperations(ops) @@ -163,7 +186,7 @@ func PatchPropertyElement(prop thingDescription.PropertyElement, types []string, return thingDescription.PropertyElement{}, err } } - prop.Forms = SetForms(hrefUri, ops, contentType) + prop.Forms = SetForms(hrefUri, ops, contentType, createFormFunc) return prop, nil } diff --git a/bridge/resources/thingDescription/ocfResources.go b/bridge/resources/thingDescription/ocfResources.go index ca704490..f335f529 100644 --- a/bridge/resources/thingDescription/ocfResources.go +++ b/bridge/resources/thingDescription/ocfResources.go @@ -40,27 +40,27 @@ func GetOCFResourcePropertyElement(resourceHref string) (thingDescription.Proper return prop, true } -func patchResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, resourceTypes []string, resourceHref string, contentType message.MediaType) (thingDescription.PropertyElement, error) { +func patchResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, resourceTypes []string, resourceHref string, contentType message.MediaType, createFormFunc bridgeTD.CreateFormFunc) (thingDescription.PropertyElement, error) { propOps := bridgeTD.GetPropertyElementOperations(pe) - return bridgeTD.PatchPropertyElement(pe, resourceTypes, true, deviceID, resourceHref, propOps.ToSupportedOperations(), contentType) + return bridgeTD.PatchPropertyElement(pe, resourceTypes, deviceID, resourceHref, propOps.ToSupportedOperations(), contentType, createFormFunc) } -func PatchDeviceResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL string, contentType message.MediaType, deviceType string) (thingDescription.PropertyElement, error) { +func PatchDeviceResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL string, contentType message.MediaType, deviceType string, createFormFunc bridgeTD.CreateFormFunc) (thingDescription.PropertyElement, error) { var types []string if deviceType != "" { types = []string{schemaDevice.ResourceType, deviceType} } - return patchResourcePropertyElement(pe, deviceID, types, baseURL+schemaDevice.ResourceURI, contentType) + return patchResourcePropertyElement(pe, deviceID, types, baseURL+schemaDevice.ResourceURI, contentType, createFormFunc) } -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 PatchMaintenanceResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL string, contentType message.MediaType, createFormFunc bridgeTD.CreateFormFunc) (thingDescription.PropertyElement, error) { + return patchResourcePropertyElement(pe, deviceID, []string{schemaMaintenance.ResourceType}, baseURL+schemaMaintenance.ResourceURI, contentType, createFormFunc) } -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 PatchCloudResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL string, contentType message.MediaType, createFormFunc bridgeTD.CreateFormFunc) (thingDescription.PropertyElement, error) { + return patchResourcePropertyElement(pe, deviceID, []string{schemaCloud.ResourceType}, baseURL+schemaCloud.ResourceURI, contentType, createFormFunc) } -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) +func PatchCredentialResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, baseURL string, contentType message.MediaType, createFormFunc bridgeTD.CreateFormFunc) (thingDescription.PropertyElement, error) { + return patchResourcePropertyElement(pe, deviceID, []string{schemaCredential.ResourceType}, baseURL+schemaCredential.ResourceURI, contentType, createFormFunc) } diff --git a/bridge/test/test.go b/bridge/test/test.go index 8edcf8c9..2db5ba74 100644 --- a/bridge/test/test.go +++ b/bridge/test/test.go @@ -120,8 +120,12 @@ func GetPropertyElement(td wotTD.ThingDescription, device bridgeDeviceTD.Device, if !ok { return wotTD.PropertyElement{}, false } - propElement, err := bridgeDeviceTD.PatchPropertyElement(propElement, resource.GetResourceTypes(), endpoint != "", device.GetID(), resource.GetHref(), - resource.SupportsOperations(), contentType) + var f bridgeDeviceTD.CreateFormFunc + if endpoint != "" { + f = bridgeDeviceTD.CreateCOAPForm + } + propElement, err := bridgeDeviceTD.PatchPropertyElement(propElement, resource.GetResourceTypes(), device.GetID(), resource.GetHref(), + resource.SupportsOperations(), contentType, f) return propElement, err == nil } @@ -163,7 +167,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, "") + deviceResource, err := thingDescriptionResource.PatchDeviceResourcePropertyElement(deviceResource, deviceID, baseURL, message.AppCBOR, "", bridgeDeviceTD.CreateCOAPForm) if err != nil { return nil, err } @@ -174,7 +178,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) + maintenanceResource, err = thingDescriptionResource.PatchMaintenanceResourcePropertyElement(maintenanceResource, deviceID, baseURL, message.AppCBOR, bridgeDeviceTD.CreateCOAPForm) if err != nil { return nil, err } @@ -185,7 +189,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) + cloudResource, err = thingDescriptionResource.PatchCloudResourcePropertyElement(cloudResource, deviceID, baseURL, message.AppCBOR, bridgeDeviceTD.CreateCOAPForm) if err != nil { return nil, err } @@ -197,7 +201,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) + credentialResource, err = thingDescriptionResource.PatchCredentialResourcePropertyElement(credentialResource, deviceID, baseURL, message.AppCBOR, bridgeDeviceTD.CreateCOAPForm) if err != nil { return nil, err } diff --git a/cmd/bridge-device/device/device.go b/cmd/bridge-device/device/device.go index e64519d3..f18e2066 100644 --- a/cmd/bridge-device/device/device.go +++ b/cmd/bridge-device/device/device.go @@ -42,10 +42,10 @@ func GetPropertyDescriptionForTestResource() thingDescription.PropertyElement { } } -func PatchTestResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, href string, contentType message.MediaType) (thingDescription.PropertyElement, error) { +func PatchTestResourcePropertyElement(pe thingDescription.PropertyElement, deviceID uuid.UUID, href string, contentType message.MediaType, createForm bridgeTD.CreateFormFunc) (thingDescription.PropertyElement, error) { propOps := bridgeTD.GetPropertyElementOperations(pe) - return bridgeTD.PatchPropertyElement(pe, []string{TestResourceType}, true, deviceID, href, - propOps.ToSupportedOperations(), contentType) + return bridgeTD.PatchPropertyElement(pe, []string{TestResourceType}, deviceID, href, + propOps.ToSupportedOperations(), contentType, createForm) } func GetAdditionalProperties() map[string]interface{} { diff --git a/cmd/bridge-device/main.go b/cmd/bridge-device/main.go index bb63d54c..6e426e48 100644 --- a/cmd/bridge-device/main.go +++ b/cmd/bridge-device/main.go @@ -97,7 +97,11 @@ 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) + var f thingDescription.CreateFormFunc + if endpoint != "" { + f = thingDescription.CreateCOAPForm + } + propElement, err := thingDescription.PatchPropertyElement(propElement, resource.GetResourceTypes(), dev.GetID(), resource.GetHref(), resource.SupportsOperations(), message.AppCBOR, f) return propElement, err == nil }