Skip to content

Commit

Permalink
Merge pull request #47 from atsign-foundation/deletereceivers
Browse files Browse the repository at this point in the history
feat: now can delete receivers
  • Loading branch information
nickelskevin authored Oct 4, 2022
2 parents 113a500 + 99c1fe3 commit 7a4f321
Showing 1 changed file with 24 additions and 51 deletions.
75 changes: 24 additions & 51 deletions flutter/iot_receiver/lib/services/hro2_data_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class Hro2DataService {
}

Future<bool> deleteAllForKey(String key) async {
var atKeys =
await AtClientManager.getInstance().atClient.getAtKeys(regex: key);
var atKeys = await AtClientManager.getInstance().atClient.getAtKeys(regex: key);
for (var atKey in atKeys) {
await delete(atKey);
}
Expand All @@ -42,9 +41,7 @@ class Hro2DataService {
Future<List<HrO2Device>> getDevices() async {
List<HrO2Device> hrO2DeviceList = [];
// var atClient = AtClientManager.getInstance().atClient;
var keys = await AtClientManager.getInstance()
.atClient
.getAtKeys(regex: AppConstants.deviceKey);
var keys = await AtClientManager.getInstance().atClient.getAtKeys(regex: AppConstants.deviceKey);
for (var element in keys) {
var data = await AtClientManager.getInstance().atClient.get(element);
_logger.info('getDevices got ${data.value}');
Expand All @@ -64,16 +61,13 @@ class Hro2DataService {
Future<bool> putDevice(HrO2Device hrO2Device) async {
AtKey atKey = AtKey()..key = AppConstants.deviceKey;
var value = jsonEncode(hrO2Device);
var response =
await AtClientManager.getInstance().atClient.put(atKey, value);
var response = await AtClientManager.getInstance().atClient.put(atKey, value);
_logger.info('putDeviceList success = $response');
return response;
}

Future<bool> deleteDevice(HrO2Device hrO2Device) async {
List<AtKey> keys = await AtClientManager.getInstance()
.atClient
.getAtKeys(regex: AppConstants.deviceKey);
List<AtKey> keys = await AtClientManager.getInstance().atClient.getAtKeys(regex: AppConstants.deviceKey);
_logger.info('deleteDevice processing ${keys.length} items');
for (var key in keys) {
var data = await AtClientManager.getInstance().atClient.get(key);
Expand All @@ -88,15 +82,12 @@ class Hro2DataService {
Future<List<HrO2Receiver>> getReceivers() async {
List<HrO2Receiver> hrO2ReceiverList = [];
// var atClient = _atClient;
var keys = await AtClientManager.getInstance()
.atClient
.getAtKeys(regex: AppConstants.receiverKey);
var keys = await AtClientManager.getInstance().atClient.getAtKeys(regex: AppConstants.receiverKey);
for (var element in keys) {
var data = await AtClientManager.getInstance().atClient.get(element);
_logger.info('getReceivers got ${data.value}');
try {
HrO2Receiver hrO2Receiver =
HrO2Receiver.fromJson(jsonDecode(data.value));
HrO2Receiver hrO2Receiver = HrO2Receiver.fromJson(jsonDecode(data.value));
hrO2ReceiverList.add(hrO2Receiver);
} catch (error) {
// found some dirty data, consider deleting
Expand All @@ -113,29 +104,26 @@ class Hro2DataService {
// ..sharedWith = hrO2Receiver.receiverAtsign
..key = AppConstants.receiverKey;
var value = jsonEncode(hrO2Receiver);
var success =
await AtClientManager.getInstance().atClient.put(atKey, value);
var success = await AtClientManager.getInstance().atClient.put(atKey, value);
_logger.info('putReceiver success = $success');
// now, share the list with the device
List<HrO2Receiver> receiverList = await getReceivers();
atKey.key = AppConstants.receiverListKey;
atKey.sharedWith = hrO2Receiver.hrO2Device.deviceAtsign;
var receiverSuccess = await AtClientManager.getInstance()
.atClient
.put(atKey, jsonEncode(receiverList));
var receiverSuccess = await AtClientManager.getInstance().atClient.put(atKey, jsonEncode(receiverList));
_logger.info('putReceiver success = $receiverSuccess');
return success && receiverSuccess;
}

Future<bool> deleteReceiver(HrO2Receiver hrO2Receiver) async {
List<AtKey> keys = await AtClientManager.getInstance()
.atClient
.getAtKeys(regex: AppConstants.receiverKey);
List<AtKey> keys = await AtClientManager.getInstance().atClient.getAtKeys(regex: AppConstants.receiverKey);
_logger.info('deleteDevice processing ${keys.length} items');
for (var key in keys) {
var data = await AtClientManager.getInstance().atClient.get(key);
if (data.value == hrO2Receiver) {
_logger.info('deleteReceiver deleting $key');
AtValue data = await AtClientManager.getInstance().atClient.get(key);
var atKeyString = jsonDecode(data.value);
HrO2Receiver rec = HrO2Receiver.fromJson(atKeyString);
if (rec.receiverAtsign == hrO2Receiver.receiverAtsign) {
_logger.info('deleteReceiver deleting ${hrO2Receiver.receiverAtsign}\'s entry');
delete(key);
}
}
Expand All @@ -151,8 +139,7 @@ class Hro2DataService {
var data = await AtClientManager.getInstance().atClient.get(element);
_logger.info('getDataOwners got ${data.value}');
try {
HrO2DataOwner hrO2DataOwner =
HrO2DataOwner.fromJson(jsonDecode(data.value));
HrO2DataOwner hrO2DataOwner = HrO2DataOwner.fromJson(jsonDecode(data.value));
hrO2DataOwnerList.add(hrO2DataOwner);
} catch (error) {
// found some dirty data, consider deleting
Expand All @@ -168,32 +155,24 @@ class Hro2DataService {
AtKey atKey = AtKey()
..sharedWith = hrO2DataOwner.dataOwnerAtsign
..key = AppConstants.dataOwnerKey;
var success = await AtClientManager.getInstance()
.atClient
.put(atKey, jsonEncode(hrO2DataOwner));
var success = await AtClientManager.getInstance().atClient.put(atKey, jsonEncode(hrO2DataOwner));
_logger.info('putDataOwner success = $success');
List<HrO2DataOwner> owners = await getDataOwners();
atKey.key = AppConstants.dataOwnerListKey;
atKey.sharedWith = hrO2DataOwner.hrO2Device.deviceAtsign;
var dataOwnerSuccess = await AtClientManager.getInstance()
.atClient
.put(atKey, jsonEncode(owners));
var dataOwnerSuccess = await AtClientManager.getInstance().atClient.put(atKey, jsonEncode(owners));
_logger.info('putDataOwner success = $dataOwnerSuccess');
return success && dataOwnerSuccess;
}

Future<List<HrO2DeviceOwner>> getDeviceOwners() async {
List<HrO2DeviceOwner> hrO2DeviceOwnerList = [];
var keys = await AtClientManager.getInstance()
.atClient
.getAtKeys(regex: AppConstants.deviceOwnerKey);
var keys = await AtClientManager.getInstance().atClient.getAtKeys(regex: AppConstants.deviceOwnerKey);
for (var element in keys) {
var deviceOwnerData =
await AtClientManager.getInstance().atClient.get(element);
var deviceOwnerData = await AtClientManager.getInstance().atClient.get(element);
_logger.info('getDeviceOwners got ${deviceOwnerData.value}');
try {
HrO2DeviceOwner hrO2DeviceOwner =
HrO2DeviceOwner.fromJson(jsonDecode(deviceOwnerData.value));
HrO2DeviceOwner hrO2DeviceOwner = HrO2DeviceOwner.fromJson(jsonDecode(deviceOwnerData.value));
hrO2DeviceOwnerList.add(hrO2DeviceOwner);
} catch (error) {
// found some dirty data, consider deleting
Expand All @@ -208,17 +187,13 @@ class Hro2DataService {
AtKey atKey = AtKey()
..key = AppConstants.deviceOwnerKey
..sharedWith = hrO2DeviceOwner.deviceOwnerAtsign;
var response = await AtClientManager.getInstance()
.atClient
.put(atKey, jsonEncode(hrO2DeviceOwner));
var response = await AtClientManager.getInstance().atClient.put(atKey, jsonEncode(hrO2DeviceOwner));
_logger.info('putDeviceOwner success = $response');
return response;
}

Future<bool> deleteDeviceOwner(HrO2DeviceOwner hrO2DeviceOwner) async {
List<AtKey> keys = await AtClientManager.getInstance()
.atClient
.getAtKeys(regex: AppConstants.deviceOwnerKey);
List<AtKey> keys = await AtClientManager.getInstance().atClient.getAtKeys(regex: AppConstants.deviceOwnerKey);
_logger.info('deleteDeviceOwner processing ${keys.length} items');
for (var key in keys) {
var data = await AtClientManager.getInstance().atClient.get(key);
Expand All @@ -238,10 +213,8 @@ class AppConstants {
static const String deviceKey = 'device.$libraryNamespace';
static const String deviceListKey = 'device_list.$libraryNamespace';
static const String deviceOwnerKey = 'device_owner.$libraryNamespace';
static const String deviceOwnerListKey =
'device_owner_list.$libraryNamespace';
static const String deviceDataOwnerKey =
'device_data_owner.$libraryNamespace';
static const String deviceOwnerListKey = 'device_owner_list.$libraryNamespace';
static const String deviceDataOwnerKey = 'device_data_owner.$libraryNamespace';
static const String deviceReceiverKey = 'device_receiver.$libraryNamespace';
static const String receiverKey = 'receiver.$libraryNamespace';
static const int responseTimeLimit = 30;
Expand Down

0 comments on commit 7a4f321

Please sign in to comment.