|
| 1 | +package ac.at.uibk.dps.dapr.bms |
| 2 | + |
| 3 | +import ac.at.uibk.dps.dapr.bms.bindings.* |
| 4 | +import java.net.URI |
| 5 | +import java.net.http.HttpClient |
| 6 | +import java.net.http.HttpRequest |
| 7 | +import java.net.http.HttpResponse |
| 8 | +import org.apache.fory.Fory |
| 9 | +import org.apache.fory.ThreadSafeFory |
| 10 | +import org.apache.fory.config.Language |
| 11 | +import org.apache.fory.memory.MemoryBuffer |
| 12 | + |
| 13 | +class BuildingServiceClient(private val baseUrl: String = "http://localhost:8005") { |
| 14 | + |
| 15 | + private val client = HttpClient.newHttpClient() |
| 16 | + |
| 17 | + companion object { |
| 18 | + private val fory: ThreadSafeFory = |
| 19 | + Fory.builder() |
| 20 | + .withLanguage(Language.XLANG) |
| 21 | + .withRefTracking(true) |
| 22 | + .buildThreadSafeFory() |
| 23 | + .apply { |
| 24 | + listOf( |
| 25 | + EmptyRequest::class.java, |
| 26 | + RoomRequest::class.java, |
| 27 | + LightLevelRequest::class.java, |
| 28 | + EvacuationLightsRequest::class.java, |
| 29 | + HvacRequest::class.java, |
| 30 | + IndoorTempResponse::class.java, |
| 31 | + BlindLevelRequest::class.java, |
| 32 | + OutdoorTempResponse::class.java, |
| 33 | + OccupancyRequest::class.java, |
| 34 | + OccupancyResponse::class.java, |
| 35 | + FireDetectionRequest::class.java, |
| 36 | + FireDetectionResponse::class.java, |
| 37 | + FireDoorRequest::class.java, |
| 38 | + ArcFaultResponse::class.java, |
| 39 | + TripCircuitBreakerRequest::class.java, |
| 40 | + GasLeakResponse::class.java, |
| 41 | + GasValveRequest::class.java, |
| 42 | + RoomTempResponse::class.java, |
| 43 | + ScheduleModeResponse::class.java, |
| 44 | + EnergyPriceResponse::class.java, |
| 45 | + GridStatusResponse::class.java, |
| 46 | + SecurityNotificationRequest::class.java, |
| 47 | + DoorRouteResponse::class.java, |
| 48 | + AuthRequest::class.java, |
| 49 | + AuthResponse::class.java, |
| 50 | + AccessRuleRequest::class.java, |
| 51 | + AccessDecisionResponse::class.java, |
| 52 | + DoorLockRequest::class.java, |
| 53 | + ) |
| 54 | + .forEach { register(it, it.simpleName) } |
| 55 | + } |
| 56 | + |
| 57 | + private val threadBuffer = ThreadLocal.withInitial { MemoryBuffer.newHeapBuffer(1024) } |
| 58 | + } |
| 59 | + |
| 60 | + private fun action(path: String, req: Any = EmptyRequest()) = execute<Unit>(path, req, null, null) |
| 61 | + |
| 62 | + private inline fun <reified T> query( |
| 63 | + path: String, |
| 64 | + req: Any = EmptyRequest(), |
| 65 | + noinline cb: (T) -> Unit, |
| 66 | + ) = execute(path, req, T::class.java, cb) |
| 67 | + |
| 68 | + private fun <T> execute(path: String, req: Any, resClass: Class<T>?, cb: ((T) -> Unit)?) { |
| 69 | + try { |
| 70 | + val buffer = threadBuffer.get().apply { writerIndex(0) } |
| 71 | + fory.serialize(buffer, req) |
| 72 | + |
| 73 | + val request = |
| 74 | + HttpRequest.newBuilder() |
| 75 | + .uri(URI.create("$baseUrl$path")) |
| 76 | + .header("Content-Type", "application/x-fury") |
| 77 | + .POST(HttpRequest.BodyPublishers.ofByteArray(buffer.getBytes(0, buffer.writerIndex()))) |
| 78 | + .build() |
| 79 | + |
| 80 | + client.sendAsync(request, HttpResponse.BodyHandlers.ofByteArray()).thenAccept { res -> |
| 81 | + if (res.statusCode() in 200..299) { |
| 82 | + if (cb != null && resClass != null) { |
| 83 | + @Suppress("UNCHECKED_CAST") cb(fory.deserialize(res.body()) as T) |
| 84 | + } |
| 85 | + } else { |
| 86 | + println(" [SERVICE ERROR] $path -> Status: ${res.statusCode()}") |
| 87 | + } |
| 88 | + } |
| 89 | + } catch (e: Exception) { |
| 90 | + println(" [SERVICE ERROR] $path: ${e.message}") |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Lighting |
| 95 | + fun turnOn(roomId: String) = action("/turnOn", RoomRequest(roomId)) |
| 96 | + |
| 97 | + fun turnOff(roomId: String) = action("/turnOff", RoomRequest(roomId)) |
| 98 | + |
| 99 | + fun dim(roomId: String) = action("/dim", RoomRequest(roomId)) |
| 100 | + |
| 101 | + fun turnUserLevel(roomId: String, level: Int) = |
| 102 | + action("/userLevelLight", LightLevelRequest(roomId, level)) |
| 103 | + |
| 104 | + fun evacuationLights(roomId: String, emergencyId: String) = |
| 105 | + action("/evacuationLights", EvacuationLightsRequest(roomId, emergencyId)) |
| 106 | + |
| 107 | + // HVAC |
| 108 | + fun setHvac(mode: String, roomId: String) = action("/setHVAC", HvacRequest(mode, roomId)) |
| 109 | + |
| 110 | + fun getIndoorTemp(roomId: String, cb: (Double) -> Unit) = |
| 111 | + query<IndoorTempResponse>("/getIndoorTemp", RoomRequest(roomId)) { cb(it.indoorTemp) } |
| 112 | + |
| 113 | + // Shading |
| 114 | + fun blindsHalf(roomId: String) = action("/blindsHalf", RoomRequest(roomId)) |
| 115 | + |
| 116 | + fun blindsOpen(roomId: String) = action("/blindsOpen", RoomRequest(roomId)) |
| 117 | + |
| 118 | + fun blindsClose(roomId: String) = action("/blindsClose", RoomRequest(roomId)) |
| 119 | + |
| 120 | + fun blindsUserLevel(roomId: String, level: Int) = |
| 121 | + action("/userLevelBlinds", BlindLevelRequest(roomId, level)) |
| 122 | + |
| 123 | + fun getOutdoorTemp(cb: (Double) -> Unit) = |
| 124 | + query<OutdoorTempResponse>("/getOutdoorTemp") { cb(it.outdoorTemp) } |
| 125 | + |
| 126 | + // Occupancy |
| 127 | + fun detectOccupancy(imageData: String, cb: (Boolean) -> Unit) = |
| 128 | + query<OccupancyResponse>("/detectOccupancy", OccupancyRequest(imageData)) { |
| 129 | + cb(it.occupancyDetected) |
| 130 | + } |
| 131 | + |
| 132 | + fun maintenance(roomId: String) = action("/maintenance", RoomRequest(roomId)) |
| 133 | + |
| 134 | + // Fire Safety |
| 135 | + fun detectFire(img: String, zone: String, cb: (String, String) -> Unit) = |
| 136 | + query<FireDetectionResponse>("/detectFire", FireDetectionRequest(img, zone)) { |
| 137 | + cb(it.fireDetectionResult, it.emergencyInRoom) |
| 138 | + } |
| 139 | + |
| 140 | + fun openFireDoor(doorId: String) = action("/openFireDoor", FireDoorRequest(doorId)) |
| 141 | + |
| 142 | + fun closeFireDoor(doorId: String) = action("/closeFireDoor", FireDoorRequest(doorId)) |
| 143 | + |
| 144 | + // Electrical |
| 145 | + fun checkArcFault(cb: (String) -> Unit) = |
| 146 | + query<ArcFaultResponse>("/checkArcFault") { cb(it.arcFaultLocation) } |
| 147 | + |
| 148 | + fun tripCircuitBreaker(loc: String) = |
| 149 | + action("/tripCircuitBreaker", TripCircuitBreakerRequest(loc)) |
| 150 | + |
| 151 | + fun acknowledgedElectrical(cb: () -> Unit) = action("/electricalFaultAcknowledged") |
| 152 | + |
| 153 | + // Gas Safety |
| 154 | + fun checkGasLeak(cb: (String) -> Unit) = |
| 155 | + query<GasLeakResponse>("/checkGasFault") { cb(it.gasLeakLocation) } |
| 156 | + |
| 157 | + fun closeGasValve(loc: String) = action("/closeGasValve", GasValveRequest(loc)) |
| 158 | + |
| 159 | + fun cutPower(loc: String) = action("/cutPower", GasValveRequest(loc)) |
| 160 | + |
| 161 | + fun gasLeakPurged() = action("/gasLeakPurged") |
| 162 | + |
| 163 | + // Temp Safety |
| 164 | + fun getRoomTemp(roomId: String, cb: (Double) -> Unit) = |
| 165 | + query<RoomTempResponse>("/getRoomTemp", RoomRequest(roomId)) { cb(it.roomTemp) } |
| 166 | + |
| 167 | + fun highRiskTemp(roomId: String) = action("/highRiskTemp", RoomRequest(roomId)) |
| 168 | + |
| 169 | + // Schedules |
| 170 | + fun getScheduleMode(cb: (String) -> Unit) = |
| 171 | + query<ScheduleModeResponse>("/getScheduleMode") { cb(it.currentSchedule) } |
| 172 | + |
| 173 | + fun initializeZone(cb: () -> Unit) = action("/initializeZone") |
| 174 | + |
| 175 | + // Energy |
| 176 | + fun getEnergyPrice(cb: (Double) -> Unit) = |
| 177 | + query<EnergyPriceResponse>("/getEnergyPrice") { cb(it.energyPrice) } |
| 178 | + |
| 179 | + fun checkGridStatus(cb: (String) -> Unit) = |
| 180 | + query<GridStatusResponse>("/checkGridStatus") { cb(it.gridStatus) } |
| 181 | + |
| 182 | + // Security & Access |
| 183 | + fun notifySecurity(msg: String) = action("/notifySecurity", SecurityNotificationRequest(msg)) |
| 184 | + |
| 185 | + fun getDoorRouteType(doorId: String, cb: (Boolean, String) -> Unit) = |
| 186 | + query<DoorRouteResponse>("/getDoorRouteType", FireDoorRequest(doorId)) { |
| 187 | + cb(it.isEvacuationRoute, it.zoneId) |
| 188 | + } |
| 189 | + |
| 190 | + fun authenticateUser(cardId: String, cb: (String, String, String) -> Unit) = |
| 191 | + query<AuthResponse>("/authenticateUser", AuthRequest(cardId)) { |
| 192 | + cb(it.userId, it.userRole, it.authenticationStatus) |
| 193 | + } |
| 194 | + |
| 195 | + fun checkAccessRule(role: String, zone: String, mode: String, cb: (String) -> Unit) = |
| 196 | + query<AccessDecisionResponse>("/checkAccessRule", AccessRuleRequest(role, zone, mode)) { |
| 197 | + cb(it.accessDecision) |
| 198 | + } |
| 199 | + |
| 200 | + fun controlDoorLock(doorId: String, cmd: String) = |
| 201 | + action("/controlDoorLock", DoorLockRequest(doorId, cmd)) |
| 202 | +} |
0 commit comments