diff --git a/App/SmartHome/.idea/misc.xml b/App/SmartHome/.idea/misc.xml
index 037f5e8..c51e5ac 100644
--- a/App/SmartHome/.idea/misc.xml
+++ b/App/SmartHome/.idea/misc.xml
@@ -27,22 +27,6 @@
-
-
-
-
-
-
-
-
-
-
- Java
-
-
-
-
-
@@ -53,7 +37,7 @@
-
+
diff --git a/App/SmartHome/app/build.gradle b/App/SmartHome/app/build.gradle
index fc507b9..bcd3676 100644
--- a/App/SmartHome/app/build.gradle
+++ b/App/SmartHome/app/build.gradle
@@ -31,6 +31,8 @@ dependencies {
compile 'com.android.support:support-v4:26.0.0-alpha1'
compile 'com.google.firebase:firebase-core:11.4.2'
compile 'com.google.firebase:firebase-database:11.4.2'
+ compile 'com.squareup.okhttp3:okhttp:3.9.0'
+ compile 'com.google.firebase:firebase-messaging:11.4.2'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
diff --git a/App/SmartHome/app/src/main/AndroidManifest.xml b/App/SmartHome/app/src/main/AndroidManifest.xml
index f73c197..8ad79ac 100644
--- a/App/SmartHome/app/src/main/AndroidManifest.xml
+++ b/App/SmartHome/app/src/main/AndroidManifest.xml
@@ -15,6 +15,15 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/Constants.java b/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/Constants.java
index 442c0d6..8b19a14 100644
--- a/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/Constants.java
+++ b/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/Constants.java
@@ -10,4 +10,5 @@ class Constants {
static final int SENSOR_OP_CODE = 2;
static final String[] OptionTitles = new String[]{"SMART HOME", "SENSORS"};
static final String ARG_OPTION_NUMBER = "OPTION_INDEX";
+ static final String DEVICE_REG_SERVER = "isecpowify-server.herokuapp.com/u/device";
}
diff --git a/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/DeviceTokenUpdater.java b/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/DeviceTokenUpdater.java
new file mode 100644
index 0000000..5c5757d
--- /dev/null
+++ b/App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/DeviceTokenUpdater.java
@@ -0,0 +1,98 @@
+package com.solutions.isecpowify.smarthome;
+
+import android.support.annotation.NonNull;
+import android.util.Log;
+import com.google.firebase.iid.FirebaseInstanceId;
+import com.google.firebase.iid.FirebaseInstanceIdService;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import okhttp3.Call;
+import okhttp3.Callback;
+import okhttp3.HttpUrl;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
+
+/**
+ * Created by mayank on 10/11/17.
+ */
+
+public class DeviceTokenUpdater extends FirebaseInstanceIdService {
+
+ private static Map queryParams;
+ private static String currentToken = null;
+ private static OkHttpClient client = new OkHttpClient().newBuilder()
+ .connectTimeout(30, TimeUnit.SECONDS)
+ .readTimeout(60, TimeUnit.SECONDS)
+ .build();
+
+ private static final Callback resCB = new Callback() {
+ @Override
+ public void onFailure(@NonNull Call call, @NonNull IOException e) {
+ Log.v(Constants.TAG,e.getMessage());
+ call.cancel();
+ }
+
+ @Override
+ public void onResponse(@NonNull Call call, @NonNull Response response)
+ throws IOException {
+
+ if (response.isSuccessful()){
+ Log.v(Constants.TAG, String.valueOf(response.body()));
+ }
+ }
+ };
+
+
+ @Override
+ public void onTokenRefresh() {
+
+ // Get updated InstanceID token.
+ currentToken = FirebaseInstanceId.getInstance().getToken();
+ }
+
+ private static String getDeviceFCMToken(){
+ return currentToken;
+ }
+
+ static void sendDetailsToServer(final String otrk){
+
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ while(getDeviceFCMToken()==null)
+ {
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ queryParams = Collections.emptyMap();
+ queryParams.put("fcm_token",getDeviceFCMToken());
+ queryParams.put("id_otr",otrk);
+ get(Constants.DEVICE_REG_SERVER,queryParams,resCB);
+ }
+ }).start();
+ }
+
+ public static void get(String url, Map params, Callback responseCallback) {
+
+ try{
+ HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder();
+
+ if (params != null) {
+ for(Map.Entry param : params.entrySet()) {
+ httpBuilder.addQueryParameter(param.getKey(),param.getValue());
+ }
+ }
+ Request request = new Request.Builder().url(httpBuilder.build()).build();
+ client.newCall(request).enqueue(responseCallback);
+
+ } catch (NullPointerException obj){
+ Log.v(Constants.TAG,"Object is null : " + obj.getMessage());
+ }
+ }
+}
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 35efdff..c8e409e 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
@@ -1,6 +1,7 @@
package com.solutions.isecpowify.smarthome;
import android.app.Activity;
+import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@@ -12,8 +13,6 @@
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
-import android.widget.ImageView;
-import android.widget.TextView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
@@ -57,6 +56,20 @@ static void restartApplication(Activity current) {
static void configSmartHome(View rootView, MainActivity current) {
+ try {
+ if( !alreadyRunningService(Class.forName("com.solutions.isecpowify.smarthome.DeviceTokenUpdater"),current)){
+ DeviceTokenUpdater
+ .sendDetailsToServer(
+ getSharedPreferences(current.getApplicationContext())
+ .getString(current.getString(R.string.OTRK),null)
+ );
+ }
+ } catch (ClassNotFoundException e) {
+ Log.v("Service Class Missing: ",e.getMessage());
+ } catch (Exception e){
+ Log.v(Constants.TAG,"Token Service Updater Exception Occurred : " + e.getMessage());
+ }
+
current.tv = rootView.findViewById(R.id.welcome);
current.tv.setText("NO INTERNET CONNECTION");
current.statusCard = rootView.findViewById(R.id.statusCard);
@@ -93,7 +106,18 @@ public void onCancelled(DatabaseError databaseError) {
});
}
+ private static boolean alreadyRunningService(Class> serviceClass,Context ctx) {
+ ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
+ for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
+ if (serviceClass.getName().equals(service.service.getClassName())) {
+ Log.i ("Check Service Running: "+serviceClass.getName(), true+"");
+ return true;
+ }
+ }
+ Log.i ("Check Service Running: "+serviceClass.getName(), false+"");
+ return false;
+ }
private static void setRoomEvent(View rootView, MainActivity curr) {
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 f19b943..eedc6f1 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
@@ -45,7 +45,6 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
selectItem(Constants.SMART_HOME_OP);
-
} else {
setContentView(R.layout.key_spec_layout);
findViewById(R.id.keySpecLayout)
diff --git a/server.js b/server.js
index 477402b..e19cc1f 100644
--- a/server.js
+++ b/server.js
@@ -315,6 +315,23 @@ app.get("/u/state",function(req,response){
pushSensorData(q.temp,q.humidity,q.light,q.inDoorMotion,q.outDoorMotion,q.ax,q.ay,q.az);
}
});
+
+app.get("/u/device",function(req,response){
+ response.writeHead(200, {'Content-type':'text/plain'});
+ response.write('Smart Home Device Registartion End Point\n');
+ response.end();
+ var q = req.query;
+
+ if( !isEmpty(q) ){
+
+ if( allValid(q.fcm_token,q.id_otr) ){
+ usersRef.child(q.id_otr).update({
+ "deviceToken" : q.fcm_token || null
+ });
+ }
+ }
+});
+
app.listen(app.get('port'),function(){
console.log('Node app is running on port',app.get('port'));
});
\ No newline at end of file