Skip to content

Commit

Permalink
Improve logging and local storage (#13)
Browse files Browse the repository at this point in the history
* Improve logging and local storage

* windows dependency sync

* Bump version
  • Loading branch information
rohitsangwan01 authored Aug 4, 2024
1 parent 1b78f90 commit 1390b2c
Show file tree
Hide file tree
Showing 26 changed files with 363 additions and 350 deletions.
52 changes: 28 additions & 24 deletions lib/app/client/client.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:signals_flutter/signals_flutter.dart';
import 'package:synergy_client_dart/synergy_client_dart.dart';
import 'package:uni_control_hub/app/client/client_screen.dart';
import 'package:uni_control_hub/app/data/dialog_handler.dart';
import 'package:uni_control_hub/app/data/logger.dart';
import 'package:uni_control_hub/app/models/client_alias.dart';
import 'package:uni_control_hub/app/models/screen_link.dart';
import 'package:uni_control_hub/app/models/synergy_client.dart';
import 'package:uni_control_hub/app/services/storage_service.dart';
import 'package:uni_control_hub/app/services/synergy_service.dart';
Expand All @@ -19,22 +19,20 @@ enum ClientType { ble, usb, uhid }
class Client {
String id;
Function(bool)? onConnectionUpdate;
late final ScreenInterface screen;
late final StorageService storageService = StorageService.to;

late final ClientScreen screen;
late final StorageService _storageService = StorageService.to;
final ClientType type;

/// Signals
Signal<bool> isConnected = Signal(false, autoDispose: true);
Signal<String?> error = Signal<String?>(null, autoDispose: true);
Signal<(int x, int y)> mouseMovement = Signal((0, 0), autoDispose: true);
Signal<ClientAlias> clientAlias =
Signal(SynergyService.to.clientAliases.first, autoDispose: true);
Signal<ClientAlias> clientAlias = Signal(
SynergyService.to.clientAliases.first,
autoDispose: true,
);

bool _lastConnectedValue = false;

SynergyClientDart? synergyClient;
Direction get direction => clientAlias.value.direction.value;
SynergyClientDart? _synergyClient;

Client({
required this.id,
Expand All @@ -43,20 +41,26 @@ class Client {
this.onConnectionUpdate,
}) {
screen = ClientScreen(
this,
inputReportHandler,
onMouseMove: (x, y) => mouseMovement.value = (x, y),
direction: clientAlias.value.direction.value,
inputReportCallback: (report) => inputReportHandler(id, report),
onConnectCallback: () => _setConnected(true),
onDisconnectCallback: () => _setConnected(false),
onErrorCallback: (error) {
logError("$id: $error");
_setConnected(false);
DialogHandler.showError("Error connecting to Synergy Server: $error");
},
);

// Try to load alias from cache
String? clientAliasCache = storageService.getClientAlias(id);
String? clientAliasCache = _storageService.getClientAlias(id);
clientAlias.value = SynergyService.to.clientAliases.firstWhere(
(element) => element.name == clientAliasCache,
orElse: () => SynergyService.to.clientAliases.first,
);
}

void setConnected(bool connected) {
void _setConnected(bool connected) {
if (_lastConnectedValue == connected) return;
_lastConnectedValue = connected;
logInfo("Client $id is connected: $connected");
Expand All @@ -73,13 +77,13 @@ class Client {
}
if (!isConnected.value) return;
clientAlias.value = alias;
storageService.setClientAlias(id, alias.name);
_storageService.setClientAlias(id, alias.name);
reConnectClient();
}

void toggleConnection() {
isConnected.value = !isConnected.value;
setConnected(isConnected.value);
_setConnected(isConnected.value);
if (isConnected.value) {
connectSynergySever();
} else {
Expand All @@ -99,8 +103,8 @@ class Client {
SynergyClient? synergyCl = await _getAddress();
logInfo("Synergy Client: $synergyCl");
if (synergyCl == null) return;
synergyClient = SynergyClientDart();
await synergyClient?.connect(
_synergyClient = SynergyClientDart();
await _synergyClient?.connect(
screen: screen,
synergyServer: SocketServer(
synergyCl.serverAddress,
Expand All @@ -120,7 +124,7 @@ class Client {
if (!SynergyService.to.isServerRunning.value) {
logInfo("Synergy Server is not running");
error.value = "Server is not running, please start the server.";
setConnected(false);
_setConnected(false);
return null;
}
}
Expand All @@ -130,7 +134,7 @@ class Client {
SynergyClient? synergyClient = StorageService.to.synergyClient;
if (synergyClient == null) {
error.value = "Please add a Synergy Server";
setConnected(false);
_setConnected(false);
return null;
}
return synergyClient;
Expand All @@ -146,8 +150,8 @@ class Client {
void disconnectSynergyServer({
bool notifyConnectionState = true,
}) {
synergyClient?.disconnect();
synergyClient = null;
if (notifyConnectionState) setConnected(false);
_synergyClient?.disconnect();
_synergyClient = null;
if (notifyConnectionState) _setConnected(false);
}
}
57 changes: 24 additions & 33 deletions lib/app/client/client_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import 'package:flutter/material.dart';
import 'package:synergy_client_dart/synergy_client_dart.dart';
import 'package:uni_control_hub/app/client/report_handler.dart';
import 'package:uni_control_hub/app/data/logger.dart';
import 'package:uni_control_hub/app/client/client.dart';
import 'package:uni_control_hub/app/models/screen_link.dart';
import 'package:uni_control_hub/app/data/dialog_handler.dart';
import 'package:uni_control_hub/app/services/storage_service.dart';

class ClientScreen extends ScreenInterface {
final Client client;
final InputReportHandler _inputReportHandler;
final Function(int x, int y)? onMouseMove;

ClientScreen(
this.client,
this._inputReportHandler, {
this.onMouseMove,
late final StorageService _storageService = StorageService.to;
Function(int x, int y)? onMouseMove;
final VoidCallback onConnectCallback;
final VoidCallback onDisconnectCallback;
final Function(String error) onErrorCallback;
final Function(List<int>) inputReportCallback;
final Direction direction;

ClientScreen({
required this.direction,
required this.inputReportCallback,
required this.onConnectCallback,
required this.onDisconnectCallback,
required this.onErrorCallback,
});

List<int> lastSent = List.filled(4, 0);
Expand Down Expand Up @@ -71,7 +75,7 @@ class ClientScreen extends ScreenInterface {
void mouseWheel(int x, int y) {
int wheel = x != 0 ? x : y;
// convert wheel in +1 or -1
if (client.storageService.invertMouseScroll) {
if (_storageService.invertMouseScroll) {
wheel = -wheel;
}

Expand All @@ -95,7 +99,7 @@ class ClientScreen extends ScreenInterface {

@override
RectObj getShape() {
Size size = StorageService.to.clientDefaultSize;
Size size = _storageService.clientDefaultSize;
return RectObj(width: size.width, height: size.height);
}

Expand Down Expand Up @@ -152,7 +156,7 @@ class ClientScreen extends ScreenInterface {
@override
bool leave() {
logInfo("ClientScreen: leave");
switch (client.direction) {
switch (direction) {
case Direction.left:
_moveMouseMultipleEvents(x: 4);
break;
Expand All @@ -172,21 +176,13 @@ class ClientScreen extends ScreenInterface {
}

@override
void onConnect() {
client.setConnected(true);
}
void onConnect() => onConnectCallback();

@override
void onDisconnect() {
client.setConnected(false);
}
void onDisconnect() => onDisconnectCallback();

@override
void onError(String error) {
logError("${client.id}: $error");
client.setConnected(false);
DialogHandler.showError("Error connecting to Synergy Server: $error");
}
void onError(String error) => onErrorCallback(error);

Future<void> _sendText(String text) async {
final List<int> report = List<int>.filled(9, 0);
Expand All @@ -201,15 +197,7 @@ class ClientScreen extends ScreenInterface {
}
}

Future<void> _addInputReport(List<int> inputReport) async {
await _inputReportHandler(client.id, inputReport);
}

void _moveMouseMultipleEvents({
int? x,
int? y,
int count = 100,
}) {
void _moveMouseMultipleEvents({int? x, int? y, int count = 100}) {
for (int i = 0; i < count; i++) {
var reportData = Uint8List(5);
reportData[0] = 0x02; // Report ID
Expand All @@ -220,4 +208,7 @@ class ClientScreen extends ScreenInterface {
_addInputReport(reportData);
}
}

Future<void> _addInputReport(List<int> inputReport) =>
inputReportCallback(inputReport);
}
1 change: 0 additions & 1 deletion lib/app/client/report_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ int? keyCodeToByte(final int keyCode) {
try {
return textToByte(utf8.decode([keyCode]));
} catch (e) {
// print("keyCode: $keyCode");
return null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/app/communication/usb/usb_device_communication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:developer';
import 'dart:ffi';
import 'dart:io';

import 'package:uni_control_hub/app/data/file_manager.dart';
import 'package:uni_control_hub/app/services/file_service.dart';
import 'package:uni_control_hub/app/services/adb_service.dart';
import 'package:uni_control_hub/app/services/communication_service.dart';
import 'package:uni_control_hub/app/client/report_handler.dart';
Expand All @@ -21,7 +21,7 @@ class UsbDeviceCommunication {

void setup() {
try {
String? libUsbFile = FileManager.libUsbBinaryPath;
String? libUsbFile = FileService.to.libUsbBinaryPath;
if (libUsbFile == null) {
logError('LibUsb not found for this platform');
return;
Expand Down
3 changes: 2 additions & 1 deletion lib/app/data/app_data.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class AppData {
static String uniHubServerPath = "assets/UniHubServer_0.1.jar";
// App name without space
static const String appName = "UniControlHub";

static const gitIssueUrl =
"https://github.com/rohitsangwan01/uni_control_hub/issues";
Expand Down
77 changes: 0 additions & 77 deletions lib/app/data/file_manager.dart

This file was deleted.

Loading

0 comments on commit 1390b2c

Please sign in to comment.