summaryrefslogtreecommitdiff
path: root/web/src/ffw/RevSDLRPC.js
blob: b4c7ff1ba7227a02348421003a2aad27085a2771 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
 * @author: Sergei Polishchuk
 * RPC for reverse SDL functionality
 */

FFW.RevSDL = FFW.RPCObserver.create({
    /*
     *	access to basic RPC functionality
     */
    client:		FFW.RPCClient.create({ componentName: "RevSDLClient" }),

    OnControlChangedProperty: "RevSDL.OnControlChanged",
    OnRadioDetailsProperty: "RevSDL.OnRadioDetails",
    OnPresetsChangedProperty: "RevSDL.OnPresetsChanged",

    OnControlChangedSubscribeRequestId: -1,
    OnRadioDetailsSubscribeRequestId: -1,
    OnPresetsChangedSubscribeRequestId: -1,
    OnControlChangedUnsubscribeRequestId: -1,
    OnRadioDetailsUnsubscribeRequestId: -1,
    OnPresetsChangedUnsubscribeRequestId: -1,

    connect: function() {
        this.client.connect(this);
    },

    disconnect: function() {
        this.OnControlChangedUnsubscribeRequestId = this.unsubscribeFromProperty(this.OnControlChangedProperty);
        this.OnRadioDetailsUnsubscribeRequestId = this.unsubscribeFromProperty(this.OnRadioDetailsProperty);
        this.OnPresetsChangedUnsubscribeRequestId = this.unsubscribeFromProperty(this.OnPresetsChangedProperty);

        this.client.disconnect();
    },

    onRPCRegistered: function () {
        this._super();
        Em.Logger.log("FFW.RevSDLClient.onRPCRegistered");

        this.OnControlChangedSubscribeRequestId = this.subscribeToProperty(this.OnControlChangedProperty);
        this.OnRadioDetailsSubscribeRequestId = this.subscribeToProperty(this.OnRadioDetailsProperty);
        this.OnPresetsChangedSubscribeRequestId = this.subscribeToProperty(this.OnPresetsChangedProperty);
    },

    onRPCUnregistered: function () {
        this._super();
        Em.Logger.log("FFW.RevSDLClient.onRPCUnregistered");
    },

    /*
     * Subscribes to property. Returns the request's id.
     */
    subscribeToProperty: function(property) {
        var msgId = this.client.generateId();
        var JSONMessage = {
            "jsonrpc":	"2.0",
            "id": 		msgId,
            "method":	"MB.subscribeTo",
            "params":	{
                "propertyName": property
            }
        };
        this.client.send(JSONMessage);
        return msgId;
    },

    /*
     * Unsubscribes from property. Returns the request's id.
     */
    unsubscribeFromProperty: function(property) {
        var msgId = this.client.generateId();
        var JSONMessage = {
            "jsonrpc":	"2.0",
            "id": 		msgId,
            "method":	"MB.unsubscribeFrom",
            "params":	{
                "propertyName": property
            }
        };
        this.client.send(JSONMessage);
        return msgId;
    },

    /**
     * when result is received from RPC component this function is called
     * It is the appropriate place to check results of request execution
     * Please use previously store requestID to determine to which request response belongs to
     */
    onRPCResult: function(response) {
        this._super();

        switch (response.id) {
            case this.GrantAccessRequestId:
                MFT.AppController.changeAccessStatus(response.result);
                break;
//            case this.StartScanRequestId:
//                MFT.MediaController.set('isFrequencyScan', response.result.success);
//                break;
//            case this.StopScanRequestId:
//                MFT.MediaController.set('isFrequencyScan', !response.result.success);
//                break;
            case this.GetRadioDetailsRequestId:
                MFT.MediaController.setSDLDirectTuneStation(response.result);
                this.sendShowRequest();
                break;
        }
    },

    onRPCError: function(response) {
        this._super();
    },

    /*
     * handle RPC requests here
     */
    onRPCRequest: function(request) {
        Em.Logger.log("FFW.RevSDLClient.onRPCRequest");
        this._super();
    },

    /*
     * handle RPC notifications here
     * for Backend component full screen setting change should be handled here
     */
    onRPCNotification: function(notification) {
        Em.Logger.log("FFW.RevSDLClient.onRPCNotification");
        this._super();

        switch (notification.method) {
            case this.OnControlChangedProperty:
                MFT.AppController.driverFocus();
                break;
            case this.OnRadioDetailsProperty:
                MFT.MediaController.setSDLDirectTuneStation(notification.params);
                break;
            case this.OnPresetsChangedProperty:
                MFT.MediaController.setSDLPresets(notification.params);
                break;
        }
    },

    /*
     * identifiers for requests
     */
    GrantAccessRequestId: -1,
    CancelAccessRequestId: -1,
    TuneRadioRequestId: -1,
    StartScanRequestId: -1,
    StopScanRequestId: -1,
    GetRadioDetailsRequestId: -1,

    /**
     * Sends a request for access to the management of HMI, through SDL interface
     **/
    sendGrantAccessRequest: function(){
        this.GrantAccessRequestId = this.client.generateId();

        var JSONMessage = {
            "jsonrpc":	"2.0",
            "id": 		this.GrantAccessRequestId,
            "method":	"RevSDL.GrantAccess"
        };
        this.client.send(JSONMessage);
    },

    /**
     * Sends a request for access to the management of HMI, through SDL interface
     **/
    sendCancelAccessRequest: function(){
        this.CancelAccessRequestId = this.client.generateId();

        var JSONMessage = {
            "jsonrpc":	"2.0",
            "id": 		this.CancelAccessRequestId,
            "method":	"RevSDL.CancelAccess"
        };
        this.client.send(JSONMessage);
    },

    sendTuneRadioRequest: function (data) {
        var frequency = data.frequency.split('.');

        this.TuneRadioRequestId = this.client.generateId();

        var JSONMessage = {
            "jsonrpc":	"2.0",
            "id": 		this.TuneRadioRequestId,
            "method":	"RevSDL.TuneRadio",
            params: {
                radioStation: {
                    frequency: Number(frequency[0]),
                    fraction: Number(frequency[1])
                }
            }
        };
        this.client.send(JSONMessage);

    },

    /**
     * Start frequency scan on head unit, through SDL interface
     **/
    sendStartScanRequest: function(){
        this.StartScanRequestId = this.client.generateId();

        var JSONMessage = {
            "jsonrpc":	"2.0",
            "id": 		this.StartScanRequestId,
            "method":	"RevSDL.StartScan"
        };
        this.client.send(JSONMessage);
    },

    /**
     * Stop frequency scan on head unit, through SDL interface
     **/
    sendStopScanRequest: function(){
        this.StopScanRequestId = this.client.generateId();

        var JSONMessage = {
            "jsonrpc":	"2.0",
            "id": 		this.StopScanRequestId,
            "method":	"RevSDL.StopScan"
        };
        this.client.send(JSONMessage);
    },

    /**
     * Stop frequency scan on head unit, through SDL interface
     **/
    sendGetRadioDetailsRequest: function(){
        this.GetRadioDetailsRequestId = this.client.generateId();

        var JSONMessage = {
            "jsonrpc":	"2.0",
            "id": 		this.GetRadioDetailsRequestId,
            "method":	"RevSDL.GetRadioDetails"
        };
        this.client.send(JSONMessage);
    },

    /**
     * Stop frequency scan on head unit, through SDL interface
     **/
    sendShowRequest: function(){
        var JSONMessage = {
            "jsonrpc":	"2.0",
            "method":	"RevSDL.Show"
        };
        this.client.send(JSONMessage);
    }
});