diff options
Diffstat (limited to 'lib/data/data_providers')
-rw-r--r-- | lib/data/data_providers/vehicle_notifier.dart | 152 | ||||
-rw-r--r-- | lib/data/data_providers/vss_provider.dart | 105 |
2 files changed, 139 insertions, 118 deletions
diff --git a/lib/data/data_providers/vehicle_notifier.dart b/lib/data/data_providers/vehicle_notifier.dart index b12cbfd..012e5dc 100644 --- a/lib/data/data_providers/vehicle_notifier.dart +++ b/lib/data/data_providers/vehicle_notifier.dart @@ -4,10 +4,28 @@ import 'package:flutter_ics_homescreen/export.dart'; import 'package:flutter/services.dart'; import 'package:protos/protos.dart'; +class KuksaConfig { + final String hostname; + final int port; + final String authorization; + final bool use_tls; + final List<int> ca_certificate; + final String tls_server_name; + + static String configFilePath = '/etc/xdg/AGL/ics-homescreen.yaml'; + static String defaultHostname = 'localhost'; + static int defaultPort = 55555; + static String defaultCaCertPath = '/etc/kuksa-val/CA.pem'; + + KuksaConfig({required this.hostname, required this.port, required this.authorization, + required this.use_tls, required this.ca_certificate, required this.tls_server_name}); +} + class VehicleNotifier extends StateNotifier<Vehicle> { VehicleNotifier(super.state); late ClientChannel channel; + late String authorization; late VALClient stub; void updateSpeed(double newValue) { @@ -123,33 +141,141 @@ class VehicleNotifier extends StateNotifier<Vehicle> { } } - void startListen() async { - String hostName = 'localhost'; - int port = 8080; + Future<KuksaConfig> readConfig() async { + String hostname = KuksaConfig.defaultHostname; + int port = KuksaConfig.defaultPort; + bool use_tls = false; + String ca_path = KuksaConfig.defaultCaCertPath; + List<int> ca_cert = []; + String tls_server_name = ""; + String token = ""; + + // Read build time configuration from bundle try { var data = await rootBundle.loadString('app-config/config.yaml'); final dynamic yamlMap = loadYaml(data); if (yamlMap.containsKey('hostname')) { - hostName = yamlMap['hostname']; + hostname = yamlMap['hostname']; } if (yamlMap.containsKey('port')) { port = yamlMap['port']; } + + if (yamlMap.containsKey('use-tls')) { + var value = yamlMap['use-tls']; + if (value is bool) + use_tls = value; + } + + if (use_tls) { + if (yamlMap.containsKey('ca-certificate')) { + ca_path = yamlMap['ca-certificate']; + } + + if (yamlMap.containsKey('tls-server-name')) { + tls_server_name = yamlMap['tls_server_name']; + } + } + + if (yamlMap.containsKey('authorization')) { + token = yamlMap['authorization']; + } + } catch (e) { //debugPrint('ERROR: Could not read from file: $configFile'); debugPrint(e.toString()); } + + // Try reading from configuration file in /etc + final configFile = File(KuksaConfig.configFilePath); + try { + print("Reading configuration ${KuksaConfig.configFilePath}"); + String content = await configFile.readAsString(); + final dynamic yamlMap = loadYaml(content); + + if (yamlMap.containsKey('hostname')) { + hostname = yamlMap['hostname']; + } + + if (yamlMap.containsKey('port')) { + port = yamlMap['port']; + } + + if (yamlMap.containsKey('use-tls')) { + var value = yamlMap['use-tls']; + if (value is bool) + use_tls = value; + } + //debugPrint("Use TLS = $use_tls"); + + if (use_tls) { + if (yamlMap.containsKey('ca-certificate')) { + ca_path = yamlMap['ca-certificate']; + } + try { + ca_cert = File(ca_path).readAsBytesSync(); + } on Exception catch(_) { + print("ERROR: Could not read CA certificate file $ca_path"); + ca_cert = []; + } + //debugPrint("CA cert = $ca_cert"); + + if (yamlMap.containsKey('tls-server-name')) { + tls_server_name = yamlMap['tls_server_name']; + } + } + + if (yamlMap.containsKey('authorization')) { + token = yamlMap['authorization']; + } + if (token.isNotEmpty) { + if (token.startsWith("/")) { + debugPrint("Reading authorization token $token"); + String tokenFile = token; + try { + token = await File(tokenFile).readAsString(); + } on Exception catch(_) { + print("ERROR: Could not read authorization token file $token"); + token = ""; + } + } + } + //debugPrint("authorization = $token"); + } catch (e) { + debugPrint('WARNING: Could not read from file: $configFile'); + //debugPrint(e.toString()); + } + return KuksaConfig( + hostname: hostname, + port: port, + authorization: token, + use_tls: use_tls, + ca_certificate: ca_cert, + tls_server_name: tls_server_name + ); + } + + void startListen() async { + KuksaConfig config = await readConfig(); + ChannelCredentials creds; + if (config.use_tls && config.ca_certificate.isNotEmpty) { + print("Using TLS"); + if (config.tls_server_name.isNotEmpty) + creds = ChannelCredentials.secure(certificates: config.ca_certificate, + authority: config.tls_server_name); + else + creds = ChannelCredentials.secure(certificates: config.ca_certificate); + } else { + creds = ChannelCredentials.insecure(); + } channel = ClientChannel( - hostName, - port: port, - options: const ChannelOptions( - credentials: ChannelCredentials.insecure(), - ), + config.hostname, + port: config.port, + options: ChannelOptions(credentials: creds) ); - - debugPrint('Start Listen on port: $port'); + debugPrint('Start Listen on port: ${config.port}'); stub = VALClient(channel); List<String> fewSignals = VSSPath().getSignalsList(); var request = SubscribeRequest(); @@ -183,7 +309,7 @@ class VehicleNotifier extends StateNotifier<Vehicle> { } void setChildLock({required String side}) { - var helper = ValClientHelper(channel: channel, stub: stub); + var helper = ValClientHelper(stub: stub, authorization: authorization); try { switch (side) { case 'left': @@ -218,7 +344,7 @@ class VehicleNotifier extends StateNotifier<Vehicle> { } void setHVACMode({required String mode}) { - var helper = ValClientHelper(channel: channel, stub: stub); + var helper = ValClientHelper(stub: stub, authorization: authorization); try { switch (mode) { case 'airCondition': diff --git a/lib/data/data_providers/vss_provider.dart b/lib/data/data_providers/vss_provider.dart deleted file mode 100644 index ca02de6..0000000 --- a/lib/data/data_providers/vss_provider.dart +++ /dev/null @@ -1,105 +0,0 @@ -import 'package:flutter_ics_homescreen/export.dart'; -import 'package:protos/protos.dart'; - -class VSS { - static handleSignalUpdates( - EntryUpdate update, - ) { - //final ref = ProviderContainer().read(vehicleStateProvider); - switch (update.entry.path) { - case VSSPath.vehicleSpeed: - if (update.entry.value.hasFloat()) { - //print(ref); - } - break; - // case VSSPath.vehicleInsideTemperature: - // if (update.entry.value.hasFloat()) { - // ref - // .read(vehicleInsideTemperature.notifier) - // .update((state) => state = update.entry.value.float); - // } - // break; - // case VSSPath.vehicleOutsideTemperature: - // if (update.entry.value.hasFloat()) { - // ref - // .read(vehicleOutSideTemperature.notifier) - // .update((state) => state = update.entry.value.float); - // } - // break; - // case VSSPath.vehicleRange: - // if (update.entry.value.hasInt32()) { - // ref - // .read(vehicleRange.notifier) - // .update((state) => state = update.entry.value.uint32); - // } - // break; - // case VSSPath.vehicleFuelLevel: - // if (update.entry.value.hasInt32()) { - // ref - // .read(vehicleFuelLevel.notifier) - // .update((state) => state = update.entry.value.uint32); - // } - // break; - // case VSSPath.vehicleMediaVolume: - // if (update.entry.value.hasInt32()) { - // ref - // .read(vehicleMediaVolume.notifier) - // .update((state) => state = update.entry.value.uint32); - // } - // break; - // case VSSPath.vehicleIsChildLockActiveLeft: - // if (update.entry.value.hasBool_12()) { - // ref - // .read(vehicleIsChildLockActiveLeft.notifier) - // .update((state) => state = update.entry.value.bool_12); - // } - // break; - // case VSSPath.vehicleIsChildLockActiveRight: - // if (update.entry.value.hasBool_12()) { - // ref - // .read(vehicleIsChildLockActiveRight.notifier) - // .update((state) => state = update.entry.value.bool_12); - // } - // break; - // case VSSPath.vehicleEngineSpeed: - // if (update.entry.value.hasFloat()) { - // ref - // .read(vehicleEngineSpeed.notifier) - // .update((state) => state = update.entry.value.float); - // } - // break; - // case VSSPath.vehicleFrontLeftTire: - // if (update.entry.value.hasFloat()) { - // ref - // .read(vehicleFrontLeftTire.notifier) - // .update((state) => state = update.entry.value.float); - // } - // break; - // case VSSPath.vehicleFrontRightTire: - // if (update.entry.value.hasFloat()) { - // ref - // .read(vehicleFrontRightTire.notifier) - // .update((state) => state = update.entry.value.float); - // } - // break; - // case VSSPath.vehicleRearLeftTire: - // if (update.entry.value.hasFloat()) { - // ref - // .read(vehicleRearLeftTire.notifier) - // .update((state) => state = update.entry.value.float); - // } - // break; - // case VSSPath.vehicleRearRightTire: - // if (update.entry.value.hasFloat()) { - // ref - // .read(vehicleRearRightTire.notifier) - // .update((state) => state = update.entry.value.float); - // } - // break; - - default: - debugPrint("ERROR: Unexpected path ${update.entry.path}"); - break; - } - } -} |