This repository was archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventListener.js
More file actions
108 lines (99 loc) · 4.34 KB
/
Copy pathEventListener.js
File metadata and controls
108 lines (99 loc) · 4.34 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const {
Message,
EventFilter,
EventList,
EventSubscription,
ClientEventsSubscribeRequest,
ClientEventsSubscribeResponse
} = require('sawtooth-sdk/protobuf');
const { TextDecoder } = require('text-encoding/lib/encoding');
const { Stream } = require('sawtooth-sdk/messaging/stream');
var decoder = new TextDecoder('utf8');
const uuid = require('uuid');
const request = require('request');
const { config } = require('./config');
function addDatatoContextBroker(body) {
request.post({
url: config.CONTEXT_BROKER + '/v2/entities',
body: body,
headers: { 'Content-Type': 'application/json', 'fiware-service': 'cattlechain', 'fiware-servicepath': '/CattleChainService' },
json: true
}, (err, response) => {
if (err) {
console.log('error in adding data', err);
}
console.log('data added'. response);
});
}
function getEventsMessage(message) {
let Eventlist = EventList.decode(message.content).events
Eventlist.map(function (event) {
if (event.eventType === 'cattlechain/add-animal') {
console.log("add animal event: ", event.data.toString());
}
if (event.eventType === 'sawtooth/block-commit') {
console.log("new block event : ", event);
}
if (event.eventType === 'cattlechain/add-animal-event') {
let attribut = event.attributes;
let payload = {};
attribut.forEach(element => {
payload['id'] = 'urn:ngsi-ld:Event:' + uuid.v4();
payload['type'] = 'add-animal-event';
if (element.key === 'payload') {
let iotEvent = JSON.parse(element.value);
for (var key in iotEvent) {
payload[key] = {
type: 'String',
value: iotEvent[key]
}
}
} else {
payload[element.key] = {
type: 'String',
value: element.value
}
}
console.log('payload', payload);
addDatatoContextBroker(payload);
});
}
})
}
function checkStatus(response) {
let msg = ""
if (response.status === 0) {
msg = ' S U B S C R I P T I O N : O K'
} if (response.status === 1) {
msg = ' S U B S C R I P T I O N : G O O D '
} else {
msg = ' S U B S C R I P T I O N F A I L E D ! ! ! ! ! '
}
return msg
}
function EventSubscribe(URL) {
let stream = new Stream(URL)
const blockCommitSubscription = EventSubscription.create({
eventType: 'sawtooth/block-commit'
})
const wordLengthSubscription = EventSubscription.create({
eventType: 'cattlechain/add-animal',
})
const wordLengthSubscription1 = EventSubscription.create({
eventType: 'cattlechain/add-animal-event',
})
const subscription_request = ClientEventsSubscribeRequest.encode({
subscriptions: [wordLengthSubscription1, wordLengthSubscription]
}).finish()
stream.connect(() => {
stream.send(Message.MessageType.CLIENT_EVENTS_SUBSCRIBE_REQUEST, subscription_request)
.then(function (response) {
return ClientEventsSubscribeResponse.decode(response)
})
.then(function (decoded_Response) {
console.log(checkStatus(decoded_Response))
})
stream.onReceive(getEventsMessage)
})
}
EventSubscribe(config.VALIDATOR_URL);