]> git.basschouten.com Git - openhab-addons.git/blob
77cb181d68e1cbe1310464bb676fac5ebf121461
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.icloud.internal;
14
15 import java.io.IOException;
16 import java.net.URI;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.icloud.internal.utilities.JsonUtils;
21
22 /**
23  * This class gives access to the find my iPhone (FMIP) service.
24  *
25  * @author Simon Spielmann - Initial Contribution.
26  */
27 @NonNullByDefault
28 public class FindMyIPhoneServiceManager {
29
30     private ICloudSession session;
31
32     private URI fmipRefreshUrl;
33
34     private URI fmipSoundUrl;
35
36     private static final String FMIP_ENDPOINT = "/fmipservice/client/web";
37
38     /**
39      * The constructor.
40      *
41      * @param session {@link ICloudSession} to use for API calls.
42      * @param serviceRoot Root URL for FMIP service.
43      */
44     public FindMyIPhoneServiceManager(ICloudSession session, String serviceRoot) {
45         this.session = session;
46         this.fmipRefreshUrl = URI.create(serviceRoot + FMIP_ENDPOINT + "/refreshClient");
47         this.fmipSoundUrl = URI.create(serviceRoot + FMIP_ENDPOINT + "/playSound");
48     }
49
50     /**
51      * Receive client information as JSON.
52      *
53      * @return Information about all clients as JSON
54      *         {@link org.openhab.binding.icloud.internal.handler.dto.json.response.ICloudDeviceInformation}.
55      *
56      * @throws IOException if I/O error occurred
57      * @throws InterruptedException if this blocking request was interrupted
58      * @throws ICloudApiResponseException if the request failed (e.g. not OK HTTP return code)
59      *
60      */
61     public String refreshClient() throws IOException, InterruptedException, ICloudApiResponseException {
62         Map<String, Object> request = Map.of("clientContext",
63                 Map.of("fmly", true, "shouldLocate", true, "selectedDevice", "All", "deviceListVersion", 1));
64         return session.post(this.fmipRefreshUrl.toString(), JsonUtils.toJson(request), null);
65     }
66
67     /**
68      * Play sound (find my iPhone) on given device.
69      *
70      * @param deviceId ID of the device to play sound on
71      * @throws IOException if I/O error occurred
72      * @throws InterruptedException if this blocking request was interrupted
73      * @throws ICloudApiResponseException if the request failed (e.g. not OK HTTP return code)
74      */
75     public void playSound(String deviceId) throws IOException, InterruptedException, ICloudApiResponseException {
76         Map<String, Object> request = Map.of("device", deviceId, "fmyl", true, "subject", "Message from openHAB.");
77         session.post(this.fmipSoundUrl.toString(), JsonUtils.toJson(request), null);
78     }
79 }