aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/kuksa.js
blob: 9e9cc22a9188f3859c9ea7c3288048cd62aa7427 (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
117
118
119
120
121
122
123
/*
 * Copyright 2022 Igalia, S.L.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { v4 as uuidv4 } from 'uuid';

const DEFAULT_TARGET = "wss://localhost:8090";

// TODO: use an application token when needed
// currently using https://github.com/eclipse/kuksa.val/blob/master/kuksa_certificates/jwt/super-admin.json.token
const TEST_TOKEN = 
    'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJrdWtzYS52YWwiLCJpc3MiOiJFY2xpcHNlIEtVS1NBIERldiIsImFkbWluIjp0cnVlLCJtb2RpZnlUcmVlIjp0cnVlLCJpYXQiOjE1MTYyMzkwMjIsImV4cCI6MTc2NzIyNTU5OSwia3Vrc2EtdnNzIjp7IioiOiJydyJ9fQ.p2cnFGH16QoQ14l6ljPVKggFXZKmD-vrw8G6Vs6DvAokjsUG8FHh-F53cMsE-GDjyZH_1_CrlDCnbGlqjsFbgAylqA7IAJWp9_N6dL5p8DHZTwlZ4IV8L1CtCALs7XVqvcQKHCCzB63Y8PgVDCAqpQSRb79JPVD4pZwkBKpOknfEY5y9wfbswZiRKdgz7o61_oFnd-yywpse-23HD6v0htThVF1SuGL1PuvGJ8p334nt9bpkZO3gaTh1xVD_uJMwHzbuBCF33_f-I5QMZO6bVooXqGfe1zvl3nDrPEjq1aPulvtP8RgREYEqE6b2hB8jouTiC_WpE3qrdMw9sfWGFbm04qC-2Zjoa1yYSXoxmYd0SnliSYHAad9aXoEmFENezQV-of7sc-NX1-2nAXRAEhaqh0IRuJwB4_sG7SvQmnanwkz-sBYxKqkoFpOsZ6hblgPDOPYY2NAsZlYkjvAL2mpiInrsmY_GzGsfwPeAx31iozImX75rao8rm-XucAmCIkRlpBz6MYKCjQgyRz3UtZCJ2DYF4lKqTjphEAgclbYZ7KiCuTn9HualwtEmVzHHFneHMKl7KnRQk-9wjgiyQ5nlsVpCCblg6JKr9of4utuPO3cBvbjhB4_ueQ40cpWVOICcOLS7_w0i3pCq1ZKDEMrYDJfz87r2sU9kw1zeFQk';

var kuksa_context = {
    authToken: TEST_TOKEN,
    socket: undefined,
    target: DEFAULT_TARGET,
}

var pathToHandler = null;

function logReceivedMessage(data) {
    console.log('Received message:', data)
}

function logConnectionStatus(status, event) {
    console.log('Connection status:', status);
}

function kuksaws_onopen(event) {
    logConnectionStatus('Connected.', event);

    authorize();
    subscribe(PATHS.leftAirDistribution);
    subscribe(PATHS.leftFanSpeed);
    subscribe(PATHS.leftSeatWarmer);
    subscribe(PATHS.rightSeatWarmer);
    subscribe(PATHS.leftSeatTemperature);
    subscribe(PATHS.rightSeatTemperature);
    subscribe(PATHS.airConditioning);
    subscribe(PATHS.recirculation);
    subscribe(PATHS.frontDefroster);
    subscribe(PATHS.rearDefroster);
}

function kuksaws_onerror(event) {
    logConnectionStatus('Failed to connect:', event);
}

function kuksaws_onmessage(event) {
    logReceivedMessage(event.data);

    var jsonData = JSON.parse(event.data);

    if (jsonData.action == 'get' ||
        jsonData.action =='subscription') {
        updateVehicleInfo(jsonData);
    }
}

function updateVehicleInfo(message) {
    var value = message.data.dp.value;
    var path = message.data.path;

    if (!pathToHandler.has(path)) {
        console.log('Handler not found for', path);
        return;
    }

    var handler = pathToHandler.get(path);
    handler.update(path, value)
}

function send(action, values) {
    var uuid = uuidv4();
    var data = Object.assign({
        action: action,
        tokens: kuksa_context.authToken,
        requestId: uuid,
    }, values);
    var message = JSON.stringify(data);
    console.log('Sent message:', message);
    kuksa_context.socket.send(message);
    return uuid;
}

function subscribe(path) {
    return send('subscribe', { path: path });
}

function authorize() {
    return send('authorize');
}

export function get(path) {
    return send('get', { path: path });
}

export function set(path, value) {
    return send('set', { path: path, value: value });
}

export function init(handlers) {
    pathToHandler = new Map(handlers);
    if (kuksa_context.socket == undefined) {
        kuksa_context.socket = new WebSocket(kuksa_context.target);
        kuksa_context.socket.onopen = kuksaws_onopen;
        kuksa_context.socket.onerror = kuksaws_onerror;
        kuksa_context.socket.onmessage = kuksaws_onmessage;
    }
}