Skip to content

Commit 3721aae

Browse files
committed
Ping Pong and prefix list code
1 parent 4c4c45c commit 3721aae

File tree

3 files changed

+105
-5
lines changed

3 files changed

+105
-5
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,19 @@
9191
import java.util.concurrent.FutureTask;
9292
import java.util.stream.Collectors;
9393

94+
import org.apache.hc.client5.http.classic.methods.HttpPost;
95+
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
96+
import org.apache.hc.client5.http.entity.mime.FileBody;
97+
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
98+
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
99+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
100+
import org.apache.hc.client5.http.impl.classic.HttpClients;
101+
import org.apache.hc.core5.http.HttpEntity;
102+
import org.apache.hc.core5.http.HttpResponse;
103+
import org.apache.hc.core5.http.NameValuePair;
104+
import org.apache.hc.core5.http.message.BasicNameValuePair;
94105
import org.jetbrains.annotations.NotNull;
106+
import org.json.JSONArray;
95107
import org.json.JSONException;
96108
import org.json.JSONObject;
97109
import io.javalin.Javalin;
@@ -423,6 +435,19 @@ private void handleWebSocketMsg(@NotNull WsMessageContext wsMessageContext){
423435

424436
break;
425437

438+
case "PREFIX_LIST" :
439+
Log.i(TAG,"handling the message in PREFIX_LIST" );
440+
sendPrefixList();
441+
((SoftwareSyncLeader) softwareSyncController.softwareSync)
442+
.broadcastRpc(
443+
SoftwareSyncController.METHOD_PREFIX_LIST,
444+
"0");
445+
446+
break;
447+
case "PING" :
448+
Log.i(TAG,"handling the message in PING" );
449+
wsMessageContext.send("PONG");
450+
break;
426451
}
427452
}
428453

@@ -682,6 +707,40 @@ private void startSoftwareSync() {
682707
}
683708
}
684709

710+
public void sendPrefixList(){
711+
HttpResponse httpResponse;
712+
int statusCode;
713+
JSONArray fileNamesArray=new JSONArray();
714+
File sdcard = Environment.getExternalStorageDirectory();
715+
String videoFilePath = sdcard.getAbsolutePath()+ "/RecSync/VID/";
716+
String clientID = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
717+
File path = new File(videoFilePath);
718+
File fileList[] = path.listFiles();
719+
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
720+
String filename;
721+
for(int i=0; i< fileList.length; i++){
722+
filename = fileList[i].getName();
723+
try{
724+
fileNamesArray.put(filename);
725+
}catch (Exception e){
726+
e.printStackTrace();
727+
}
728+
729+
}
730+
nameValuePairs.add(new BasicNameValuePair("file_list", fileNamesArray.toString()));
731+
nameValuePairs.add(new BasicNameValuePair("client_id",clientID ));
732+
try (final CloseableHttpClient httpClient = HttpClients.createDefault()) {
733+
String endpoint = "http://192.168.5.1:5000/namelist";
734+
final HttpPost httpPost = new HttpPost(endpoint);
735+
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
736+
httpResponse = httpClient.execute(httpPost);
737+
statusCode = httpResponse.getCode();
738+
System.out.println("Response Status:" + statusCode);
739+
740+
} catch (IOException e) {
741+
e.printStackTrace();
742+
}
743+
}
685744
private PhaseConfig loadPhaseConfigFile() throws JSONException {
686745
// Load phase config file and pass to phase aligner.
687746

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public class SoftwareSyncController implements Closeable {
6464
public static final int METHOD_STOP_RECORDING = 200_004;
6565
public static final int METHOD_UPLOAD_RECORDED_FILES = 200_005;
6666
public static final int METHOD_EMPTY_DEVICE = 200_006;
67+
public static final int METHOD_PREFIX_LIST = 200_007;
6768

6869
private long upcomingTriggerTimeNs;
6970

@@ -219,6 +220,14 @@ private void setupSoftwareSync() {
219220
() -> context.sendFilesToServer(payload)
220221
);
221222
});
223+
clientRpcs.put(
224+
METHOD_PREFIX_LIST,
225+
payload -> {
226+
Log.v(TAG, "Sending Files List");
227+
context.runOnUiThread(
228+
() -> context.sendPrefixList()
229+
);
230+
});
222231

