|
1 | 1 | package net.cryptic_game.microservice.network.communication; |
2 | 2 |
|
3 | 3 | import net.cryptic_game.microservice.MicroService; |
4 | | -import net.cryptic_game.microservice.utils.JSONUtils; |
| 4 | +import net.cryptic_game.microservice.utils.JSON; |
| 5 | +import net.cryptic_game.microservice.utils.JSONBuilder; |
5 | 6 | import org.json.simple.JSONObject; |
6 | 7 |
|
7 | 8 | import java.util.UUID; |
8 | 9 |
|
| 10 | +import static net.cryptic_game.microservice.utils.JSONBuilder.simple; |
| 11 | + |
9 | 12 | public class Device { |
10 | 13 |
|
11 | | - private static boolean isOwner(UUID device, UUID user) { |
12 | | - JSONObject result = MicroService.getInstance().contactMicroservice("device", new String[] { "owner" }, |
13 | | - JSONUtils.simple("device_uuid", device.toString())); |
| 14 | + private static boolean isOwner(UUID device, UUID user) { |
| 15 | + JSON result = new JSON(MicroService.getInstance().contactMicroService("device", new String[]{"owner"}, |
| 16 | + simple("device_uuid", device.toString()))); |
| 17 | + |
| 18 | + UUID owner = result.getUUID("owner"); |
| 19 | + |
| 20 | + return owner != null && owner.equals(user); |
| 21 | + } |
| 22 | + |
| 23 | + private static boolean isPartOwner(UUID device, UUID user) { |
| 24 | + JSON result = new JSON(MicroService.getInstance().contactMicroService("service", new String[]{"check_part_owner"}, |
| 25 | + JSONBuilder.anJSON().add("device_uuid", device.toString()).add("user_uuid", user.toString()).build())); |
| 26 | + |
| 27 | + Boolean ok = result.get("ok", Boolean.class); |
| 28 | + |
| 29 | + return ok != null && ok; |
| 30 | + } |
14 | 31 |
|
15 | | - return result.containsKey("owner") && UUID.fromString((String) result.get("owner")).equals(user); |
16 | | - } |
| 32 | + public static boolean checkPermissions(UUID device, UUID user) { |
| 33 | + return isOwner(device, user) || isPartOwner(device, user); |
| 34 | + } |
17 | 35 |
|
18 | | - private static boolean isPartOwner(UUID device, UUID user) { |
19 | | - JSONObject result = MicroService.getInstance().contactMicroservice("service", new String[] { "check_part_owner" }, |
20 | | - JSONUtils.json().add("device_uuid", device.toString()).add("user_uuid", user.toString()).build()); |
| 36 | + /** |
| 37 | + * Checks if a Device is online. |
| 38 | + * |
| 39 | + * @param device the {@link UUID} of the Device. |
| 40 | + * @return <code>true</code>: The Device is Online.<br><code>false</code>: The Device is Offline or can't be found. |
| 41 | + */ |
| 42 | + public static boolean isOnline(final UUID device) { |
| 43 | + final JSONObject result = MicroService.getInstance().contactMicroService("device", new String[]{"ping"}, |
| 44 | + simple("device_uuid", device.toString())); |
21 | 45 |
|
22 | | - return result.containsKey("ok") && (boolean) result.get("ok"); |
23 | | - } |
24 | | - |
25 | | - public static boolean checkPermissions(UUID device, UUID user) { |
26 | | - return isOwner(device, user) || isPartOwner(device, user); |
27 | | - } |
| 46 | + if (result.containsKey("online")) { |
| 47 | + return (boolean) result.get("online"); |
| 48 | + } |
28 | 49 |
|
| 50 | + return false; |
| 51 | + } |
29 | 52 | } |
0 commit comments