Skip to content

Commit

Permalink
refactor(coap_client): refactor subscription helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Feb 19, 2022
1 parent 22788f0 commit 8a7b92b
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,25 @@ class CoapClient extends ProtocolClient {
void Function(Exception error)? error,
required void Function() complete,
}) async {
OperationType operationType;
final op = form.op ?? ["observeproperty"];
// TODO(JKRhb): Create separate function for this.
if (op.contains("subscribeevent")) {
operationType = OperationType.subscribeevent;
} else {
operationType = OperationType.observeproperty;
}
final OperationType operationType = _determineSubscribeOperationType(form);

final request = _createRequest(form, operationType);

return await request.startObservation(next, complete);
}

static OperationType _determineSubscribeOperationType(Form form) {
final op = form.op ?? [];
if (op.contains("subscribeevent")) {
return OperationType.subscribeevent;
} else if (op.contains("observeproperty")) {
return OperationType.observeproperty;
}

throw ArgumentError("Subscription form contained neither 'subscribeevent'"
"nor 'observeproperty' operation type.");
}

@override
Future<void> start() async {
// Do nothing
Expand Down Expand Up @@ -326,7 +331,6 @@ _Subprotocol? _determineSubprotocol(Form form, OperationType operationType) {
}

CoapRequestMethod _requestMethodFromOperationType(OperationType operationType) {
// TODO(JKRhb): Handle observe/subscribe case
switch (operationType) {
case OperationType.readproperty:
case OperationType.readmultipleproperties:
Expand All @@ -338,11 +342,9 @@ CoapRequestMethod _requestMethodFromOperationType(OperationType operationType) {
case OperationType.invokeaction:
return CoapRequestMethod.post;
case OperationType.observeproperty:
return CoapRequestMethod.get;
case OperationType.unobserveproperty:
return CoapRequestMethod.get;
case OperationType.subscribeevent:
return CoapRequestMethod.get;
case OperationType.unsubscribeevent:
return CoapRequestMethod.get;
}
Expand Down

0 comments on commit 8a7b92b

Please sign in to comment.