UNPKG

9.29 kBHTMLView Raw
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <title>JSDoc: Source: lib/model/Stream.js</title>
6
7 <script src="scripts/prettify/prettify.js"> </script>
8 <script src="scripts/prettify/lang-css.js"> </script>
9 <!--[if lt IE 9]>
10 <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11 <![endif]-->
12 <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
13 <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
14</head>
15
16<body>
17
18<div id="main">
19
20 <h1 class="page-title">Source: lib/model/Stream.js</h1>
21
22
23
24
25
26
27 <section>
28 <article>
29 <pre class="prettyprint source linenums"><code>var _ = require('lodash');
30
31var util = require('../util');
32
33var RecordSet = require('./RecordSet');
34var ResultSet = require('./ResultSet');
35var Channel = require('./Channel');
36
37/**
38 * A Stream object
39 * @constructor
40 * @augments Container
41 * @param {Object} obj An object with the Stream properties
42 */
43var Stream = function (data, serviceObject) {
44
45 this.data = {
46 name: null,
47 description: null,
48 type: null,
49 channels: {},
50 };
51
52 if(!serviceObject) {
53 throw new Error("Missing serviceObject parameter on Stream creation");
54 }
55
56 this.setServiceObject(serviceObject);
57 this.client = this.getContainer().client;
58
59 this.exportProperties();
60
61 if(data) this.parseJSON(data);
62};
63util.extends(Stream, 'Container');
64
65Stream.prototype.validate = function () {
66
67 if(!this.name)
68 throw new Error("Stream name is required");
69
70 if(!this.channels || !_.size(this.channels))
71 throw new Error("At least one stream channel is required");
72
73 _.forEach(this.channels, function (channel) {
74 channel.validate();
75 });
76
77};
78
79/**
80 * @inheritdoc
81 */
82Stream.prototype.toJSON = function () {
83 var json = this.__super__.toJSON.call(this);
84 json.channels = {};
85 _.forEach(this.channels, function (channel) {
86 json.channels[channel.name] = channel.toJSON();
87 });
88
89 return json;
90};
91
92/**
93 * @inheritdoc
94 */
95Stream.prototype.parseJSON = function (data) {
96
97 var me = this;
98
99 var parseChannels = function (channelsData) {
100 _.forEach(channelsData, function (raw, channelName) {
101
102 if(typeof raw === 'string') {
103 raw = {
104 name: channelName,
105 type: raw
106 };
107 }
108
109 if(!raw.name &amp;&amp; typeof channelName === "string")
110 raw.name = channelName;
111
112 var channel = new Channel(raw, me);
113 me.channels[channel.name] = channel;
114 });
115 };
116
117 if(data.name)
118 this.data.name = data.name;
119
120 if(data.description)
121 this.data.description = data.description;
122
123 if(data.type)
124 this.data.type = data.type;
125
126 if(data.channels) {
127 parseChannels(data.channels);
128 }
129
130};
131
132/**
133 * Create a pubsub subscription for the stream
134 * @return {Promise} A promise for the subscription object creation
135 */
136Stream.prototype.subscribe = function (fn) {
137 var topic = this.getServiceObject().id + '/streams/' + this.data.name + '/updates';
138 return this.client.subscribe(topic, fn).bind(this);
139};
140
141/**
142 * Remove a subscription for a stream
143 *
144 * @param {Function} fn Callback to be called when data is received
145 * @return {Stream} The current stream
146 */
147Stream.prototype.unsubscribe = function (fn) {
148 var topic = this.serviceObject.id + '/streams/' + this.data.name + '/updates';
149 return this.client.unsubscribe(topic, fn).bind(this);
150};
151
152/**
153 * Send data to a ServiceObject stream
154 *
155 * @return {Promise} Promise callback with result
156 */
157Stream.prototype.push = function (data, timestamp) {
158
159 var url = '/' + this.getServiceObject().id + '/streams/' + this.name;
160
161 var record = new RecordSet(data, this);
162 if(timestamp) record.setTimestamp(timestamp);
163
164 return this.client.put(url, record);
165};
166
167/**
168 * Retieve data from a ServiceObject stream
169 *
170 * @param {int} size optional, the number of elements to return
171 * @param {int} from optional, the first value to get from the list for paging
172 *
173 * @return {Promise} Promise callback with result
174 */
175Stream.prototype.pull = function (size, offset) {
176 var qs = util.createQueryString(size, offset);
177 var url = '/' + this.getServiceObject().id + '/streams/' + this.name + '/list' + qs;
178 return this.client.get(url, null).bind(this).then(function (res) {
179 return Promise.resolve(new ResultSet((res instanceof Array) ? res : [res], this));
180 });
181};
182
183/**
184 * Retieve last updated data from a ServiceObject stream
185 *
186 * @return {Promise} Promise callback with result
187 */
188Stream.prototype.lastUpdate = function () {
189 var url = '/' + this.getServiceObject().id + '/streams/' + this.name;
190 return this.client.get(url).bind(this).then(function (res) {
191 return Promise.resolve(new RecordSet(res, this));
192 });
193};
194
195/**
196 * Search data of a ServiceObject stream
197 *
198 * @param {Object} params search params
199 * @param {int} size optional, the number of elements to return
200 * @param {int} offset optional, the first value to get from the list for paging
201 *
202 * @return {Promise} Promise callback with result
203 */
204Stream.prototype.search = function (params, size, offset) {
205
206 if(!params) {
207 return Promise.reject(new Error("No params provided for search"));
208 }
209
210 var qs = util.createQueryString(size, offset);
211
212 var url = '/' + this.getServiceObject().id + '/streams/' + this.name + '/search' + qs;
213 var query = require('./search/parser').parse(params, this);
214
215 return this.getClient().post(url, query).bind(this).then(function (res) {
216 return Promise.resolve(new ResultSet(res, this));
217 });
218};
219
220/**
221 * Search data of a ServiceObject by distance from a point
222 *
223 * @param {Object} position An object representing a geo-position, eg `{ latitude: 123 , longitude: 321 }`
224 * @param {Number} distance The distance value
225 * @param {String} unit Optional unit, default to `km`
226 *
227 * @return {Promise} Promise callback with result
228 */
229Stream.prototype.searchByDistance = function (position, distance, unit) {
230 return this.search({
231 distance: {
232 position: position,
233 value: distance,
234 unit: unit
235 }
236 });
237};
238
239/**
240 * Search data of a ServiceObject in a Bounding Box
241 *
242 * @param {Array} bbox An array of 4 elements representing the bounding box, eg
243 * ```
244 * [
245 * upperLat, upperLng,
246 * bottomLat, bottomLng
247 * ]
248 * ```
249 * or an Array with 2 elements each one as an object eg
250 * ```
251 * [
252 * { latitude: 123, longitude: 321 }, // upper
253 * { latitude: 321, longitude: 123 } // bottom
254 * ]
255 * ```
256 *
257 * @return {Promise} Promise callback with result
258 */
259Stream.prototype.searchByBoundingBox = function (bbox) {
260 return this.search({
261 bbox: {
262 coords: bbox
263 }
264 });
265};
266
267/**
268 * Search text for a channel of a ServiceObject stream
269 *
270 * @param {String} channel The channel name where to search in
271 * @param {Number} string The string query to search for
272 *
273 * @return {Promise} Promise callback with result
274 */
275Stream.prototype.searchByText = function (channel, string) {
276 return this.search({
277 match: {
278 string: string,
279 channel: channel
280 }
281 });
282};
283
284/**
285 * Search data by the update time range of a ServiceObject stream
286 *
287 * @param {Object} params An object with at least one of `from` or `to` properties
288 *
289 * @return {Promise} Promise callback with result
290 */
291Stream.prototype.searchByTime = function (params) {
292 if(!(typeof params == "object" &amp;&amp; (params.from || params.to))) {
293 params = {
294 from: util.parseDate(arguments[0]),
295 to: arguments[1] ? util.parseDate(arguments[1]) : null
296 };
297 }
298 return this.search({
299 time: params
300 });
301};
302
303/**
304 * Search data by a numeric value of a ServiceObject stream
305 *
306 * @param {String} channel Channel name to search for
307 * @param {Object} params An object with at least one of `from` or `to` properties
308 *
309 * @return {Promise} Promise callback with result
310 */
311Stream.prototype.searchByNumber = function (channel, params) {
312 if(typeof params !== 'object') {
313 params = {
314 from: arguments[1],
315 to: arguments[2]
316 };
317 }
318 params.channel = channel;
319 return this.search({
320 numeric: params
321 });
322};
323
324module.exports = Stream;
325</code></pre>
326 </article>
327 </section>
328
329
330
331
332</div>
333
334<nav>
335 <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Action.html">Action</a></li><li><a href="Channel.html">Channel</a></li><li><a href="Client.html">Client</a></li><li><a href="Raptor.html">Raptor</a></li><li><a href="RecordSet.html">RecordSet</a></li><li><a href="ResultSet.html">ResultSet</a></li><li><a href="ServiceObject.html">ServiceObject</a></li><li><a href="Stream.html">Stream</a></li><li><a href="User.html">User</a></li></ul><h3>Global</h3><ul><li><a href="global.html#Container">Container</a></li></ul>
336</nav>
337
338<br class="clear">
339
340<footer>
341 Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.2</a> on Wed Nov 02 2016 11:15:06 GMT+0100 (CET)
342</footer>
343
344<script> prettyPrint(); </script>
345<script src="scripts/linenumber.js"> </script>
346</body>
347</html>