Skip to content

Commit

Permalink
feedback circulation done
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhiljainjain committed Oct 16, 2019
1 parent 610208e commit b5c3534
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 27 deletions.
24 changes: 24 additions & 0 deletions app/Feedback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Feedback{

constructor(){
this.data = {
smoke: {
value: 685,
trust: 10
},

dht: {
temperature: 14,
humidity: 65,
trust: 10
}
};
}

replaceData(data){
console.log('New feedback data updated');
this.data = data;
}
}

module.exports = Feedback;
36 changes: 14 additions & 22 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,11 @@ let logger = require('morgan');

const P2pServer = require('./p2p-server');
const Blockchain = require('../blockchain');
const Feedback = require("./Feedback");

let bc = new Blockchain();
let p2pServer = new P2pServer(bc);
let data = {
smoke: {
value: 685,
trust: 10
},

dht: {
temperature: 14,
humidity: 65,
trust: 10
}
};
let fb = new Feedback();
let p2pServer = new P2pServer(bc, fb);

const HTTP_PORT = process.env.PORT || 3001; //http port

Expand Down Expand Up @@ -55,9 +45,9 @@ app.get("/query", (req, res)=>{
console.log(req.query);
let msg = null;
if (req.query.location === "x"){
msg = ( data.dht.temperature > 30) ? "OK" : "NOT ADVISED";
msg = ( fb.data.dht.temperature > 30) ? "OK" : "NOT ADVISED";
}else if (req.query.location === "y"){
msg = ( data.smoke.value < 700 ) ? "OK" : "NOT ADVISED";
msg = ( fb.data.smoke.value < 700 ) ? "OK" : "NOT ADVISED";
}
res.json({ msg });
});
Expand All @@ -67,31 +57,33 @@ app.post("/feedback", (req, res)=>{
res.json({ msg: "Thank for your valuable feedback."});
if (sensor === "dht"){
if (trust === "false")
(data.dht.trust > 1) ? (data.dht.trust--):null;
(fb.data.dht.trust > 1) ? (fb.data.dht.trust--):null;
else
(data.dht.trust < 10) ? (data.dht.trust++):null;
(fb.data.dht.trust < 10) ? (fb.data.dht.trust++):null;
}else{
if (trust === "false")
(data.smoke.trust > 1) ? (data.smoke.trust--):null;
(fb.data.smoke.trust > 1) ? (fb.data.smoke.trust--):null;
else
(data.smoke.trust < 10) ? (data.smoke.trust++):null;
(fb.data.smoke.trust < 10) ? (fb.data.smoke.trust++):null;
}
p2pServer.broadcastFeedback(fb.data);
});

//Get data dht sensor data
app.post("/smoke-data", (req, res)=>{
data.smoke.value = (Math.random());//req.body.data;
fb.data.smoke.value = req.body.data;
console.log(req.body);
if (process.env.DEVICE === "RPI"){
sensor.read(11, 26, function(err, temperature, humidity) {
if (!err) {
console.log(`temp: ${temperature}°C, humidity: ${humidity}%`);
data.dht.temperature = temperature;
data.dht.humidity = humidity;
fb.data.dht.temperature = temperature;
fb.data.dht.humidity = humidity;
}
});
}
res.json({msg: "Smoke sensor data received"});
p2pServer.broadcastFeedback(fb.data);
});

//Getting data and mine to the blockchain
Expand Down
28 changes: 25 additions & 3 deletions app/p2p-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ const WebSocket = require('ws');

const P2P_PORT = process.env.P2P_PORT || 5001;
const peers = process.env.PEERS ? process.env.PEERS.split(',') : [];
const MESSAGE_TYPE = {
chain: 'CHAIN',
feedback: 'FEEDBACK'
};

class P2pServer {

constructor(blockchain) {
constructor(blockchain, feedback) {
this.blockchain = blockchain;
this.feedback = feedback;
this.sockets = [];
}

Expand All @@ -33,13 +38,30 @@ class P2pServer {
}

sendChain(socket) {
socket.send(JSON.stringify({ chain: this.blockchain.chain }));
socket.send(JSON.stringify({ type: MESSAGE_TYPE.chain, chain: this.blockchain.chain }));
}

sendFeedback(socket, feedback) {
socket.send(JSON.stringify({ type: MESSAGE_TYPE.feedback, feedback }));
}

messageHandler(socket) {
socket.on('message', message => {
const data = JSON.parse(message);
this.blockchain.replaceChain(data.chain);
switch (data.type) {
case MESSAGE_TYPE.chain:
this.blockchain.replaceChain(data.chain);
break;
case MESSAGE_TYPE.feedback:
this.feedback.replaceData(data.feedback);
break;
}
});
}

broadcastFeedback(feedback) {
this.sockets.forEach(socket => {
this.sendFeedback(socket, feedback);
});
}

Expand Down
4 changes: 2 additions & 2 deletions nodemcu/nodemcu.ino
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void loop() {
request_send("http://192.168.43.121:3001/smoke-data/", analogSensor);

//sending data to rpi
//request_send("http://192.168.43.124:3001/smoke-data/", analogSensor);
request_send("http://192.168.43.124:3001/smoke-data/", analogSensor);

delay(1000);
digitalWrite(LED, LOW);
Expand All @@ -83,7 +83,7 @@ void request_send(String url, int data){
http.addHeader("Cache-Control","max-age=0");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");

int httpCode = http.POST(String(data));
int httpCode = http.POST(String("{'data'"+data+"}"));
String payload = http.getString(); //Get the request response payload
Serial.println(payload);
http.end();
Expand Down

0 comments on commit b5c3534

Please sign in to comment.