1 |
|
2 | var d = require("debug")("raptorjs:stream")
|
3 | var Base = require("./Base")
|
4 | var Record = require("./model/Record")
|
5 | var util = require("./util")
|
6 | var Pager = require("./pager")
|
7 |
|
8 | class Stream extends Base {
|
9 |
|
10 | constructor(container) {
|
11 | super(container)
|
12 | }
|
13 |
|
14 | list(stream, paging={}) {
|
15 | d("Fetch data for %s on %s", stream.name, stream.deviceId)
|
16 | const url = this.route("STREAM_PULL", stream.deviceId, stream.name) + Pager.buildQuery(paging, null)
|
17 | return this.getClient().get(url)
|
18 | .then((list) => Promise.resolve(Pager.create(list, (record) => new Record(record, stream))))
|
19 | }
|
20 |
|
21 | lastUpdate(stream) {
|
22 | d("Fetch last update for %s on %s", stream.name, stream.deviceId)
|
23 | return this.getClient().get(this.route("STREAM_LAST_UPDATE", stream.deviceId, stream.name))
|
24 | .then((r) => Promise.resolve(new Record(r, stream)))
|
25 | }
|
26 |
|
27 | push(data, stream) {
|
28 | stream = data.getStream ? data.getStream() : stream
|
29 | if(!stream) {
|
30 | throw new Error("stream must be provided in Record or as second argument")
|
31 | }
|
32 | d("Save data for %s on %s", stream.name, stream.deviceId)
|
33 | return this.getClient().put(this.route("STREAM_PUSH", stream.deviceId, stream.name), data)
|
34 | }
|
35 |
|
36 | search(stream, q, paging = {}) {
|
37 | d("Search data for %s on %s", stream.name, stream.deviceId)
|
38 | const url = this.route("STREAM_SEARCH", stream.deviceId, stream.name) + Pager.buildQuery(paging)
|
39 | return this.getClient().post(url, q)
|
40 | .then((list) => {
|
41 | let contents = list.content.map((r) => new Record(r, stream))
|
42 | return Promise.resolve(contents)
|
43 | })
|
44 | }
|
45 |
|
46 | delete(stream) {
|
47 | d("Remove data for %s on %s", stream.name, stream.deviceId)
|
48 | return this.getClient().delete(this.route("STREAM_LIST", stream.deviceId, stream.name))
|
49 | }
|
50 |
|
51 | subscribe(stream, fn) {
|
52 | return this.getClient().subscribe("stream/" + stream.deviceId + "/" + stream.name, fn)
|
53 | .then(() => Promise.resolve(stream))
|
54 | }
|
55 |
|
56 | unsubscribe(stream, fn) {
|
57 | return this.getClient().unsubscribe("stream/" + stream.deviceId + "/" + stream.name, fn)
|
58 | .then(() => Promise.resolve(stream))
|
59 | }
|
60 |
|
61 | }
|
62 |
|
63 | module.exports = Stream
|