Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefWN committed Apr 20, 2022
1 parent b882c7d commit fcc5f66
Show file tree
Hide file tree
Showing 33 changed files with 297 additions and 791 deletions.
2 changes: 1 addition & 1 deletion example/put_resource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ FutureOr<void> main(List<String> args) async {
print('Sending put /create1 to ${uri.host}');
var response =
await client.put('create1', options: [opt], payload: 'SJHTestPut');
print('/create1 response: ${response.payloadString}');
print('/create1 response status: ${response.statusCodeString}');

client.close();
} catch (e) {
Expand Down
2 changes: 0 additions & 2 deletions lib/coap.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import 'dart:math';
import 'dart:typed_data';

import 'package:collection/collection.dart' as collection;
import 'package:date_format/date_format.dart' as df;
import 'package:event_bus/event_bus.dart' as events;
import 'package:executor/executor.dart' as tasking;
import 'package:hex/hex.dart' as hex;
Expand Down Expand Up @@ -69,7 +68,6 @@ part 'src/net/coap_matcher.dart';
part 'src/net/coap_multicast_exchange.dart';
part 'src/network/coap_inetwork.dart';
part 'src/network/coap_network_udp.dart';
part 'src/observe/coap_observe_notification_orderer.dart';
part 'src/observe/coap_observe_relation.dart';
part 'src/observe/coap_observing_endpoint.dart';
part 'src/resources/coap_iresource.dart';
Expand Down
8 changes: 4 additions & 4 deletions lib/src/coap_block_option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ class CoapBlockOption extends CoapOption {

/// Strips leading zeros for 32 bit integers
typed.Uint8Buffer? _compressValueBytes() {
if (valueBytes!.length == 4) {
if (valueBytes![3] == 0) {
return typed.Uint8Buffer()..addAll(valueBytes!.take(3).toList());
if (byteValue.length == 4) {
if (byteValue[3] == 0) {
return typed.Uint8Buffer()..addAll(byteValue.take(3).toList());
}
}
return valueBytes;
return byteValue;
}
}
2 changes: 1 addition & 1 deletion lib/src/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class CoapClient {
..observe = 0
..maxRetransmit = maxRetransmit;
await _prepare(request);
final relation = CoapObserveClientRelation(request, _config);
final relation = CoapObserveClientRelation(request);
unawaited(() async {
_endpoint!.sendEpRequest(request);
final response = await _waitForResponse(request, timeout ?? this.timeout);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/coap_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class CoapConstants {
///
/// Empty token
///
static typed.Uint8Buffer emptyToken = typed.Uint8Buffer(1);
static typed.Uint8Buffer emptyToken = typed.Uint8Buffer(0);

///
/// The lowest value of a request code.
Expand Down
30 changes: 11 additions & 19 deletions lib/src/coap_link_attribute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ part of coap;
/// Class for linkformat attributes.
class CoapLinkAttribute {
/// Initializes an attribute.
CoapLinkAttribute(String? name, Object? value) {
_name = name;
_value = value;
}
CoapLinkAttribute(this._name, this._value);

String? _name;
final String _name;

/// Name
String? get name => _name;
String get name => _name;

Object? _value;
final Object? _value;

/// Value
Object? get value => _value;
Expand All @@ -34,18 +31,15 @@ class CoapLinkAttribute {
/// Serializes this attribute into its string representation.
void serialize(StringBuffer builder) {
// check if there's something to write
if (_name != null && _value != null) {
if (_value != null) {
if (_value is bool) {
// flag attribute
builder.write(_name);
} else {
// name-value-pair
builder.write(_name);
builder.write('=');
builder.write('$_name=');
if (_value is String) {
builder.write('"');
builder.write(_value);
builder.write('"');
builder.write('"$_value"');
} else if (_value is int) {
builder.write(_value);
} else {
Expand All @@ -61,14 +55,12 @@ class CoapLinkAttribute {

@override
bool operator ==(Object other) {
if (other is CoapLinkAttribute) {
if (_name == other.name && _value == other.value) {
return true;
}
if (other is! CoapLinkAttribute) {
return false;
}
return false;
return _name == other.name && _value == other.value;
}

@override
int get hashCode => name.hashCode;
int get hashCode => _name.hashCode;
}
17 changes: 4 additions & 13 deletions lib/src/coap_link_format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ class CoapLinkFormat {
}

static void _serializeResource(CoapIResource resource, StringBuffer sb) {
sb.write('<');
sb.write(resource.path);
sb.write(resource.name);
sb.write('>');
sb.write('<${resource.path}${resource.name}>');
_serializeAttributes(resource.attributes!, sb);
}

Expand All @@ -169,13 +166,9 @@ class CoapLinkFormat {

static void _serializeAttribute(
String name, Iterable<String?> values, StringBuffer sb) {
const delimiter = '=';
sb.write(name);
for (final value in values) {
sb.write(delimiter);
sb.write('"');
sb.write(value);
sb.write('"');
sb.write('="$value"');
}
}

Expand All @@ -189,9 +182,7 @@ class CoapLinkFormat {
// always skip non-matching resources.
if ((!resource.hidden && resource.name.isNotEmpty || !recursive) &&
_matchesOption(resource, query)) {
linkFormat.write('<');
linkFormat.write(resource.path);
linkFormat.write('>');
linkFormat.write('<${resource.path}>');

// Reverse the attribute list to re-create the original
final attrs = resource.attributes.toList().reversed.toList();
Expand Down Expand Up @@ -269,7 +260,7 @@ class CoapLinkFormat {
value ??= 0;
}
}
return CoapLinkAttribute(name, value);
return CoapLinkAttribute(name!, value);
}
return null;
}
Expand Down
31 changes: 8 additions & 23 deletions lib/src/coap_media_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,34 +268,19 @@ class CoapMediaType {
}

/// Parse
static int? parse(String type) {
int? keyRet;
_registry.forEach((int key, List<String> value) {
if (value[0].toLowerCase() == type.toLowerCase()) {
keyRet = key;
}
});
if (keyRet != null) {
return keyRet;
} else {
return CoapMediaType.undefined;
}
}
static int? parse(String type) =>
_registry.keys.firstWhereOrNull((int key) =>
_registry[key]![0].toLowerCase() == type.toLowerCase()) ??
CoapMediaType.undefined;

/// Wildcard parse
static List<int>? parseWildcard(String? regex) {
if (regex == null) {
return null;
}
final res = <int>[];
final regex1 = regex.substring(0, regex.indexOf('*')).trim() + '.*';
final r = RegExp(regex1);
_registry.forEach((int key, List<String> value) {
final mime = value[0];
if (r.hasMatch(mime)) {
res.add(key);
}
});
return res;
final r = RegExp(regex.substring(0, regex.indexOf('*')).trim() + '.*');
return _registry.keys
.where((int key) => r.hasMatch(_registry[key]![0]))
.toList();
}
}
68 changes: 30 additions & 38 deletions lib/src/coap_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ class CoapMessage {
typed.Uint8Buffer? get token => _token;

/// As a string
String? get tokenString =>
_token != null ? CoapByteArrayUtil.toHexString(_token!) : null;
String get tokenString =>
_token != null ? CoapByteArrayUtil.toHexString(_token!) : '';

set token(typed.Uint8Buffer? value) {
if (value != null && value.length > 8) {
Expand Down Expand Up @@ -337,9 +337,9 @@ class CoapMessage {
@override
String toString() => '\nType: ${type.toString()}, Code: $codeString, '
'Id: ${id.toString()}, '
'Token: $tokenString, '
'\nOptions =\n[\n${CoapUtil.optionsToString(this)}], '
'\nPayload :\n$payloadString';
'Token: \'$tokenString\',\n'
'Options: ${CoapUtil.optionsToString(this)},\n'
'Payload: $payloadString';

/// Select options helper
Iterable<CoapOption> _selectOptions(int optionType) {
Expand Down Expand Up @@ -376,7 +376,7 @@ class CoapMessage {
final opt = CoapUtil.firstOrDefault(
list,
(CoapOption o) =>
CoapUtil.areSequenceEqualTo(opaque, o.valueBytes, equality));
CoapUtil.areSequenceEqualTo(opaque, o.byteValue, equality));
if (opt != null) {
_optionMap[optionTypeIfMatch]!.remove(opt);
if (_optionMap[optionTypeIfMatch]!.isEmpty) {
Expand Down Expand Up @@ -409,7 +409,7 @@ class CoapMessage {
/// Contains an opaque E-tag
bool containsETagOpaque(typed.Uint8Buffer what) => CoapUtil.contains(
getOptions(optionTypeETag)!,
(CoapOption o) => CoapUtil.areSequenceEqualTo(what, o.valueBytes));
(CoapOption o) => CoapUtil.areSequenceEqualTo(what, o.byteValue));

/// Add an opaque ETag
CoapMessage addETagOpaque(typed.Uint8Buffer? opaque) {
Expand Down Expand Up @@ -443,7 +443,7 @@ class CoapMessage {
final opt = CoapUtil.firstOrDefault(
list,
(CoapOption o) =>
CoapUtil.areSequenceEqualTo(opaque, o.valueBytes, equality));
CoapUtil.areSequenceEqualTo(opaque, o.byteValue, equality));
if (opt != null) {
_optionMap[optionTypeETag]!.remove(opt);
if (_optionMap[optionTypeETag]!.isEmpty) {
Expand Down Expand Up @@ -493,7 +493,7 @@ class CoapMessage {
final opt = CoapUtil.firstOrDefault(
list,
(CoapOption o) =>
CoapUtil.areSequenceEqualTo(opaque, o.valueBytes, equality));
CoapUtil.areSequenceEqualTo(opaque, o.byteValue, equality));
if (opt != null) {
_optionMap[optionTypeIfNoneMatch]!.remove(opt);
if (_optionMap[optionTypeIfNoneMatch]!.isEmpty) {
Expand Down Expand Up @@ -562,14 +562,12 @@ class CoapMessage {
String get uriPathsString {
final sb = StringBuffer();
for (final option in uriPaths) {
sb.write('${option.stringValue}/');
}
final out = sb.toString();
if (out.isNotEmpty) {
return out.substring(0, out.length - 1);
} else {
return out;
sb.write(option.stringValue);
if (option != uriPaths.last) {
sb.write('/');
}
}
return sb.toString();
}

/// Add a URI path
Expand Down Expand Up @@ -626,14 +624,12 @@ class CoapMessage {
String get uriQueriesString {
final sb = StringBuffer();
for (final option in uriQueries) {
sb.write('${option.stringValue}&');
}
final out = sb.toString();
if (out.isNotEmpty) {
return '?${out.substring(0, out.length - 1)}';
} else {
return '?$out';
sb.write(option.stringValue);
if (option != uriQueries.last) {
sb.write('&');
}
}
return '?${sb.toString()}';
}

/// Add a URI query
Expand Down Expand Up @@ -688,14 +684,12 @@ class CoapMessage {
String get locationPathsString {
final sb = StringBuffer();
for (final option in locationPaths) {
sb.write('${option.stringValue}/');
}
final out = sb.toString();
if (out.isNotEmpty) {
return out.substring(0, out.length - 1);
} else {
return out;
sb.write(option.stringValue);
if (option != locationPaths.last) {
sb.write('/');
}
}
return sb.toString();
}

/// Set the location path from a string
Expand Down Expand Up @@ -780,14 +774,12 @@ class CoapMessage {
String get locationQueriesString {
final sb = StringBuffer();
for (final option in locationQueries) {
sb.write('${option.stringValue}&');
}
final out = sb.toString();
if (out.isNotEmpty) {
return '?${out.substring(0, out.length - 1)}';
} else {
return '?$out';
sb.write(option.stringValue);
if (option != locationQueries.last) {
sb.write('&');
}
}
return '?${sb.toString()}';
}

/// Add a location query
Expand Down Expand Up @@ -919,7 +911,7 @@ class CoapMessage {
/// Observe
int? get observe {
final opt = getFirstOption(optionTypeObserve);
return opt?.value ?? -1;
return opt?.value;
}

@protected
Expand Down
Loading

0 comments on commit fcc5f66

Please sign in to comment.