1 |
|
2 | var d = require("debug")("raptorjs:action")
|
3 | var Base = require("./Base")
|
4 |
|
5 | class Action extends Base {
|
6 |
|
7 | constructor(container) {
|
8 | super(container)
|
9 | }
|
10 |
|
11 | set(action, data) {
|
12 | d("Set action %s state", action.name)
|
13 | return this.getClient().request({
|
14 | method: "PUT",
|
15 | url: this.route("ACTION_STATUS", action.deviceId, action.name),
|
16 | headers: {
|
17 | "Content-Type": "text/plain"
|
18 | },
|
19 | body: data.toString(),
|
20 | })
|
21 | }
|
22 |
|
23 | get(action) {
|
24 | d("Get action %s state", action.name)
|
25 | return this.getClient().request({
|
26 | method: "GET",
|
27 | url: this.route("ACTION_STATUS", action.deviceId, action.name),
|
28 | headers: {
|
29 | "Content-Type": "text/plain"
|
30 | }
|
31 | })
|
32 | }
|
33 |
|
34 | invoke(action, data) {
|
35 | d("Invoking %s on %s", action.name, action.deviceId)
|
36 | return this.getClient().request({
|
37 | method: "PUT",
|
38 | url: this.route("ACTION_STATUS", action.deviceId, action.name),
|
39 | headers: {
|
40 | "Content-Type": "text/plain"
|
41 | },
|
42 | body: data.toString(),
|
43 | })
|
44 | return Promise.resolve(true)
|
45 | }
|
46 |
|
47 | subscribe(action, fn) {
|
48 | return this.getClient().subscribe("action/" + action.deviceId + "/" + action.name, fn).then(() => Promise.resolve(action))
|
49 | }
|
50 |
|
51 | unsubscribe(action, fn) {
|
52 | return this.getClient().unsubscribe("action/" + action.deviceId + "/" + action.name, fn).then(() => Promise.resolve(action))
|
53 | }
|
54 |
|
55 | }
|
56 |
|
57 | module.exports = Action
|