223232
clientRpcs.put(
224233
METHOD_EMPTY_DEVICE,

remote_control/remote_control.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from PyQt5 import QtCore, QtGui, QtWidgets
1313
from PyQt5.QtWidgets import QMessageBox
1414
import websocket
15-
15+
import rel
16+
import threading
1617

1718
class RemoteController(object):
1819

@@ -79,6 +80,15 @@ def delete_all_btn(self):
7980
def clearStatusBtn(self):
8081
self.status_label.setPlainText("")
8182

83+
def prefixList(self):
84+
try:
85+
self.ws.send("PREFIX_LIST")
86+
except Exception as e:
87+
self.show_popup()
88+
with open('last_prefix.txt','w+') as file:
89+
file.writelines(self.download_prefix_text.toPlainText())
90+
sys.exit()
91+
8292
def downloadBtn(self):
8393
endpoint = self.api_input.toPlainText()
8494
download_prefix = self.download_prefix_text.toPlainText()
@@ -93,12 +103,26 @@ def isPrefix(self, prefix_text):
93103
return False
94104
return True
95105

106+
def asyncTask(self, f_stop):
107+
self.ws.send("PING")
108+
if not f_stop.is_set():
109+
# call f() again in 60 seconds
110+
threading.Timer(5, self.asyncTask, [f_stop]).start()
111+
96112
def setupUi(self, MainWindow):
97113
# Setup the WEB SOCKET
114+
#self.ws = websocket.WebSocketApp("ws://192.168.5.2:7867/remotecon")
98115
self.ws = websocket.WebSocket()
99-
100-
self.ws.connect("ws://192.168.5.2:7867/remotecon", ping_interval=1)
101-
116+
self.ws.connect("ws://192.168.5.2:7867/remotecon")
117+
f_stop = threading.Event()
118+
self.asyncTask(f_stop)
119+
# self.ws = websocket.WebSocketApp("ws://192.168.5.2:7867/remotecon")
120+
#
121+
# self.ws.run_forever(ping_interval=1)
122+
# self.ws.run_forever(dispatcher=rel, reconnect=5)
123+
# rel.signal(2, rel.abort)
124+
# rel.dispatch()
125+
# await ws.send('2')
102126
# Setup the GUI
103127
MainWindow.setObjectName("MainWindow")
104128
MainWindow.resize(800, 800)
@@ -139,8 +163,15 @@ def setupUi(self, MainWindow):
139163
self.download_prefix_text.setText(data[0])
140164
except:
141165
pass
166+
167+
self.prefix_list_btn = QtWidgets.QPushButton(self.centralwidget)
168+
self.prefix_list_btn.setGeometry(QtCore.QRect(120, 380, 241, 61))
169+
self.prefix_list_btn.setFont(font)
170+
self.prefix_list_btn.setObjectName("prefix_list_button")
171+
self.prefix_list_btn.clicked.connect(self.prefixList)
172+
142173
self.download_btn = QtWidgets.QPushButton(self.centralwidget)
143-
self.download_btn.setGeometry(QtCore.QRect(280, 380, 161, 61))
174+
self.download_btn.setGeometry(QtCore.QRect(380, 380, 241, 61))
144175
self.download_btn.setFont(font)
145176
self.download_btn.setObjectName("pushButton_4")
146177
self.download_btn.clicked.connect(self.downloadBtn)
@@ -186,6 +217,7 @@ def retranslateUi(self, MainWindow):
186217
self.api_input.setPlaceholderText(_translate("MainWindow", "Please enter the api endpoint where you want the files to be uploaded."))
187218
self.download_prefix_text.setPlaceholderText(_translate("MainWindow", " Enter Session Prefix"))
188219
self.download_btn.setText(_translate("MainWindow", "Download"))
220+
self.prefix_list_btn.setText(_translate("MainWindow", "Prefix List"))
189221
self.status_label.setPlaceholderText(_translate("MainWindow", "No status "))
190222

191223

0 commit comments

Comments
 (0)