001package Torello.Browser.BrowserAPI; 002 003import java.util.*; 004import javax.json.*; 005import javax.json.stream.*; 006import java.io.*; 007 008import java.lang.reflect.Method; 009import java.lang.reflect.Parameter; 010import java.util.function.Function; 011 012import Torello.Browser.BrowserEvent; 013import Torello.Browser.JavaScriptAPI.*; 014import Torello.Browser.helper.*; 015 016import Torello.Java.Additional.*; 017import Torello.Java.JSON.*; 018 019import static Torello.Java.JSON.JFlag.*; 020 021import Torello.Java.StrCmpr; 022import Torello.JavaDoc.StaticFunctional; 023import Torello.JavaDoc.JDHeaderBackgroundImg; 024import Torello.JavaDoc.Excuse; 025 026/** 027 * <SPAN CLASS=COPIEDJDK><B><CODE>[No Description Provided by Google]</CODE></B></SPAN> 028 * 029 * <EMBED CLASS='external-html' DATA-FILE-ID=CODE_GEN_NOTE> 030 */ 031@StaticFunctional(Excused={"counter"}, Excuses={Excuse.CONFIGURATION}) 032@JDHeaderBackgroundImg(EmbedTagFileID="WOOD_PLANK_NOTE") 033public class DeviceAccess 034{ 035 // ******************************************************************************************** 036 // ******************************************************************************************** 037 // Class Header Stuff 038 // ******************************************************************************************** 039 // ******************************************************************************************** 040 041 042 // No Pubic Constructors 043 private DeviceAccess () { } 044 045 // These two Vector's are used by all the "Methods" exported by this class. java.lang.reflect 046 // is used to generate the JSON String's. It saves thousands of lines of Auto-Generated Code. 047 private static final Map<String, Vector<String>> parameterNames = new HashMap<>(); 048 private static final Map<String, Vector<Class<?>>> parameterTypes = new HashMap<>(); 049 050 // Some Methods do not take any parameters - for instance all the "enable()" and "disable()" 051 // I simply could not get ride of RAW-TYPES and UNCHECKED warnings... so there are now, 052 // offically, two empty-vectors. One for String's, and the other for Classes. 053 054 private static final Vector<String> EMPTY_VEC_STR = new Vector<>(); 055 private static final Vector<Class<?>> EMPTY_VEC_CLASS = new Vector<>(); 056 057 static 058 { 059 for (Method m : DeviceAccess.class.getMethods()) 060 { 061 // This doesn't work! The parameter names are all "arg0" ... "argN" 062 // It works for java.lang.reflect.Field, BUT NOT java.lang.reflect.Parameter! 063 // 064 // Vector<String> parameterNamesList = new Vector<>(); -- NOPE! 065 066 Vector<Class<?>> parameterTypesList = new Vector<>(); 067 068 for (Parameter p : m.getParameters()) parameterTypesList.add(p.getType()); 069 070 parameterTypes.put( 071 m.getName(), 072 (parameterTypesList.size() > 0) ? parameterTypesList : EMPTY_VEC_CLASS 073 ); 074 } 075 } 076 077 static 078 { 079 Vector<String> v = null; 080 081 parameterNames.put("enable", EMPTY_VEC_STR); 082 083 parameterNames.put("disable", EMPTY_VEC_STR); 084 085 v = new Vector<String>(2); 086 parameterNames.put("selectPrompt", v); 087 Collections.addAll(v, new String[] 088 { "id", "deviceId", }); 089 090 v = new Vector<String>(1); 091 parameterNames.put("cancelPrompt", v); 092 Collections.addAll(v, new String[] 093 { "id", }); 094 } 095 096 097 // ******************************************************************************************** 098 // ******************************************************************************************** 099 // Types - Static Inner Classes 100 // ******************************************************************************************** 101 // ******************************************************************************************** 102 103 // public static class RequestId => String 104 105 // public static class DeviceId => String 106 107 /** Device information displayed in a user prompt to select a device. */ 108 public static class PromptDevice 109 extends BaseType 110 implements java.io.Serializable 111 { 112 /** For Object Serialization. java.io.Serializable */ 113 protected static final long serialVersionUID = 1; 114 115 public boolean[] optionals() 116 { return new boolean[] { false, false, }; } 117 118 /** <CODE>[No Description Provided by Google]</CODE> */ 119 public final String id; 120 121 /** Display name as it appears in a device request user prompt. */ 122 public final String name; 123 124 /** 125 * Constructor 126 * 127 * @param id - 128 * 129 * @param name Display name as it appears in a device request user prompt. 130 */ 131 public PromptDevice(String id, String name) 132 { 133 // Exception-Check(s) to ensure that if any parameters which are not declared as 134 // 'Optional', but have a 'null' value anyway, that a NullPointerException shall throw. 135 136 if (id == null) THROWS.throwNPE("id"); 137 if (name == null) THROWS.throwNPE("name"); 138 139 this.id = id; 140 this.name = name; 141 } 142 143 /** 144 * JSON Object Constructor 145 * @param jo A Json-Object having data about an instance of {@code 'PromptDevice'}. 146 */ 147 public PromptDevice (JsonObject jo) 148 { 149 this.id = ReadJSON.getString(jo, "id", false, true); 150 this.name = ReadJSON.getString(jo, "name", false, true); 151 } 152 153 154 /** Checks whether {@code 'this'} equals an input Java-{@code Object} */ 155 public boolean equals(Object other) 156 { 157 if (this == other) return true; 158 if (other == null) return false; 159 if (other.getClass() != this.getClass()) return false; 160 161 PromptDevice o = (PromptDevice) other; 162 163 return 164 Objects.equals(this.id, o.id) 165 && Objects.equals(this.name, o.name); 166 } 167 168 /** Generates a Hash-Code for {@code 'this'} instance */ 169 public int hashCode() 170 { 171 return 172 Objects.hashCode(this.id) 173 + Objects.hashCode(this.name); 174 } 175 } 176 177 /** 178 * A device request opened a user prompt to select a device. Respond with the 179 * selectPrompt or cancelPrompt command. 180 */ 181 public static class deviceRequestPrompted 182 extends BrowserEvent 183 implements java.io.Serializable 184 { 185 /** For Object Serialization. java.io.Serializable */ 186 protected static final long serialVersionUID = 1; 187 188 public boolean[] optionals() 189 { return new boolean[] { false, false, }; } 190 191 /** <CODE>[No Description Provided by Google]</CODE> */ 192 public final String id; 193 194 /** <CODE>[No Description Provided by Google]</CODE> */ 195 public final DeviceAccess.PromptDevice[] devices; 196 197 /** 198 * Constructor 199 * 200 * @param id - 201 * 202 * @param devices - 203 */ 204 public deviceRequestPrompted(String id, DeviceAccess.PromptDevice[] devices) 205 { 206 super("DeviceAccess", "deviceRequestPrompted", 2); 207 208 // Exception-Check(s) to ensure that if any parameters which are not declared as 209 // 'Optional', but have a 'null' value anyway, that a NullPointerException shall throw. 210 211 if (id == null) THROWS.throwNPE("id"); 212 if (devices == null) THROWS.throwNPE("devices"); 213 214 this.id = id; 215 this.devices = devices; 216 } 217 218 /** 219 * JSON Object Constructor 220 * @param jo A Json-Object having data about an instance of {@code 'deviceRequestPrompted'}. 221 */ 222 public deviceRequestPrompted (JsonObject jo) 223 { 224 super("DeviceAccess", "deviceRequestPrompted", 2); 225 226 this.id = ReadJSON.getString(jo, "id", false, true); 227 this.devices = (jo.getJsonArray("devices") == null) 228 ? null 229 : RJArrIntoStream.objArr(jo.getJsonArray("devices"), null, 0, DeviceAccess.PromptDevice.class).toArray(DeviceAccess.PromptDevice[]::new); 230 231 } 232 233 234 /** Checks whether {@code 'this'} equals an input Java-{@code Object} */ 235 public boolean equals(Object other) 236 { 237 if (this == other) return true; 238 if (other == null) return false; 239 if (other.getClass() != this.getClass()) return false; 240 241 deviceRequestPrompted o = (deviceRequestPrompted) other; 242 243 return 244 Objects.equals(this.id, o.id) 245 && Arrays.deepEquals(this.devices, o.devices); 246 } 247 248 /** Generates a Hash-Code for {@code 'this'} instance */ 249 public int hashCode() 250 { 251 return 252 Objects.hashCode(this.id) 253 + Arrays.deepHashCode(this.devices); 254 } 255 } 256 257 258 // Counter for keeping the WebSocket Request ID's distinct. 259 private static int counter = 1; 260 261 /** 262 * Enable events in this domain. 263 * 264 * @return An instance of <CODE>{@link Script}<String, {@link JsonObject}, 265 * {@link Ret0}></CODE> 266 * 267 * <BR /><BR />This {@code Script} instance must be <B STYLE='color:red'>executed</B> before the 268 * browser receives the invocation-request. 269 * 270 * <BR /><BR />This Browser-Function <I>does not have</I> a return-value. You may choose to 271 * <B STYLE='color: red'>await</B> the {@link Promise}{@code <JsonObject,} {@link Ret0} 272 * {@code >} to ensure the Browser Function has run to completion. 273 */ 274 public static Script<String, JsonObject, Ret0> enable() 275 { 276 final int webSocketID = 49000000 + counter++; 277 final boolean[] optionals = new boolean[0]; 278 279 // Convert Method Parameters into JSON. Build the JSON Request-Object (as a String) 280 String requestJSON = WriteJSON.get( 281 parameterTypes.get("enable"), 282 parameterNames.get("enable"), 283 optionals, webSocketID, 284 "DeviceAccess.enable" 285 ); 286 287 // This Remote Command does not have a Return-Value. 288 return new Script<> 289 (webSocketID, requestJSON, VOID_RETURN.NoReturnValues); 290 } 291 292 /** 293 * Disable events in this domain. 294 * 295 * @return An instance of <CODE>{@link Script}<String, {@link JsonObject}, 296 * {@link Ret0}></CODE> 297 * 298 * <BR /><BR />This {@code Script} instance must be <B STYLE='color:red'>executed</B> before the 299 * browser receives the invocation-request. 300 * 301 * <BR /><BR />This Browser-Function <I>does not have</I> a return-value. You may choose to 302 * <B STYLE='color: red'>await</B> the {@link Promise}{@code <JsonObject,} {@link Ret0} 303 * {@code >} to ensure the Browser Function has run to completion. 304 */ 305 public static Script<String, JsonObject, Ret0> disable() 306 { 307 final int webSocketID = 49001000 + counter++; 308 final boolean[] optionals = new boolean[0]; 309 310 // Convert Method Parameters into JSON. Build the JSON Request-Object (as a String) 311 String requestJSON = WriteJSON.get( 312 parameterTypes.get("disable"), 313 parameterNames.get("disable"), 314 optionals, webSocketID, 315 "DeviceAccess.disable" 316 ); 317 318 // This Remote Command does not have a Return-Value. 319 return new Script<> 320 (webSocketID, requestJSON, VOID_RETURN.NoReturnValues); 321 } 322 323 /** 324 * Select a device in response to a DeviceAccess.deviceRequestPrompted event. 325 * 326 * @param id - 327 * 328 * @param deviceId - 329 * 330 * @return An instance of <CODE>{@link Script}<String, {@link JsonObject}, 331 * {@link Ret0}></CODE> 332 * 333 * <BR /><BR />This {@code Script} instance must be <B STYLE='color:red'>executed</B> before the 334 * browser receives the invocation-request. 335 * 336 * <BR /><BR />This Browser-Function <I>does not have</I> a return-value. You may choose to 337 * <B STYLE='color: red'>await</B> the {@link Promise}{@code <JsonObject,} {@link Ret0} 338 * {@code >} to ensure the Browser Function has run to completion. 339 */ 340 public static Script<String, JsonObject, Ret0> selectPrompt(String id, String deviceId) 341 { 342 // Exception-Check(s) to ensure that if any parameters which are not declared as 343 // 'Optional', but have a 'null' value anyway, that a NullPointerException shall throw. 344 345 if (id == null) THROWS.throwNPE("id"); 346 if (deviceId == null) THROWS.throwNPE("deviceId"); 347 348 final int webSocketID = 49002000 + counter++; 349 final boolean[] optionals = { false, false, }; 350 351 // Convert Method Parameters into JSON. Build the JSON Request-Object (as a String) 352 String requestJSON = WriteJSON.get( 353 parameterTypes.get("selectPrompt"), 354 parameterNames.get("selectPrompt"), 355 optionals, webSocketID, 356 "DeviceAccess.selectPrompt", 357 id, deviceId 358 ); 359 360 // This Remote Command does not have a Return-Value. 361 return new Script<> 362 (webSocketID, requestJSON, VOID_RETURN.NoReturnValues); 363 } 364 365 /** 366 * Cancel a prompt in response to a DeviceAccess.deviceRequestPrompted event. 367 * 368 * @param id - 369 * 370 * @return An instance of <CODE>{@link Script}<String, {@link JsonObject}, 371 * {@link Ret0}></CODE> 372 * 373 * <BR /><BR />This {@code Script} instance must be <B STYLE='color:red'>executed</B> before the 374 * browser receives the invocation-request. 375 * 376 * <BR /><BR />This Browser-Function <I>does not have</I> a return-value. You may choose to 377 * <B STYLE='color: red'>await</B> the {@link Promise}{@code <JsonObject,} {@link Ret0} 378 * {@code >} to ensure the Browser Function has run to completion. 379 */ 380 public static Script<String, JsonObject, Ret0> cancelPrompt(String id) 381 { 382 // Exception-Check(s) to ensure that if any parameters which are not declared as 383 // 'Optional', but have a 'null' value anyway, that a NullPointerException shall throw. 384 385 if (id == null) THROWS.throwNPE("id"); 386 387 final int webSocketID = 49003000 + counter++; 388 final boolean[] optionals = { false, }; 389 390 // Convert Method Parameters into JSON. Build the JSON Request-Object (as a String) 391 String requestJSON = WriteJSON.get( 392 parameterTypes.get("cancelPrompt"), 393 parameterNames.get("cancelPrompt"), 394 optionals, webSocketID, 395 "DeviceAccess.cancelPrompt", 396 id 397 ); 398 399 // This Remote Command does not have a Return-Value. 400 return new Script<> 401 (webSocketID, requestJSON, VOID_RETURN.NoReturnValues); 402 } 403 404}