Skip to content

Commit

Permalink
Tidy up #3
Browse files Browse the repository at this point in the history
Renaming classes and variables for better understanding.
  • Loading branch information
DariusIMP committed Mar 24, 2022
1 parent 27b08c4 commit 4f5a946
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 44 deletions.
35 changes: 18 additions & 17 deletions lib/datacollection/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import 'package:lsa_gloves/edgeimpulse/api_client.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:developer' as developer;

class GloveEventsStorage {
static const String TAG = "GloveEventsStorage";
static final GloveEventsStorage _singleton = GloveEventsStorage._internal();
/// Class to manage the stored files with sensors measurements from the gloves.
class FileManager {
static const String TAG = "FileManager";
static final FileManager _singleton = FileManager._internal();

factory GloveEventsStorage() {
factory FileManager() {
return _singleton;
}

GloveEventsStorage._internal();
FileManager._internal();

Future<String> get _localPath async {
final directory = await getExternalStorageDirectory();
Expand Down Expand Up @@ -55,11 +56,12 @@ class GloveEventsStorage {
}
}

/// Class to handle the storage of the received measurements in a json file.
class DeviceMeasurementsFile {
static const TAG = "DeviceMeasurementsFile";
final File file;
final DateTime lastModificationDate;
SensorMeasurements? fileContent;
BufferedSensorMeasurements? fileContent;

String get path => file.path;

Expand All @@ -71,11 +73,11 @@ class DeviceMeasurementsFile {
static Future<DeviceMeasurementsFile> create(
String deviceName, String deviceId, String word) async {
var creationDate = DateTime.now();
SensorMeasurements json = new SensorMeasurements(
BufferedSensorMeasurements json = new BufferedSensorMeasurements(
deviceName, deviceId, word, <List<double>>[], <int>[]);
String datetimeStr = format(creationDate);
var filename = "${deviceName.substring(0, 1)}_${word}_$datetimeStr";
var file = await new GloveEventsStorage().createFile(filename);
var file = await new FileManager().createFile(filename);
return DeviceMeasurementsFile._(file, creationDate, json);
}

Expand All @@ -92,7 +94,6 @@ class DeviceMeasurementsFile {

Future<bool> save() async {
try {
//TODO proteger concunrrencia, mutex??
String json = jsonEncode(this.fileContent);
developer.log("saving $json");
await this.file.writeAsString(json);
Expand All @@ -114,7 +115,6 @@ class DeviceMeasurementsFile {

Future<String> _readAllAsString() async {
try {
//TODO proteger concunrrencia, mutex??
final contents = await file.readAsString();
return contents;
} catch (e) {
Expand All @@ -123,14 +123,14 @@ class DeviceMeasurementsFile {
}
}

Future<SensorMeasurements> readJsonContent() async {
Future<BufferedSensorMeasurements> readJsonContent() async {
String fileContent = await _readAllAsString();
return SensorMeasurements.fromJson(json.decode(fileContent));
return BufferedSensorMeasurements.fromJson(json.decode(fileContent));
}

Future<bool> upload() async {
try {
SensorMeasurements measurementsJson = await readJsonContent();
BufferedSensorMeasurements measurementsJson = await readJsonContent();
return EdgeImpulseApiClient.uploadFile(
measurementsJson, lastModificationDate);
}catch(e, stacktrace) {
Expand All @@ -150,14 +150,15 @@ class DeviceMeasurementsFile {
}
}

class SensorMeasurements {
/// Class to buffer the incoming sensor measurements in an internal list.
class BufferedSensorMeasurements {
final String deviceName;
final String deviceId;
final String word;
final List<int> timestamps;
final List<List<double>> values;

SensorMeasurements(this.deviceName, this.deviceId, this.word,this.values,
BufferedSensorMeasurements(this.deviceName, this.deviceId, this.word,this.values,
this.timestamps);

bool add(GloveMeasurement gloveMeasurement) {
Expand Down Expand Up @@ -188,7 +189,7 @@ class SensorMeasurements {
return measurementList;
}

factory SensorMeasurements.fromJson(dynamic json) {
factory BufferedSensorMeasurements.fromJson(dynamic json) {
List<List<double>> _values = <List<double>>[];
List<int> _timestamps = <int>[];

Expand All @@ -203,7 +204,7 @@ class SensorMeasurements {
var jsonLists = json['timestamps'] as List;
_timestamps = jsonLists.map((v) => v as int).toList();
}
return SensorMeasurements(
return BufferedSensorMeasurements(
json['device_name'] as String,
json['device_id'] as String,
json['word'] as String,
Expand Down
2 changes: 1 addition & 1 deletion lib/edgeimpulse/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EdgeImpulseApiClient {
static const int OK_STATUS_CODE = 200;

static Future<bool> uploadFile(
SensorMeasurements sensorMeasurements, DateTime datetime) async {
BufferedSensorMeasurements sensorMeasurements, DateTime datetime) async {
var fileName = sensorMeasurements.word + '-' + datetime.toString();

var protected = Protected(
Expand Down
8 changes: 4 additions & 4 deletions lib/pages/file_content_chart_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class _FileContentChartPageState extends State<FileContentChartPage> {

@override
Widget build(BuildContext context) {
SensorMeasurements sensorMeasurements =
ModalRoute.of(context)!.settings.arguments as SensorMeasurements;
BufferedSensorMeasurements sensorMeasurements =
ModalRoute.of(context)!.settings.arguments as BufferedSensorMeasurements;

return Consumer<BluetoothBackend>(
builder: (context, backend, _) => SafeArea(
Expand Down Expand Up @@ -78,7 +78,7 @@ class _FileContentChartPageState extends State<FileContentChartPage> {
}

class MeasurementsChart extends StatefulWidget {
final SensorMeasurements measurements;
final BufferedSensorMeasurements measurements;
final FingerValue finger;
final SensorValue sensor;
final String title;
Expand All @@ -99,7 +99,7 @@ class MeasurementsChart extends StatefulWidget {
}

class _MeasurementsChartState extends State<MeasurementsChart> {
SensorMeasurements _measurements;
BufferedSensorMeasurements _measurements;
FingerValue finger;
String title;
bool legend;
Expand Down
39 changes: 17 additions & 22 deletions lib/pages/file_manager_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class _FileManagerPage extends State<FileManagerPage> {
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
StreamBuilder<List<DeviceMeasurementsFile>>(
stream: GloveEventsStorage().getListOfFiles().asStream(),
stream: FileManager().getListOfFiles().asStream(),
initialData: [],
builder: (c, snapshot) => Column(
children: snapshot.data!
Expand All @@ -40,7 +40,8 @@ class _FileManagerPage extends State<FileManagerPage> {
leading: IconButton(
icon: Icon(Icons.folder_open_sharp),
onPressed: () async {
SensorMeasurements measurements = await f.readJsonContent();
BufferedSensorMeasurements measurements =
await f.readJsonContent();
Navigator.pushNamed(
context,
FileContentChartPage.routeName,
Expand All @@ -65,8 +66,9 @@ class _FileManagerPage extends State<FileManagerPage> {
setState(() {});
},
),
UploadButton(onButtonPressed: () =>
uploadFileCallBack(f))
UploadButton(
onButtonPressed: () =>
uploadFileCallBack(f))
],
),
),
Expand Down Expand Up @@ -96,9 +98,7 @@ class _FileManagerPage extends State<FileManagerPage> {
class UploadButton extends StatefulWidget {
final Function onButtonPressed;

const UploadButton(
{Key? key,
required this.onButtonPressed})
const UploadButton({Key? key, required this.onButtonPressed})
: super(key: key);

@override
Expand All @@ -113,32 +113,29 @@ class _UploadButton extends State<UploadButton> {
UploadResult _status;
final Function onButtonPressed;

_UploadButton(this.onButtonPressed): _status = UploadResult.ready;
_UploadButton(this.onButtonPressed) : _status = UploadResult.ready;

@override
void initState() {
super.initState();
Timer.periodic(Duration(seconds: 5), (Timer t) {
if(_status == UploadResult.failed){
setState(() {
_status = UploadResult.ready;
});
}});
if (_status == UploadResult.failed) {
setState(() {
_status = UploadResult.ready;
});
}
});
}



@override
Widget build(BuildContext context) {
switch (_status) {
case UploadResult.uploading:
return new CircularProgressIndicator();
case UploadResult.failed:
return IconButton(
icon: Icon(Icons.close,
color: Theme.of(context).errorColor),
onPressed: null
);
icon: Icon(Icons.close, color: Theme.of(context).errorColor),
onPressed: null);
default:
return IconButton(
icon: Icon(
Expand All @@ -150,14 +147,12 @@ class _UploadButton extends State<UploadButton> {
_status = UploadResult.uploading;
});
var result = await onButtonPressed.call();
if(!result){
if (!result) {
setState(() {
_status = UploadResult.failed;
});
}
});
}


}
}

0 comments on commit 4f5a946

Please sign in to comment.