commonMain.com.huawei.hilink.c2c.integration.helper.model.DeviceSnapshotTranslation.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helper-jvm Show documentation
Show all versions of helper-jvm Show documentation
The helper library streamlines the HiLink C2C integration and exposes a simple API.
package com.huawei.hilink.c2c.integration.helper.model
import com.huawei.hilink.c2c.integration.helper.LengthLimits
import com.huawei.hilink.c2c.integration.helper.ParamLenViolationException
import com.huawei.hilink.c2c.integration.helper.api.DeviceConversion
import com.huawei.hilink.c2c.integration.helper.api.DeviceSnapshot
import com.huawei.hilink.c2c.integration.helper.api.UserInfo
import com.huawei.hilink.c2c.integration.helper.callbackAdapters.snapshotSuspending
import com.huawei.hilink.c2c.integration.helper.dto.common.HiLinkDeviceServiceSnapshot
import com.huawei.hilink.c2c.integration.helper.dto.common.HiLinkDeviceSnapshot
import com.huawei.hilink.c2c.integration.helper.prepareExceptionIfViolatedBy
import com.huawei.hilink.c2c.integration.helper.time.toHiLinkTimeStamp
import kotlinx.serialization.json.Json
internal fun DeviceSnapshot.toHiLinkDeviceSnapshot(objectMapper: Json): HiLinkDeviceSnapshot {
return HiLinkDeviceSnapshot(
thirdPartyDevId = deviceId,
status = if (isOnline) "online" else "offline",
services = services.map {
HiLinkDeviceServiceSnapshot(
ts = it.timestamp.toHiLinkTimeStamp(),
sid = it.serviceIdInPartnerSystem,
data = objectMapper.parseToJsonElement(it.serviceDataJson)
)
}
)
}
internal suspend fun DeviceConversion.hiLinkSnapshotOf(userInfo: UserInfo, deviceId: String, objectMapper: Json) =
snapshotSuspending(userInfo, deviceId).toHiLinkDeviceSnapshot(objectMapper)
internal fun DeviceSnapshot.firstParamViolationOrNull(siteName: String): ParamLenViolationException? {
LengthLimits.DEVICE_ID.prepareExceptionIfViolatedBy(deviceId, siteName)
?.let { exception -> return exception }
services.forEach { mapping ->
LengthLimits.PARTNER_SERVICE_ID.prepareExceptionIfViolatedBy(
mapping.serviceIdInPartnerSystem,
siteName
)?.let { exception -> return exception }
}
return null
}