aboutsummaryrefslogtreecommitdiffstats
path: root/lib/vehicle-signals/vss_provider.dart
blob: e0fa1b8cfc3651589c2a1f762f848b5b5348f98f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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_homescreen/generated/kuksa/val/v1/val.pbgrpc.dart';
import 'package:flutter_homescreen/generated/kuksa/val/v1/types.pb.dart';
import 'package:flutter_homescreen/config.dart';
import 'package:flutter_homescreen/vehicle-signals/vss_client.dart';
import 'package:flutter_homescreen/vehicle-signals/vss_path.dart';
import 'package:flutter_homescreen/vehicle-signals/vss_signal_providers.dart';

class HomescreenVssClient extends VssClient {
  @override
  final List<String> signals = [
      VSSPath.vehicleMediaVolume,
      VSSPath.vehicleInsideTemperature,
      VSSPath.vehicleOutsideTemperature
    ];

  HomescreenVssClient({required super.config, required super.channel, required super.stub, required super.ref});

  @override
  void handleSignalUpdates(EntryUpdate update) {
    switch (update.entry.path) {
      case VSSPath.vehicleMediaVolume:
        if (update.entry.value.hasUint32()) {
          ref
            .read(vehicleSignalMediaVolumeProvider.notifier)
            .update(volume: update.entry.value.uint32);
        }
        break;
      case VSSPath.vehicleInsideTemperature:
        if (update.entry.value.hasFloat()) {
          ref
            .read(vehicleSignalInsideTempProvider.notifier)
            .update(temp: update.entry.value.float);
        }
        break;
      case VSSPath.vehicleOutsideTemperature:
        if (update.entry.value.hasFloat()) {
          ref
            .read(vehicleSignalOutsideTempProvider.notifier)
            .update(temp: update.entry.value.float);
        }
        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 HomescreenVssClient(config: config, channel: channel, stub: stub, ref: ref);
});