diff --git a/.gitignore b/.gitignore
index 7987d4e..718e1a0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -60,7 +60,6 @@ typings/
# All json files
*.json
-!serviceAccountKey.json
!package*
# All rules
diff --git a/App/SmartHome/.idea/misc.xml b/App/SmartHome/.idea/misc.xml
index c51e5ac..4840bc4 100644
--- a/App/SmartHome/.idea/misc.xml
+++ b/App/SmartHome/.idea/misc.xml
@@ -37,7 +37,7 @@
-
+
diff --git a/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/Helpers.java b/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/Helpers.java
index c69eb0f..dcbb2de 100644
--- a/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/Helpers.java
+++ b/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/Helpers.java
@@ -74,7 +74,9 @@ static void configSmartHome(View rootView, MainActivity current) {
current.tv.setText("NO INTERNET CONNECTION");
current.statusCard = rootView.findViewById(R.id.statusCard);
current.alertCard = rootView.findViewById(R.id.notifCard);
-
+ current.statusCard.setVisibility(View.INVISIBLE);
+ current.alertCard.setVisibility(View.INVISIBLE);
+ current.roomCard.setVisibility(View.INVISIBLE);
rootView.findViewById(R.id.homePage)
.setBackgroundDrawable(current.getResources().getDrawable(R.drawable.app_background));
Helpers.fixBackgroundRepeat(rootView.findViewById(R.id.homePage));
@@ -88,6 +90,10 @@ public void onDataChange(DataSnapshot dataSnapshot) {
User regUser = dataSnapshot.getValue(User.class);
if(regUser != null ){
finalCurrent.tv.setText("Registered User :\nName- " + regUser.name + "\nPhone- " + regUser.contact);
+ finalCurrent.statusCard.setVisibility(View.VISIBLE);
+ finalCurrent.alertCard.setVisibility(View.VISIBLE);
+ v.findViewById(R.id.roomCard).setVisibility(View.VISIBLE);
+ finalCurrent.roomCard.setVisibility(View.VISIBLE);
finalCurrent.statusCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
@@ -240,11 +246,7 @@ public void onCancelled(DatabaseError databaseError) {
});
}
- static void configSensorData(View rootView, MainActivity current) {
- showLatestSensorReadings(rootView, current);
- }
-
- private static void showLatestSensorReadings(View rootView,MainActivity current) {
+ static void showLatestSensorReadings(View rootView,MainActivity current) {
rootView.findViewById(R.id.sensorsStatus)
.setBackgroundDrawable(current.getResources().getDrawable(R.drawable.app_background));
@@ -253,8 +255,7 @@ private static void showLatestSensorReadings(View rootView,MainActivity current)
current.lightValue = rootView.findViewById(R.id.lightValue);
current.tempValue = rootView.findViewById(R.id.tempValue);
current.humValue = rootView.findViewById(R.id.humValue);
- current.inStatus = rootView.findViewById(R.id.inSwitch);
- current.outStatus = rootView.findViewById(R.id.outSwitch);
+ current.motion = rootView.findViewById(R.id.mSwitch);
final MainActivity finalCurrent = current;
@@ -267,8 +268,7 @@ public void onChildAdded(DataSnapshot snap, String s) {
finalCurrent.lightValue.setText(String.valueOf(latestSensor.light));
finalCurrent.humValue.setText(String.valueOf(latestSensor.humidity));
finalCurrent.tempValue.setText(String.valueOf(latestSensor.temp));
- finalCurrent.inStatus.setChecked(latestSensor.inDoorMotion);
- finalCurrent.outStatus.setChecked(latestSensor.outDoorMotion);
+ finalCurrent.motion.setChecked(latestSensor.motion);
}
}
diff --git a/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/MainActivity.java b/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/MainActivity.java
index 57ed762..440eb43 100644
--- a/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/MainActivity.java
+++ b/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/MainActivity.java
@@ -33,7 +33,7 @@ public class MainActivity extends AppCompatActivity {
EditText OTRK;
TextView tv,lightValue,humValue,tempValue,msg;
DatabaseReference userDB,sensorDB,alertsDB;
- Switch inStatus,outStatus;
+ Switch motion;
CardView statusCard,roomCard,alertCard;
ImageView img;
RecyclerView alertsList;
@@ -167,7 +167,7 @@ private void setView(View rootView, int option, MainActivity current) {
break;
case Constants.SENSOR_OP_CODE:
if (current != null && rootView != null)
- Helpers.configSensorData(rootView, current);
+ Helpers.showLatestSensorReadings(rootView, current);
break;
case Constants.SMART_HOME_OP:
if (current != null && rootView != null)
diff --git a/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/State.java b/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/State.java
index 6595d12..f3e2900 100644
--- a/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/State.java
+++ b/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/State.java
@@ -5,8 +5,8 @@
*/
class State {
- public Double temp,humidity,light,ax,ay,az,timestamp,latestIndoorMovement,latestOutdoorMovement;
- public boolean inDoorMotion,outDoorMotion;
+ public Double temp,humidity,light,ax,ay,az,timestamp,latestMovement;
+ public boolean motion;
State(){
// empty constructor
}
diff --git a/App/SmartHome/app/src/main/res/layout/fragment_sensors.xml b/App/SmartHome/app/src/main/res/layout/fragment_sensors.xml
index de40fbd..d957ff6 100644
--- a/App/SmartHome/app/src/main/res/layout/fragment_sensors.xml
+++ b/App/SmartHome/app/src/main/res/layout/fragment_sensors.xml
@@ -16,8 +16,8 @@
android:layout_height="wrap_content">
-
-
-
-
-
-
@@ -93,6 +95,7 @@
android:id="@+id/notifCard"
android:layout_alignTop="@+id/roomCard"
android:layout_toStartOf="@+id/roomCard"
+ android:visibility="invisible"
android:layout_marginEnd="57dp"
android:layout_marginTop="7dp">
@@ -127,6 +130,7 @@
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/groupCard"
+ android:visibility="invisible"
android:layout_marginBottom="9dp"
android:layout_above="@+id/roomCard"
android:layout_toStartOf="@+id/statusCard"
diff --git a/server.js b/server.js
index bb37f4a..98bf480 100644
--- a/server.js
+++ b/server.js
@@ -4,15 +4,17 @@ var colors = require('colors');
var nodemailer = require('nodemailer');
var admin = require("firebase-admin");
var serviceAccount = require("./serviceAccountKey.json");
-var chance = require('chance').Chance();
var express = require('express');
var app = express();
-const threads = require('threads');
-const spawn = threads.spawn;
-const thread = spawn(function() {});
-const TEMP_THRESH = 200;
+const TEMP_THRESH = 125;
const LIGHT_THRESH = 375;
-const HUM_THRESH = 35;
+const HUM_THRESH = 30;
+
+
+// var chance = require('chance').Chance();
+// const threads = require('threads');
+// const spawn = threads.spawn;
+// const thread = spawn(function() {});
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
@@ -57,33 +59,6 @@ function addNewUser( user ){
});
}
-// store 10 random users in database
-function addRandomUsers(){
- var pushCount = 0;
- while( pushCount < 10 ){
- var gen = chance.gender();
- chance.mixin({
- 'user': function() {
- return {
- name : chance.name(
- { middle: true,
- prefix : true,
- gender : gen
- }),
- gender : gen,
- dob: chance.birthday({string: true, american: false}),
- email: chance.email({domain: 'gmail.com'}),
- ssn : chance.ssn(),
- profession : chance.profession(),
- contact : chance.phone({ formatted: false, mobile: true})
- };
- }
- });
- addNewUser(chance.user());
- pushCount++;
- }
-}
-
function isEmpty(obj) {
return !Object.keys(obj).length > 0;
@@ -91,8 +66,6 @@ function isEmpty(obj) {
app.get("/u/users",function(req,response){
response.writeHead(200, {'Content-type':'text/plain'});
- response.write('Smart Home IoT Admin created Users\n');
- response.end();
usersRef.on("child_added",function(snap,prevChildKey){
var key = snap.key;
@@ -127,7 +100,9 @@ app.get("/u/users",function(req,response){
var q = req.query;
if( isEmpty(q) ){
- addRandomUsers();
+// addRandomUsers();
+ response.write('No user specified\n');
+
} else {
if( allValid(q.contact,q.dob,q.email,q.gender,q.name,q.profession,q.ssn) ){
var newUser = {
@@ -140,10 +115,15 @@ app.get("/u/users",function(req,response){
ssn : q.ssn
}
addNewUser(newUser);
+ response.write('Smart Home IoT Admin created User\n');
+
} else {
console.log('Missing Parameters Exception');
}
}
+
+ response.end();
+
});
function detectAnomaly(state){
@@ -162,9 +142,10 @@ function detectAnomaly(state){
// INTRUDER ALERT
if( state.inDoorMotion == true ){
- stateRef.child(state.timestamp).child('members').once("value",function(snap){
- const m = snap.val();
- if( m ){
+ stateRef.child(state.timestamp).once("value",function(snap){
+
+ var m = snap.exists() && snap.child("members").exists();
+ if( m === true ){
console.log("OKAY ! There are members inside the room");
} else {
alerts.push({
@@ -184,17 +165,19 @@ function detectAnomaly(state){
stateRef
.orderByChild('timestamp')
.endAt(state.timestamp)
- .limitToLast(2*60)
+ .limitToLast(90)
.once("value",function(snap){
+ var l = 0;
snap.forEach(
function(data){
- if( data.val().light < LIGHT_THRESH ){
+ if( data.child("members").exists() || data.val().light <= LIGHT_THRESH ){
wastage = false;
}
+ l++;
}
);
- if( wastage ){
+ if( wastage && l >= 45 ){
alerts.push({
type : "POWER_ALERT",
message : "Activate Power Management System if available",
@@ -229,108 +212,10 @@ function detectAnomaly(state){
}
-// store data packets of sensor readings for 45 minutes in database maintaining 10 minutes window of state each second
-function updateStateRecordWindow(){
-
- var Now = Date.now();
- var Limit = Now + 45*60*1000;
-
- thread
- .run(function(limit, done, progress) {
- var Now = Date.now();
-
- function sleep(time) {
- var stop = Date.now();
- while(Date.now() < stop + time) {
- ;
- }
- }
- setTimeout(done, limit - Now); // 45 minutes time limit
- while(Now <= limit){
- sleep(1000); // publish progress after 1 second
- Now = Date.now();
- progress(Now,limit);
- }
- })
- .send(Limit)
- .on('progress', function(Now,limit) {
-
- if( Now >= limit ){
- ;
- } else{
- var Old = Now - 10*60*1000; // 10 minutes older data remove
- stateRef.orderByChild('timestamp').endAt(Old).once('value', function(snapshot) {
- snapshot.forEach(function(snap){
- stateRef.child(snap.val().timestamp).remove();
- });
- });
- var lim,lom,gx,gy,gz,gs,im,om;
- chance.mixin({
- 'state': function() {
-
- im = chance.bool({likelihood: 40});
- om = chance.bool({likelihood: 60});
- if( im == true ){
- lim = Now;
- }
- if( om == true ){
- lom = Now;
- }
- if( lim == null || lim == undefined ){
- lim = 0;
- }
- if( lom == null || lom == undefined ){
- lom = 0;
- }
- do{
- gx = chance.floating({min: 0, max: 8, fixed : 2});
- gy = chance.floating({min: 0, max: 8, fixed : 2});
- gz = chance.floating({min: 0, max: 8, fixed : 2});
- gs = gx*gx + gy*gy + gz*gz;
- }while( gs <= 80 || gs >= 144 );
-
- return {
- light : chance.natural({min: 150, max: 500}),
- temp : chance.floating({min: -40, max: 250, fixed : 3}),
- humidity : chance.floating({min: 0, max: 100, fixed : 2}) ,
- inDoorMotion : im,
- outDoorMotion : om,
- ax : gx,
- ay : gy,
- az : gz,
- latestIndoorMovement : lim,
- latestOutdoorMovement : lom,
- timestamp : Now
-
- };
- }
- });
- var n = chance.state();
- stateRef.child(Now).set(n,function(error){
- if (error) {
- console.log("Data could not be saved." + error);
- } else {
- console.log("Data saved successfully.");
- // send push notification and update Alerts DB
- detectAnomaly(n);
- }
- });
- }
-
- })
- .on('done', function() {
- if( thread != null ){
- thread.kill();
- thread = null;
- }
- });
-
-}
-
function pushSensorData(t,h,l,im,om,gx,gy,gz){
var Now = Date.now();
- var lim,lom;
+ var lim;
if( im == null || im == undefined){
im = false;
@@ -344,32 +229,19 @@ function pushSensorData(t,h,l,im,om,gx,gy,gz){
}
- if( om == null || om == undefined ){
- om = false;
- lom = 0;
- } else if( om == "true"){
- om = true;
- lom = Now;
- } else {
- om = false;
- lom = 0;
- }
-
var state = {
light : parseFloat(l) || null,
temp : parseFloat(t) || null,
humidity : parseFloat(h) || null,
- inDoorMotion : im,
- outDoorMotion : om,
+ motion : im,
ax : parseFloat(gx) || null ,
ay : parseFloat(gy) || null,
az : parseFloat(gz) || null ,
- latestIndoorMovement : 0,
- latestOutdoorMovement : 0,
+ latestMovement : lim,
timestamp : Now
};
- var Old = Now - 10*60*1000; // remove all data older than 10 minutes
+ var Old = Now - 30*60*1000; // remove all data older than 30 minutes
stateRef.orderByChild('timestamp').endAt(Old).once('value', function(snapshot) {
snapshot.forEach(function(snap){
@@ -402,25 +274,16 @@ app.get("/u/state",function(req,response){
prevState = snap.val();
if( prevState != null && prevState != undefined && newState != null && newState != undefined){
var lim,lom,ts;
- lim = newState.latestIndoorMovement;
- lom = newState.latestOutdoorMovement;
+ lim = newState.latestMovement;
ts = newState.timestamp;
if( lim == 0)
{
var changes = {
- latestIndoorMovement : prevState.latestIndoorMovement
+ latestMovement : prevState.latestMovement
}
if( ts != undefined && ts != null )
stateRef.child(""+ts).update(changes);
}
- if( lom == 0 ){
-
- var changes = {
- latestOutdoorMovement : prevState.latestOutdoorMovement
- }
- if( ts != undefined && ts != null )
- stateRef.child(""+ts).update(changes);
- }
}
});
}
@@ -429,7 +292,7 @@ app.get("/u/state",function(req,response){
if( isEmpty(q) ){
// acts as direct continous uploading server
- updateStateRecordWindow();
+ // updateStateRecordWindow();
}
else {
@@ -456,4 +319,130 @@ app.get("/u/device",function(req,response){
app.listen(app.get('port'),function(){
console.log('Node app is running on port',app.get('port'));
-});
\ No newline at end of file
+});
+
+// // store data packets of sensor readings for 45 minutes in database maintaining 10 minutes window of state each second
+// function updateStateRecordWindow(){
+
+// var Now = Date.now();
+// var Limit = Now + 45*60*1000;
+
+// thread
+// .run(function(limit, done, progress) {
+// var Now = Date.now();
+
+// function sleep(time) {
+// var stop = Date.now();
+// while(Date.now() < stop + time) {
+// ;
+// }
+// }
+// setTimeout(done, limit - Now); // 45 minutes time limit
+// while(Now <= limit){
+// sleep(1000); // publish progress after 1 second
+// Now = Date.now();
+// progress(Now,limit);
+// }
+// })
+// .send(Limit)
+// .on('progress', function(Now,limit) {
+
+// if( Now >= limit ){
+// ;
+// } else{
+// var Old = Now - 10*60*1000; // 10 minutes older data remove
+// stateRef.orderByChild('timestamp').endAt(Old).once('value', function(snapshot) {
+// snapshot.forEach(function(snap){
+// stateRef.child(snap.val().timestamp).remove();
+// });
+// });
+// var lim,lom,gx,gy,gz,gs,im,om;
+// chance.mixin({
+// 'state': function() {
+
+// im = chance.bool({likelihood: 40});
+// om = chance.bool({likelihood: 60});
+// if( im == true ){
+// lim = Now;
+// }
+// if( om == true ){
+// lom = Now;
+// }
+// if( lim == null || lim == undefined ){
+// lim = 0;
+// }
+// if( lom == null || lom == undefined ){
+// lom = 0;
+// }
+// do{
+// gx = chance.floating({min: 0, max: 8, fixed : 2});
+// gy = chance.floating({min: 0, max: 8, fixed : 2});
+// gz = chance.floating({min: 0, max: 8, fixed : 2});
+// gs = gx*gx + gy*gy + gz*gz;
+// }while( gs <= 80 || gs >= 144 );
+
+// return {
+// light : chance.natural({min: 150, max: 500}),
+// temp : chance.floating({min: -40, max: 250, fixed : 3}),
+// humidity : chance.floating({min: 0, max: 100, fixed : 2}) ,
+// inDoorMotion : im,
+// outDoorMotion : om,
+// ax : gx,
+// ay : gy,
+// az : gz,
+// latestIndoorMovement : lim,
+// latestOutdoorMovement : lom,
+// timestamp : Now
+
+// };
+// }
+// });
+// var n = chance.state();
+// stateRef.child(Now).set(n,function(error){
+// if (error) {
+// console.log("Data could not be saved." + error);
+// } else {
+// console.log("Data saved successfully.");
+// // send push notification and update Alerts DB
+// detectAnomaly(n);
+// }
+// });
+// }
+
+// })
+// .on('done', function() {
+// if( thread != null ){
+// thread.kill();
+// thread = null;
+// }
+// });
+
+// }
+
+
+// // store 10 random users in database
+// function addRandomUsers(){
+// var pushCount = 0;
+// while( pushCount < 10 ){
+// var gen = chance.gender();
+// chance.mixin({
+// 'user': function() {
+// return {
+// name : chance.name(
+// { middle: true,
+// prefix : true,
+// gender : gen
+// }),
+// gender : gen,
+// dob: chance.birthday({string: true, american: false}),
+// email: chance.email({domain: 'gmail.com'}),
+// ssn : chance.ssn(),
+// profession : chance.profession(),
+// contact : chance.phone({ formatted: false, mobile: true})
+// };
+// }
+// });
+// addNewUser(chance.user());
+// pushCount++;
+// }
+// }