UNPKG

1.63 kBJavaScriptView Raw
1
2const Model = require("./Model")
3const GeoPoint = require("./data/GeoPoint")
4const util = require("../util")
5
6class Result extends Model {
7
8 constructor(json, stream) {
9 super(json)
10 this.setStream(stream)
11 }
12
13 defaultFields() {
14 return {
15 userId: {
16 type: String,
17 required: true
18 },
19 deviceId: {
20 type: String,
21 required: true
22 },
23 streamId: {
24 type: String,
25 required: true
26 },
27 location: {
28 type: GeoPoint,
29 required: true
30 },
31 timestamp: {
32 type: Number,
33 required: true,
34 default: () => util.toUNIX()
35 },
36 channels: { type: Object, required: true }
37 }
38 }
39
40 setStream(stream) {
41
42 this.stream = stream
43 if(stream) {
44 this.streamId = stream.name
45 if(typeof stream.getDevice === "function") {
46 this.userId = stream.getDevice().userId
47 this.deviceId = stream.getDevice().id
48 }
49 }
50 else {
51 this.streamId = null
52 this.userId = null
53 this.deviceId = null
54 }
55 }
56
57 getStream() {
58 return this.stream || null
59 }
60
61 fromJSON(json) {
62 if(!json.channels) {
63 json = {
64 timestamp: util.toUNIX(),
65 channels: Object.assign({}, json)
66 }
67 }
68 super.fromJSON(json)
69 }
70
71}
72
73module.exports = Result