@@ -32,14 +32,17 @@ import { Server, createServer, registerFormat, IncomingMessage, OutgoingMessage
3232import slugify from "slugify" ;
3333import { Readable } from "stream" ;
3434import { MdnsIntroducer } from "./mdns-introducer" ;
35- import { PropertyElement , DataSchema } from "wot-thing-description-types" ;
35+ import { PropertyElement , DataSchema , ActionElement , EventElement } from "wot-thing-description-types" ;
3636import { CoapServerConfig } from "./coap" ;
3737import { DataSchemaValue } from "wot-typescript-definitions" ;
38+ import { filterPropertyObserveOperations , getPropertyOpValues } from "./util" ;
3839
3940const { debug, warn, info, error } = createLoggers ( "binding-coap" , "coap-server" ) ;
4041
4142type CoreLinkFormatParameters = Map < string , string [ ] | number [ ] > ;
4243
44+ type AffordanceElement = PropertyElement | ActionElement | EventElement ;
45+
4346// TODO: Move to core?
4447type AugmentedInteractionOptions = WoT . InteractionOptions & { formIndex : number } ;
4548
@@ -228,21 +231,45 @@ export default class CoapServer implements ProtocolServer {
228231 return opValues ;
229232 }
230233
234+ private addFormToAffordance ( form : TD . Form , affordance : AffordanceElement ) : void {
235+ const affordanceForms = affordance . forms ;
236+ if ( affordanceForms == null ) {
237+ affordance . forms = [ form ] ;
238+ } else {
239+ affordanceForms . push ( form ) ;
240+ }
241+ }
242+
231243 private fillInPropertyBindingData ( thing : ExposedThing , base : string , offeredMediaType : string ) {
232244 for ( const [ propertyName , property ] of Object . entries ( thing . properties ) ) {
233- const opValues = ProtocolHelpers . getPropertyOpValues ( property ) ;
234- const form = this . createAffordanceForm (
235- base ,
236- this . PROPERTY_DIR ,
237- offeredMediaType ,
238- opValues ,
239- thing . uriVariables ,
240- propertyName ,
241- property . uriVariables
242- ) ;
245+ const [ readWriteOpValues , observeOpValues ] = getPropertyOpValues ( property ) ;
246+ for ( const formOpValues of [ observeOpValues , readWriteOpValues ] ) {
247+ if ( formOpValues . length === 0 ) {
248+ continue ;
249+ }
250+
251+ let subprotocol : string | undefined ;
252+
253+ const observeOpValues = filterPropertyObserveOperations ( formOpValues ) ;
254+
255+ if ( observeOpValues . length > 0 ) {
256+ subprotocol = "cov:observe" ;
257+ }
258+
259+ const form = this . createAffordanceForm (
260+ base ,
261+ this . PROPERTY_DIR ,
262+ offeredMediaType ,
263+ formOpValues ,
264+ thing . uriVariables ,
265+ propertyName ,
266+ property . uriVariables ,
267+ subprotocol
268+ ) ;
243269
244- property . forms . push ( form ) ;
245- this . logHrefAssignment ( form , "Property" , propertyName ) ;
270+ this . addFormToAffordance ( form , property ) ;
271+ this . logHrefAssignment ( form , "Property" , propertyName ) ;
272+ }
246273 }
247274 }
248275
@@ -258,7 +285,7 @@ export default class CoapServer implements ProtocolServer {
258285 action . uriVariables
259286 ) ;
260287
261- action . forms . push ( form ) ;
288+ this . addFormToAffordance ( form , action ) ;
262289 this . logHrefAssignment ( form , "Action" , actionName ) ;
263290 }
264291 }
@@ -272,10 +299,11 @@ export default class CoapServer implements ProtocolServer {
272299 [ "subscribeevent" , "unsubscribeevent" ] ,
273300 thing . uriVariables ,
274301 eventName ,
275- event . uriVariables
302+ event . uriVariables ,
303+ "cov:observe"
276304 ) ;
277305
278- event . forms . push ( form ) ;
306+ this . addFormToAffordance ( form , event ) ;
279307 this . logHrefAssignment ( form , "Event" , eventName ) ;
280308 }
281309 }
@@ -287,7 +315,8 @@ export default class CoapServer implements ProtocolServer {
287315 opValues : string | string [ ] ,
288316 thingUriVariables : PropertyElement [ "uriVariables" ] ,
289317 affordanceName ?: string ,
290- affordanceUriVariables ?: PropertyElement [ "uriVariables" ]
318+ affordanceUriVariables ?: PropertyElement [ "uriVariables" ] ,
319+ subprotocol ?: string
291320 ) : TD . Form {
292321 const affordanceNamePattern = Helpers . updateInteractionNameWithUriVariablePattern (
293322 affordanceName ?? "" ,
@@ -303,6 +332,7 @@ export default class CoapServer implements ProtocolServer {
303332
304333 const form = new TD . Form ( href , offeredMediaType ) ;
305334 form . op = opValues ;
335+ form . subprotocol = subprotocol ;
306336
307337 return form ;
308338 }
0 commit comments