summaryrefslogtreecommitdiffstats
path: root/lib/vehicle-signals/vss_provider.dart
blob: 09ce5a1a6ad9fb9789eb04a262b822bf949ff518 (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_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);
});