1 package net.osdn.gokigen.pkremote.camera.vendor.sony.wrapper;
3 import android.util.Log;
5 import androidx.annotation.NonNull;
7 import net.osdn.gokigen.pkremote.camera.utils.SimpleHttpClient;
9 import org.json.JSONArray;
10 import org.json.JSONObject;
12 import java.util.ArrayList;
13 import java.util.List;
15 class SonyCameraApi implements ISonyCameraApi
17 private static final String TAG = SonyCameraApi.class.getSimpleName();
18 private static final boolean FULL_LOG = true;
20 // API server device you want to send requests.
21 private final ISonyCamera sonyCamera;
22 private int requestId;
25 public static ISonyCameraApi newInstance(@NonNull ISonyCamera target)
27 return (new SonyCameraApi(target));
30 private SonyCameraApi(final @NonNull ISonyCamera target)
36 private String findActionListUrl(String service)
38 List<ISonyApiService> services = sonyCamera.getApiServices();
39 for (ISonyApiService apiService : services)
41 if (apiService.getName().equals(service))
43 return (apiService.getActionUrl());
46 Log.v(TAG, "actionUrl not found. service : " + service);
60 private void log(String msg)
69 private JSONObject communicateJSON(@NonNull String service, @NonNull String method, @NonNull JSONArray params, @NonNull String version, int timeoutMs)
73 JSONObject requestJson = new JSONObject().put("method", method)
74 .put("params", params)
76 .put("version", version);
77 String url = findActionListUrl(service) + "/" + service;
78 log("Request: " + requestJson.toString());
79 String responseJson = SimpleHttpClient.httpPost(url, requestJson.toString(), timeoutMs);
80 log("Response: " + responseJson);
81 return (new JSONObject(responseJson));
85 log("Exception : " + method + " " + version);
88 return (new JSONObject());
92 public JSONObject getAvailableApiList()
96 return (communicateJSON("camera", "getAvailableApiList", new JSONArray(), "1.0", -1));
102 return (new JSONObject());
106 public JSONObject getApplicationInfo()
110 return (communicateJSON("camera", "getApplicationInfo", new JSONArray(), "1.0", -1));
116 return (new JSONObject());
120 public JSONObject getShootMode()
124 return (communicateJSON("camera", "getShootMode", new JSONArray(), "1.0", -1));
130 return (new JSONObject());
134 public JSONObject setShootMode(@NonNull String shootMode)
138 return (communicateJSON("camera", "getShootMode", new JSONArray().put(shootMode), "1.0", -1));
144 return (new JSONObject());
148 public JSONObject getAvailableShootMode()
151 return (communicateJSON("camera", "getAvailableShootMode", new JSONArray(), "1.0", -1));
152 } catch (Exception e) {
155 return (new JSONObject());
159 public JSONObject getSupportedShootMode()
162 return (communicateJSON("camera", "getSupportedShootMode", new JSONArray(), "1.0", -1));
163 } catch (Exception e) {
166 return (new JSONObject());
170 public JSONObject setTouchAFPosition(double Xpos, double Ypos)
174 Log.v(TAG, "setTouchAFPosition (" + Xpos + ", " + Ypos + ")");
175 return (communicateJSON("camera", "setTouchAFPosition", new JSONArray().put(Xpos).put(Ypos), "1.0", -1));
181 return (new JSONObject());
185 public JSONObject getTouchAFPosition()
189 return (communicateJSON("camera", "getTouchAFPosition", new JSONArray(), "1.0", -1));
195 return (new JSONObject());
199 public JSONObject cancelTouchAFPosition()
203 return (communicateJSON("camera", "cancelTouchAFPosition", new JSONArray(), "1.0", -1));
209 return (new JSONObject());
213 public JSONObject actHalfPressShutter()
217 return (communicateJSON("camera", "actHalfPressShutter", new JSONArray(), "1.0", -1));
223 return (new JSONObject());
227 public JSONObject cancelHalfPressShutter()
231 return (communicateJSON("camera", "cancelHalfPressShutter", new JSONArray(), "1.0", -1));
237 return (new JSONObject());
241 public JSONObject setFocusMode(String focusMode)
245 Log.v(TAG, "setFocusMode (" + focusMode + ")");
246 return (communicateJSON("camera", "setFocusMode", new JSONArray().put(focusMode), "1.0", -1));
252 return (new JSONObject());
256 public JSONObject getFocusMode()
260 return (communicateJSON("camera", "getFocusMode", new JSONArray(), "1.0", -1));
266 return (new JSONObject());
270 public JSONObject getSupportedFocusMode()
274 return (communicateJSON("camera", "getSupportedFocusMode", new JSONArray(), "1.0", -1));
280 return (new JSONObject());
284 public JSONObject getAvailableFocusMode()
288 return (communicateJSON("camera", "getAvailableFocusMode", new JSONArray(), "1.0", -1));
294 return (new JSONObject());
298 public JSONObject startLiveview()
301 return (communicateJSON("camera", "startLiveview", new JSONArray(), "1.0", -1));
302 } catch (Exception e) {
305 return (new JSONObject());
310 public JSONObject stopLiveview()
313 return (communicateJSON("camera", "stopLiveview", new JSONArray(), "1.0", -1));
314 } catch (Exception e) {
317 return (new JSONObject());
322 public JSONObject startRecMode()
325 return (communicateJSON("camera", "startRecMode", new JSONArray(), "1.0", -1));
326 } catch (Exception e) {
329 return (new JSONObject());
334 public JSONObject actTakePicture()
337 return (communicateJSON("camera", "actTakePicture", new JSONArray(), "1.0", -1));
338 } catch (Exception e) {
341 return (new JSONObject());
345 public JSONObject awaitTakePicture()
349 return (communicateJSON("camera", "awaitTakePicture", new JSONArray(), "1.0", -1));
355 return (new JSONObject());
359 public JSONObject startMovieRec()
362 return (communicateJSON("camera", "startMovieRec", new JSONArray(), "1.0", -1));
363 } catch (Exception e) {
366 return (new JSONObject());
370 public JSONObject stopMovieRec()
373 return (communicateJSON("camera", "stopMovieRec", new JSONArray(), "1.0", -1));
374 } catch (Exception e) {
377 return (new JSONObject());
381 public JSONObject actZoom(@NonNull String direction, @NonNull String movement)
384 return (communicateJSON("camera", "actZoom", new JSONArray().put(direction).put(movement), "1.0", -1));
385 } catch (Exception e) {
388 return (new JSONObject());
393 public JSONObject getEvent(@NonNull String version, boolean longPollingFlag)
396 int longPollingTimeout = (longPollingFlag) ? 20000 : 8000; // msec
397 return (communicateJSON("camera", "getEvent", new JSONArray().put(longPollingFlag), version, longPollingTimeout));
398 } catch (Exception e) {
401 return (new JSONObject());
406 public JSONObject setCameraFunction(@NonNull String cameraFunction)
409 return (communicateJSON("camera", "setCameraFunction", new JSONArray().put(cameraFunction), "1.0", -1));
410 } catch (Exception e) {
413 return (new JSONObject());
418 public JSONObject getCameraMethodTypes()
421 return (communicateJSON("camera", "getCameraMethodTypes", new JSONArray(), "1.0", -1));
422 } catch (Exception e) {
425 return (new JSONObject());
430 public JSONObject getAvcontentMethodTypes()
433 return (communicateJSON("avContent", "getMethodTypes", new JSONArray(), "1.0", -1));
434 } catch (Exception e) {
437 return (new JSONObject());
441 public JSONObject getStorageInformation()
444 return (communicateJSON("camera", "getStorageInformation", new JSONArray(), "1.0", -1));
445 } catch (Exception e) {
448 return (new JSONObject());
452 public JSONObject getSchemeList()
455 return (communicateJSON("avContent", "getSchemeList", new JSONArray(), "1.0", -1));
456 } catch (Exception e) {
459 return (new JSONObject());
464 public JSONObject getSourceList(String scheme)
467 JSONObject params = new JSONObject().put("scheme", scheme);
468 return (communicateJSON("avContent", "getSourceList", new JSONArray().put(0, params), "1.0", -1));
469 } catch (Exception e) {
472 return (new JSONObject());
476 public JSONObject getContentCountFlatAll(String uri)
479 JSONObject params = new JSONObject();
480 params.put("uri", uri);
481 params.put("target", "all");
482 params.put("view", "flat");
483 return (communicateJSON("avContent", "getContentCount", new JSONArray().put(0, params), "1.2", -1));
484 } catch (Exception e) {
487 return (new JSONObject());
491 public JSONObject getContentList(JSONArray params)
494 return (communicateJSON("avContent", "getContentList", params, "1.3", -1));
495 } catch (Exception e) {
498 return (new JSONObject());
502 public JSONObject setStreamingContent(String uri)
505 JSONObject params = new JSONObject().put("remotePlayType", "simpleStreaming").put("uri", uri);
506 return (communicateJSON("avContent", "setStreamingContent", new JSONArray().put(0, params), "1.0", -1));
507 } catch (Exception e) {
510 return (new JSONObject());
515 public JSONObject startStreaming()
518 return (communicateJSON("avContent", "startStreaming", new JSONArray(), "1.0", -1));
519 } catch (Exception e) {
522 return (new JSONObject());
527 public JSONObject stopStreaming()
530 return (communicateJSON("avContent", "stopStreaming", new JSONArray(), "1.0", -1));
531 } catch (Exception e) {
534 return (new JSONObject());
539 public List<String> getSonyApiServiceList()
543 List<String> serviceList = new ArrayList<>();
544 List<ISonyApiService> services = sonyCamera.getApiServices();
545 for (ISonyApiService apiService : services)
547 serviceList.add(apiService.getName());
549 return (serviceList);
559 public JSONObject callGenericSonyApiMethod(@NonNull String service, @NonNull String method, @NonNull JSONArray params, @NonNull String version)
561 return (communicateJSON(service, method, params, version, -1));
564 static boolean isErrorReply(JSONObject replyJson)
566 return ((replyJson != null && replyJson.has("error")));