aboutsummaryrefslogtreecommitdiffstats
path: root/lib/vehicle-signals/viss_methods.dart
blob: 8adcc806e821b61094f0f71c5f3543ad78bd80a6 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// SPDX-License-Identifier: Apache-2.0
import 'dart:convert';
import 'dart:io';

import 'package:flutter_homescreen/vehicle-signals/vss_providers.dart';
import 'package:flutter_homescreen/vehicle-signals/vss_path.dart';
import 'package:flutter_homescreen/config.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class VISS {
  static const requestId = "test-id";

  static void init(WebSocket socket, WidgetRef ref) {
    authorize(socket, ref);
    subscribe(socket, ref, VSSPath.vehicleMediaVolume);
  }

  static void update(WebSocket socket, WidgetRef ref) {
    get(socket, ref, VSSPath.vehicleMediaVolume);
  }

  static void authorize(WebSocket socket, WidgetRef ref) {
    final config = ref.read(ConfigStateprovider);

    Map<String, dynamic> map = {
      "action": "authorize",
      "tokens": config.kuksaAuthToken,
      "requestId": requestId
    };
    socket.add(jsonEncode(map));
  }

  static void get(WebSocket socket, WidgetRef ref, String path) {
    final config = ref.read(ConfigStateprovider);

    Map<String, dynamic> map = {
      "action": "get",
      "tokens": config.kuksaAuthToken,
      "path": path,
      "requestId": requestId
    };
    socket.add(jsonEncode(map));
  }

  static void set(WebSocket socket, WidgetRef ref, String path, String value) {
    final config = ref.read(ConfigStateprovider);
    Map<String, dynamic> map = {
      "action": "set",
      "tokens": config.kuksaAuthToken,
      "path": path,
      "requestId": requestId,
      "value": value
    };
    socket.add(jsonEncode(map));
  }

  static void subscribe(WebSocket socket, WidgetRef ref, String path) {
    final config = ref.read(ConfigStateprovider);

    Map<String, dynamic> map = {
      "action": "subscribe",
      "tokens": config.kuksaAuthToken,
      "path": path,
      "requestId": requestId
    };
    socket.add(jsonEncode(map));
  }

  static void parseData(WidgetRef ref, String data) {
    Map<String, dynamic> dataMap = jsonDecode(data);
    if (dataMap["action"] == "subscription" || dataMap["action"] == "get") {
      if (dataMap.containsKey("data")) {
        if ((dataMap["data"] as Map<String, dynamic>).containsKey("dp") &&
            (dataMap["data"] as Map<String, dynamic>).containsKey("path")) {
          String path = dataMap["data"]["path"];
          Map<String, dynamic> dp = dataMap["data"]["dp"];
          if (dp.containsKey("value")) {
            if (dp["value"] != "---") {
              switch (path) {
                case VSSPath.vehicleMediaVolume:
                  ref
                      .read(vehicleSignalMediaVolumeProvider.notifier)
                      .update(volume: dp["value"]);
                  break;
                case VSSPath.vehicleInsideTemperature:
                  ref
                      .read(vehicleSignalInsideTempProvider.notifier)
                      .update(temp: dp["value"]);
                  break;
                case VSSPath.vehicleOutsideTemperature:
                  ref
                      .read(vehicleSignalOutsideTempProvider.notifier)
                      .update(temp: dp["value"]);
                  break;
                default:
                  break;
              }
            } else {
              print("ERROR: Invalid VIS response, data not available");
            }
          } else {
            print("ERROR: Invalid VIS response, no 'value' key");
          }
        } else if ((!dataMap["data"] as Map<String, dynamic>)
            .containsKey("path")) {
          print("ERROR: Invalid VIS response, no 'path' key");
        } else if ((dataMap["data"] as Map<String, dynamic>)
            .containsKey("dp")) {
          print("ERROR: Invalid VIS response, no 'dp' key");
        }
      } else {
        print("ERROR: Invalid VIS response, no 'data' key");
      }
    }
  }
}