Skip to content

Commit

Permalink
Handlebar warning (#91)
Browse files Browse the repository at this point in the history
* warn of incorrectly (un)locked handlebars. untested.

* Shortening wait (from 3000 seconds lol) and fixing boolean mixup
  • Loading branch information
TheFreal authored Dec 17, 2024
1 parent 3944c75 commit 7ba1af5
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 6 deletions.
1 change: 1 addition & 0 deletions assets/anim/handlebars.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
"home_reconnect_button": "Suchen",
"seat_alert_title": "Sitzbank offen!",
"seat_alert_body": "Aus Sicherheitsgründen kann der Roller nicht verriegelt werden, so lange die Sitzbank offen ist.",
"locked_handlebar_alert_title": "Lenker ist noch verriegelt!",
"locked_handlebar_alert_body": "Dreh den Lenker ganz nach links, und starte dann den Roller neu.",
"locked_handlebar_alert_action": "Roller ausschalten",
"unlocked_handlebar_alert_title": "Lenker ist nicht verriegelt!",
"unlocked_handlebar_alert_body": "Dreh den Lenker ganz nach links, und starte dann den Roller neu.",
"unlocked_handlebar_alert_action": "Roller einschalten",
"state_name_standby": "Standby",
"state_name_off": "Ausgeschaltet",
"state_name_parked": "Geparkt",
Expand Down
6 changes: 6 additions & 0 deletions assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
"home_reconnect_button": "Reconnect",
"seat_alert_title": "Seat is open!",
"seat_alert_body": "For safety reasons, the scooter can't be locked while the seat is open.",
"locked_handlebar_alert_title": "Handlebars are still locked!",
"locked_handlebar_alert_body": "Push your handlebars all the way to the left, and start the scooter again.",
"locked_handlebar_alert_action": "Turn off scooter",
"unlocked_handlebar_alert_title": "Handlebars did not lock!",
"unlocked_handlebar_alert_body": "Turn your handlebars all the way to the left, and lock the scooter again.",
"unlocked_handlebar_alert_action": "Turn on scooter",
"state_name_standby": "Standby",
"state_name_off": "Powered off",
"state_name_parked": "Parked",
Expand Down
6 changes: 6 additions & 0 deletions assets/i18n/pi.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
"home_reconnect_button": "Reconnect",
"seat_alert_title": "Arrr! The treasure chest be open!",
"seat_alert_body": "Avast ye! Fer safety reasons, ye can't be lockin' the treasure chest while it be open, arrr!",
"locked_handlebar_alert_title": "Avast! Yar helm be locked!",
"locked_handlebar_alert_body": "Turn ye helm all the way to the left, and restart ye ship.",
"locked_handlebar_alert_action": "Power down ship",
"unlocked_handlebar_alert_title": "Yar helm ain't secured!",
"unlocked_handlebar_alert_body": "Turn ye helm all the way to the left, and secure ye ship again.",
"unlocked_handlebar_alert_action": "Power up ship",
"state_name_standby": "Sleepin' like a drunken sailor",
"state_name_off": "Powered off, like a sunken ship",
"state_name_parked": "Docked and ready for plunderin'",
Expand Down
77 changes: 72 additions & 5 deletions lib/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:local_auth/local_auth.dart';
import 'package:logging/logging.dart';
import 'package:lottie/lottie.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../control_screen.dart';
Expand Down Expand Up @@ -359,6 +360,12 @@ class _HomeScreenState extends State<HomeScreen> {
log.warning(
"Seat is open, showing alert");
showSeatWarning();
} on HandlebarLockException catch (_) {
log.warning(
"Handlebars are still unlocked, showing alert");
showHandlebarWarning(
didNotUnlock: false,
);
} catch (e, stack) {
log.severe(
"Problem opening the seat",
Expand All @@ -369,11 +376,20 @@ class _HomeScreenState extends State<HomeScreen> {
}
}
: (_scooterState == ScooterState.standby
? () {
widget.scooterService.unlock();
if (widget.scooterService
.hazardLocking) {
_flashHazards(2);
? () async {
try {
await widget.scooterService
.unlock();
if (widget.scooterService
.hazardLocking) {
_flashHazards(2);
}
} on HandlebarLockException catch (_) {
log.warning(
"Handlebars are still locked, showing alert");
showHandlebarWarning(
didNotUnlock: true,
);
}
}
: widget.scooterService
Expand Down Expand Up @@ -470,6 +486,57 @@ class _HomeScreenState extends State<HomeScreen> {
);
}

void showHandlebarWarning({required bool didNotUnlock}) {
showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Lottie.asset(
"assets/anim/handlebars.json",
height: 160,
),
const SizedBox(height: 24),
Text(FlutterI18n.translate(context,
"${didNotUnlock ? "locked" : "unlocked"}_handlebar_alert_title")),
],
),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(FlutterI18n.translate(context,
"${didNotUnlock ? "locked" : "unlocked"}_handlebar_alert_body")),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: Text(FlutterI18n.translate(context,
"${didNotUnlock ? "locked" : "unlocked"}_handlebar_alert_action")),
onPressed: () {
if (didNotUnlock) {
widget.scooterService.lock();
} else {
widget.scooterService.unlock();
}
Navigator.of(context).pop();
},
),
],
);
},
);
}

void redirectOrStart() async {
List<String> ids = await widget.scooterService.getSavedScooterIds();
log.info("Saved scooters: $ids");
Expand Down
21 changes: 20 additions & 1 deletion lib/scooter_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ class ScooterService {

// SCOOTER ACTIONS

void unlock() {
Future<void> unlock() async {
_sendCommand("scooter:state unlock");
HapticFeedback.heavyImpact();

Expand All @@ -534,6 +534,13 @@ class ScooterService {
if (_openSeatOnUnlock) {
openSeat();
}

await Future.delayed(const Duration(seconds: 2), () {
if (_handlebarController.value == true) {
log.warning("Handlebars didn't unlock, sending warning");
throw HandlebarLockException();
}
});
}

Future<void> wakeUpAndUnlock() async {
Expand All @@ -548,6 +555,7 @@ class ScooterService {
}

Future<void> lock() async {
// double-check for open seat
if (_seatClosedController.value == false) {
log.warning("Seat seems to be open, checking again...");
// make really sure nothing has changed
Expand All @@ -560,13 +568,22 @@ class ScooterService {
"Seat state was ${_seatClosedController.value} this time, proceeding...");
}
}

// send the command
_sendCommand("scooter:state lock");
HapticFeedback.heavyImpact();

if (_hazardLocking) {
hazard(times: 1);
}

await Future.delayed(const Duration(seconds: 2), () {
if (_handlebarController.value == false) {
log.warning("Handlebars didn't lock, sending warning");
throw HandlebarLockException();
}
});

// don't immediately unlock again automatically
_autoUnlockCooldown = true;
await _sleepSeconds(keylessCooldownSeconds.toDouble());
Expand Down Expand Up @@ -900,3 +917,5 @@ class ScooterService {
class SeatOpenException {}

class UnavailableCharacteristicsException {}

class HandlebarLockException {}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ flutter:
- assets/faq_pi.json
- assets/anim/scanning.json
- assets/anim/found.json
- assets/anim/handlebars.json

fonts:
- family: icomoon
Expand Down

0 comments on commit 7ba1af5

Please sign in to comment.