|
|
@ -40,6 +40,7 @@ data class StsConfig( |
|
|
var deviceId: String, |
|
|
var deviceId: String, |
|
|
var deviceSecret: String, |
|
|
var deviceSecret: String, |
|
|
val agentId: String, |
|
|
val agentId: String, |
|
|
|
|
|
val cmei: String = "", |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -60,10 +61,18 @@ class StsAgent( |
|
|
override val coroutineContext = Dispatchers.IO + job |
|
|
override val coroutineContext = Dispatchers.IO + job |
|
|
|
|
|
|
|
|
init { |
|
|
init { |
|
|
Log.i(TAG, "[YIDONG] StsAgent初始化: productId=${config.productId} deviceNo=${config.deviceNo} deviceId=${config.deviceId} agentId=${config.agentId} hasSecret=${config.deviceSecret.isNotEmpty()}") |
|
|
Log.i(TAG, "[YIDONG] StsAgent初始化: productId=${config.productId} deviceNo=${config.deviceNo} deviceId=${config.deviceId} agentId=${config.agentId} cmei=${config.cmei} hasSecret=${config.deviceSecret.isNotEmpty()}") |
|
|
fetchCredentials() |
|
|
fetchCredentials() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** 动态更新 CMEI(首次初始化时可能尚未获取到) */ |
|
|
|
|
|
fun updateCmei(cmei: String) { |
|
|
|
|
|
if (cmei.isNotEmpty() && cmei != config.cmei) { |
|
|
|
|
|
config = config.copy(cmei = cmei) |
|
|
|
|
|
Log.i(TAG, "[YIDONG] updateCmei: cmei已更新为 $cmei") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// ── WebSocket ───────────────────────────────────────────────────────────── |
|
|
// ── WebSocket ───────────────────────────────────────────────────────────── |
|
|
private val okHttpClient: OkHttpClient = buildTrustAllClient() |
|
|
private val okHttpClient: OkHttpClient = buildTrustAllClient() |
|
|
private var webSocket: WebSocket? = null |
|
|
private var webSocket: WebSocket? = null |
|
|
@ -190,7 +199,7 @@ class StsAgent( |
|
|
) |
|
|
) |
|
|
credentialReady = config.deviceSecret.isNotEmpty() |
|
|
credentialReady = config.deviceSecret.isNotEmpty() |
|
|
if (credentialReady) { |
|
|
if (credentialReady) { |
|
|
Log.i(TAG, "[YIDONG] fetchCredentials: 密钥获取成功 deviceId=${config.deviceId} deviceNo=${config.deviceNo}") |
|
|
Log.i(TAG, "[YIDONG] fetchCredentials: 密钥获取成功 deviceId=${config.deviceId} deviceNo=${config.deviceNo} cmei=${config.cmei}") |
|
|
onCredentialsFetched?.invoke(config.deviceId, config.deviceSecret, config.deviceNo) |
|
|
onCredentialsFetched?.invoke(config.deviceId, config.deviceSecret, config.deviceNo) |
|
|
reportHeartbeat() // 凭证获取成功后立即上报心跳 |
|
|
reportHeartbeat() // 凭证获取成功后立即上报心跳 |
|
|
} else { |
|
|
} else { |
|
|
@ -216,7 +225,7 @@ class StsAgent( |
|
|
Log.w(TAG, "[YIDONG] reportHeartbeat: 凭证未就绪,跳过上报") |
|
|
Log.w(TAG, "[YIDONG] reportHeartbeat: 凭证未就绪,跳过上报") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
Log.i(TAG, "[YIDONG] reportHeartbeat: 开始上报 deviceId=${config.deviceId} deviceNo=${config.deviceNo}") |
|
|
Log.i(TAG, "[YIDONG] reportHeartbeat: 开始上报 deviceId=${config.deviceId} deviceNo=${config.deviceNo} cmei=${config.cmei}") |
|
|
launch { |
|
|
launch { |
|
|
try { |
|
|
try { |
|
|
val netType = try { |
|
|
val netType = try { |
|
|
@ -243,7 +252,7 @@ class StsAgent( |
|
|
put("sdkVersion", "ai_http_1.0") |
|
|
put("sdkVersion", "ai_http_1.0") |
|
|
put("firmwareVersion", "1.0.0") |
|
|
put("firmwareVersion", "1.0.0") |
|
|
put("imei", "") |
|
|
put("imei", "") |
|
|
put("cmei", config.deviceNo) // 非蜂窝设备使用 SN 作为 cmei |
|
|
put("cmei", config.cmei.ifEmpty { config.deviceNo }) // 优先使用 UserDevice 的 CMEI |
|
|
put("mac", "") |
|
|
put("mac", "") |
|
|
}) |
|
|
}) |
|
|
}.toString().toRequestBody("application/json".toMediaType()) |
|
|
}.toString().toRequestBody("application/json".toMediaType()) |
|
|
|