mirror of
https://github.com/dz0ny/meshcore-sar.git
synced 2026-08-11 16:30:28 +00:00
feat: Add Sensor telemetry alerts
ref:
This commit is contained in:
@@ -142,6 +142,7 @@ void main() {
|
||||
final contactTypes = <ContactType>[
|
||||
ContactType.chat,
|
||||
ContactType.repeater,
|
||||
ContactType.sensor,
|
||||
ContactType.room,
|
||||
ContactType.channel,
|
||||
];
|
||||
|
||||
@@ -57,6 +57,25 @@ void main() {
|
||||
);
|
||||
}
|
||||
|
||||
Contact buildSensor({required int seed, required String name}) {
|
||||
final publicKey = Uint8List(32);
|
||||
publicKey[0] = seed;
|
||||
publicKey[1] = seed + 1;
|
||||
|
||||
return Contact(
|
||||
publicKey: publicKey,
|
||||
type: ContactType.sensor,
|
||||
flags: 0,
|
||||
outPathLen: -1,
|
||||
outPath: Uint8List(0),
|
||||
advName: name,
|
||||
lastAdvert: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
||||
advLat: 46056000 + seed,
|
||||
advLon: 14505000 + seed,
|
||||
lastMod: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> pumpContactsTab(
|
||||
WidgetTester tester, {
|
||||
List<Contact> contacts = const [],
|
||||
@@ -192,4 +211,14 @@ void main() {
|
||||
expect(find.text('Others'), findsNothing);
|
||||
expect(find.text('Lone Relay'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('sensor contacts render in their own section', (tester) async {
|
||||
await pumpContactsTab(
|
||||
tester,
|
||||
contacts: [buildSensor(seed: 60, name: 'WX Station')],
|
||||
);
|
||||
|
||||
expect(find.text('Sensors'), findsOneWidget);
|
||||
expect(find.text('WX Station'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -360,6 +360,78 @@ void main() {
|
||||
expect(accel['z'], closeTo(2.0, 0.001));
|
||||
});
|
||||
|
||||
test('extended MeshCore LPP types decode to structured extra data', () {
|
||||
const lppGenericSensor = 100;
|
||||
const lppCurrent = 117;
|
||||
const lppFrequency = 118;
|
||||
const lppAltitude = 121;
|
||||
const lppConcentration = 125;
|
||||
const lppPower = 128;
|
||||
const lppSpeed = 129;
|
||||
const lppDistance = 130;
|
||||
const lppEnergy = 131;
|
||||
const lppDirection = 132;
|
||||
const lppUnixTime = 133;
|
||||
const lppColour = 135;
|
||||
const lppSwitch = 142;
|
||||
|
||||
final payload = Uint8List.fromList([
|
||||
2, lppGenericSensor, 0x00, 0x00, 0x01, 0x2C, // 300
|
||||
3, lppCurrent, 0x00, 0x0F, // 0.015 A
|
||||
4, lppFrequency, 0x00, 0x00, 0x03, 0xE8, // 1000 Hz
|
||||
5, lppAltitude, 0x01, 0xF4, // 500 m
|
||||
6, lppConcentration, 0x01, 0x9F, // 415 ppm
|
||||
7, lppPower, 0x00, 0xFA, // 250 W
|
||||
8, lppSpeed, 0x04, 0xD2, // 12.34 m/s
|
||||
9, lppDistance, 0x00, 0x00, 0x04, 0xD2, // 1.234 m
|
||||
10, lppEnergy, 0x00, 0x00, 0x04, 0xD2, // 1.234 kWh
|
||||
11, lppDirection, 0x01, 0x0E, // 270 deg
|
||||
12, lppUnixTime, 0x65, 0xF0, 0x00, 0x00, // 1710221312
|
||||
13, lppColour, 0xFF, 0x80, 0x40, // #FF8040
|
||||
14, lppSwitch, 0x01, // on
|
||||
]);
|
||||
|
||||
final decoded = CayenneLppParser.parse(payload);
|
||||
|
||||
expect(decoded.extraSensorData, isNotNull);
|
||||
expect(decoded.extraSensorData!['generic_sensor_2'], equals(300.0));
|
||||
expect(decoded.extraSensorData!['current_3'], closeTo(0.015, 0.0001));
|
||||
expect(decoded.extraSensorData!['frequency_4'], equals(1000.0));
|
||||
expect(decoded.extraSensorData!['altitude_5'], equals(500.0));
|
||||
expect(decoded.extraSensorData!['concentration_6'], equals(415.0));
|
||||
expect(decoded.extraSensorData!['power_7'], equals(250.0));
|
||||
expect(decoded.extraSensorData!['speed_8'], closeTo(12.34, 0.001));
|
||||
expect(decoded.extraSensorData!['distance_9'], closeTo(1.234, 0.0001));
|
||||
expect(decoded.extraSensorData!['energy_10'], closeTo(1.234, 0.0001));
|
||||
expect(decoded.extraSensorData!['direction_11'], equals(270.0));
|
||||
expect(decoded.extraSensorData!['unixtime_12'], equals(1710227456));
|
||||
expect(
|
||||
decoded.extraSensorData!['colour_13'],
|
||||
equals({'r': 255, 'g': 128, 'b': 64}),
|
||||
);
|
||||
expect(decoded.extraSensorData!['switch_14'], equals(1));
|
||||
});
|
||||
|
||||
test(
|
||||
'percentage battery and non-battery voltage channels are preserved separately',
|
||||
() {
|
||||
const lppPercentage = 120;
|
||||
|
||||
final payload = Uint8List.fromList([
|
||||
1, lppPercentage, 66, // battery %
|
||||
2, MeshCoreConstants.lppVoltageSensor, 0x01, 0x81, // 3.85 V
|
||||
2, MeshCoreConstants.lppTemperatureSensor, 0x00, 0xEB, // 23.5 C
|
||||
]);
|
||||
|
||||
final decoded = CayenneLppParser.parse(payload);
|
||||
|
||||
expect(decoded.batteryPercentage, equals(66.0));
|
||||
expect(decoded.extraSensorData!['voltage_2'], closeTo(3.85, 0.001));
|
||||
expect(decoded.temperature, closeTo(23.5, 0.1));
|
||||
expect(decoded.extraSensorData!['temperature_2'], closeTo(23.5, 0.1));
|
||||
},
|
||||
);
|
||||
|
||||
test('unknown sensor type is skipped gracefully', () {
|
||||
final buffer = ByteData(5);
|
||||
buffer.setUint8(0, 0);
|
||||
|
||||
25
test/services/notification_service_test.dart
Normal file
25
test/services/notification_service_test.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_sar_app/services/notification_service.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
setUp(() {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
});
|
||||
|
||||
test('discovery notification preference persists independently', () async {
|
||||
final service = NotificationService();
|
||||
|
||||
await service.setMessageNotificationsEnabled(true);
|
||||
await service.setDiscoveryNotificationsEnabled(false);
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
expect(service.messageNotificationsEnabled, isTrue);
|
||||
expect(service.discoveryNotificationsEnabled, isFalse);
|
||||
expect(prefs.getBool('notifications_messages_enabled'), isTrue);
|
||||
expect(prefs.getBool('notifications_discovery_enabled'), isFalse);
|
||||
});
|
||||
}
|
||||
@@ -31,7 +31,9 @@ void main() {
|
||||
Future<void> pumpAvatar(WidgetTester tester, Contact contact) async {
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(body: Center(child: ContactAvatar(contact: contact))),
|
||||
home: Scaffold(
|
||||
body: Center(child: ContactAvatar(contact: contact)),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -85,7 +87,11 @@ void main() {
|
||||
testWidgets('renders non-hash label avatar for channels', (tester) async {
|
||||
await pumpAvatar(
|
||||
tester,
|
||||
buildContact(name: 'Command Net', type: ContactType.channel, secondByte: 3),
|
||||
buildContact(
|
||||
name: 'Command Net',
|
||||
type: ContactType.channel,
|
||||
secondByte: 3,
|
||||
),
|
||||
);
|
||||
|
||||
expect(find.text('CN'), findsOneWidget);
|
||||
@@ -103,4 +109,15 @@ void main() {
|
||||
expect(find.byType(CircleAvatar), findsOneWidget);
|
||||
expect(find.byIcon(Icons.person), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('renders sensor icon avatar for sensor contacts', (tester) async {
|
||||
await pumpAvatar(
|
||||
tester,
|
||||
buildContact(name: 'WX Station', type: ContactType.sensor),
|
||||
);
|
||||
|
||||
expect(find.byType(CircleAvatar), findsOneWidget);
|
||||
expect(find.byIcon(Icons.sensors), findsOneWidget);
|
||||
expect(find.text('WS'), findsNothing);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -104,4 +104,58 @@ void main() {
|
||||
expect(find.text(contact.publicKeyShort), findsNothing);
|
||||
expect(find.byIcon(Icons.key_outlined), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('sensor contacts can be added to sensors', (tester) async {
|
||||
await pumpTile(
|
||||
tester,
|
||||
buildContact(name: 'WX Station', type: ContactType.sensor),
|
||||
);
|
||||
|
||||
await tester.tap(find.text('WX Station'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Add to Sensors'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('sensor preview shows telemetry card', (tester) async {
|
||||
final contact = buildContact(name: 'WX Station', type: ContactType.sensor)
|
||||
.copyWith(
|
||||
telemetry: ContactTelemetry(
|
||||
batteryPercentage: 84,
|
||||
temperature: 21.5,
|
||||
humidity: 58.0,
|
||||
extraSensorData: const {
|
||||
'co2': 415.0,
|
||||
'illuminance_2': 500.0,
|
||||
'current_2': 0.015,
|
||||
'power_2': 0.25,
|
||||
'distance_2': 1.234,
|
||||
},
|
||||
timestamp: DateTime.now().subtract(const Duration(minutes: 1)),
|
||||
),
|
||||
);
|
||||
|
||||
await pumpTile(tester, contact);
|
||||
|
||||
await tester.tap(find.text('WX Station'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Preview'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('Preview'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Battery'), findsOneWidget);
|
||||
expect(find.text('84%'), findsOneWidget);
|
||||
expect(find.text('Temperature'), findsOneWidget);
|
||||
expect(find.text('21.5°C'), findsOneWidget);
|
||||
expect(find.text('CO2'), findsOneWidget);
|
||||
expect(find.text('415 ppm'), findsOneWidget);
|
||||
expect(find.text('Illuminance (ch 2)'), findsOneWidget);
|
||||
expect(find.text('~4.2 W/m2 daylight'), findsOneWidget);
|
||||
expect(find.text('Current (ch 2)'), findsOneWidget);
|
||||
expect(find.text('15 mA'), findsOneWidget);
|
||||
expect(find.text('Power (ch 2)'), findsOneWidget);
|
||||
expect(find.text('Distance (ch 2)'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user