|
1 /** |
|
2 * Sensor.js |
|
3 * |
|
4 * Nokia Web Runtime Service API emulation |
|
5 * WRT v1.1 |
|
6 * |
|
7 * Copyright 2009 Nokia Corporation. All rights reserved. |
|
8 */ |
|
9 |
|
10 (function(){ |
|
11 |
|
12 var provider = 'Service.Sensor', |
|
13 Interface = 'ISensor'; |
|
14 var transID = new Array(); |
|
15 /** |
|
16 * Sensor service |
|
17 */ |
|
18 var SensorService = function(){ |
|
19 this.FindSensorChannel = __FindSensorChannel; |
|
20 this.RegisterForNotification = __RegisterForNotification; |
|
21 this.Cancel = __Cancel; |
|
22 this.GetChannelProperty = __GetChannelProperty; |
|
23 } |
|
24 |
|
25 device.implementation.extend(provider, Interface, new SensorService() ); |
|
26 |
|
27 /******************************************************/ |
|
28 /******************************************************/ |
|
29 /******************************************************/ |
|
30 |
|
31 var context = device.implementation.context, |
|
32 _t = context._t, |
|
33 method = '', |
|
34 result = false, |
|
35 DBase = null; |
|
36 |
|
37 |
|
38 /** |
|
39 * Sensor: FindSensorChannel |
|
40 * @param {Object} criteria |
|
41 */ |
|
42 function __FindSensorChannel(criteria){ |
|
43 method = 'FindSensorChannel'; |
|
44 if(!criteria) |
|
45 return error(device.implementation.ERR_MISSING_ARGUMENT, msg.msgCriteriaMissing); |
|
46 |
|
47 if(typeof criteria != 'object') |
|
48 return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgCriteriaMissing); |
|
49 |
|
50 if(typeof criteria.SearchCriterion == 'undefined') |
|
51 return error(device.implementation.ERR_MISSING_ARGUMENT, msg.msgDataMissing); |
|
52 |
|
53 if(typeof criteria.SearchCriterion != 'string') |
|
54 return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgidInvalid); |
|
55 |
|
56 if(!(criteria.SearchCriterion== "All" || criteria.SearchCriterion== "AccelerometerAxis" || criteria.SearchCriterion=="AccelerometerDoubleTapping" || criteria.SearchCriterion=="Orientation" || criteria.SearchCriterion=="Rotation")) |
|
57 return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgSearchParamInvalid); |
|
58 |
|
59 if(arguments.length > 1) |
|
60 return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgCriteriaMissing); |
|
61 |
|
62 DBase = context.getData(provider); |
|
63 var returnValue; |
|
64 returnValue = DBase[criteria.SearchCriterion] || [] ; |
|
65 |
|
66 return context.Result(returnValue,0); |
|
67 } |
|
68 |
|
69 |
|
70 |
|
71 /** |
|
72 * Sensor: RegisterForNotification |
|
73 * @param {Object} criteria, callback |
|
74 */ |
|
75 function __RegisterForNotification(criteria, callback, flag){ |
|
76 flag = flag || false; |
|
77 method = 'RegisterForNotification'; |
|
78 context.notify(_t('%s:: RegisterForNotification not implemented in preview').arg(provider)); |
|
79 |
|
80 if(arguments.length >2 && (typeof flag != "undefined" && typeof flag != "boolean")) |
|
81 return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgCriteriaMissing); |
|
82 |
|
83 if(typeof callback != 'function') |
|
84 return error(device.implementation.ERR_MISSING_ARGUMENT, msg.msgInsufficentArgument); |
|
85 |
|
86 |
|
87 if(!criteria) |
|
88 return error(device.implementation.ERR_MISSING_ARGUMENT, msg.msgIncompleteInput); |
|
89 |
|
90 if(typeof criteria != 'object') |
|
91 return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgIncompleteInput); |
|
92 |
|
93 if(typeof criteria.ListeningType == 'undefined' || typeof criteria.ChannelInfoMap == 'undefined') |
|
94 return error(device.implementation.ERR_MISSING_ARGUMENT, msg.msgIncompleteInput); |
|
95 |
|
96 if(typeof criteria.ListeningType != 'string') |
|
97 return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgListenTypeInvalid); |
|
98 |
|
99 if(typeof criteria.ChannelInfoMap != 'object') |
|
100 return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgChannelInfoMapInvalid); |
|
101 |
|
102 if(!(criteria.ListeningType== "ChannelData" )) |
|
103 return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgOutofRange); |
|
104 |
|
105 if(typeof callback == 'function') |
|
106 { |
|
107 var result = context.callAsync(this, arguments.callee, criteria, callback); |
|
108 transID.push(result.TransactionID); |
|
109 return result; |
|
110 } |
|
111 if(flag) |
|
112 transID.shift(); |
|
113 |
|
114 return context.ErrorResult(); |
|
115 } |
|
116 |
|
117 |
|
118 /** |
|
119 * Sensor: Cancel |
|
120 * @param {Object} criteria |
|
121 */ |
|
122 function __Cancel(criteria){ |
|
123 method = 'Cancel'; |
|
124 |
|
125 if(arguments.length > 1 && typeof criteria != "object" && typeof criteria.TransactionID != "number" && arguments[1]) |
|
126 return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgCriteriaMissing); |
|
127 |
|
128 if (!criteria || typeof criteria.TransactionID == 'undefined') |
|
129 return error(device.implementation.ERR_MISSING_ARGUMENT, msg.msgTransIDMissing); |
|
130 |
|
131 if (criteria.TransactionID == Infinity || criteria.TransactionID == -Infinity) |
|
132 return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgTransIDMissing); |
|
133 |
|
134 if (typeof criteria.TransactionID != 'number') |
|
135 return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgIncorrectTransID); |
|
136 |
|
137 for (var i=0; i<transID.length; i++) { |
|
138 if (criteria.TransactionID == transID[i]){ |
|
139 clearTimeout(criteria.TransactionID); |
|
140 return context.ErrorResult(); |
|
141 } |
|
142 }; |
|
143 |
|
144 return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgInvalidTransID); |
|
145 |
|
146 } |
|
147 |
|
148 |
|
149 /** |
|
150 * Sensor: GetChannelProperty |
|
151 * @param {Object} criteria |
|
152 */ |
|
153 function __GetChannelProperty(criteria){ |
|
154 method = 'GetChannelProperty'; |
|
155 |
|
156 if(!criteria) |
|
157 return error(device.implementation.ERR_MISSING_ARGUMENT, msg.msgIncompleteInput); |
|
158 |
|
159 if(typeof criteria != 'object') |
|
160 return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgIncompleteInput); |
|
161 |
|
162 if(typeof criteria.ChannelInfoMap == 'undefined' || typeof criteria.PropertyId == 'undefined') |
|
163 return error(device.implementation.ERR_MISSING_ARGUMENT, msg.msgIncompleteInput); |
|
164 |
|
165 if(typeof criteria.ChannelInfoMap != 'object') |
|
166 return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgChannelInfoMapInvalid); |
|
167 |
|
168 if(typeof criteria.PropertyId != 'string') |
|
169 return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgInvalidPropertyID); |
|
170 |
|
171 if(criteria.PropertyId != 'Availability' && criteria.PropertyId != "ChannelAccuracy" && criteria.PropertyId != "ChannelDataFormat" && criteria.PropertyId != "ChannelScale" && criteria.PropertyId != "ChannelUnit" && criteria.PropertyId != "ConnectionType" && criteria.PropertyId != "DataRate" && criteria.PropertyId != "Description" && criteria.PropertyId != "MeasureRange" && criteria.PropertyId != "ScaledRange" && criteria.PropertyId != "SensorModel") |
|
172 return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgInvalidPropertyID); |
|
173 |
|
174 if(arguments.length > 1) |
|
175 return error(device.implementation.ERR_INVALID_SERVICE_ARGUMENT, msg.msgCriteriaMissing); |
|
176 |
|
177 DBase = context.getData(provider); |
|
178 var property = DBase['SensorProperty']; |
|
179 if(typeof criteria.ChannelInfoMap['ChannelId'] == 'undefined' || typeof criteria.ChannelInfoMap['ChannelId'] != 'number') |
|
180 return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgChannelInfoMapInvalid); |
|
181 |
|
182 var channel = null; |
|
183 if(criteria.ChannelInfoMap['ChannelId'] == 7) |
|
184 { |
|
185 channel = 'AccelerometerAxis'; |
|
186 } |
|
187 else if(criteria.ChannelInfoMap['ChannelId'] == 8) |
|
188 { |
|
189 channel = 'AccelerometerDoubleTapping'; |
|
190 } |
|
191 else if(criteria.ChannelInfoMap['ChannelId'] == 10) |
|
192 { |
|
193 channel = 'Orientation'; |
|
194 } |
|
195 else if(criteria.ChannelInfoMap['ChannelId'] == 11) |
|
196 { |
|
197 channel = 'Rotation'; |
|
198 } |
|
199 |
|
200 if(channel == null) |
|
201 return error(device.implementation.ERR_BAD_ARGUMENT_TYPE, msg.msgChannelInfoMapInvalid); |
|
202 |
|
203 var returnValue = property[channel][criteria.PropertyId]; |
|
204 if(typeof returnValue == 'undefined') |
|
205 return context.ErrorResult(device.implementation.ERR_NOT_FOUND); |
|
206 return context.Result(returnValue,0) |
|
207 } |
|
208 |
|
209 /** |
|
210 * Sensor: error |
|
211 * @param {number,string} ErrorCode and ErrorString |
|
212 * Replaces Error String with method name |
|
213 */ |
|
214 function error(code, msg /*, args...*/){ |
|
215 |
|
216 var args = ['Sensors',method].concat([].slice.call(arguments,2)); |
|
217 msg = msg ? _t().arg.apply(msg,args) : undefined; |
|
218 return context.ErrorResult(code, msg); |
|
219 } |
|
220 |
|
221 |
|
222 /** |
|
223 * error messages |
|
224 * order of %s args: Service name, method name, parameter name |
|
225 */ |
|
226 var msg = { |
|
227 msgInterfaceNotSupported : '%s:Requested interface not supported by the provider', |
|
228 msgInterfaceMissing : '%s:Interface name missing', |
|
229 msgInsufficentArgument : '%s:%s:Insufficent argument for asynchronous request', |
|
230 msgListenTypeMissing : '%s:%s:Listening type missing', |
|
231 msgListenTypeInvalid : '%s:%s:Listening type is invalid', |
|
232 msgChannelInfoMissing : '%s:%s:ChannelInfoMap missing', |
|
233 msgIncompleteInput : '%s:%s:Incomplete input param list', |
|
234 msgOutofRange : '%s:%s:Listening type is out of allowed range', |
|
235 msgCallbackMissing : '%s:%s:Callback missing', |
|
236 msgAlreadyRegistered : '%s:%s:Notification is already registered on this channel', |
|
237 msgCriteriaMissing : '%s:%s:Search criterion is missing', |
|
238 msgInvalidSearchCriteria : '%s:%s:Invalid Search Criterion', |
|
239 msgChannelSearchInvalid : '%s:%s:Channel search param type invalid', |
|
240 msgSearchParamInvalid : '%s:%s:Invalid channel search param', |
|
241 msgTransIDMissing : '%s:%s:Transaction id is missing', |
|
242 msgIncorrectTransID : '%s:%s:Incorrect TransactionID', |
|
243 msgInvalidTransID : '%s:%s:Invalid TransactionID', |
|
244 msgPropertyIDMissing : '%s:%s:Property id missing', |
|
245 msgInvalidPropertyID : '%s:%s:Property id is invalid', |
|
246 msgChannelNotSupported : '%s:%s:Channel property not supported', |
|
247 msgChannelInfoMapInvalid : '%s:%s:ChannelInfoMap Type Invalid' |
|
248 }; |
|
249 |
|
250 }) () |