Skip to content

Commit

Permalink
Merge pull request #54 from Gloveland/tidying_up
Browse files Browse the repository at this point in the history
Tidying up
  • Loading branch information
DariusIMP authored Mar 24, 2022
2 parents ac1869f + cc4c55a commit 3b488dd
Show file tree
Hide file tree
Showing 22 changed files with 325 additions and 521 deletions.
19 changes: 0 additions & 19 deletions lib/connection/ble/bluetooth_client.dart

This file was deleted.

94 changes: 0 additions & 94 deletions lib/datacollection/file_content.dart

This file was deleted.

36 changes: 19 additions & 17 deletions lib/datacollection/storage.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'package:lsa_gloves/model/finger.dart';
import 'package:lsa_gloves/model/glove_measurement.dart';
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 +57,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 +74,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 +95,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 +116,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 +124,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 +151,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 +190,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 +205,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
4 changes: 0 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

import 'package:flutter/material.dart';
import 'package:lsa_gloves/datacollection/file_content.dart';
import 'package:lsa_gloves/pages/ble_connection_error_page.dart';
import 'package:lsa_gloves/pages/ble_devices_connection_page.dart';
import 'package:lsa_gloves/pages/file_content_chart_page.dart';
import 'package:lsa_gloves/pages/home_page.dart';
Expand All @@ -24,10 +22,8 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
routes: {
FileContentPage.routeName: (context) => FileContentPage(),
FileContentChartPage.routeName: (context) => FileContentChartPage(),
BleGloveConnectionPage.routeName: (context) => BleGloveConnectionPage(),
BleConnectionErrorPage.routeName: (context) => BleConnectionErrorPage(),
},
title: 'Lengua de señas Argentina',
theme: ThemeData(
Expand Down
29 changes: 29 additions & 0 deletions lib/model/acceleration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

import 'package:lsa_gloves/model/vector3.dart';

/// Class to encapsulate the acceleration values received from the glove.
class Acceleration with Vector3 {
final double x;
final double y;
final double z;

Acceleration(this.x, this.y, this.z);

Acceleration.fromJson(Map<String, dynamic> json)
: x = json['x'], y = json['y'], z = json['z'];

Map<String, dynamic> toJson() => {
'x': x,
'y': y,
'z': z,
};

@override
double getX() => x;

@override
double getY() => y;

@override
double getZ() => z;
}
59 changes: 59 additions & 0 deletions lib/model/finger.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'sensor_value.dart';
import 'vector3.dart';
import 'acceleration.dart';
import 'gyro.dart';

/// Enumeration for each of the hand's fingers.
enum FingerValue {
Pinky,
Ring,
Middle,
Index,
Thumb
}

extension FingerValueTranslation on FingerValue {
String spanishName() {
switch (this) {
case FingerValue.Pinky:
return "Meñique";
case FingerValue.Ring:
return "Anular";
case FingerValue.Middle:
return "Medio";
case FingerValue.Index:
return "Índice";
case FingerValue.Thumb:
return "Pulgar";
}
}
}

/// Class to handle the sensor measurements from a finger's mpu6050.
class Finger {
final Acceleration acc;
final Gyro gyro;

Finger(this.acc, this.gyro);

Finger.fromJson(Map<String, dynamic> json)
: acc = Acceleration.fromJson(json['acc'] as Map<String, dynamic>),
gyro = Gyro.fromJson(json['gyro'] as Map<String, dynamic>);
Map<String, dynamic> toJson() => {
'acc': acc.toJson(),
'gyro': gyro.toJson(),
};

Finger.fromList(List<double> m):
acc = Acceleration(m[0],m[1], m[2]),
gyro = Gyro(m[3],m[4], m[5]);

Vector3 getSensorValues(SensorValue sensorName) {
switch (sensorName) {
case SensorValue.Acceleration:
return acc;
case SensorValue.Gyroscope:
return gyro;
}
}
}
Loading

0 comments on commit 3b488dd

Please sign in to comment.