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;
8 import net.osdn.gokigen.pkremote.camera.utils.XmlElement;
10 import java.util.ArrayList;
11 import java.util.List;
13 public class SonyCameraDeviceProvider implements ISonyCamera
15 private static final String TAG = SonyCameraDeviceProvider.class.getSimpleName();
16 private final List<ISonyApiService> apiServices;
17 private final String ddUrl;
18 private final String udn;
19 private final String friendlyName;
20 private final String modelName;
21 private final String iconUrl;
24 * コンストラクタ: staticメソッド searchPanasonicCameraDevice() で生成する
27 private SonyCameraDeviceProvider(String ddUrl, String friendlyName, String modelName, String udn, String iconUrl)
30 this.friendlyName = friendlyName;
31 this.modelName = modelName;
33 this.iconUrl = iconUrl;
34 Log.v(TAG, "Sony Device : " + this.friendlyName + "(" + this.modelName + ") " + this.ddUrl + " " + this.udn + " " + this.iconUrl);
36 apiServices = new ArrayList<>();
44 public boolean hasApiService(@NonNull String serviceName)
48 for (ISonyApiService apiService : apiServices)
50 if (serviceName.equals(apiService.getName()))
55 Log.v(TAG, "no API Service : " + serviceName + "[" + apiServices.size() + "]");
69 public List<ISonyApiService> getApiServices()
79 public String getFriendlyName()
81 return (friendlyName);
89 public String getModelName()
99 public String getDdUrl()
109 private void addApiService(String name, String actionUrl)
111 Log.v(TAG, "API : " + name + " : " + actionUrl);
112 SonyApiService service = new SonyApiService(name, actionUrl);
113 apiServices.add(service);
120 public static ISonyCamera searchSonyCameraDevice(@NonNull String ddUrl)
122 SonyCameraDeviceProvider device = null;
126 ddXml = SimpleHttpClient.httpGet(ddUrl, -1);
127 Log.d(TAG, "fetch () httpGet done. : " + ddXml.length());
128 if (ddXml.length() < 2)
131 Log.v(TAG, "NO BODY");
142 //Log.v(TAG, "ddXml : " + ddXml);
143 XmlElement rootElement = XmlElement.parse(ddXml);
146 if ("root".equals(rootElement.getTagName()))
149 XmlElement deviceElement = rootElement.findChild("device");
150 String friendlyName = deviceElement.findChild("friendlyName").getValue();
151 String modelName = deviceElement.findChild("modelName").getValue();
152 String udn = deviceElement.findChild("UDN").getValue();
156 XmlElement iconListElement = deviceElement.findChild("iconList");
157 List<XmlElement> iconElements = iconListElement.findChildren("icon");
158 for (XmlElement iconElement : iconElements)
160 // Choose png icon to show Android UI.
161 if ("image/png".equals(iconElement.findChild("mimetype").getValue()))
163 String uri = iconElement.findChild("url").getValue();
164 String hostUrl = toSchemeAndHost(ddUrl);
165 iconUrl = hostUrl + uri;
168 device = new SonyCameraDeviceProvider(ddUrl, friendlyName, modelName, udn, iconUrl);
170 // "av:X_ScalarWebAPI_DeviceInfo"
171 XmlElement wApiElement = deviceElement.findChild("X_ScalarWebAPI_DeviceInfo");
172 XmlElement wApiServiceListElement = wApiElement.findChild("X_ScalarWebAPI_ServiceList");
173 List<XmlElement> wApiServiceElements = wApiServiceListElement.findChildren("X_ScalarWebAPI_Service");
174 for (XmlElement wApiServiceElement : wApiServiceElements)
176 String serviceName = wApiServiceElement.findChild("X_ScalarWebAPI_ServiceType").getValue();
177 String actionUrl = wApiServiceElement.findChild("X_ScalarWebAPI_ActionList_URL").getValue();
178 device.addApiService(serviceName, actionUrl);
186 Log.d(TAG, "fetch () parsing XML done.");
189 Log.v(TAG, "device is null.");
194 private static String toSchemeAndHost(String url)
196 int i = url.indexOf("://"); // http:// or https://
201 int j = url.indexOf("/", i + 3);
206 return (url.substring(0, j));
209 private static String toHost(String url)
211 int i = url.indexOf("://"); // http:// or https://
216 int j = url.indexOf(":", i + 3);
220 return (url.substring(i + 3, j));