Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var Particle = require('particle-api-js');
- var particle = new Particle();
- var mongooose = require('mongoose');
- var bodyParser = require('body-parser');
- var secure_token;
- var app = express();
- var Schema = mongooose.Schema;
- var readingSchema = new Schema({
- Device_Id:String,
- Time_Stamp: String,
- Gas_Id:String,
- Concentraction: String,
- });
- var nodemailer = require('nodemailer');
- var mailOptions;
- var path = require('path');
- app.use(express.static(path.join(__dirname, 'public')));
- var gas_name = "CarbonMonoxide"
- app.use(bodyParser.json());
- app.set('views','views');
- app.set('view engine', 'ejs');
- var Reading = mongooose.model('Reading',readingSchema);
- var glo_db;
- app.use(bodyParser.json());
- app.use(bodyParser.urlencoded({ extended: true }));
- var name = [];
- var result_send = [];
- var devices_pre_id;
- var hours,minutes;
- var avg_1;
- var counter_stop_signal,counter_start_signal,counter_stop_reading,counter_start_reading;
- var transporter = nodemailer.createTransport({
- service: 'gmail',
- auth: {
- user: 'fourthyearscet@gmail.com',
- pass: 'scet1234@'
- }
- });
- //mongodb connection for globle access.........
- var uri = 'mongodb://localhost/user1';
- var promise = mongooose.connect(uri,{
- useMongoClient: true,
- }
- );
- promise.openUri(uri,function(errr,db){
- if(errr){
- throw errr;
- }else{
- console.log("Connection Successfull");
- glo_db = db;
- //console.log(glo_db);
- }
- })
- var collection_check_var = 0;
- var subscribeReading = function(channel, token) {
- var saveReading = function(data) {
- var temp = JSON.stringify(data.data);
- var device_ids = JSON.stringify(data.coreid);
- //console.log(JSON.stringify(data));
- //console.log(JSON.stringify(data.coreid ));
- //console.log(JSON.stringify(data.published_at));
- //console.log( JSON.stringify(data.data));
- //code for store data in Specific Collection
- if(collection_check_var == 0 && device_ids != 'null'){
- //console.log("Towords to create a Collection");
- create_collection("Reading",glo_db);
- collection_check_var = 1;
- }
- //code to show the collection in Mongodb
- //This block is for checking the reding and to save in a Database....
- if(temp != 'null' ){
- var temp_name = parseInt(data.data);
- //console.log(typeof temp_name);
- //var fg =parseInt(temp_name);
- //console.log(typeof temp_name);
- //var temp_time = JSON.stringify(data.published_at) ;
- var temp_time = new Date();
- var user_temp_name = Reading({
- Device_Id: device_ids,
- Time_Stamp: temp_time,
- Gas_Id:gas_name,
- Concentraction: temp_name
- });
- save_data_into_collection("reading",device_ids,temp_time,gas_name,temp_name,glo_db);
- }
- };
- //Get all events
- particle.getEventStream({name: channel, auth: token}).then(function(stream) {
- stream.on('event', saveReading);
- });
- //Get your devices events
- // particle.getEventStream({ deviceId: 'mine', auth: token }).then(function(stream) {
- // stream.on('event', function(data) {
- // console.log("Event: " + data);
- // });
- // });
- //Get test event for specific device
- // particle.getEventStream({ deviceId: 'DEVICE_ID', name: 'test', auth: token }).then(function(stream) {
- // stream.on('event', function(data) {
- // console.log("Event: " + data);
- // });
- // });
- }
- particle.login({username: 'rahul@winrtech.com', password: 'Scet@207@IOT'}).then(
- function(data) {
- console.log(data.body);
- secure_token = data.body.access_token;
- subscribeReading("RCHAN1", secure_token);
- subscribeReading("RCHAN2",secure_token);
- },
- function (err) {
- console.log('Could not log in.', err);
- }
- );
- function create_collection(collection_name,glo_db){
- var collection = glo_db.collection(collection_name);
- if(collection){
- console.log("Collectoin Creatd");
- }
- }
- function save_data_into_collection(name_colletion,Device_Id,Time_Stamp,Gas_Id,Concentraction,db){
- var collection = db.collection(name_colletion);
- collection.insertOne({Device_Id,Time_Stamp,Gas_Id,Concentraction},function(err,res){
- if(err){
- throw err;
- }else{
- console.log("Data Saved into Collection");
- collection.count(function (err,result_count) {
- //console.log("hiii");
- // body...
- if(err){ throw err; }
- else{
- //console.log(result_count);
- collection.find({},{_id: false,reading: true,time:true}).toArray(function(err,result){
- // console.log("hii2");
- if(err){ throw err;
- // console.log("hii3");
- }
- else{
- // collection.aggregate([{
- // $group:{
- // _id:{Gas_Name:"$Gas_Id",Device_Id:"$Device_Id"},
- // data:{
- // $last:"$Concentraction"
- // }
- // }}]).toArray(function(err,data){
- // if(err){ throw err; }
- // else{
- // console.log(data)
- // result_send = data;
- // }
- // });
- collection.aggregate([
- {$group:{
- _id:{Gas_Name:"$Gas_Id",Device_Id:"$Device_Id"},
- average_ppm: {
- $avg : "$Concentraction"
- }
- }}]).toArray(function(err,result_avg){
- if(err){ throw err;
- }
- else{
- avg_1 = result_avg;
- console.log(avg_1);
- }
- });
- console.log("----------------------------------------------");
- //
- }
- });
- }
- });
- }
- });
- }
- app.get('/home',function (req,res) {
- // body...
- res.render('tabel_ajax');
- console.log("Hii")
- });
- app.post('/home',function(req,res){
- // console.log("Avg is" +avg_1)
- res.status(200).send({name1:avg_1,name2:result_send})
- })
- var flag;
- app.get('/flash',function(req,res){
- console.log("Flash Get");
- res.render('flash');
- });
- counter_stop_reading=0;
- counter_start_reading = 0;
- counter_start_signal = 0;
- counter_stop_signal = 0;
- app.post('/flash',function (req,res){
- // body...
- var temp = req.body.status;
- var send_device = [];
- console.log(req.body.test);
- var temp1 = req.body.test;
- // console.log("ID"+temp1)
- console.log(temp);
- if(temp == "Status"){
- devicesPr = particle.listDevices({ auth: secure_token }).then(
- function(devices){
- console.log(devices)
- var len = devices.body.length;
- //console.log(devices);
- for(var i=0;i<len;i++){
- send_device[i] = devices.body[i];
- }
- flag =1;
- //console.log(Ids);
- res.render("flash1",{pranav:send_device});
- //res.send({pranav:Ids});
- },
- function(err) {
- flag = 0;
- console.log('List devices call failed: ', err)
- res.render('err_file',{temp:flag,});
- });
- }
- else if(temp == "stopReading"){
- particle.flashDevice({ deviceId: temp1, files: { file1: './off_sensor.bin' }, auth: secure_token }).then(function(data) {
- flag = 1;
- console.log('Device Stop');
- res.send({data:"Decvice "+temp1+" reading is STOP Please wait for Some Time to come Device Online Again"})
- }, function(err) {
- counter_stop_reading++;
- if(counter_stop_reading==2 ){
- counter_stop_reading=0;
- res.send({data:"Can't able stop reading of Device "+temp1+"..! Email has send to the Technician according to the Problem.Your prblem will solved shortly."})
- flag = 0;
- mailOptions = {
- from: 'fourthyearscet@gmail.com',
- to: 'fourthyearscetadmi@gmail.com',
- subject:"Error in Stop Reading"+temp1,
- text: "There is a error in device who's Device Id is: "+temp1 //please add the area information tooo....!
- }
- transporter.sendMail(mailOptions, function(error, info){
- if (error) {
- console.log(error);
- } else {
- console.log('Email sent: ' + info.response);
- }
- });
- }else{
- res.send({data:"Fail To Stop Reading of Device "+temp1+" try agian"})
- }
- })
- }
- else if(temp == "startReading"){
- particle.flashDevice({ deviceId: temp1, files: { file1: './start_sensor.bin' }, auth: secure_token }).then(function(data) {
- flag = 1;
- console.log('Device Started');
- res.send({data:"Decvice "+temp1+" reading is START Please wait for Some Time to come Device Online Again"})
- //res.render("flash1",{pranav:send_device});
- }, function(err) {
- flag = 0;
- res.send({data:"Can't able start reading of Device "+temp1+"..! Email has send to the Technician according to the Problem.Your prblem will solved shortly."})
- mailOptions = {
- from: 'fourthyearscet@gmail.com',
- to: 'fourthyearscetadmi@gmail.com',
- subject:"Error in Start Reading"+temp1,
- text: "There is a error in device who's Device Id is: "+temp1 //please add the area information tooo....!
- }
- transporter.sendMail(mailOptions, function(error, info){
- if (error) {
- console.log(error);
- } else {
- console.log('Email sent: ' + info.response);
- }
- });
- })
- }
- else if(temp == "signalDevice"){
- particle.signalDevice({ deviceId: temp1, signal: true, auth: secure_token }).then(function(data) {
- flag = 1;
- console.log('Signal started....!!!!!!');
- res.send({data:"Decvice "+temp1+" signal is START check "+temp1+" dicvice LED light"})
- // res.render("flash1",{pranav:send_device});
- }, function(err) {
- flag = 0;
- res.send({data:"Can't Signal to Device "+temp1+"..! Email has send to the Technician according to the Problem.Your prblem will solved shortly."})
- mailOptions = {
- from: 'fourthyearscet@gmail.com',
- to: 'fourthyearscetadmi@gmail.com',
- subject:"Error in Signal Signal"+temp1,
- text: "There is a error in device who's Device Id is: "+temp1 //please add the area information tooo....!
- }
- transporter.sendMail(mailOptions, function(error, info){
- if (error) {
- console.log(error);
- } else {
- console.log('Email sent: ' + info.response);
- }
- })
- ; })
- }
- else if(temp =="stopsignalDevice"){
- particle.signalDevice({ deviceId: temp1, signal: false, auth: secure_token }).then(function(data) {
- flag = 1;
- console.log('Signal Stop....!!!!!!!');
- res.send({data:"Decvice "+temp1+" signal is STOP check "+temp1+" dicvice LED light"})
- }, function(err) {
- res.send({data:"Can't Stop Signal to Device "+temp1+"..! Email has send to the Technician according to the Problem.Your prblem will solved shortly."})
- flag = 0
- ;mailOptions = {
- from: 'fourthyearscet@gmail.com',
- to: 'fourthyearscetadmi@gmail.com',
- subject:"Error in Stop Signal"+temp1,
- text: "There is a error in device who's Device Id is: "+temp1 //please add the area information tooo....!
- }
- transporter.sendMail(mailOptions, function(error, info){
- if (error) {
- console.log(error);
- } else {
- console.log('Email sent: ' + info.response);
- }
- });
- })
- }
- });
- app.get('/issue_form',function (req,res) {
- // body...
- res.render('issue_from');
- });
- app.get('/area_form',function(req,res){
- res.render('area_form')
- })
- app.get('/index',function(req,res){
- res.render('test_date_server');
- });
- app.post('/gas_form',function(req,res){
- var gas_id = req.body.gas_id;
- var gas_code = req.body.gas_code;
- var gas_name = req.body.gas_name;
- console.log(gas_id);
- console.log(gas_code);
- console.log(gas);
- })
- app.post('/index',function(req,res){
- var test,test1,test2;
- test = req.body.area;
- test1 = req.body.gas;
- test2 = req.body.s_date;
- test3 = req.body.e_date;
- var areas = req.body.area;
- var gases = req.body.gas;
- var start_date = new Date(test2);
- var end_date = new Date(test3);
- //console.log(start_date)
- hours = start_date.getHours()-5;
- minutes = start_date.getMinutes()-30;
- start_date.setHours(hours);start_date.setMinutes(minutes);
- //console.log("Hour"+hours+"\n"+"Minutes"+minutes);
- //console.log(start_date)
- hours = end_date.getHours()-5;
- minutes = end_date.getMinutes()-30;
- //console.log("Hour"+hours+"\n"+"Minutes"+minutes);
- end_date.setHours(hours);end_date.setMinutes(minutes);
- //console.log(end_date)
- //var date = new Date(test2.toISOString());
- //console.log(date2)
- var collection1 = glo_db.collection('Reading');
- // collection1.find({},{_id:true ,Gas:true,PPM:true,time_stamp:true}).toArray(function(err,result){
- // if(err){ throw err }
- // else{
- // console.log(result)
- // }
- // });
- collection1.aggregate([{$match:{Time_Stamp:{$gte: start_date,$lte:end_date}}}
- ,{$group:{_id:{deviceid:"$_id",month:{$month:"$Time_Stamp"} , min: { $minute: "$Time_Stamp" }, shour: { $hour: "$Time_Stamp" }}, avg:{$avg: "$Concentraction"},
- }}]).toArray(function (err,result) {
- // body..
- if(err){ throw err}
- else{
- console.log(result)
- // var output = [];
- // output.push(["Area "].concat(gases));
- // res.send({result})
- }
- })
- // collection1.aggregate( [ {$group: { _id: "$Gas", abc:{ $push:{ gases: "$Gas" }} } }])
- // .toArray(function(err,result){
- // if (err){throw err;}
- // else{
- // console.log(result);
- // }
- // });
- });
- app.get('/area',function(req,res){
- res.render("area_find");
- })
- app.post('/area',function (req,res) {
- // body...
- console.log("Area");
- var collection1 = glo_db.collection("Area");
- collection1.find({},{_id:false,Area_Id:true}).toArray(function(err,data) {
- if(err){throw err;}
- else{
- console.log(data);
- res.send(data)
- }
- // body...
- })
- })
- var area_name ;
- var j=0;
- var temp_area
- app.post('/data',function(req,res) {
- // body..
- //console.log(req.body.area_name);
- var device_result = [] ;
- var flag = req.body.flag;
- //console.log(typeof(flag));
- if(flag == "1"){
- temp_area = "";
- temp_area = req.body.area_name;
- }
- else if(flag == "0"){
- temp_area = "";
- temp_area = req.body.area_name;
- }
- // //temp_area = "";
- var collection1 = glo_db.collection("Area");
- var collection2 = glo_db.collection("Device");
- var collection3 = glo_db.collection("reading");
- collection1.find({Area_Id:temp_area},{_id:false,Area_Id:true}).toArray(function(err,data){
- if(err){ throw err ;}
- else{
- area_name = data[0].Area_Id;
- temp_area = "";
- //console.log(data[0].Area_Id)
- collection2.find({Area_Id:data[0].Area_Id},{_id:false,Device_Id:true}).toArray(function(err,data){
- if(err){ throw err; }
- else{
- for(var i=0;i<data.length;i++){
- device_result.push(data[i].Device_Id)
- }
- //console.log(device_result);
- collection3.aggregate([
- {$match:{Device_Id:{$in:device_result}}},
- {$group:{_id:"$Device_Id",average_ppm:{$avg:"$Concentraction"}}}
- ]).toArray(function(err,data1){
- if(err){ throw err; }else{
- console.log(data1)
- res.send(data1)
- }
- });
- }
- })
- }
- })
- })
- app.get('/tebs',function(req,res){
- res.render('tabs');
- })
- app.get('/html',function(req,res){
- res.render('test1')
- })
- // app.post('/result',function(req,res) {
- // // body...
- // var collection3 = glo_db.collection("reading")
- // console.log("result")
- // console.log(area_name)
- // console.log(device_result)
- // collection3.aggregate([
- // {$match:{Device_Id:device_result}},
- // {$group:{_id:"$Device_Id",average_ppm:{$avg:"$Concentraction"}}}
- // ]).toArray(function(err,data){
- // if(err){ throw err; }else{
- // console.log(data)
- // }
- // })
- // })
- app.listen(2525);
- console.log("Magic started on 2525");
- //$gt: ISODate("2018-02-19T00:00:00.000Z")
Add Comment
Please, Sign In to add comment