save sensor data as json file

This commit is contained in:
2024-12-04 14:57:43 +08:00
parent 20711ea05c
commit 5d1099086b
9 changed files with 144 additions and 31 deletions

View File

@@ -0,0 +1,11 @@
import 'dart:ffi';
class LightSensorEvent {
final double lux;
final int timestamp;
LightSensorEvent(this.lux, this.timestamp);
Map<String, dynamic> toJson() => {
'lux': lux,
'timestamp': timestamp,
};
}

View File

@@ -1,5 +1,10 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:light_sesnor_capture/light_sensor_event.dart';
import 'package:intl/intl.dart';
void main() {
runApp(const MyApp());
@@ -56,18 +61,10 @@ class MyHomePage extends StatefulWidget {
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
int mState = 0;
static const MethodChannel methodChannel =
MethodChannel('tw.moonjuice.light_sensor.method');
List mLightSensorEvents = [];
@override
Widget build(BuildContext context) {
@@ -107,12 +104,15 @@ class _MyHomePageState extends State<MyHomePage> {
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Spacer(),
StreamBuilder<String>(
StreamBuilder<LightSensorEvent>(
stream: streamSensorEventFromNative(),
builder: (context, snapshot) {
if (snapshot.hasData) {
if (mState == 1) {
mLightSensorEvents.add(snapshot.data);
}
return Text(
'Sensor Data: ${snapshot.data}',
'Sensor Data: ${jsonEncode(snapshot.data)}',
style: Theme.of(context).textTheme.headlineLarge,
);
} else {
@@ -125,10 +125,13 @@ class _MyHomePageState extends State<MyHomePage> {
children: [
Spacer(),
ElevatedButton(
onPressed: onPressed, child: Text('Start Record')),
onPressed: (mState == 0) ? onPressed : null,
child: Text('Start Record'),
),
Spacer(),
ElevatedButton(
onPressed: onPressed, child: Text('Stop Record')),
onPressed: (mState == 1) ? onPressed : null,
child: Text('Stop Record')),
Spacer(),
],
),
@@ -139,12 +142,28 @@ class _MyHomePageState extends State<MyHomePage> {
);
}
void onPressed() {}
Future<void> onPressed() async {
setState(() {
mState = ((mState == 0) ? 1 : 0);
});
if (mState == 0) {
final String downloadsPath =
await methodChannel.invokeMethod('getExternalDownloadsPath');
DateTime now = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd_HH-mm-ss').format(now);
File file = File('$downloadsPath/$formattedDate.txt');
var sink = file.openWrite();
sink.write(jsonEncode(mLightSensorEvents));
await sink.flush();
await sink.close();
mLightSensorEvents.clear();
}
}
Stream<String> streamSensorEventFromNative() {
Stream<LightSensorEvent> streamSensorEventFromNative() {
const eventChannel = EventChannel('tw.moonjuice.light_sensor.stream');
return eventChannel
.receiveBroadcastStream()
.map((event) => event.toString());
.map((event) => LightSensorEvent(event['lux'], event['timestamp']));
}
}