OSDN Git Service

SONYの画像取得のために確認中。いったん保存。
[gokigen/PKRemote.git] / app / src / main / java / net / osdn / gokigen / pkremote / camera / vendor / sony / wrapper / SonyCameraDeviceProvider.java
1 package net.osdn.gokigen.pkremote.camera.vendor.sony.wrapper;
2
3 import android.util.Log;
4
5 import androidx.annotation.NonNull;
6
7 import net.osdn.gokigen.pkremote.camera.utils.SimpleHttpClient;
8 import net.osdn.gokigen.pkremote.camera.utils.XmlElement;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 public class SonyCameraDeviceProvider implements ISonyCamera
14 {
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;
22
23     /**
24      *   コンストラクタ: staticメソッド searchPanasonicCameraDevice() で生成する
25      *
26      */
27     private SonyCameraDeviceProvider(String ddUrl, String friendlyName, String modelName, String udn, String iconUrl)
28     {
29         this.ddUrl = ddUrl;
30         this.friendlyName = friendlyName;
31         this.modelName = modelName;
32         this.udn = udn;
33         this.iconUrl = iconUrl;
34         Log.v(TAG, "Sony Device : " + this.friendlyName + "(" + this.modelName + ") " + this.ddUrl + "  " + this.udn + " " + this.iconUrl);
35
36         apiServices = new ArrayList<>();
37     }
38
39     /**
40      *
41      *
42      */
43     @Override
44     public boolean hasApiService(@NonNull String serviceName)
45     {
46         try
47         {
48             for (ISonyApiService apiService : apiServices)
49             {
50                 if (serviceName.equals(apiService.getName()))
51                 {
52                     return (true);
53                 }
54             }
55             Log.v(TAG, "no API Service : " + serviceName + "[" + apiServices.size() + "]");
56         }
57         catch (Exception e)
58         {
59             e.printStackTrace();
60         }
61         return (false);
62     }
63
64     /**
65      *
66      *
67      */
68     @Override
69     public List<ISonyApiService> getApiServices()
70     {
71         return (apiServices);
72     }
73
74     /**
75      *
76      *
77      */
78     @Override
79     public String getFriendlyName()
80     {
81         return (friendlyName);
82     }
83
84     /**
85      *
86      *
87      */
88     @Override
89     public String getModelName()
90     {
91         return (modelName);
92     }
93
94     /**
95      *
96      *
97      */
98     @Override
99     public String getDdUrl()
100     {
101         return (ddUrl);
102     }
103
104
105     /**
106      *
107      *
108      */
109     private void addApiService(String name, String actionUrl)
110     {
111         Log.v(TAG, "API : " + name + "  : " + actionUrl);
112         SonyApiService service = new SonyApiService(name, actionUrl);
113         apiServices.add(service);
114     }
115
116     /**
117      *
118      *
119      */
120     public static ISonyCamera searchSonyCameraDevice(@NonNull String ddUrl)
121     {
122         SonyCameraDeviceProvider device = null;
123         String ddXml;
124         try
125         {
126             ddXml = SimpleHttpClient.httpGet(ddUrl, -1);
127             Log.d(TAG, "fetch () httpGet done. : " + ddXml.length());
128             if (ddXml.length() < 2)
129             {
130                 // 内容がないときは...終了する
131                 Log.v(TAG, "NO BODY");
132                 return (null);
133             }
134         }
135         catch (Exception e)
136         {
137             e.printStackTrace();
138             return (null);
139         }
140         try
141         {
142             //Log.v(TAG, "ddXml : " + ddXml);
143             XmlElement rootElement = XmlElement.parse(ddXml);
144
145             // "root"
146             if ("root".equals(rootElement.getTagName()))
147             {
148                 // "device"
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();
153
154                 // "iconList"
155                 String iconUrl = "";
156                 XmlElement iconListElement = deviceElement.findChild("iconList");
157                 List<XmlElement> iconElements = iconListElement.findChildren("icon");
158                 for (XmlElement iconElement : iconElements)
159                 {
160                     // Choose png icon to show Android UI.
161                     if ("image/png".equals(iconElement.findChild("mimetype").getValue()))
162                     {
163                         String uri = iconElement.findChild("url").getValue();
164                         String hostUrl = toSchemeAndHost(ddUrl);
165                         iconUrl = hostUrl + uri;
166                     }
167                 }
168                 device = new SonyCameraDeviceProvider(ddUrl, friendlyName, modelName, udn, iconUrl);
169
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)
175                 {
176                     String serviceName = wApiServiceElement.findChild("X_ScalarWebAPI_ServiceType").getValue();
177                     String actionUrl = wApiServiceElement.findChild("X_ScalarWebAPI_ActionList_URL").getValue();
178                     device.addApiService(serviceName, actionUrl);
179                 }
180             }
181         }
182         catch (Exception e)
183         {
184             e.printStackTrace();
185         }
186         Log.d(TAG, "fetch () parsing XML done.");
187         if (device == null)
188         {
189             Log.v(TAG, "device is null.");
190         }
191         return (device);
192     }
193
194     private static String toSchemeAndHost(String url)
195     {
196         int i = url.indexOf("://"); // http:// or https://
197         if (i == -1) {
198             return ("");
199         }
200
201         int j = url.indexOf("/", i + 3);
202         if (j == -1) {
203             return ("");
204         }
205
206         return (url.substring(0, j));
207     }
208
209     private static String toHost(String url)
210     {
211         int i = url.indexOf("://"); // http:// or https://
212         if (i == -1) {
213             return ("");
214         }
215
216         int j = url.indexOf(":", i + 3);
217         if (j == -1) {
218             return ("");
219         }
220         return (url.substring(i + 3, j));
221     }
222 }