Do some refactoring for protocol v2

This commit is contained in:
zontreck 2025-06-15 02:05:52 -07:00
parent d9c79a4ee9
commit 8adaf6169a
5 changed files with 122 additions and 3 deletions

View file

@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math' as math;
import 'dart:typed_data';
import 'dart:ui';
@ -45,6 +46,12 @@ class SessionData {
static bool IsSavedData = false;
static String SaveDataType = "";
/// This indicates whether the app is in a live session.
static bool Recording = false;
/// This is the version number of the recording as specified by the server.
static int RecordingVersion = 0;
/// Is true if the try-catch is tripped or if not running on Android
static bool isWeb = false;
@ -236,6 +243,8 @@ class SessionData {
);
}
await _create();
_listener = Geolocator.getPositionStream(
locationSettings: TTConsts.LOCATION_SETTINGS,
).listen((pos) {
@ -274,6 +283,50 @@ class SessionData {
_upload(nbtData);
}
/// v2 Create function.
///
/// This function sets the Session ID globally. It will also set the Recording flag to true.
static Future<void> _create() async {
Dio dio = Dio();
Map<String, dynamic> payload = {"cmd": "createv2"};
try {
var reply = await dio.post(
TTConsts.SESSION_SERVER,
data: json.encode(payload),
);
if (reply.statusCode == null) {
throw Exception("Fatal error while creating");
}
if (reply.statusCode! != 200) {
throw Exception("Fatal error while creating");
}
Map<String, dynamic> replyJs = json.decode(reply.data as String);
if (replyJs["status"] == "created") {
print("Successful creation");
LastSessionID = replyJs['session'] as String;
Recording = true;
RecordingVersion = 0;
Calls.dispatch();
}
} catch (E) {
// Retry in 2 seconds
DisplayError =
"Error: Something went wrong during session creation. Retry in 5 seconds...";
Calls.dispatch();
Timer(Duration(seconds: 5), () {
_create();
});
}
}
/// Deprecated: This function uploads using the v1 API, and is intended to be called at the conclusion of data recording.
@Deprecated(
"This function utilizes the Protocol v1 Create method, which has been replaced by createv2 and patch. This function will be removed.",
)
static Future<void> _upload(List<int> nbtData) async {
Dio dio = Dio();
@ -325,6 +378,7 @@ class SessionData {
DisplayError = "";
IsReadOnly = false;
ContainsTripTimes = true;
Recording = false;
if (FlutterBackground.isBackgroundExecutionEnabled) {
FlutterBackground.disableBackgroundExecution();
}
@ -367,6 +421,15 @@ class SessionData {
static Future<void> _deserialize(CompoundTag ct) async {
IsOnTheClock = NbtUtils.readBoolean(ct, "inprog");
if (ct.containsKey("record"))
Recording = NbtUtils.readBoolean(ct, "record");
else
Recording = false;
if (Recording && isWeb) {
IsOnTheClock = false;
IsReadOnly = true;
}
if (IsOnTheClock) {
await Login();
@ -416,6 +479,7 @@ class SessionData {
CompoundTag ct = CompoundTag();
NbtUtils.writeBoolean(ct, "inprog", IsOnTheClock);
NbtUtils.writeBoolean(ct, "record", Recording);
// No need to write the contains trip times flag, it is set during deserialization. For inprog sessions, it will be set to true by the system.
ct.put("start", StringTag.valueOf(StartTime.toIso8601String()));
if (EndTime.year > 2000) {
@ -603,6 +667,29 @@ class SessionData {
return tm.toString();
}
static Future<int> FetchVersion() async {
Dio dio = Dio();
var packet = json.encode({"cmd": "get_version", "id": LastSessionID});
var response = await dio.post(TTConsts.SESSION_SERVER, data: packet);
if (response.statusCode == null) {
DisplayError = "Error: Version retrieval failed";
return -1;
}
if (response.statusCode! != 200) {
DisplayError = "Error: Session server HTTP Response code";
return -2;
}
var reply = json.decode(response.data as String);
if (reply["status"] == "version_back") {
return reply["version"] as int;
} else {
DisplayError = "Error: Unknown get_version reply";
return -3;
}
}
}
class Delivery {