MobileApp/lib/boxes/state.dart

84 lines
2.1 KiB
Dart

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:ju_rc_app/lib/boxes.dart';
import 'package:ju_rc_app/lib/serial.dart';
class ControllerState extends JuBox {
const ControllerState({super.key});
@override
State<StatefulWidget> createState() => _ControllerStateState();
}
class _ControllerStateState extends State<ControllerState> {
USerial serial = getSerial();
late Timer timer;
int connected = 0;
bool mode = false; //true is ble
int arc = 0;
double akku = 0.1;
@override
void initState() {
super.initState();
serial.listen(serialListen);
timer = Timer.periodic(const Duration(seconds: 1), (_) async {
serial.sprintln("<ping>", system: true);
connected--;
serial.sprintln("<getSendMode>", system: true);
serial.sprintln("<getVoltage>", system: true);
serial.sprintln("<getRfArc>", system: true);
});
}
@override
void dispose() {
serial.removeListen(serialListen);
timer.cancel();
super.dispose();
}
void serialListen(String line, SerialCommand? cmdo) {
if (cmdo == null) return;
var cmd = cmdo;
setState(() {
if (cmd.command == "pong") connected = 2;
if (cmd.command == "sendMode") {
if (cmd.arg == "RF") {
mode = false;
} else if (cmd.arg == "BLE") {
mode = true;
}
}
if(cmd.command == "rfArc"){
arc = int.tryParse(cmd.arg) ?? 15;
}
if(cmd.command == "voltage"){
akku = double.tryParse(cmd.arg) ?? 0.0;
}
});
}
@override
Widget build(BuildContext context) => Column(
children: [
const Text("Remote controll state"),
Text(
"Serial Connection: ${connected > 0 ? "connected" : "disconnected"}"),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text("RC Mode: "),
if (mode)
const Icon(Icons.bluetooth)
else
const Icon(Icons.settings_input_antenna)
],
),
Text("ARC: $arc"),
Text("Akku: ${akku}V")
],
);
}