All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.huawei.hilink.c2c.integration.helper.model.DeviceSnapshotTranslation.kt Maven / Gradle / Ivy

Go to download

The helper library streamlines the HiLink C2C integration and exposes a simple API.

There is a newer version: 1.3.2
Show newest version
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
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy