Skip to content

Commit cef59d5

Browse files
committed
"code till everything works with prefix"
1 parent 2ed3ba2 commit cef59d5

File tree

8 files changed

+162
-53
lines changed

8 files changed

+162
-53
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
23
package="com.googleresearch.capturesync">
34

45
<uses-permission android:name="android.permission.CAMERA" />
56
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
7+
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"
8+
tools:ignore="ProtectedPermissions" />
69
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
710
<uses-permission android:name="android.permission.INTERNET"/>
811
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
12+
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
913

1014
<application
1115
android:allowBackup="true"

app/src/main/java/com/googleresearch/capturesync/MainActivity.java

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package com.googleresearch.capturesync;
1919

20+
import static java.security.AccessController.getContext;
21+
2022
import android.Manifest.permission;
2123
import android.annotation.SuppressLint;
2224
import android.app.Activity;
@@ -38,11 +40,14 @@
3840
import android.media.CamcorderProfile;
3941
import android.media.MediaCodec;
4042
import android.media.MediaRecorder;
43+
import android.net.wifi.WifiInfo;
4144
import android.net.wifi.WifiManager;
4245
import android.os.Bundle;
4346
import android.os.Environment;
4447
import android.os.Handler;
4548
import android.os.HandlerThread;
49+
import android.provider.Settings;
50+
import android.telephony.TelephonyManager;
4651
import android.util.Log;
4752
import android.util.Size;
4853
import android.view.Gravity;
@@ -57,6 +62,7 @@
5762
import android.widget.SeekBar.OnSeekBarChangeListener;
5863
import android.widget.TextView;
5964
import android.widget.Toast;
65+
import android.provider.Settings.Secure;
6066

6167
import com.googleresearch.capturesync.softwaresync.CSVLogger;
6268
import com.googleresearch.capturesync.softwaresync.SoftwareSyncLeader;
@@ -65,6 +71,7 @@
6571
import com.googleresearch.capturesync.softwaresync.phasealign.PhaseConfig;
6672

6773
import java.io.File;
74+
import java.io.FileNotFoundException;
6875
import java.io.IOException;
6976
import java.io.InputStream;
7077
import java.nio.charset.StandardCharsets;
@@ -77,8 +84,10 @@
7784
import java.util.Collections;
7885
import java.util.Comparator;
7986
import java.util.Date;
87+
import java.util.HashMap;
8088
import java.util.List;
8189
import java.util.Locale;
90+
import java.util.Map;
8291
import java.util.concurrent.FutureTask;
8392
import java.util.stream.Collectors;
8493

@@ -388,26 +397,13 @@ private void handleWebSocketMsg(@NotNull WsMessageContext wsMessageContext){
388397

389398

390399
case "UPLOAD" :
391-
Log.i(TAG,"handling the message in DOWNLOAD:" + infoParts[1] );
392-
try {
393-
File sdcard = Environment.getExternalStorageDirectory();
394-
String filePath = sdcard.getAbsolutePath()+ "/RecSync/VID/";
395-
File path = new File(filePath);
396-
File list[] = path.listFiles();
397-
String filename;
398-
for(int i=0; i< list.length; i++){
399-
filename = list[i].getName();
400-
if(filename.startsWith(infoParts[2])){
401-
String[] fileList = {filePath + filename};
402-
new UploadFileToServer().execute(fileList);
403-
}
404-
}
405-
}
406-
catch(Exception e){
407-
Log.i(TAG,"In exception block");
408-
e.printStackTrace();
409-
}
410-
400+
Log.i(TAG,"handling the message in DOWNLOAD:" + infoParts[1]);
401+
// infoParts[1] is the payload
402+
sendFilesToServer(infoParts[1]);
403+
((SoftwareSyncLeader) softwareSyncController.softwareSync)
404+
.broadcastRpc(
405+
SoftwareSyncController.METHOD_UPLOAD_RECORDED_FILES,
406+
infoParts[1]);
411407

412408
break;
413409

@@ -1111,6 +1107,47 @@ public boolean isVideoRecording() {
11111107
return isVideoRecording;
11121108
}
11131109

1110+
public void sendFilesToServer(String payload){
1111+
try {
1112+
1113+
String []payloadParams = payload.split(",");
1114+
String clientID = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
1115+
File sdcard = Environment.getExternalStorageDirectory();
1116+
String videoFilePath = sdcard.getAbsolutePath()+ "/RecSync/VID/";
1117+
String csvFilePath = sdcard.getAbsolutePath()+ "/RecSync/";
1118+
File path = new File(videoFilePath);
1119+
File list[] = path.listFiles();
1120+
Map<String,String> postRequestDataMap = new HashMap<>();
1121+
String filename = "FileNotFound";
1122+
for(int i=0; i< list.length; i++){
1123+
filename = list[i].getName();
1124+
if(filename.startsWith(payloadParams[1])){
1125+
postRequestDataMap.put("VIDEO_FILE_PATH", videoFilePath + filename);
1126+
break;
1127+
}
1128+
1129+
}
1130+
if(filename != "FileNotFound"){
1131+
postRequestDataMap.put("CSV_FILE_PATH", csvFilePath + filename.split("\\.")[0] + ".csv" );
1132+
postRequestDataMap.put("CLIENT_ID", clientID);
1133+
postRequestDataMap.put("API_ENDPOINT", payloadParams[0]);
1134+
postRequestDataMap.put("SESSION_PREFIX", payloadParams[1]);
1135+
1136+
new UploadFileToServer().execute(postRequestDataMap);
1137+
}else{
1138+
throw new FileNotFoundException();
1139+
}
1140+
for (String name: postRequestDataMap.keySet()) {
1141+
String key = name;
1142+
String value = postRequestDataMap.get(name);
1143+
System.out.println(key + ":" + value);
1144+
}
1145+
1146+
}catch (Exception e){
1147+
e.printStackTrace();
1148+
}
1149+
}
1150+
11141151
public void startVideo(boolean wantAutoExp, String prefixText) {
11151152
Log.d(TAG, "Starting video.");
11161153
//Toast.makeText(this, "Started recording video", Toast.LENGTH_LONG).show();

app/src/main/java/com/googleresearch/capturesync/SoftwareSyncController.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class SoftwareSyncController implements Closeable {
6262
public static final int METHOD_SET_2A = 200_002;
6363
public static final int METHOD_START_RECORDING = 200_003;
6464
public static final int METHOD_STOP_RECORDING = 200_004;
65+
public static final int METHOD_UPLOAD_RECORDED_FILES = 200_005;
6566

6667
private long upcomingTriggerTimeNs;
6768

@@ -206,6 +207,15 @@ private void setupSoftwareSync() {
206207
context::stopVideo
207208
);
208209
});
210+
clientRpcs.put(
211+
METHOD_UPLOAD_RECORDED_FILES,
212+
payload -> {
213+
Log.v(TAG, "Uploading Recorded Files");
214+
context.runOnUiThread(
215+
() -> context.sendFilesToServer(payload)
216+
);
217+
});
218+
209219

210220
clientRpcs.put(
211221
SyncConstants.METHOD_MSG_OFFSET_UPDATED,

app/src/main/java/com/googleresearch/capturesync/UploadFileToServer.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,31 @@
44

55

66
import org.apache.hc.client5.http.classic.methods.HttpPost;
7+
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
78
import org.apache.hc.client5.http.entity.mime.FileBody;
89
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
910
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
1011
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
1112
import org.apache.hc.client5.http.impl.classic.HttpClients;
13+
import org.apache.hc.core5.http.ContentType;
1214
import org.apache.hc.core5.http.HttpEntity;
1315
import org.apache.hc.core5.http.HttpResponse;
16+
import org.apache.hc.core5.http.NameValuePair;
17+
import org.apache.hc.core5.http.io.entity.StringEntity;
18+
import org.apache.hc.core5.http.message.BasicNameValuePair;
19+
import org.json.JSONObject;
20+
1421
import java.io.IOException;
22+
import java.nio.charset.Charset;
23+
import java.util.ArrayList;
24+
import java.util.Map;
25+
import java.net.Proxy.Type.*;
26+
1527
import android.os.AsyncTask;
1628

1729

1830

19-
public class UploadFileToServer extends AsyncTask<String, Integer, String> {
31+
public class UploadFileToServer extends AsyncTask<Map<String, String>, Integer, String> {
2032
@Override
2133
protected void onPreExecute() {
2234
// setting progress bar to zero
@@ -29,25 +41,50 @@ protected void onProgressUpdate(Integer... progress) {
2941
}
3042

3143
@Override
32-
protected String doInBackground(String... selectedFilePaths) {
33-
return uploadFile(selectedFilePaths);
44+
protected String doInBackground(Map<String, String>... postRequestDataMap) {
45+
return uploadFile(postRequestDataMap[0]);
3446
}
3547

3648
@SuppressWarnings("deprecation")
37-
private String uploadFile(String... selectedFilePaths) {
49+
private String uploadFile(Map<String, String> postRequestDataMap) {
3850
String responseString = "All Good";
3951
HttpResponse httpResponse;
52+
ArrayList<NameValuePair> postParameters;
4053
int statusCode;
4154
String response = "";
4255

4356

4457
try (final CloseableHttpClient httpClient = HttpClients.createDefault()) {
45-
final HttpPost httpPost = new HttpPost("http://192.168.5.1:5000/upload");
46-
47-
final File video_file = new File(selectedFilePaths[0]);
58+
String endpoint = "http://192.168.5.1:5000/upload";
59+
// if(postRequestDataMap.get("API_ENDPOINT") != ""){
60+
// endpoint = postRequestDataMap.get("API_ENDPOINT");
61+
// }
62+
63+
final HttpPost httpPost = new HttpPost(endpoint);
64+
final File video_file = new File(postRequestDataMap.get("VIDEO_FILE_PATH"));
65+
final File csv_file = new File(postRequestDataMap.get("CSV_FILE_PATH"));
4866
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
4967
builder.setMode(HttpMultipartMode.LEGACY);
5068
builder.addPart("file", new FileBody(video_file));
69+
builder.addPart("csv_file", new FileBody(csv_file));
70+
builder.addTextBody("client_id", postRequestDataMap.get("CLIENT_ID"));
71+
builder.addTextBody("session_prefix", postRequestDataMap.get("SESSION_PREFIX"));
72+
73+
// JSONObject jsonObj = new JSONObject();
74+
// try{
75+
// jsonObj.put("client_id", postRequestDataMap.get("CLIENT_ID"));
76+
// jsonObj.put("session_prefix", postRequestDataMap.get("SESSION_PREFIX"));
77+
// }catch(Exception e){
78+
// e.printStackTrace();
79+
// }
80+
//
81+
//// Create the POST object and add the parameters
82+
// StringEntity sentity = new StringEntity(jsonObj.toString(), ContentType.APPLICATION_JSON, "UTF-8", false);
83+
//
84+
//// postParameters = new ArrayList<NameValuePair>();
85+
//// postParameters.add(new BasicNameValuePair("client_id", postRequestDataMap.get("CLIENT_ID")));
86+
//// postParameters.add(new BasicNameValuePair("session_prefix",postRequestDataMap.get("SESSION_PREFIX") ));
87+
// httpPost.setEntity(sentity);
5188
HttpEntity entity = builder.build();
5289
httpPost.setEntity(entity);
5390
httpResponse = httpClient.execute(httpPost);
@@ -56,7 +93,7 @@ private String uploadFile(String... selectedFilePaths) {
5693
System.out.println("FILE UPLOADED");
5794

5895
} catch (IOException e) {
59-
responseString = e.toString();
96+
e.printStackTrace();
6097
}
6198
return responseString;
6299

app/src/main/java/com/googleresearch/capturesync/softwaresync/ClientInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.googleresearch.capturesync.softwaresync;
1818

19+
import android.provider.Settings;
20+
1921
import java.net.InetAddress;
2022

2123
/**

app/src/main/java/com/googleresearch/capturesync/softwaresync/SoftwareSyncBase.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package com.googleresearch.capturesync.softwaresync;
1818

19+
import android.content.ContentResolver;
1920
import android.os.HandlerThread;
21+
import android.provider.Settings;
2022
import android.util.Log;
2123
import java.io.Closeable;
2224
import java.io.IOException;
@@ -32,7 +34,7 @@
3234
import java.util.Map;
3335
import java.util.concurrent.ExecutorService;
3436
import java.util.concurrent.Executors;
35-
37+
import android.provider.Settings.Secure;
3638
/**
3739
* SoftwareSyncBase is the abstract base class to SoftwareSyncLeader and SoftwareSyncClient, holding
3840
* shared objects such as UDP ports and sockets, local client information and methods for starting

fileserver/file_server.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
UPLOAD_FOLDER = 'static/uploads/'
1818
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
1919

20-
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'mov', 'mp4'])
20+
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'mov', 'mp4', 'csv'])
2121

2222
def allowed_file(filename):
2323
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@@ -34,32 +34,45 @@ def upload_file():
3434
resp.status_code = 400
3535
return resp
3636

37-
files = request.files.getlist('file')
37+
print(request.files)
38+
print(request.form)
39+
3840

41+
files = request.files.getlist('file')
42+
files.extend(request.files.getlist('csv_file'))
3943
errors = {}
4044
success = False
4145

46+
# create a directory based on session and client id
47+
session_prefix, client_id = request.form.get("session_prefix"), request.form.get("client_id")
48+
directory_path = os.path.join(app.config['UPLOAD_FOLDER'],session_prefix + "/" + client_id + "/")
49+
print(directory_path)
50+
if not os.path.exists(directory_path):
51+
os.makedirs(directory_path)
52+
# import pdb;pdb.set_trace()
4253
for file in files:
4354
if file and allowed_file(file.filename):
4455
filename = secure_filename(file.filename)
45-
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
56+
file.save(os.path.join(directory_path, filename))
4657
success = True
4758
else:
4859
errors[file.filename] = 'File type is not allowed'
49-
50-
if success and errors:
51-
errors['message'] = 'File(s) successfully uploaded'
52-
resp = jsonify(errors)
53-
resp.status_code = 500
54-
return resp
55-
if success:
56-
resp = jsonify({'message' : 'Files successfully uploaded'})
57-
resp.status_code = 201
58-
return resp
59-
else:
60-
resp = jsonify(errors)
61-
resp.status_code = 500
62-
return resp
60+
resp = jsonify({'message' : 'Files successfully uploaded'})
61+
resp.status_code = 201
62+
return resp
63+
# if success and errors:
64+
# errors['message'] = 'File(s) successfully uploaded'
65+
# resp = jsonify(errors)
66+
# resp.status_code = 500
67+
# return resp
68+
# if success:
69+
# resp = jsonify({'message' : 'Files successfully uploaded'})
70+
# resp.status_code = 201
71+
# return resp
72+
# else:
73+
# resp = jsonify(errors)
74+
# resp.status_code = 500
75+
# return resp
6376

6477
if __name__ == '__main__':
6578
app.run(host='0.0.0.0', port=5000,debug=True)

0 commit comments

Comments
 (0)