Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added connection latency to charge point view #31

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ object ChargePointTable : LongIdTable("charge_point") {
val apiUrl = varchar("api_url", 1024)
val maxKw = double("max_kw")

val messageCount = integer("message_count")
.default(0)
val averageLatencyMillis = long("average_latency_millis")
.default(0)

// Heartbeat
val heartbeatAt = timestamp("heartbeat_at")

Expand Down Expand Up @@ -152,6 +157,9 @@ class ChargePointDAO(
var apiUrl by ChargePointTable.apiUrl
var maxKw by ChargePointTable.maxKw

var averageLatencyMillis by ChargePointTable.averageLatencyMillis
var messageCount by ChargePointTable.messageCount

var heartbeatAt by ChargePointTable.heartbeatAt

var status by ChargePointTable.status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ fun chargePointComponent(
modifier = Modifier.align(Alignment.CenterEnd)
)
}

Text(
"Identity: ${chargePoint.identity}",
modifier = Modifier.clickable {
clipboardManager.setText(AnnotatedString(chargePoint.identity))
}
)
Text("Latency: ${chargePoint.averageLatencyMillis} ms (${chargePoint.messageCount})")
Text("Status: ${chargePoint.status}")
Text("Status At: ${chargePoint.statusAt.toReadable()}")
Text("Firmware: ${chargePoint.firmware}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ import com.monta.library.ocpp.v16.core.StatusNotificationRequest
import com.monta.ocpp.emulator.chargepoint.entity.ChargePointDAO
import com.monta.ocpp.emulator.chargepoint.service.ChargePointService
import com.monta.ocpp.emulator.common.idValue
import com.monta.ocpp.emulator.common.util.MontaSerialization
import com.monta.ocpp.emulator.common.util.injectAnywhere
import com.monta.ocpp.emulator.interceptor.MessageInterceptor
import com.monta.ocpp.emulator.logger.GlobalLogger
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.*
import io.ktor.util.collections.*
import io.ktor.websocket.*
import kotlinx.coroutines.delay
import mu.KotlinLogging
import java.time.Duration
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicLong
import kotlin.math.max
import kotlin.math.min
import kotlin.math.pow
Expand All @@ -41,6 +46,10 @@ class ChargePointConnection(
}
private var connectionAttempts = 1

private val requestIdMap = ConcurrentMap<String, Long>()
private val totalLatencyNanos = AtomicLong(0L)
private val messageCount = AtomicInteger(0)

val chargePoint: ChargePointDAO
get() = chargePointService.getById(chargePointId)

Expand Down Expand Up @@ -91,6 +100,7 @@ class ChargePointConnection(
sendFrame = { message ->
GlobalLogger.logSend(chargePoint, message)
this.send(message)
logLatency(message)
},
closeConnection = { reason ->
this.close(CloseReason(CloseReason.Codes.NORMAL, reason))
Expand All @@ -113,7 +123,10 @@ class ChargePointConnection(
val message = frame.readText()
val newMessage = interceptor.intercept(chargePoint.identity, message)
GlobalLogger.logReceive(chargePoint, message)
if (newMessage != null) ocppClientV16.receiveMessage(chargePoint.identity, newMessage)
if (newMessage != null) {
ocppClientV16.receiveMessage(chargePoint.identity, newMessage)
}
logLatency(message)
}

else -> {
Expand All @@ -135,6 +148,29 @@ class ChargePointConnection(
}
}

private suspend fun logLatency(
websocketMessage: String
) {
val jsonNode = MontaSerialization.objectMapper.readTree(websocketMessage)
val requestId = jsonNode.get(1).asText()
if (requestIdMap.contains(requestId)) {
val timestamp = requestIdMap.remove(requestId)
if (timestamp == null) {
return
}
val latency = System.nanoTime() - timestamp
val total = totalLatencyNanos.addAndGet(latency)
val messages = messageCount.incrementAndGet()
val averageLatencyMillis = Duration.ofNanos(total / messages).toMillis()
chargePointService.update(chargePoint) {
this.messageCount = messages
this.averageLatencyMillis = averageLatencyMillis
}
} else {
requestIdMap[requestId] = System.nanoTime()
}
}

suspend fun disconnect(
closeReason: CloseReason = CloseReason(
code = CloseReason.Codes.NORMAL,
Expand Down