UNPKG

5.22 kBHTMLView Raw
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <title>JSDoc: Source: lib/model/RecordSet.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/RecordSet.js</h1>
21
22
23
24
25
26
27 <section>
28 <article>
29 <pre class="prettyprint source linenums"><code>var util = require('../util');
30var _ = require('lodash');
31
32/**
33 * Contains a record of a stream
34 * @constructor
35 * @augments Container
36 * @param {Object} raw raw data object
37 * @param {Stream} stream stream reference
38 */
39var RecordSet = function (raw, stream) {
40
41 this.data = {
42 channels: {},
43 timestamp: null,
44 userId: null,
45 objectId: null,
46 streamId: null
47 };
48
49 this.setStream(stream);
50 this.exportProperties();
51
52 if(raw) this.fromJSON(raw);
53};
54util.extends(RecordSet, 'Container');
55/**
56 * @inheritdoc
57 */
58RecordSet.prototype.fromJSON = function (raw) {
59 if(raw.channels) {
60 this.setChannels(raw.channels);
61 if(raw.timestamp) {
62 this.setTimestamp(raw.timestamp);
63 }
64 } else {
65 if(typeof raw === 'object') {
66 this.setChannels(raw);
67 }
68 }
69 this.validate();
70};
71/**
72 * @inheritdoc
73 */
74RecordSet.prototype.toJSON = function () {
75
76 var me = this;
77 var json = {
78 channels: {},
79 timestamp: util.toUNIX(this.timestamp || new Date())
80 };
81
82 this.getChannelList().forEach(function (name) {
83 json.channels[name] = json.channels[name] || {};
84 json.channels[name] = me.data.channels[name];
85 });
86
87 return json;
88};
89/**
90 * @inheritdoc
91 */
92RecordSet.prototype.validate = function () {
93
94 var me = this;
95
96 if(!this.stream) return;
97
98 _.forEach(this.stream.channels, function (channel) {
99
100 var val = me.channels[channel.name];
101 if(val === null || val === undefined) return;
102
103 var type = channel.dataTypes.get(channel.type);
104 if(!type) {
105 throw new Error("Unsupported data type: " + channel.type);
106 }
107
108 if(!type.validate(val)) {
109 throw new Error("Data type validation failed for " + channel.name);
110 }
111
112 });
113
114};
115
116/**
117 * Set a stream reference
118 * @param {Stream} stream a stream object reference
119 */
120RecordSet.prototype.setStream = function (s) {
121 this.stream = s;
122 if(s &amp;&amp; s.getServiceObject) {
123 this.setServiceObject(s.getServiceObject());
124 }
125};
126
127/**
128 * Get a stream reference
129 * @return {Stream} stream the stream object reference
130 */
131RecordSet.prototype.stream = RecordSet.prototype.getStream = function () {
132 return this.stream;
133};
134
135/**
136 * Get the list of known channels
137 * @param {Array} channels labels of channels
138 */
139RecordSet.prototype.getChannelList = function () {
140 if(this.getStream()) {
141 return Object.keys(this.getStream().channels);
142 }
143 return Object.keys(this.channels);
144};
145
146/**
147 * Get channel value
148 * @param {Mixed} value channel value
149 */
150RecordSet.prototype.getChannel = function (name) {
151 return this.channels[name];
152};
153
154/**
155 * Set channels and values
156 * @param {Mixed} channels key/value map of channels &amp; values
157 */
158RecordSet.prototype.setChannels = function (channels) {
159 var me = this;
160 var channelsList = this.getChannelList();
161 channelsList.forEach(function (channelName) {
162 if(channels[channelName] === undefined) return;
163
164 // use legacy format with current-value
165 if(channels[channelName]['current-value'] !== undefined) {
166 me.channels[channelName] = channels[channelName]['current-value']
167 return;
168 }
169
170 me.channels[channelName] = channels[channelName]
171 });
172};
173
174/**
175 * Set the timestamp reference of this record
176 * @param {Mixed} timestamp a parsable time reference. Can be a timestamp of millis/seconds or Date
177 */
178RecordSet.prototype.setTimestamp = function (timestamp) {
179 this.data.timestamp = util.parseDate(timestamp);
180};
181
182/**
183 * Get the timestamp reference of this record
184 * @param {Date} timestamp the Date reference related to this record
185 */
186RecordSet.prototype.getTimestamp = function () {
187 return this.timestamp;
188};
189
190module.exports = RecordSet;
191</code></pre>
192 </article>
193 </section>
194
195
196
197
198</div>
199
200<nav>
201 <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>
202</nav>
203
204<br class="clear">
205
206<footer>
207 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)
208</footer>
209
210<script> prettyPrint(); </script>
211<script src="scripts/linenumber.js"> </script>
212</body>
213</html>