Skip to content

Commit

Permalink
feat: some general cleanup and prep for refactoring
Browse files Browse the repository at this point in the history
getting set to move away from json docs to individual keys in order to more easily support editing of individual keys
  • Loading branch information
nickelskevin committed Oct 2, 2022
1 parent 4a5c11a commit 258c34a
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 54 deletions.
83 changes: 56 additions & 27 deletions dart/iot_sender/lib/iot_mqtt_listener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:at_client/at_client.dart';
//import 'package:at_commons/at_commons.dart';
import 'package:iot_sender/models/send_hr02_receiver.dart';
import 'package:iot_sender/models/hro2_data_owner.dart';
import 'package:iot_sender/models/hro2_device.dart';

final client = MqttServerClient('localhost', '');
final AtSignLogger logger = AtSignLogger('iotListen');
Expand All @@ -19,7 +18,8 @@ Random random = Random();
int fakeO2IntMinValue = 950;
int fakeO2IntMaxValue = 995;
// fakeO2 value in int, convert to double by dividing by 10 when publishing
int currentFakeO2IntValue = random.nextInt(fakeO2IntMaxValue - fakeO2IntMinValue) + fakeO2IntMinValue;
int currentFakeO2IntValue =
random.nextInt(fakeO2IntMaxValue - fakeO2IntMinValue) + fakeO2IntMinValue;

