-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (83 loc) · 2.72 KB
/
index.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
const express = require("express");
var mqtt = require("mqtt");
var hexToBinary = require("hex-to-binary");
var atob = require("atob");
const cors = require("cors");
var axios = require("axios");
const bodyParser = require("body-parser");
const wakeUpDyno = require("./wokeDyno");
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const API_URL = "https://iot-smart-meter.herokuapp.com/new_recording";
const API_MQTT_URL = "https://iot-smart-meter-mqtt.herokuapp.com/";
const corsWhitelist = [
"http://localhost:8070/",
"http://localhost:8100/",
"http://localhost:3000/",
"http://localhost:8080/",
];
const port = process.env.PORT || 8071;
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
if (corsWhitelist.indexOf(req.headers.origin) !== -1) {
res.header("Access-Control-Allow-Origin", req.headers.origin);
res.header(
"Access-Control-Allow-Headers",
"Origin, Content-Type, X-Requested-With, Accept, Authorization"
);
}
if (req.method === "OPTIONS") {
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, PATCH, DELETE");
res.status(200).json({});
}
next();
});
var client = mqtt.connect("mqtts://influx.itu.dk", {
username: "smartreader",
password: "4mp3r3h0ur",
port: 8883,
rejectUnauthorized: false,
});
client.on("connect", function () {
console.log("CONNECTED");
client.subscribe("IoT2020sec/meters", function (err) {
console.log("SUBSCRIBED");
});
});
client.on("message", function (topic, message) {
const base64Message = message.toString();
const bin = atob(base64Message);
let hexString = "";
for (let i = 0; i < bin.length; i++) {
const hex = bin.charCodeAt(i).toString(16);
hexString += hex.length === 2 ? hex : "0" + hex;
}
console.log("hexString: " + hexString);
const binaryMessage = hexToBinary(hexString);
console.log("binary: " + binaryMessage);
const measurementType = binaryMessage.slice(0, 1);
const meterId = binaryMessage.slice(1, 8);
const timestamp_binary = binaryMessage.slice(8, 40);
const reading = binaryMessage.slice(40, 56);
const timestamp = new Date(parseInt(timestamp_binary, 2) * 1000);
const req = {
meter_id: parseInt(meterId, 2),
reading: parseInt(reading, 2),
timestamp: timestamp,
type: measurementType,
};
axios.post(API_URL, req, { headers: { mqtt_key: "G3.j*8d*~oT8x!w" } }).then(
(response) =>
console.log("Successfully sent data to DB for meter id:" + req.meter_id),
(err) => console.log(err)
);
});
client.on("error", function (error) {
console.log("Can't connect" + error);
});
app.listen(port, () => {
console.log("Listening to 8070...");
wakeUpDyno(API_MQTT_URL);
});