diff options
Diffstat (limited to 'lib/vehicle-signals/vss_provider.dart')
-rw-r--r-- | lib/vehicle-signals/vss_provider.dart | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/vehicle-signals/vss_provider.dart b/lib/vehicle-signals/vss_provider.dart new file mode 100644 index 0000000..09ce5a1 --- /dev/null +++ b/lib/vehicle-signals/vss_provider.dart @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: Apache-2.0 +import 'dart:io'; +import 'package:meta/meta.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:grpc/grpc.dart'; +import 'package:flutter_hvac/generated/kuksa/val/v1/val.pbgrpc.dart'; +import 'package:flutter_hvac/generated/kuksa/val/v1/types.pb.dart'; +import 'package:flutter_hvac/config.dart'; +import 'package:flutter_hvac/vehicle-signals/vss_client.dart'; +import 'package:flutter_hvac/vehicle-signals/vss_signal_providers.dart'; +import 'package:flutter_hvac/vehicle-signals/vss_path.dart'; + +class HvacVssClient extends VssClient { + @override + final List<String> signals = [ + VSSPath.vehicleFrontLeftTemp, + VSSPath.vehicleFrontRightTemp, + VSSPath.vehicleFrontLeftFanSpeed + ]; + + HvacVssClient({required super.config, required super.channel, required super.stub, required super.ref}); + + @override + void handleSignalUpdates(EntryUpdate update) { + switch (update.entry.path) { + case VSSPath.vehicleFrontLeftTemp: + if (update.entry.value.hasInt32()) { + ref + .read(LeftClimateTempProvider.notifier) + .update(update.entry.value.int32); + } + break; + case VSSPath.vehicleFrontRightTemp: + if (update.entry.value.hasInt32()) { + ref + .read(RightClimateTempProvider.notifier) + .update(update.entry.value.int32); + } + break; + case VSSPath.vehicleFrontLeftFanSpeed: + if (update.entry.value.hasInt32()) { + ref + .read(fanSpeedProvider.notifier) + .update(update.entry.value.int32); + } + break; + default: + print("ERROR: Unexpected path ${update.entry.path}"); + break; + } + } +} + +final vssClientProvider = Provider((ref) { + var config = ref.read(kuksaConfigProvider); + debugPrint("Using ${config.hostname}:${config.port}"); + 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(); + } + final channel = ClientChannel(config.hostname, + port: config.port, + options: ChannelOptions(credentials: creds)); + + final stub = VALClient(channel); + + return HvacVssClient(config: config, channel: channel, stub: stub, ref: ref); +}); |