Skip to content

Commit

Permalink
chore: adapt CoAP binding to new library API
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 16, 2023
1 parent d6fbcda commit 8756d27
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
33 changes: 15 additions & 18 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class CoapClient extends ProtocolClient {
final ClientSecurityProvider? _clientSecurityProvider;

Future<coap.CoapRequest> _createRequest(
coap.CoapCode code,
coap.RequestMethod requestMethod,
Uri uri, {
Content? content,
coap.CoapMediaType? format,
Expand All @@ -109,7 +109,7 @@ class CoapClient extends ProtocolClient {
payload.addAll((await content.byteBuffer).asUint8List());
}

final request = coap.CoapRequest(code)
final request = coap.CoapRequest(requestMethod)
..payload = payload
..uriPath = uri.path
..accept = accept
Expand Down Expand Up @@ -145,7 +145,7 @@ class CoapClient extends ProtocolClient {
// limitations of the CoAP library
Future<Content> _sendRequest(
Uri uri,
coap.CoapCode method, {
coap.RequestMethod method, {
Content? content,
required Form? form,
coap.CoapMediaType? format,
Expand All @@ -156,7 +156,7 @@ class CoapClient extends ProtocolClient {
}) async {
final coapClient = coap.CoapClient(
uri,
_InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
config: _InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
pskCredentialsCallback:
_createPskCallback(uri, form, _clientSecurityProvider),
);
Expand Down Expand Up @@ -199,7 +199,7 @@ class CoapClient extends ProtocolClient {

Future<DiscoveryContent> _sendDiscoveryRequest(
Uri uri,
coap.CoapCode method, {
coap.RequestMethod method, {
Content? content,
required Form? form,
coap.CoapMediaType? format,
Expand Down Expand Up @@ -240,7 +240,7 @@ class CoapClient extends ProtocolClient {

final coapClient = coap.CoapClient(
creationHintUri,
_InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
config: _InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
);

final response = await coapClient.send(request);
Expand Down Expand Up @@ -314,7 +314,7 @@ class CoapClient extends ProtocolClient {

final client = coap.CoapClient(
request.uri.replace(scheme: 'coaps'),
coap.CoapConfigTinydtls(),
config: coap.CoapConfigTinydtls(),
pskCredentialsCallback: (identityHint) => pskCredentials,
);

Expand Down Expand Up @@ -429,14 +429,12 @@ class CoapClient extends ProtocolClient {

final coapClient = coap.CoapClient(
form.resolvedHref,
_InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
config: _InternalCoapConfig(_coapConfig ?? CoapConfig(), form),
);

if (subprotocol == CoapSubprotocol.observe) {
final observeClientRelation = await coapClient.observe(request);
observeClientRelation.stream.listen((event) {
handleResponse(event.resp);
});
observeClientRelation.listen(handleResponse);
return CoapSubscription(coapClient, observeClientRelation, complete);
}

Expand All @@ -460,7 +458,7 @@ class CoapClient extends ProtocolClient {
final streamController = StreamController<DiscoveryContent>();
final multicastResponseHandler = coap.CoapMulticastResponseHandler(
(data) {
streamController.add(data.resp.determineDiscoveryContent(uri.scheme));
streamController.add(data.determineDiscoveryContent(uri.scheme));
},
onError: streamController.addError,
onDone: () async {
Expand All @@ -470,7 +468,7 @@ class CoapClient extends ProtocolClient {

final content = _sendDiscoveryRequest(
uri,
coap.CoapCode.get,
coap.RequestMethod.get,
form: null,
accept: coap.CoapMediaType.applicationTdJson,
multicastResponseHandler: multicastResponseHandler,
Expand All @@ -485,7 +483,7 @@ class CoapClient extends ProtocolClient {
) async* {
yield await _sendDiscoveryRequest(
uri,
coap.CoapCode.get,
coap.RequestMethod.get,
form: null,
accept: coap.CoapMediaType.applicationTdJson,
);
Expand All @@ -496,8 +494,7 @@ class CoapClient extends ProtocolClient {
Uri uri, {
bool disableMulticast = false,
}) async* {
final config = CoapConfigDefault();
final client = coap.CoapClient(uri, config);
final client = coap.CoapClient(uri);

if (uri.isMulticastAddress) {
if (!disableMulticast) {
Expand All @@ -517,7 +514,7 @@ class CoapClient extends ProtocolClient {
if (uri.isMulticastAddress) {
multicastResponseHandler = coap.CoapMulticastResponseHandler(
(data) {
streamController.add(data.resp.determineDiscoveryContent(uri.scheme));
streamController.add(data.determineDiscoveryContent(uri.scheme));
},
onError: streamController.addError,
onDone: () async {
Expand All @@ -528,7 +525,7 @@ class CoapClient extends ProtocolClient {

final content = await _sendDiscoveryRequest(
uri,
coap.CoapCode.get,
coap.RequestMethod.get,
form: null,
accept: coap.CoapMediaType.applicationLinkFormat,
multicastResponseHandler: multicastResponseHandler,
Expand Down
16 changes: 8 additions & 8 deletions lib/src/binding_coap/coap_definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ final coapPrefixMapping =
/// Defines the available CoAP request methods.
enum CoapRequestMethod {
/// Corresponds with the GET request method.
get(CoapCode.get),
get(RequestMethod.get),

/// Corresponds with the PUT request method.
put(CoapCode.put),
put(RequestMethod.put),

/// Corresponds with the POST request method.
post(CoapCode.post),
post(RequestMethod.post),

/// Corresponds with the DELETE request method.
delete(CoapCode.delete),
delete(RequestMethod.delete),

/// Corresponds with the FETCH request method.
fetch(CoapCode.fetch),
fetch(RequestMethod.fetch),

/// Corresponds with the PATCH request method.
patch(CoapCode.patch),
patch(RequestMethod.patch),

/// Corresponds with the iPATCH request method.
ipatch(CoapCode.ipatch);
ipatch(RequestMethod.ipatch);

/// Constructor
const CoapRequestMethod(this.code);

/// The numeric code of this [CoapRequestMethod].
final CoapCode code;
final RequestMethod code;

static final _registry = HashMap.fromEntries(
values.map((e) => MapEntry(e.code.description, e)),
Expand Down
6 changes: 3 additions & 3 deletions lib/src/binding_coap/coap_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ extension ResponseExtension on CoapResponse {
AuthServerRequestCreationHint? get creationHint {
const unauthorizedAceCodes = [
// TODO: Should other response codes be included as well?
CoapCode.unauthorized,
CoapCode.methodNotAllowed,
CoapCode.forbidden,
ResponseCode.unauthorized,
ResponseCode.methodNotAllowed,
ResponseCode.forbidden,
];

final responsePayload = payload;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/codecs/link_format_codec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LinkFormatCodec extends ContentCodec {
) {
// TODO(JKRhb): The question which value types are allowed needs to be
// revisited.
if (value is CoapIResource) {
if (value is CoapResource) {
return Uint8List.fromList(CoapLinkFormat.serialize(value).codeUnits)
.buffer;
}
Expand Down

0 comments on commit 8756d27

Please sign in to comment.