Skip to content

Commit

Permalink
Testing again the fix with the timer for #3
Browse files Browse the repository at this point in the history
  • Loading branch information
guyluz11 committed Jun 9, 2021
1 parent 968e224 commit 9125d97
Showing 1 changed file with 8 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class CloudValueChangeU {

static CloudValueChangeU? _cloudValueChangeU;
CloudValueChangeE? _cloudValueChangeEntity;
StreamSubscription<dynamic>? collectionsStream;

void setNewFirebaseAccounInfo(
FirebaseAccountsInformationD firebaseAccountsInformationD) {
Expand Down Expand Up @@ -72,10 +73,16 @@ class CloudValueChangeU {
}

listenToCollectionChange();

Timer.periodic(const Duration(minutes: 1), (Timer t) {
listenToCollectionChange();
});
}

Future<void> listenToCollectionChange() async {
_cloudValueChangeEntity!
await collectionsStream?.cancel();

collectionsStream = _cloudValueChangeEntity!
.listenToCollectionDataBase()
.listen((List<Document> documentList) {
print('Change detected in Firestore');
Expand Down Expand Up @@ -115,44 +122,4 @@ class CloudValueChangeU {
});
});
}

Future<void> listenToDocumentChange() async {
_cloudValueChangeEntity!
.listenToDocumentDataBase()
.listen((Document? document) {
final Document firestoreDocument = document!;
print('Change detected to Document Firestore');

final Map<SmartDeviceBaseAbstract, String> devicesNamesThatValueChanged =
<SmartDeviceBaseAbstract, String>{};

MySingleton.getSmartDevicesList()
.forEach((SmartDeviceBaseAbstract element) {
if (firestoreDocument.map.containsKey(element.id)) {
if (element.getDeviceState() != firestoreDocument.map[element.id]) {
devicesNamesThatValueChanged[element] =
firestoreDocument.map[element.id].toString();
}
}
});

devicesNamesThatValueChanged.forEach(
(SmartDeviceBaseAbstract smartDeviceBaseAbstract, String value) {
print('FireBase "${smartDeviceBaseAbstract.id}" have different value,'
' will now change to $value');
DeviceActions deviceAction;

if (value == DeviceActions.on.toString()) {
deviceAction = DeviceActions.on;
} else if (value == DeviceActions.off.toString()) {
deviceAction = DeviceActions.off;
} else {
deviceAction = EnumHelper.stringToDeviceActions(value)!;
}

ActionsToPreformU.executeDeviceAction(smartDeviceBaseAbstract,
deviceAction, DeviceStateGRPC.waitingInFirebase);
});
});
}
}

0 comments on commit 9125d97

Please sign in to comment.