int getNextFakeO2IntValue() {
// get random int in range -5..+5
Expand All @@ -40,8 +40,12 @@ bool _sendO2 = true;
int _putCounterHR = 0;
int _putCounterO2 = 0;

Future<void> iotListen(AtClientManager atClientManager, NotificationService notificationService, String fromAtsign,
String ownerAtsign, String deviceName) async {
Future<void> iotListen(
AtClientManager atClientManager,
NotificationService notificationService,
String fromAtsign,
String ownerAtsign,
String deviceName) async {
client.logging(on: false);
client.setProtocolV311();
client.keepAlivePeriod = 20;
Expand Down Expand Up @@ -71,14 +75,17 @@ Future<void> iotListen(AtClientManager atClientManager, NotificationService noti
logger.info('Mosquitto client connected');
} else {
/// Use status here rather than state if you also want the broker return code.
logger.severe('ERROR Mosquitto client connection failed - disconnecting, status is ${client.connectionStatus}');
logger.severe(
'ERROR Mosquitto client connection failed - disconnecting, status is ${client.connectionStatus}');
client.disconnect();
exit(-1);
}

logger.info('calling atClient.put for HeartRate to ensure AtClient connection goes through authorization exchange');
logger.info(
'calling atClient.put for HeartRate to ensure AtClient connection goes through authorization exchange');

logger.info('Initial put complete, AtClient connection should now be authorized');
logger.info(
'Initial put complete, AtClient connection should now be authorized');

/// Ok, lets try a subscription
logger.info('Subscribing to the mqtt/mwc_hr topic');
Expand All @@ -98,14 +105,17 @@ Future<void> iotListen(AtClientManager atClientManager, NotificationService noti

client.updates!.listen((List<MqttReceivedMessage<MqttMessage?>>? c) async {
final recMess = c![0].payload as MqttPublishMessage;
final pt = MqttPublishPayload.bytesToStringAsString(recMess.payload.message);
final pt =
MqttPublishPayload.bytesToStringAsString(recMess.payload.message);

// Get list of dataOwners
//
List<HrO2DataOwner> dataOwnerAtsigns = await getDataOwners(atClient, ownerAtsign, deviceName);
List<HrO2DataOwner> dataOwnerAtsigns =
await getDataOwners(atClient, ownerAtsign, deviceName);
List<SendHrO2Receiver> toAtsigns = [];
for (HrO2DataOwner owner in dataOwnerAtsigns) {
var addtoAtsigns = await getReceivers(atClient, owner.dataOwnerAtsign, owner.hrO2Device.sensorName);
var addtoAtsigns = await getReceivers(
atClient, owner.dataOwnerAtsign, owner.hrO2Device.sensorName);
toAtsigns.addAll(addtoAtsigns);
}

Expand All @@ -122,15 +132,23 @@ Future<void> iotListen(AtClientManager atClientManager, NotificationService noti
lastHeartRateDoubleValue = heartRateDoubleValue;
for (var receiver in toAtsigns) {
if (receiver.sendHR) {
await shareHeartRate(heartRateDoubleValue, fromAtsign, receiver.receiverAtsign, receiver.receiverShortname,
await shareHeartRate(
heartRateDoubleValue,
fromAtsign,
receiver.receiverAtsign,
receiver.receiverShortname,
notificationService);
}

if (fakingO2SatValues) {
// get random int between 0 and 101, then subtract 50 to get a number in range -50..+50
currentFakeO2IntValue = getNextFakeO2IntValue();
double fakeO2DoubleValue = currentFakeO2IntValue / 10;
await shareO2Sat(fakeO2DoubleValue, fromAtsign, receiver.receiverAtsign, receiver.receiverShortname,
await shareO2Sat(
fakeO2DoubleValue,
fromAtsign,
receiver.receiverAtsign,
receiver.receiverShortname,
notificationService);
}
}
Expand All @@ -144,7 +162,11 @@ Future<void> iotListen(AtClientManager atClientManager, NotificationService noti
for (var receiver in toAtsigns) {
if (receiver.sendO2) {
await shareO2Sat(
o2SatDoubleValue, fromAtsign, receiver.receiverAtsign, receiver.receiverShortname, notificationService);
o2SatDoubleValue,
fromAtsign,
receiver.receiverAtsign,
receiver.receiverShortname,
notificationService);
}
}
}
Expand All @@ -161,17 +183,18 @@ Future<void> iotListen(AtClientManager atClientManager, NotificationService noti
bpm = double.parse(beatBpmSpo[1]);
spo = double.parse(beatBpmSpo[2]);
} catch (e) {
logger.severe('Error in message sent to mqtt/mwc_beat_hr_o2 format HR,O2 and this was received: $pt');
logger.severe(
'Error in message sent to mqtt/mwc_beat_hr_o2 format HR,O2 and this was received: $pt');
}
if (hrDetect) {
for (var receiver in toAtsigns) {
if (receiver.sendHR) {
await shareHeartRate(
bpm, fromAtsign, receiver.receiverAtsign, receiver.receiverShortname, notificationService);
await shareHeartRate(bpm, fromAtsign, receiver.receiverAtsign,
receiver.receiverShortname, notificationService);
}
if (receiver.sendO2) {
await shareO2Sat(
spo, fromAtsign, receiver.receiverAtsign, receiver.receiverShortname, notificationService);
await shareO2Sat(spo, fromAtsign, receiver.receiverAtsign,
receiver.receiverShortname, notificationService);
}
}
}
Expand All @@ -180,7 +203,8 @@ Future<void> iotListen(AtClientManager atClientManager, NotificationService noti
});
}

Future<List<HrO2DataOwner>> getDataOwners(AtClient atClient, String ownerAtsign, String sensorName) async {
Future<List<HrO2DataOwner>> getDataOwners(
AtClient atClient, String ownerAtsign, String sensorName) async {
String dataOwnersJson = '';
//Using a Set first dedupes the list we eventually send back
Set<HrO2DataOwner> dataOwners = {};
Expand Down Expand Up @@ -212,7 +236,8 @@ Future<List<HrO2DataOwner>> getDataOwners(AtClient atClient, String ownerAtsign,
return (dataOwners.toList());
}

Future<List<SendHrO2Receiver>> getReceivers(AtClient atClient, String ownerAtsign, String sensorName) async {
Future<List<SendHrO2Receiver>> getReceivers(
AtClient atClient, String ownerAtsign, String sensorName) async {
String receiversString = '';
const String libraryNamespace = 'iot_receiver';

Expand Down Expand Up @@ -245,8 +270,8 @@ Future<List<SendHrO2Receiver>> getReceivers(AtClient atClient, String ownerAtsig
return toAtsigns;
}

Future<void> shareHeartRate(double heartRate, String atsign, String toAtsign, String sendToShortname,
NotificationService notificationService) async {
Future<void> shareHeartRate(double heartRate, String atsign, String toAtsign,
String sendToShortname, NotificationService notificationService) async {
if (!_sendHR) {
return;
}
Expand All @@ -258,7 +283,9 @@ Future<void> shareHeartRate(double heartRate, String atsign, String toAtsign, St
logger.info('calling atClient.put for HeartRate #$thisHRPutNo');
try {
await notificationService.notify(
NotificationParams.forText('HR:$heartRateAsString:$sendToShortname', toAtsign, shouldEncrypt: true),
NotificationParams.forText(
'HR:$heartRateAsString:$sendToShortname', toAtsign,
shouldEncrypt: true),
checkForFinalDeliveryStatus: false, onSuccess: (notification) {
logger.info('SUCCESS:$notification');
}, onError: (notification) {
Expand All @@ -272,8 +299,8 @@ Future<void> shareHeartRate(double heartRate, String atsign, String toAtsign, St
logger.info('atClient.put #$thisHRPutNo complete');
}

Future<void> shareO2Sat(double o2Sat, String atsign, String toAtsign, String sendToShortname,
NotificationService notificationService) async {
Future<void> shareO2Sat(double o2Sat, String atsign, String toAtsign,
String sendToShortname, NotificationService notificationService) async {
if (!_sendO2) {
return;
}
Expand Down Expand Up @@ -312,10 +339,12 @@ void onSubscribed(String topic) {
/// The unsolicited disconnect callback
void onDisconnected() {
logger.info('OnDisconnected client callback - Client disconnection');
if (client.connectionStatus!.disconnectionOrigin == MqttDisconnectionOrigin.solicited) {
if (client.connectionStatus!.disconnectionOrigin ==
MqttDisconnectionOrigin.solicited) {
logger.info('OnDisconnected callback is solicited, this is correct');
} else {
logger.severe('OnDisconnected callback is unsolicited or none, this is incorrect - exiting');
logger.severe(
'OnDisconnected callback is unsolicited or none, this is incorrect - exiting');
exit(-1);
}
}
Expand Down
1 change: 1 addition & 0 deletions flutter/iot_receiver/lib/screens/data_owners_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class _DataOwnersScreenState extends State<DataOwnersScreen> {

@override
Widget build(BuildContext context) {
_hrO2DataService.getDataOwners();
return Scaffold(
appBar: NewGradientAppBar(
title: const AutoSizeText(
Expand Down
2 changes: 2 additions & 0 deletions flutter/iot_receiver/lib/screens/receivers_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class _ReceiversScreenState extends State<ReceiversScreen> {

@override
Widget build(BuildContext context) {
_hrO2DataService.getReceivers();

return Scaffold(
appBar: NewGradientAppBar(
title: const AutoSizeText(
Expand Down
Loading

0 comments on commit 258c34a

Please sign in to comment.