1 |
|
2 | const Model = require("./Model")
|
3 | const Channel = require("./Channel")
|
4 | const Record = require("./Record")
|
5 |
|
6 | class Stream extends Model {
|
7 |
|
8 | constructor(json, device) {
|
9 | super(json)
|
10 | this.setDevice(device)
|
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 | dynamic: Boolean,
|
24 | name: String,
|
25 | type: String,
|
26 | channels: {
|
27 | listOf: Channel,
|
28 | required: true,
|
29 | default: {},
|
30 | transform: (channel, name) => {
|
31 |
|
32 | if(typeof channel === "string") {
|
33 | channel = {
|
34 | type: channel
|
35 | }
|
36 | }
|
37 |
|
38 | if(name) {
|
39 | channel.name = name
|
40 | }
|
41 |
|
42 | return channel
|
43 | }
|
44 | }
|
45 | }
|
46 | }
|
47 |
|
48 | getChannel(name) {
|
49 | return this.channels[name] || null
|
50 | }
|
51 |
|
52 | setDevice(device) {
|
53 |
|
54 | this.device = device
|
55 |
|
56 | if(device) {
|
57 | this.userId = device.userId
|
58 | this.deviceId = device.id
|
59 | }
|
60 | else {
|
61 | this.userId = null
|
62 | this.deviceId = null
|
63 | }
|
64 |
|
65 | Object.keys(this.channels).forEach((name) => {
|
66 | this.channels[name].setStream(this.stream)
|
67 | })
|
68 |
|
69 | }
|
70 |
|
71 | getDevice() {
|
72 | return this.device || null
|
73 | }
|
74 |
|
75 | createRecord(data) {
|
76 | return new Record(data, this)
|
77 | }
|
78 | }
|
79 |
|
80 | module.exports = Stream
|