Skip to content

Commit

Permalink
allow to custom form properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jkralik committed May 10, 2024
1 parent e10f602 commit 9604b26
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 30 deletions.
43 changes: 33 additions & 10 deletions bridge/device/thingDescription/thingDescription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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
}

Expand Down
20 changes: 10 additions & 10 deletions bridge/resources/thingDescription/ocfResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
16 changes: 10 additions & 6 deletions bridge/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/bridge-device/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down
6 changes: 5 additions & 1 deletion cmd/bridge-device/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 9604b26

Please sign in to comment.