cb0cfd5252deee7a38dd2d308a0053ca2e5e90df
[profile/ivi/automotive-message-broker.git] / plugins / websocketsink / test / events.js
1 /*
2  * Copyright (c) 2012, Intel Corporation.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 /* ---------------------- vehicle event typedef --------------------------- */
11
12 function VehicleEventType()
13 {
14     this.event = [
15 "Randomize",
16 "AirConditioning",
17 "AirRecirculation",
18 "AirflowDirection",
19 "AvgKW",
20 "BatteryStatus",
21 "ChildLock",
22 "Defrost",
23 "ExteriorBrightness",
24 "ExteriorTemperature",
25 "FanSpeed",
26 "FrontWheelRadius",
27 "FullBatteryRange",
28 "InteriorTemperature",
29 "LightHazard",
30 "LightHead",
31 "LightParking",
32 "NightMode",
33 "Odometer",
34 "SeatHeater",
35 "TargetTemperature",
36 "TransmissionShiftPosition",
37 "VehicleSpeed",
38 "Weather"
39     ];
40     this.value = [];
41
42     /* set random initial values for all the props */
43     for(i in this.event)
44     {
45         var prop = this.event[i];
46         this.value[prop] = Math.floor(Math.random() * 1000000);
47     }
48 }
49
50 VehicleEventType.prototype.getSupportedEventList = function(val)
51 {
52     /* for undefined just assume everything */
53     if((val == undefined)||(val === ""))
54         return this.event;
55
56     /* grab every event with case insensitive prefix of val */
57     var value = val.toLowerCase();
58     var list = [];
59     for(i in this.event)
60     {
61         var prop = this.event[i].toLowerCase();
62         if(prop.indexOf(value) === 0)
63         {
64             list[list.length] = prop;
65         }
66     }
67
68     /* if the target val isn't alone, remove it, it's a grouping */
69     var idx = list.indexOf(value);
70     if((idx >= 0)&&(list.length > 1))
71     {
72         list.splice(idx, 1);
73     }
74     return list;
75 }
76
77 VehicleEventType.prototype.getValueEventList = function(val)
78 {
79     var i, j, list = this.getSupportedEventList(val);
80     for(i = 0; i < list.length; i++)
81     {
82         for(j = i + 1; j < list.length; j++)
83         {
84             if(list[j].indexOf(list[i]) === 0)
85             {
86                 list.splice(i, 1);
87                 i--;
88             }
89         }
90     }
91     return list;
92 }
93
94 VehicleEventType.prototype.getValuesEventList = function(vals)
95 {
96     var i, j, list = [];
97     for(i = 0; i < vals.length; i++)
98     {
99         var sublist = this.getValueEventList(vals[i]);
100         for(j = 0; j < sublist.length; j++)
101         {
102             if(list.indexOf(sublist[j]) < 0)
103             {
104                 list[list.length] = sublist[j];
105             }
106         }
107     }
108     return list;
109 }
110
111 VehicleEventType.prototype.isValueEvent = function(val)
112 {
113     var list = this.getValueEventList(val);
114     return(list.length === 1);
115 }
116
117 VehicleEventType.prototype.getValue = function(prop)
118 {
119     return this.value[prop];
120 }
121
122 VehicleEventType.prototype.isValid = function(prop)
123 {
124     return (this.event.indexOf(prop) >= 0);
125 }
126
127 VehicleEventType.prototype.setValue = function(prop, newval)
128 {
129     this.value[prop] = newval;
130 }