2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.icloud.internal;
15 import java.io.IOException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.icloud.internal.utilities.JsonUtils;
23 * This class gives access to the find my iPhone (FMIP) service.
25 * @author Simon Spielmann - Initial Contribution.
28 public class FindMyIPhoneServiceManager {
30 private ICloudSession session;
32 private URI fmipRefreshUrl;
34 private URI fmipSoundUrl;
36 private static final String FMIP_ENDPOINT = "/fmipservice/client/web";
41 * @param session {@link ICloudSession} to use for API calls.
42 * @param serviceRoot Root URL for FMIP service.
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");
51 * Receive client information as JSON.
53 * @return Information about all clients as JSON
54 * {@link org.openhab.binding.icloud.internal.handler.dto.json.response.ICloudDeviceInformation}.
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)
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);
68 * Play sound (find my iPhone) on given device.
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)
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);