Skip to content

Commit e26242b

Browse files
committed
fixed sync between mqtt and firebase
type: update description: sync
1 parent bdd81ff commit e26242b

File tree

10 files changed

+227
-76
lines changed

10 files changed

+227
-76
lines changed

Diff for: camera_app_/app/src/main/AndroidManifest.xml

+5-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@
5353
android:name=".ui.LabomaticCamera.base.ManualAnalysisLandscapeActivity"
5454
android:label="ControllerAndCamera"/>
5555

56-
<activity android:name=".ui.LabomaticCamera.base.AutomaticAnalysisActivity">
56+
<activity
57+
android:name=".ui.LabomaticCamera.base.AutomaticAnalysisActivity"
58+
android:label="AutomaticAnalysis"
59+
android:configChanges="screenSize|orientation">
5760
<intent-filter>
58-
<action android:name="android.intent.action.DEFAULT" />
61+
<action android:name="android.intent.action.MAIN" />
5962
<category android:name="android.intent.category.LAUNCHER" />
6063
</intent-filter>
6164
</activity>

Diff for: camera_app_/app/src/main/java/ai/labomatic/data/model/NameSettings.java

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public final class NameSettings {
88
// Static setting names
99
public static final String AUTOMATIC_START = "AutomaticStart";
1010
public static final String AUTOMATIC_UPLOAD = "AutomaticUpload";
11+
public static final String IP_ADDRESS_MQTT = "IPAddress";
1112

1213
public NameSettings(){
1314
// Nothing

Diff for: camera_app_/app/src/main/java/ai/labomatic/ui/LabomaticCamera/base/AutomaticAnalysisScreenFragment.java

+22-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.Intent;
77
import android.content.pm.ActivityInfo;
88
import android.os.Bundle;
9+
import android.support.design.widget.Snackbar;
910
import android.support.v4.app.Fragment;
1011
import android.util.Log;
1112
import android.view.LayoutInflater;
@@ -85,6 +86,8 @@ public void onViewCreated(final View view, Bundle savedInstanceState) {
8586
// Initial states
8687
nameUserEditText.setText("");
8788
automaticAnalisisButton.setEnabled(true);
89+
// Restart stage since this fragment is the entry point
90+
publishMessage(Initializer.MACROS_TOPIC, Initializer.STAGE_RESTART_HOME);
8891
}
8992

9093
@Override
@@ -161,9 +164,7 @@ public void publishMessage(String topic, String message) {
161164
}
162165
}
163166

164-
/**
165-
* MQTT Receiver
166-
* */
167+
// MQTT receiver
167168
private MQTTServiceReceiver receiver =
168169
new MQTTServiceReceiver() {
169170
private static final String TAG = "Receiver";
@@ -194,7 +195,7 @@ public void onMessageArrived(Context context, String topic, byte[] payload) {
194195
// If the handshake is authenticated, then we can continue.
195196
handshakeWithListener = true;
196197
// Always restart home (XY), when opening QR activity.
197-
publishMessage(Initializer.MACROS_TOPIC, Initializer.STAGE_RESTART_HOME);
198+
publishMessage(Initializer.MACROS_TOPIC, Initializer.STAGE_RESTART_INITIAL);
198199
// Start barcode activity
199200
Intent intent = new Intent(getActivity(), BarcodeCaptureActivity.class);
200201
startActivity(intent);
@@ -264,4 +265,21 @@ public void run() {
264265
}
265266
}
266267

268+
/**
269+
* Shows a snackbar on the UI thread.
270+
* @param text String that contains the message to show
271+
* */
272+
private void showSnackbar(final String text){
273+
final Activity activity = getActivity();
274+
if (activity != null){
275+
activity.runOnUiThread(new Runnable() {
276+
@Override
277+
public void run() {
278+
Snackbar.make(getActivity().findViewById(R.id.automatic_layout_container),
279+
text, Snackbar.LENGTH_LONG).show();
280+
}
281+
});
282+
}
283+
}
284+
267285
}

Diff for: camera_app_/app/src/main/java/ai/labomatic/ui/LabomaticCamera/composer/AutomaticAnalysisFragment.java

+23-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.Manifest;
44
import android.app.Activity;
55
//import android.app.Fragment;
6+
import android.support.design.widget.Snackbar;
67
import android.support.v4.app.Fragment;
78
import android.content.ComponentName;
89
import android.content.Context;
@@ -389,8 +390,6 @@ public View onCreateView(LayoutInflater inflater,
389390
@Override
390391
public void onViewCreated(final View view, Bundle savedInstanceState) {
391392
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
392-
// Create presenter instance
393-
//automaticAnalysisFragmentPresenter = new AutomaticAnalysisFragmentPresenter();
394393
// Databases
395394
clientsDB = new ClientsDatabaseHandler(getActivity().getApplicationContext());
396395
settingsDB = new SettingsDatabaseHandler(getActivity().getApplicationContext());
@@ -475,7 +474,6 @@ public void onDestroy(){
475474

476475
@Override
477476
public void onSubscriptionSuccessful(Context context, String requestId, String topic) {
478-
//MQTTServiceCommand.publish(context, "/cameraApp", payload);
479477
}
480478

481479
@Override
@@ -530,14 +528,17 @@ else if (command.equals("requestService") && target.equals("autofocus") &&
530528
intent.setComponent(new ComponentName("com.example.root.autofocus_app",
531529
"com.example.root.autofocus_app.AutofocusActivity"));
532530
startActivity(intent);
533-
} else {
531+
} else if (command.equals("keepalive")){
534532

533+
} else{
534+
// Do nothing
535535
}
536536
}
537537

538538
@Override
539539
public void onConnectionSuccessful(Context context, String requestId) {
540-
Log.i(TAG, "Connected!");
540+
Log.i(TAG, "Connected from AutomaticAnalysis fragment!");
541+
showSnackbar("MQTT Connected");
541542
}
542543

543544
@Override
@@ -1000,6 +1001,23 @@ public void run() {
10001001
}
10011002
}
10021003

1004+
/**
1005+
* Shows a snackbar on the UI thread.
1006+
* @param text String that contains the message to show
1007+
* */
1008+
private void showSnackbar(final String text){
1009+
final Activity activity = getActivity();
1010+
if (activity != null){
1011+
activity.runOnUiThread(new Runnable() {
1012+
@Override
1013+
public void run() {
1014+
Snackbar.make(getActivity().findViewById(R.id.automatic_layout_container),
1015+
text, Snackbar.LENGTH_LONG).show();
1016+
}
1017+
});
1018+
}
1019+
}
1020+
10031021
public void finishFragment(){
10041022
getActivity().finish();
10051023
}

Diff for: camera_app_/app/src/main/java/ai/labomatic/ui/Login/LoginActivity.java

+82-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.app.ProgressDialog;
5+
import android.content.Context;
56
import android.content.Intent;
67
import android.content.pm.ActivityInfo;
78
import android.graphics.drawable.AnimationDrawable;
@@ -23,7 +24,9 @@
2324
import ai.labomatic.data.local.UsersDatabaseHandler;
2425

2526
import ai.labomatic.R;
27+
import ai.labomatic.ui.BarcodeReader.BarcodeCaptureActivity;
2628
import ai.labomatic.ui.NavigationMenu;
29+
import ai.labomatic.util.Initializer;
2730

2831
import com.google.android.gms.tasks.OnCompleteListener;
2932
import com.google.android.gms.tasks.Task;
@@ -32,6 +35,8 @@
3235
import com.google.firebase.auth.FirebaseAuthUserCollisionException;
3336
import com.google.firebase.auth.FirebaseUser;
3437

38+
import net.igenius.mqttservice.MQTTServiceReceiver;
39+
3540
import java.util.List;
3641

3742
/**
@@ -64,6 +69,10 @@ public class LoginActivity extends Activity {
6469
// Firebase
6570
private FirebaseAuth mAuth;
6671

72+
// Authentication states
73+
private boolean firebaseAuthentication = false;
74+
private boolean mqttAuthentication = false;
75+
6776
@Override
6877
protected void onCreate(Bundle savedInstanceState) {
6978
super.onCreate(savedInstanceState);
@@ -223,11 +232,18 @@ public void onStart(){
223232
anim.start();
224233
}
225234

235+
@Override
236+
public void onResume(){
237+
super.onResume();
238+
receiver.register(this);
239+
}
240+
226241
@Override
227242
public void onPause(){
228243
super.onPause();
229244
if (anim != null && anim.isRunning())
230245
anim.stop();
246+
receiver.unregister(this);
231247
}
232248

233249
public boolean validatePassword(String password){
@@ -325,10 +341,10 @@ public void onComplete(@NonNull Task<AuthResult> task) {
325341
// Sign in success, update UI with the signed-in user's information
326342
Log.i(TAG, "signInWithEmail:success");
327343
FirebaseUser user = mAuth.getCurrentUser();
328-
progressDialog.dismiss();
329-
Intent menuActivity = new Intent(LoginActivity.this,
330-
NavigationMenu.class);
331-
startActivity(menuActivity);
344+
// Change firebase auth variable to true
345+
firebaseAuthentication = true;
346+
// try to start the next activity
347+
validateConnections();
332348
} else {
333349
// If sign in fails, display a message to the user.
334350
Log.e(TAG, "signInWithEmail:failure", task.getException());
@@ -355,15 +371,15 @@ public void onComplete(@NonNull Task<AuthResult> task) {
355371
FirebaseUser user = mAuth.getCurrentUser();
356372
// Save in local database
357373
db.createUser(new User(email, password));
358-
// Kill progressDialog
359-
progressDialog.dismiss();
374+
// If creating user was successful, then sign in
375+
firebaseSignInWithEmailAndPassword(email+emailExtension, password);
360376
} else {
361377
if (task.getException() instanceof FirebaseAuthUserCollisionException) {
362378
showToast("User with this email already exist.");
363379
// Save in local database
364380
db.createUser(new User(email, password));
365-
// Kill progressDialog
366-
progressDialog.dismiss();
381+
// User already exists, so save in db and sign in
382+
firebaseSignInWithEmailAndPassword(email+emailExtension, password);
367383
} else {
368384
// If sign in fails, display a message to the user.
369385
Log.e(TAG, "createUserWithEmail:failure", task.getException());
@@ -377,6 +393,64 @@ public void onComplete(@NonNull Task<AuthResult> task) {
377393
});
378394
}
379395

396+
/**
397+
* Check mqtt and firebase are connected and start the next activity.
398+
* */
399+
public void validateConnections(){
400+
if (mqttAuthentication == true && firebaseAuthentication == true){
401+
// If both variables are set to true, then start the next activity
402+
progressDialog.dismiss();
403+
Intent intent = new Intent(LoginActivity.this, NavigationMenu.class);
404+
startActivity(intent);
405+
} else{
406+
Log.i(TAG, "both services are not connected yet");
407+
}
408+
}
409+
410+
// MQTT receiver
411+
private MQTTServiceReceiver receiver =
412+
new MQTTServiceReceiver() {
413+
private static final String TAG = "Receiver";
414+
@Override
415+
public void onSubscriptionSuccessful(Context context, String requestId, String topic) {
416+
Log.i(TAG, "Subscribed to " + topic);
417+
}
418+
@Override
419+
public void onSubscriptionError(Context context, String requestId, String topic, Exception exception) {
420+
Log.i(TAG, "Can't subscribe to " + topic, exception);
421+
}
422+
@Override
423+
public void onPublishSuccessful(Context context, String requestId, String topic) {
424+
Log.i(TAG, "Successfully published on topic: " + topic);
425+
}
426+
@Override
427+
public void onMessageArrived(Context context, String topic, byte[] payload) {
428+
Log.i(TAG, "New message on " + topic + ": " + new String(payload));
429+
}
430+
431+
@Override
432+
public void onConnectionSuccessful(Context context, String requestId) {
433+
// Feedback
434+
showToast("Connected");
435+
Log.i(TAG, "Connected!");
436+
// Change mqtt auth variable to true
437+
mqttAuthentication = true;
438+
// Try to move to the next activity
439+
validateConnections();
440+
}
441+
442+
@Override
443+
public void onException(Context context, String requestId, Exception exception) {
444+
exception.printStackTrace();
445+
Log.i(TAG, requestId + " exception");
446+
}
447+
448+
@Override
449+
public void onConnectionStatus(Context context, boolean connected) {
450+
451+
}
452+
};
453+
380454
public void showToast(String message){
381455
Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
382456
}

0 commit comments

Comments
 (0)