34 48
         void onSessionEnd(String session);
35 49
     }
36 50
 
37 51
     public void startSession(){
52
+        cancelTask(sessionStartTask);
53
+        HashMap<String,String> params = new HashMap<>();
54
+        params.put("lensman",lensmanId);
55
+        params.put("session",sessionId);
56
+        sessionStartTask = new HttpPostTask(params){
57
+            @Override
58
+            protected boolean parseResponse(String response) {
59
+                LogHelper.d(TAG,"startSession get response string = "+response);
60
+                try{
61
+                    JSONObject json = new JSONObject(response);
62
+                    int status = json.getInt("status");
63
+                    if(status == 200){
64
+                        return true;
65
+                    }
66
+                }catch (Exception e){
67
+                    e.printStackTrace();
68
+                }
69
+                return false;
70
+            }
38 71
 
72
+            @Override
73
+            protected void onPostFail() {
74
+                super.onPostFail();
75
+                listener.onSessionStartError(sessionId);
76
+            }
77
+
78
+            @Override
79
+            protected void onPostSuccess() {
80
+                super.onPostSuccess();
81
+                listener.onSessionStartSuccess(sessionId);
82
+                startCapture();
83
+            }
84
+        };
85
+        sessionStartTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), BoxUrlContainer.SESSION_START_URL);
39 86
     }
40 87
 
41 88
     public void startCapture() {
@@ -46,16 +93,85 @@ public class SessionInteractor {
46 93
         timer = new Timer();
47 94
         timer.schedule(new TimerTask() {
48 95
             @Override
49
-            public void run() {}
50
-        },1000,1000);
96
+            public void run() {
97
+                fetchThumbnailTask();
98
+            }
99
+        },1000,5000);
100
+    }
101
+
102
+    private void fetchThumbnailTask(){
103
+        cancelTask(fetchThumbnailTask);
104
+        HashMap<String,String> params = new HashMap<>();
105
+        params.put("lensman",lensmanId);
106
+        params.put("session",sessionId);
107
+        fetchThumbnailTask = new HttpPostTask(params){
108
+            ArrayList<PhotoBean> photoList = new ArrayList<>();
109
+            @Override
110
+            protected boolean parseResponse(String response) {
111
+                LogHelper.d(TAG,"fetchThumbnailTask get response string = "+response);
112
+                try{
113
+                    JSONObject json = new JSONObject(response);
114
+                    int status = json.getInt("status");
115
+                    if(status == 200){
116
+                        JSONObject data = json.getJSONObject("data");
117
+                        JSONArray files = data.getJSONArray("files");
118
+                        if(files!=null && files.length()>0){
119
+                            for(int k = 0 ; k< files.length();k++){
120
+                                JSONObject file = files.getJSONObject(k);
121
+                                PhotoBean bean = new PhotoBean();
122
+                                bean.photoName = file.getString("name");
123
+                                bean.photoId = file.getLong("id");
124
+                                bean.photoPath = file.getString("path");
125
+                                bean.captureTime = bean.photoId;
126
+                                bean.isRawPhoto = false;
127
+                                bean.isUploaded = false;
128
+                                bean.sessionId = sessionId;
129
+                                photoList.add(bean);
130
+                            }
131
+                        }
132
+                        return true;
133
+                    }
134
+                }catch (Exception e){
135
+                    e.printStackTrace();
136
+                }
137
+                return false;
138
+            }
139
+
140
+            @Override
141
+            protected void onPostFail() {
142
+                super.onPostFail();
143
+            }
144
+
145
+            @Override
146
+            protected void onPostSuccess() {
147
+                super.onPostSuccess();
148
+                listener.onSessionPhotoCaptured(photoList);
149
+            }
150
+        };
151
+        fetchThumbnailTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), BoxUrlContainer.FETCH_THUMBNAIL_URL);
51 152
     }
52 153
 
53 154
     public void endSession(){
155
+        cancelTask(sessionEndTask);
156
+        HashMap<String,String> params = new HashMap<>();
157
+        params.put("lensman",lensmanId);
158
+        params.put("session",sessionId);
159
+        sessionEndTask = new HttpPostTask(params);
160
+        sessionEndTask.executeOnExecutor(ThreadExecutor.getInstance().getExecutor(), BoxUrlContainer.SESSION_END_URL);
54 161
         if(timer!=null){
55 162
             timer.cancel();
56 163
             timer = null;
57 164
         }
165
+        listener.onSessionEnd(sessionId);
166
+    }
58 167
 
168
+    private void cancelTask(HttpPostTask task){
169
+        if(task==null){
170
+            return;
171
+        }
172
+        if(task.getStatus()== AsyncTask.Status.RUNNING){
173
+            task.cancel(true);
174
+        }
59 175
     }
60 176
 
61 177
 }

+ 9 - 10
app/src/main/java/ai/pai/lensman/session/SessionPresenter.java

@@ -1,23 +1,19 @@
1 1
 package ai.pai.lensman.session;
2 2
 
3
-import android.content.Context;
4
-
5 3
 import java.util.ArrayList;
6 4
 
7 5
 import ai.pai.lensman.bean.PhotoBean;
8 6
 
9 7
 public class SessionPresenter implements SessionContract.Presenter,SessionInteractor.SessionListener{
10 8
 
11
-    private Context context;
12 9
     private SessionInteractor interactor;
13 10
     private ArrayList<PhotoBean> photoList;
14 11
     private SessionContract.View sessionView;
15 12
 
16
-    public SessionPresenter(Context context, String sessionId, SessionContract.View view){
17
-        this.context = context;
13
+    public SessionPresenter(String lensmanId, String sessionId, SessionContract.View view){
18 14
         this.sessionView = view;
19 15
         photoList = new ArrayList<>();
20
-        interactor = new SessionInteractor(context,sessionId,this);
16
+        interactor = new SessionInteractor(lensmanId,sessionId,this);
21 17
     }
22 18
 
23 19
     @Override
@@ -37,18 +33,21 @@ public class SessionPresenter implements SessionContract.Presenter,SessionIntera
37 33
 
38 34
     @Override
39 35
     public void onSessionStartSuccess(String session) {
40
-
36
+        sessionView.showToast("session启动成功");
41 37
     }
42 38
 
43 39
     @Override
44 40
     public void onSessionStartError(String session) {
45
-
41
+        sessionView.showToast("session启动失败");
46 42
     }
47 43
 
48 44
     @Override
49
-    public void onSessionPhotoCaptured(final PhotoBean bean) {
45
+    public void onSessionPhotoCaptured(final ArrayList<PhotoBean> beans) {
50 46
         sessionView.showPhotoRecyclerView();
51
-        sessionView.addNewPhoto(bean);
47
+        sessionView.showToast("发现新增照片"+beans.size()+"张");
48
+        for (PhotoBean bean : beans){
49
+            sessionView.addNewPhoto(bean);
50
+        }
52 51
     }
53 52
 
54 53
     @Override

kodo - Gogs: Go Git Service

Keine Beschreibung

models.py 58B

    from django.db import models # Create your models here.