Skip to content

Commit

Permalink
refactor: rename method to build payload, refactor MessageContainer
Browse files Browse the repository at this point in the history
use getters in message container, add comments

DT-2604
  • Loading branch information
matejglejtek committed Sep 11, 2023
1 parent cdacbaf commit cb59744
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ protected void writeValue(@NonNull ByteArrayOutputStream stream, Object value) {
public interface PayloadApi {

@NonNull
ODIDPayload getPayload(@NonNull byte[] rawData, @NonNull MessageSource source, @NonNull String macAddress, @NonNull Long rssi, @NonNull Long receivedTimestamp);
ODIDPayload buildPayload(@NonNull byte[] rawData, @NonNull MessageSource source, @NonNull String macAddress, @NonNull Long rssi, @NonNull Long receivedTimestamp);

/** The codec used by PayloadApi. */
static @NonNull MessageCodec<Object> getCodec() {
Expand All @@ -669,7 +669,7 @@ static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable PayloadApi
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(
binaryMessenger, "dev.flutter.pigeon.flutter_opendroneid.PayloadApi.getPayload", getCodec());
binaryMessenger, "dev.flutter.pigeon.flutter_opendroneid.PayloadApi.buildPayload", getCodec());
if (api != null) {
channel.setMessageHandler(
(message, reply) -> {
Expand All @@ -681,7 +681,7 @@ static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable PayloadApi
Number rssiArg = (Number) args.get(3);
Number receivedTimestampArg = (Number) args.get(4);
try {
ODIDPayload output = api.getPayload(rawDataArg, sourceArg, macAddressArg, (rssiArg == null) ? null : rssiArg.longValue(), (receivedTimestampArg == null) ? null : receivedTimestampArg.longValue());
ODIDPayload output = api.buildPayload(rawDataArg, sourceArg, macAddressArg, (rssiArg == null) ? null : rssiArg.longValue(), (receivedTimestampArg == null) ? null : receivedTimestampArg.longValue());
wrapped.add(0, output);
}
catch (Throwable exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class BluetoothScanner(
odidPayloadStreamHandler: StreamHandler,
private val bluetoothStateHandler: StreamHandler,
) : ODIDScanner(odidPayloadStreamHandler) {

companion object {
const val BT_OFFSET = 6
const val MAX_BLE_ADV_SIZE = 31
}

private val TAG: String = BluetoothScanner::class.java.getSimpleName()
private val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent

/// Contains common functinality for ODID scanners
/// Contains common functinality for ODID scanners.
/// Derived scanners should use receiveData method that takes raw data and metadata
/// and sends it to stream
/// Creates [ODIDPayload] instances implementing Pigeon PayloadAPI
Expand All @@ -14,10 +14,6 @@ abstract class ODIDScanner(

companion object {
const val MAX_MESSAGE_SIZE = 25
const val BT_OFFSET = 6
const val MAX_BLE_ADV_SIZE = 31
const val WIFI_BEACON_OFFSET = 5
const val WIFI_NAN_OFFSET = 1
}

var isScanning = false
Expand All @@ -38,7 +34,7 @@ abstract class ODIDScanner(

abstract fun onAdapterStateReceived()

override fun getPayload(rawData: ByteArray, source: Pigeon.MessageSource, macAddress: String, rssi: Long, receivedTimestamp: Long): Pigeon.ODIDPayload {
override fun buildPayload(rawData: ByteArray, source: Pigeon.MessageSource, macAddress: String, rssi: Long, receivedTimestamp: Long): Pigeon.ODIDPayload {
val builder = Pigeon.ODIDPayload.Builder()

builder.setRawData(rawData)
Expand All @@ -54,7 +50,7 @@ abstract class ODIDScanner(
fun receiveData(
data: ByteArray, macAddress: String, source: Pigeon.MessageSource, rssi: Long = 0
) {
val payload = getPayload(
val payload = buildPayload(
data, source, macAddress, rssi, System.currentTimeMillis()
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class WifiNaNScanner (
private val wifiAwareManager: WifiAwareManager?,
private val context: Context
) : ODIDScanner(odidPayloadStreamHandler) {

companion object {
const val WIFI_NAN_OFFSET = 1
}

private val TAG: String = WifiNaNScanner::class.java.getSimpleName()
private val wifiScanEnabled = true
private var wifiAwareSupported = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class WifiScanner (
private val wifiManager: WifiManager?,
private val context: Context
) : ODIDScanner(odidPayloadStreamHandler) {

companion object {
const val WIFI_BEACON_OFFSET = 5
}

private val TAG: String = WifiScanner::class.java.getSimpleName()
private val CIDLen = 3
private val DRICID = intArrayOf(0xFA, 0x0B, 0xBC)
Expand Down
4 changes: 2 additions & 2 deletions ios/Classes/Scanner/BluetoothScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class BluetoothScanner: NSObject, CBCentralManagerDelegate, DTGPayloadApi {
)
}

func getPayloadRawData(_ rawData: FlutterStandardTypedData, source: DTGMessageSource, macAddress: String, rssi: NSNumber, receivedTimestamp: NSNumber, error: AutoreleasingUnsafeMutablePointer<FlutterError?>) -> DTGODIDPayload? {
func buildPayloadRawData(_ rawData: FlutterStandardTypedData, source: DTGMessageSource, macAddress: String, rssi: NSNumber, receivedTimestamp: NSNumber, error: AutoreleasingUnsafeMutablePointer<FlutterError?>) -> DTGODIDPayload? {
return DTGODIDPayload.make(withRawData: rawData, receivedTimestamp: receivedTimestamp, macAddress: macAddress, rssi: rssi, source: source)
}

Expand All @@ -106,7 +106,7 @@ class BluetoothScanner: NSObject, CBCentralManagerDelegate, DTGPayloadApi {
}
var err: FlutterError?
let systimestamp = Int(Date().timeIntervalSince1970 * 1000)
let payload = getPayloadRawData(data, source: DTGMessageSource.bluetoothLegacy, macAddress: peripheral.identifier.uuidString, rssi: RSSI.intValue as NSNumber, receivedTimestamp: systimestamp as NSNumber, error: &err)
let payload = buildPayloadRawData(data, source: DTGMessageSource.bluetoothLegacy, macAddress: peripheral.identifier.uuidString, rssi: RSSI.intValue as NSNumber, receivedTimestamp: systimestamp as NSNumber, error: &err)

odidPayloadStreamHandler.send(payload!.toList() as Any)
}
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/pigeon.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ NSObject<FlutterMessageCodec> *DTGPayloadApiGetCodec(void);

@protocol DTGPayloadApi
/// @return `nil` only when `error != nil`.
- (nullable DTGODIDPayload *)getPayloadRawData:(FlutterStandardTypedData *)rawData source:(DTGMessageSource)source macAddress:(NSString *)macAddress rssi:(NSNumber *)rssi receivedTimestamp:(NSNumber *)receivedTimestamp error:(FlutterError *_Nullable *_Nonnull)error;
- (nullable DTGODIDPayload *)buildPayloadRawData:(FlutterStandardTypedData *)rawData source:(DTGMessageSource)source macAddress:(NSString *)macAddress rssi:(NSNumber *)rssi receivedTimestamp:(NSNumber *)receivedTimestamp error:(FlutterError *_Nullable *_Nonnull)error;
@end

extern void DTGPayloadApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<DTGPayloadApi> *_Nullable api);
Expand Down
6 changes: 3 additions & 3 deletions ios/Classes/pigeon.m
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,11 @@ void DTGPayloadApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<DTG
{
FlutterBasicMessageChannel *channel =
[[FlutterBasicMessageChannel alloc]
initWithName:@"dev.flutter.pigeon.flutter_opendroneid.PayloadApi.getPayload"
initWithName:@"dev.flutter.pigeon.flutter_opendroneid.PayloadApi.buildPayload"
binaryMessenger:binaryMessenger
codec:DTGPayloadApiGetCodec()];
if (api) {
NSCAssert([api respondsToSelector:@selector(getPayloadRawData:source:macAddress:rssi:receivedTimestamp:error:)], @"DTGPayloadApi api (%@) doesn't respond to @selector(getPayloadRawData:source:macAddress:rssi:receivedTimestamp:error:)", api);
NSCAssert([api respondsToSelector:@selector(buildPayloadRawData:source:macAddress:rssi:receivedTimestamp:error:)], @"DTGPayloadApi api (%@) doesn't respond to @selector(buildPayloadRawData:source:macAddress:rssi:receivedTimestamp:error:)", api);
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
NSArray *args = message;
FlutterStandardTypedData *arg_rawData = GetNullableObjectAtIndex(args, 0);
Expand All @@ -350,7 +350,7 @@ void DTGPayloadApiSetup(id<FlutterBinaryMessenger> binaryMessenger, NSObject<DTG
NSNumber *arg_rssi = GetNullableObjectAtIndex(args, 3);
NSNumber *arg_receivedTimestamp = GetNullableObjectAtIndex(args, 4);
FlutterError *error;
DTGODIDPayload *output = [api getPayloadRawData:arg_rawData source:arg_source macAddress:arg_macAddress rssi:arg_rssi receivedTimestamp:arg_receivedTimestamp error:&error];
DTGODIDPayload *output = [api buildPayloadRawData:arg_rawData source:arg_source macAddress:arg_macAddress rssi:arg_rssi receivedTimestamp:arg_receivedTimestamp error:&error];
callback(wrapResult(output, error));
}];
} else {
Expand Down
52 changes: 26 additions & 26 deletions lib/models/message_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import 'package:flutter_opendroneid/pigeon.dart' as pigeon;
import 'package:dart_opendroneid/src/types.dart';
import 'package:flutter_opendroneid/utils/compare_extension.dart';

/// The [MessageContainer] groups together messages of different types
/// from one device. It contains one instance of each message. The container is
/// then sent using stream to client of the library.
class MessageContainer {
final String macAddress;
final DateTime lastUpdate;
Expand Down Expand Up @@ -143,39 +146,36 @@ class MessageContainer {
};
}

pigeon.MessageSource getPackSource() => source;
pigeon.MessageSource get packSource => source;

bool operatorIDSet() {
return operatorIdMessage != null &&
operatorIdMessage!.operatorID != OPERATOR_ID_NOT_SET;
}
bool get operatorIDSet =>
operatorIdMessage != null &&
operatorIdMessage!.operatorID != OPERATOR_ID_NOT_SET;

bool operatorIDValid() {
bool get operatorIDValid {
final validCharacters = RegExp(r'^[a-zA-Z0-9]+$');
return operatorIdMessage != null &&
operatorIdMessage!.operatorID.length == 16 &&
validCharacters.hasMatch(operatorIdMessage!.operatorID);
}

bool systemDataValid() {
return systemDataMessage != null &&
systemDataMessage?.operatorLocation != null &&
systemDataMessage!.operatorLocation!.latitude != INV_LAT &&
systemDataMessage?.operatorLocation!.longitude != INV_LON &&
systemDataMessage!.operatorLocation!.latitude <= MAX_LAT &&
systemDataMessage!.operatorLocation!.latitude >= MIN_LAT &&
systemDataMessage!.operatorLocation!.longitude <= MAX_LON &&
systemDataMessage!.operatorLocation!.longitude >= MIN_LON;
}
bool get systemDataValid =>
systemDataMessage != null &&
systemDataMessage?.operatorLocation != null &&
systemDataMessage!.operatorLocation!.latitude != INV_LAT &&
systemDataMessage?.operatorLocation!.longitude != INV_LON &&
systemDataMessage!.operatorLocation!.latitude <= MAX_LAT &&
systemDataMessage!.operatorLocation!.latitude >= MIN_LAT &&
systemDataMessage!.operatorLocation!.longitude <= MAX_LON &&
systemDataMessage!.operatorLocation!.longitude >= MIN_LON;

bool locationValid() {
return locationMessage != null &&
locationMessage?.location != null &&
locationMessage!.location!.latitude != INV_LAT &&
locationMessage!.location!.longitude != INV_LON &&
locationMessage!.location!.latitude <= MAX_LAT &&
locationMessage!.location!.longitude <= MAX_LON &&
locationMessage!.location!.latitude >= MIN_LAT &&
locationMessage!.location!.longitude >= MIN_LON;
}
bool get locationValid =>
locationMessage != null &&
locationMessage?.location != null &&
locationMessage!.location!.latitude != INV_LAT &&
locationMessage!.location!.longitude != INV_LON &&
locationMessage!.location!.latitude <= MAX_LAT &&
locationMessage!.location!.longitude <= MAX_LON &&
locationMessage!.location!.latitude >= MIN_LAT &&
locationMessage!.location!.longitude >= MIN_LON;
}
4 changes: 2 additions & 2 deletions lib/pigeon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,9 @@ class PayloadApi {

static const MessageCodec<Object?> codec = _PayloadApiCodec();

Future<ODIDPayload> getPayload(Uint8List arg_rawData, MessageSource arg_source, String arg_macAddress, int arg_rssi, int arg_receivedTimestamp) async {
Future<ODIDPayload> buildPayload(Uint8List arg_rawData, MessageSource arg_source, String arg_macAddress, int arg_rssi, int arg_receivedTimestamp) async {
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
'dev.flutter.pigeon.flutter_opendroneid.PayloadApi.getPayload', codec,
'dev.flutter.pigeon.flutter_opendroneid.PayloadApi.buildPayload', codec,
binaryMessenger: _binaryMessenger);
final List<Object?>? replyList =
await channel.send(<Object?>[arg_rawData, arg_source.index, arg_macAddress, arg_rssi, arg_receivedTimestamp]) as List<Object?>?;
Expand Down
2 changes: 1 addition & 1 deletion pigeon/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ abstract class Api {
// ODIDPayload is not generated until used in API
@HostApi()
abstract class PayloadApi {
ODIDPayload getPayload(Uint8List rawData, MessageSource source,
ODIDPayload buildPayload(Uint8List rawData, MessageSource source,
String macAddress, int rssi, int receivedTimestamp);
}

0 comments on commit cb59744

Please sign in to comment.