Skip to content

Commit a98ed6a

Browse files
committed
Added device UUID to communicate with the server
1 parent a181f8b commit a98ed6a

12 files changed

Lines changed: 115 additions & 45 deletions

File tree

app/src/main/java/uk/co/digitme/machinemonitoring/DataEntryActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ private void send(Boolean ignoreValidation) {
336336
RequestQueue queue = Volley.newRequestQueue(this);
337337
// The URL is sent by the requesting activity
338338
String url = dbHelper.getServerAddress() + getIntent().getStringExtra("url");
339-
339+
resultJson.put("device_uuid", dbHelper.getDeviceUuid());
340340
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
341341
url,
342342
resultJson,

app/src/main/java/uk/co/digitme/machinemonitoring/Default/Workflow.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ protected Intent noUserFlow() throws JSONException {
7272
Log.d(TAG, "State: no user");
7373
Intent loginIntent = new Intent(context, LoginActivity.class);
7474
String machineText = "Could not get assigned machine";
75-
String IP = "";
76-
if (serverResponse.has("ip")) {
77-
IP = serverResponse.getString("ip");
75+
String tabletName = "";
76+
if (serverResponse.has("device_name")) {
77+
tabletName = serverResponse.getString("device_name");
7878
}
7979
if (serverResponse.has("machine")) {
8080
machineText = serverResponse.getString("machine");
8181
}
8282
loginIntent.putExtra("machineText", machineText);
83-
loginIntent.putExtra("IP", IP);
83+
loginIntent.putExtra("tabletName", tabletName);
8484
return loginIntent;
8585
}
8686

app/src/main/java/uk/co/digitme/machinemonitoring/Helpers/DbHelper.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package uk.co.digitme.machinemonitoring.Helpers;
22

3+
import android.annotation.SuppressLint;
34
import android.content.ContentValues;
45
import android.content.Context;
56
import android.database.Cursor;
@@ -14,6 +15,7 @@
1415

1516
import java.net.URI;
1617
import java.net.URISyntaxException;
18+
import java.util.UUID;
1719

1820
public class DbHelper extends SQLiteOpenHelper {
1921

@@ -24,6 +26,7 @@ public class DbHelper extends SQLiteOpenHelper {
2426

2527
public static final String COLUMN_ID = "ID";
2628
public static final String COLUMN_SERVER_ADDRESS = "SERVER_ADDRESS";
29+
public static final String COLUMN_DEVICE_UUID = "DEVICE_UUID";
2730

2831
public DbHelper(@Nullable Context context) {
2932
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@@ -33,31 +36,29 @@ public DbHelper(@Nullable Context context) {
3336
public void onCreate(SQLiteDatabase db) {
3437
final String SQL_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS SETTINGS (" +
3538
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
36-
COLUMN_SERVER_ADDRESS + " TEXT NOT NULL); ";
39+
COLUMN_SERVER_ADDRESS + " TEXT NOT NULL, " +
40+
COLUMN_DEVICE_UUID + " TEXT " +
41+
"); ";
3742

3843
db.execSQL(SQL_CREATE_TABLE);
3944

4045
ContentValues cv = new ContentValues();
4146
cv.put(COLUMN_ID, 1);
4247
cv.put(COLUMN_SERVER_ADDRESS, DEFAULT_URL);
48+
cv.put(COLUMN_DEVICE_UUID, UUID.randomUUID().toString());
4349

4450
db.replace("SETTINGS", null ,cv);
4551
Log.d(TAG, "Saving server address: " + DEFAULT_URL);
4652
}
4753

4854
@Override
4955
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
50-
String address = getServerAddress();
51-
5256
}
5357

58+
@SuppressLint("Range")
5459
public String getServerAddress(){
5560
SQLiteDatabase db = getReadableDatabase();
5661

57-
final String SQL_GET_ADDRESS = "SELECT SERVER_ADDRESS FROM SETTINGS WHERE ID=1 Limit 1";
58-
//Cursor cursor = db.rawQuery(SQL_GET_ADDRESS, null);
59-
//Cursor cursor = db.query("SETTINGS", new String[] {"SERVER_ADDRESS"},
60-
// "ID=1", null, null, null, null);
6162
Cursor cursor = db.rawQuery("SELECT * FROM SETTINGS",null);
6263
if(cursor.moveToFirst()) {
6364
String address;
@@ -81,19 +82,16 @@ public void saveServerAddress(String address){
8182
cv.put(COLUMN_ID, 1);
8283
cv.put(COLUMN_SERVER_ADDRESS, address);
8384

84-
db.replace("SETTINGS", null ,cv);
85+
db.update("SETTINGS", cv, "ID = ?", new String[] {"1"});
8586
Log.d(TAG, "Saving server address: " + address);
8687
}
8788

89+
@SuppressLint("Range")
8890
public URI getServerURI() throws URISyntaxException {
8991
SQLiteDatabase db = getReadableDatabase();
9092
String address;
9193
URI uri;
9294

93-
final String SQL_GET_ADDRESS = "SELECT SERVER_ADDRESS FROM SETTINGS WHERE ID=1 Limit 1";
94-
//Cursor cursor = db.rawQuery(SQL_GET_ADDRESS, null);
95-
//Cursor cursor = db.query("SETTINGS", new String[] {"SERVER_ADDRESS"},
96-
// "ID=1", null, null, null, null);
9795
Cursor cursor = db.rawQuery("SELECT * FROM SETTINGS",null);
9896
if(cursor.moveToFirst()) {
9997
try {
@@ -123,4 +121,25 @@ public URI getServerURI() throws URISyntaxException {
123121
}
124122
return new URI(scheme + uri.getHost() + port + "/api/activity-updates");
125123
}
124+
125+
@SuppressLint("Range")
126+
public String getDeviceUuid() {
127+
SQLiteDatabase db = getReadableDatabase();
128+
String uuid;
129+
130+
Cursor cursor = db.rawQuery("SELECT * FROM SETTINGS",null);
131+
if(cursor.moveToFirst()) {
132+
try {
133+
uuid = cursor.getString(cursor.getColumnIndex(COLUMN_DEVICE_UUID));
134+
cursor.close();
135+
} catch (CursorIndexOutOfBoundsException e) {
136+
Log.e(TAG, "Failed to get uuid");
137+
uuid = "";
138+
}
139+
} else {
140+
Log.e(TAG, "Failed to get uuid");
141+
uuid = "";
142+
}
143+
return uuid;
144+
}
126145
}

app/src/main/java/uk/co/digitme/machinemonitoring/Helpers/LoggedInActivity.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
import com.android.volley.Request;
1313
import com.android.volley.RequestQueue;
14-
import com.android.volley.Response;
15-
import com.android.volley.VolleyError;
1614
import com.android.volley.toolbox.JsonObjectRequest;
1715
import com.android.volley.toolbox.Volley;
1816

17+
import org.json.JSONObject;
18+
1919
import uk.co.digitme.machinemonitoring.R;
2020

2121

@@ -47,17 +47,17 @@ public boolean onOptionsItemSelected(MenuItem item) {
4747
RequestQueue queue = Volley.newRequestQueue(this);
4848
String url = dbHelper.getServerAddress() + "/android-logout";
4949

50+
JSONObject jsonBody = new JSONObject();
51+
jsonBody.put("device_uuid", dbHelper.getDeviceUuid());
52+
5053
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
5154
url,
52-
null,
55+
jsonBody,
5356
new EndActivityResponseListener(this),
54-
new Response.ErrorListener() {
55-
@Override
56-
public void onErrorResponse(VolleyError error) {
57-
Log.v("ErrorListener", String.valueOf(error));
58-
Toast.makeText(getApplicationContext(), String.valueOf(error), Toast.LENGTH_LONG).show();
59-
finish();
60-
}
57+
error -> {
58+
Log.v("ErrorListener", String.valueOf(error));
59+
Toast.makeText(getApplicationContext(), String.valueOf(error), Toast.LENGTH_LONG).show();
60+
finish();
6161
});
6262

6363
queue.add(jsonObjectRequest);

app/src/main/java/uk/co/digitme/machinemonitoring/Helpers/ServerDiscovery.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,5 @@ public static boolean findServer(Context context) {
106106
return false;
107107

108108
}
109+
109110
}

app/src/main/java/uk/co/digitme/machinemonitoring/JobActivityBase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ public void updateActivity(int activityCodeId){
195195
RequestQueue queue = Volley.newRequestQueue(this);
196196
JSONObject jsonBody = new JSONObject();
197197

198+
jsonBody.put("device_uuid", dbHelper.getDeviceUuid());
198199
jsonBody.put("activity_code_id", activityCodeId);
199200
// Send the request. Don't listen for the response and ignore any failures, this isn't a critical update.
200201
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,

app/src/main/java/uk/co/digitme/machinemonitoring/LoginActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ protected void onCreate(Bundle savedInstanceState) {
4949

5050
dbHelper = new DbHelper(getApplicationContext());
5151
setContentView(R.layout.activity_login);
52-
String IP = getIntent().getStringExtra("IP");
53-
Objects.requireNonNull(getSupportActionBar()).setTitle(IP + " - Log in");
52+
String tabletName = getIntent().getStringExtra("tabletName");
53+
Objects.requireNonNull(getSupportActionBar()).setTitle(tabletName + " - Log in");
5454

5555
//Set up the custom keyboard
5656
CustomNumpadView cnv = findViewById(R.id.keyboard_view);
@@ -119,6 +119,7 @@ private void logIn() {
119119
JSONObject jsonBody = new JSONObject();
120120
jsonBody.put("user_id", userIdEditText.getText().toString());
121121
jsonBody.put("password", pinCodeEditText.getText().toString());
122+
jsonBody.put("device_uuid", dbHelper.getDeviceUuid());
122123

123124
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
124125
url,

app/src/main/java/uk/co/digitme/machinemonitoring/MainActivity.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import android.view.View;
1111
import android.widget.Button;
1212
import android.widget.TextView;
13-
import android.widget.Toast;
1413

1514
import androidx.appcompat.app.AppCompatActivity;
1615

@@ -49,6 +48,7 @@ public class MainActivity extends AppCompatActivity {
4948
TextView mStatusText;
5049
Button mRetryButton;
5150
Button mSetAddressButton;
51+
Button mFindServerButton;
5252

5353
DbHelper dbHelper;
5454
SharedPreferences prefs = null;
@@ -67,14 +67,28 @@ protected void onCreate(Bundle savedInstanceState) {
6767
// These will become visible after failing to connect.
6868
mStatusText = findViewById(R.id.main_activity_status);
6969
mRetryButton = findViewById(R.id.retry_button);
70+
mFindServerButton = findViewById(R.id.find_server_button);
7071

7172
// The retry button attempts to contact the server again.
7273
mRetryButton.setOnClickListener(new OnOneOffClickListener() {
7374
@Override
7475
public void onSingleClick(View v) {
75-
checkState();
76+
try {
77+
checkState();
78+
} catch (Exception e) {
79+
showError(e.getMessage());
80+
}
7681
}
7782
});
83+
84+
// The find server button tries to discover the server address automatically
85+
mFindServerButton.setOnClickListener(new OnOneOffClickListener() {
86+
@Override
87+
public void onSingleClick(View v) {
88+
discoverServer();
89+
}
90+
});
91+
7892
// The "set address" button opens a new activity allowing the user to change the server ip
7993
mSetAddressButton = findViewById(R.id.set_address_button);
8094
mSetAddressButton.setOnClickListener(v -> {
@@ -91,33 +105,52 @@ protected void onResume() {
91105
discoverServer();
92106
mStatusText.setVisibility(View.INVISIBLE);
93107
mRetryButton.setVisibility(View.INVISIBLE);
108+
mFindServerButton.setVisibility(View.VISIBLE);
94109
mSetAddressButton.setVisibility(View.VISIBLE);
95110
prefs.edit().putBoolean("firstrun", false).apply();
96111
} else {
97112
// Hide the buttons by default. This stops them showing during transitions between activities
98113
mStatusText.setVisibility(View.INVISIBLE);
99114
mRetryButton.setVisibility(View.INVISIBLE);
100115
mSetAddressButton.setVisibility(View.INVISIBLE);
116+
mFindServerButton.setVisibility(View.INVISIBLE);
101117
// When arriving at this page, immediately contact the server to see which screen the app
102118
// should be on, and start that activity
103-
checkState();
119+
try {
120+
checkState();
121+
} catch (Exception e) {
122+
showError(e.getMessage());
123+
}
104124
}
105125
}
106126

107127

108-
private boolean discoverServer(){
128+
@SuppressLint("SetTextI18n")
129+
private void discoverServer() {
109130
mStatusText.setText("Searching for OEE Server...");
110-
return findServer(getApplicationContext());
131+
new Thread() {
132+
public void run() {
133+
runOnUiThread(() -> {
134+
boolean success = findServer(getApplicationContext());
135+
if (success){
136+
checkState();
137+
mStatusText.setText("Server address found. Connecting...");
138+
} else {
139+
showError("Server discovery failed");
140+
}
141+
});
142+
}
143+
}.start();
144+
111145
}
112146

113147

114148
private void showError(String errorText) {
115-
if (!discoverServer()) {
116-
mStatusText.setText(errorText);
117-
mStatusText.setVisibility(View.VISIBLE);
118-
mRetryButton.setVisibility(View.VISIBLE);
119-
mSetAddressButton.setVisibility(View.VISIBLE);
120-
}
149+
mStatusText.setText(errorText);
150+
mStatusText.setVisibility(View.VISIBLE);
151+
mRetryButton.setVisibility(View.VISIBLE);
152+
mFindServerButton.setVisibility(View.VISIBLE);
153+
mSetAddressButton.setVisibility(View.VISIBLE);
121154
}
122155

123156
/**
@@ -126,7 +159,7 @@ private void showError(String errorText) {
126159
*/
127160
private void checkState() {
128161
RequestQueue queue = Volley.newRequestQueue(this);
129-
String url = dbHelper.getServerAddress() + "/check-state";
162+
String url = dbHelper.getServerAddress() + "/check-state?device_uuid=" + dbHelper.getDeviceUuid();
130163
@SuppressLint("SetTextI18n") JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
131164
url,
132165
null,
@@ -142,9 +175,9 @@ private void checkState() {
142175
showError("Could not connect to network");
143176
} else if (error instanceof ServerError) {
144177
showError("Could not connect to server");
178+
showError(error.getMessage());
145179
}
146180
Log.v("ErrorListener", String.valueOf(error));
147-
Toast.makeText(getApplicationContext(), String.valueOf(error), Toast.LENGTH_LONG).show();
148181
});
149182
queue.add(jsonObjectRequest);
150183
}

app/src/main/java/uk/co/digitme/machinemonitoring/Pausable/JobPausedActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ private void resumeJob(){
117117
String url = dbHelper.getServerAddress() + "/pausable-resume-job";
118118
JSONObject jsonBody = new JSONObject();
119119

120+
jsonBody.put("device_uuid", dbHelper.getDeviceUuid());
120121
jsonBody.put("downtime_reason", activityCodeSpinner.getSelectedItem().toString());
121122
jsonBody.put("notes", mNotes.getText().toString());
122123

app/src/main/java/uk/co/digitme/machinemonitoring/Pausable/PausableJobActivity.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.android.volley.toolbox.JsonObjectRequest;
1414
import com.android.volley.toolbox.Volley;
1515

16+
import org.json.JSONObject;
17+
1618
import java.util.Objects;
1719

1820
import uk.co.digitme.machinemonitoring.Default.ActivityCode;
@@ -63,10 +65,12 @@ private void pauseJob(){
6365
try {
6466
RequestQueue queue = Volley.newRequestQueue(this);
6567
String url = dbHelper.getServerAddress() + "/pausable-pause-job";
68+
JSONObject jsonRequest = new JSONObject();
69+
jsonRequest.put("device_uuid", dbHelper.getDeviceUuid());
6670

6771
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
6872
url,
69-
null,
73+
jsonRequest,
7074
new EndActivityResponseListener(this),
7175
error -> {
7276
Log.v("ErrorListener", String.valueOf(error));

0 commit comments

Comments
 (0)