-
Notifications
You must be signed in to change notification settings - Fork 29
/
script.js
86 lines (74 loc) · 3.19 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// output refers to the generated template folder
// You require the generated server. Running this code starts the server
// App exposes API to send messages
const { client } = require('../output'); // library is in the current directory. Modify require path if needed
client.init(); // starts the app
// Generated handlers that we use to react on consumer / produced messages are attached to the client
// through which we can register middleware functions
/**
*
*
* Example of how to process a message before it is sent to the broker
*
* Modify the following in the function \`testPublish\` according to your usecase
* 1. channel / topic name
* 2. tool used to publish message according to the procotol used (amqp/mqtt/etc ...)
* 3. name of the invocated middleware function (based on operation Id)
*/
function testPublish() {
// mosquitto_sub -h test.mosquitto.org -p 1883 -t "smartylighting/streetlights/1/0/action/12/turn/on"
// Registering your custom logic in a channel-specific handler
// the passed handler function is called once the app sends a message to the channel
// For example \`client.app.send\` sends a message to some channel using and before it is sent, you want to perform some other actions
// in such a case, you can register middlewares like below
client.registerTurnOnMiddleware((message) => { // \`turnOn\` is the respective operationId
console.log('hitting the middleware before publishing the message');
console.log('sending turn on message to streetlight test', message.payload);
});
client.app.send(
{ command: 'off' },
{},
'smartylighting/streetlights/1/0/action/12/turn/on'
);
}
/**
*
*
* Example of how to work with generated code as a consumer
*
* Modify the following in the function \`testSubscribe\` according to your usecase
* 1. channel / topic name
* 2. tool used to publish message according to the procotol used (amqp/mqtt/etc ...)
* 3. name of the invocated middleware function (based on operation Id)
*
*/
function testSubscribe() {
// mosquitto_pub -h test.mosquitto.org -p 1883 -t "smartylighting/streetlights/1/0/event/101/lighting/measured" -m '{"lumens": 10}'
// Writing your custom logic that should be triggered when your app receives as message from a given channel
// Registering your custom logic in a channel-specific handler
// the passed handler functions are called once the app gets message sent to the channel
client.registerReceiveLightMeasurementMiddleware((message) => { // \`recieveLightMeasurement\` is the respective operationId
console.log('recieved in middleware 1', message.payload);
});
client.registerReceiveLightMeasurementMiddleware((message) => {
console.log('recieved in middleware 2', message.payload);
});
}
testPublish();
testSubscribe();
/**
*
*
* Example of how to produce a message using API of generated app independently from the handlers
* Again, will have to modify the below according to your usecase:
* 1. payload
* 2. channel / topic name
*
*/
(function myLoop (i) {
setTimeout(() => {
console.log('producing custom message');
client.app.send({percentage: 1}, {}, 'smartylighting/streetlights/1/0/action/1/turn/on');
if (--i) myLoop(i);
}, 1000);
}(3));