-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
145 lines (106 loc) · 4.5 KB
/
server.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const path = require('path');
const EventHubReader = require('./scripts/event-hub-reader.js');
const iotHubConnectionString = "HostName=modbus-web-iot-hub.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=JHSImIa3wXsTRGSh9gg+URLhWRLR/HBhU9xebSVcSHo=";
if (!iotHubConnectionString) {
console.error(`Environment variable IotHubConnectionString must be specified.`);
}
console.log(`Using IoT Hub connection string [${iotHubConnectionString}]`);
const eventHubConsumerGroup = "consumergroup2";
console.log(eventHubConsumerGroup);
if (!eventHubConsumerGroup) {
console.error(`Environment variable EventHubConsumerGroup must be specified.`);
}
console.log(`Using event hub consumer group [${eventHubConsumerGroup}]`);
// Redirect requests to the public subdirectory to the root
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use((req, res /* , next */) => {
res.redirect('/');
});
let server = http.createServer(app);
let wss = new WebSocket.Server({ server });
const eventHubReader = new EventHubReader(iotHubConnectionString, eventHubConsumerGroup);
(async () => {
await eventHubReader.startReadMessage((message, date, deviceId) => {
try {
const payload = {
Type : message[0].type,
IotData: message,
MessageDate: date || Date.now().toISOString(),
DeviceId: deviceId,
};
wss.broadcast(JSON.stringify(payload));
} catch (err) {
console.error('Error broadcasting: [%s] from [%s].', err, message);
}
});
})().catch();
wss.on('connection', ws => {
ws.on('message', message => {
console.log("Client Message : " + message)
if ( JSON.parse(message).method == 'changeDeviceIp' ) {
var Client2 = require('azure-iothub').Client;
var Message2 = require('azure-iot-common').Message;
var connectionString = "HostName=modbus-web-iot-hub.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=JHSImIa3wXsTRGSh9gg+URLhWRLR/HBhU9xebSVcSHo=";
var targetDevice = 'myPi';
var serviceClient2 = Client2.fromConnectionString(connectionString);
serviceClient2.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Service client connected');
let deviceIpComesFromClient = String(JSON.parse(message).deviceIp)
var message2 = new Message2("changeDeviceIp" + "," + deviceIpComesFromClient)
console.log('Sending message: ' + message2.getData());
serviceClient2.send(targetDevice, message2);
console.log("Buraya geldi writeRegister")
}
});
console.log("Buraya geldi changeDeviceIp")
}
if (JSON.parse(message).method == 'writeRegister' ) {
console.log("WRITE REGISTER METHOD WORKING")
console.log("Coming data is" + JSON.parse(message).sendRegisterId, JSON.parse(message).sendRegisterValue)
sendRegisterId = String(JSON.parse(message).sendRegisterId)
sendRegisterValue = String(JSON.parse(message).sendRegisterValue)
var Client = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;
var connectionString = "HostName=modbus-web-iot-hub.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=JHSImIa3wXsTRGSh9gg+URLhWRLR/HBhU9xebSVcSHo=";
var targetDevice = 'myPi';
var serviceClient = Client.fromConnectionString(connectionString);
serviceClient.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Service client connected');
var message = new Message("changeRegisterValue"+","+sendRegisterId+"," +sendRegisterValue);
console.log('Sending message: ' + message.getData());
serviceClient.send(targetDevice, message);
console.log("Buraya geldi writeRegister")
}
});
}
// if(JSON.parse(message).method == 'stopCommunication') {
// console.log("Stop Methodu")
// // eventHubReader.stopReadMessage()
// }
})
})
wss.broadcast = (data) => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
try {
//console.log(`Broadcasting data ${data}`);
client.send(data);
} catch (e) {
console.error(e);
}
}
});
};
server.listen(process.env.PORT || '3030', () => {
console.log('Listening on %d.', server.address().port);
});