Lua 应用开发Vela 能力模块

dataman

数据订阅模块,绑定设备数据源到 LVGL 对象

数据订阅模块,绑定设备数据源到 LVGL 对象。

dataman.subscribe(key, obj, callback)

local token = dataman.subscribe("timeHour", labelObj, function(obj, value)
    local hour = value // 256
    obj:set { text = tostring(hour) }
end)
参数类型说明
keystring数据源键名
objuserdata绑定的 LVGL 对象,回调中作为第一参数传回
callbackfunction(obj, value)数据更新回调

value 经过 256 倍编码,使用前必须 value // 256。值为 2147483647(INT_MAX)表示数据无效。

dataman.pause(token) / dataman.resume(token)

dataman.pause(token)
dataman.resume(token)
参数类型说明
tokenanysubscribe 的返回值

已知键名

键名说明值范围
timeHour小时0–23
timeMinute分钟0–59
timeSecond秒0–59
dateDay日期未知
dateMonth月份未知
dateWeek星期未知
systemStatusBattery电池电量未知
timeHourHigh高位小时未知
timeHourLow低位小时未知
timeMinuteHigh高位分钟未知
timeMinuteLow低位分钟未知
timeSecondHigh低位秒未知
timeSecondLow低位秒未知
timeCentiSecond厘秒未知
healthPressureIndex压力指数0–100
systemSensorFusionAltitude海拔(米)整数,INT_MAX 无效
weatherCurrentSunRiseHour日出时0–23,INT_MAX 无效
weatherCurrentSunRiseMinute日出分0–59,INT_MAX 无效
weatherCurrentSunSetHour日落时0–23,INT_MAX 无效
weatherCurrentSunSetMinute日落分0–59,INT_MAX 无效
batteryLevelAOD/表盘绑定(观察到)未知
calorieAOD/表盘绑定(观察到)未知
heartRateAOD/表盘绑定(观察到)未知
healthHeartRate心率未知
healthStepCount步数未知
stepAOD/表盘绑定(观察到)未知
weatherConditionAOD/表盘绑定(观察到)未知

标记为“观察到”的键来自设备表盘/AOD 资源绑定,尚未通过 dataman.subscribe 验证。键名规则为驼峰式:healthXxx、weatherXxx、timeXxx、systemXxx。

社区补充(洛汐文档库)

以下内容来自洛汐文档库《dataman 数据订阅》:https://docs.luoxe.cn/docs/vela/lua/dataman/

dataman 把表盘数据源绑定到一个 LVGL 对象。数据变化时,宿主把对象和值传给回调。subscribe(key, object, callback) 三个参数均为必填,回调签名为 function(object, value)。

local token = dataman.subscribe("timeHour", label, function(obj, value)
  local hour = value // 256
  obj:set { text = string.format("%02d", hour) }
end)

Q24.8 定点数值解码

大多数数值采用 Q24.8 定点格式,即真实值乘以 256。使用整数除法 value // 256 解码。无效值通常为该运行环境的整数最大值(如 2147483647);使用数据前应按数据源定义判断。

pause / resume 语义

pause 暂停派发但保留订阅;resume 恢复。建议页面隐藏时暂停,显示时恢复。

时间与日期数据源(补充键名)

数据源含义
timeHourLow / timeHourHigh小时低位/高位
timeMinuteLow / timeMinuteHigh分钟低位/高位
timeSecondLow / timeSecondHigh秒低位/高位
timeCentiSecond厘秒
timeCentiSecondLow / timeCentiSecondHigh厘秒低位/高位
dateYear年
dateYearDigit1..4年的各位数字
dateMonth / dateMonthLow / dateMonthHigh月及拆分位
dateDay / dateDayLow / dateDayHigh日及拆分位
dateWeek星期
dateLunarYear / dateLunarMonth / dateLunarDay农历年月日
dateWeekStringShortCN / dateWeekStringFullCN中文星期文本
dateWeekStringFullPascalEN / FullUpperEN / FullLowerEN英文完整星期文本
dateWeekStringShortPascalEN / ShortUpperEN / ShortLowerEN英文短星期文本
dateMonthStringShortCN中文月份文本
dateMonthStringFullPascalEN / FullUpperEN / FullLowerEN英文完整月份文本
dateMonthStringShortPascalEN / ShortUpperEN / ShortLowerEN英文短月份文本
miscIsPM是否下午
miscIs24H是否 24 小时制
miscTimeSection时间段
miscdateYestarday / miscdateTomorrow昨日/明日标识;拼写以接口为准
dualtimeHour / dualtimeMinute / dualtimeSecond第二时区时间
dualtimeHour12H / dualtimeHour24H第二时区小时格式
dualtimeCity第二时区城市

部分环境还提供 timeHour、timeMinute、timeSecond 组合值。

健康数据源(补充键名)

分类数据源
步数healthStepCount、healthStepCountDigit1..5、healthStepProgress、healthStepKiloMeter、healthStepTarget
心率healthHeartRate、healthHeartRateZone、healthHeartRateMin、healthHeartRateMax
热量healthCalorie、healthCalorieValue、healthCalorieProgress、healthCalorieTarget
站立healthStandCount、healthStandProgress、healthStandTarget
血氧/压力healthOxygenSpO2、healthPressureIndex
血压healthBloodDiastolicPressureMmhg、healthBloodSystolicPressureMmhg、healthBloodDiastolicPressureKpa、healthBloodSystolicPressureKpa、healthBloodPressureUnit
睡眠healthSleepDuration、healthSleepDurationMinute、healthSleepScore、healthSleepQuality、healthSleepTargetProgress
运动healthExerciseDuration、healthExerciseProgress、healthExerciseTarget、healthEnergyConsumed
其他healthMiscRecoveryTime、healthMiscRunPowerIndex、healthMiscTodayVitalityValue、healthMiscSevenDaysVitalityValue
血糖healthBloodSugarValue、healthBloodSugarUpdateTsString

天气数据源(补充键名)

分类数据源
日出日落weatherCurrentSunRise、weatherCurrentSunSet、weatherCurrentSunRiseHour、weatherCurrentSunRiseMinute、weatherCurrentSunSetHour、weatherCurrentSunSetMinute
温度weatherTemperatureUnit、weatherCurrentTemperature、weatherCurrentTemperatureFahrenheit、weatherCurrentTemperatureFeel
当前天气weatherCurrentHumidity、weatherCurrentWeather、weatherCurrentWindDirection、weatherCurrentWindAngle、weatherCurrentWindSpeed、weatherCurrentWindLevel
环境weatherCurrentAirQualityIndex、weatherCurrentAirQualityLevel、weatherCurrentChanceOfRain、weatherCurrentPressure、weatherCurrentVisibility、weatherCurrentUVIndex、weatherCurrentDressIndex
今日weatherTodayTemperatureMax、weatherTodayTemperatureMin、weatherTodayTemperatureMaxFahrenheit、weatherTodayTemperatureMinFahrenheit
明日weatherTomorrowTemperatureMax、weatherTomorrowTemperatureMin、weatherTomorrowTemperatureMaxFahrenheit、weatherTomorrowTemperatureMinFahrenheit

系统与闹钟数据源(补充键名)

systemStatusBattery、systemStatusCharge、systemStatusDisturb、systemStatusBluetooth、systemStatusWifi、systemStatusScreenLock、systemSensorFusionAltitude、appAlarmHour、appAlarmMinute。

完整示例

local tokens = {}

local function bind(key, obj, render)
  tokens[#tokens + 1] = dataman.subscribe(key, obj, function(target, raw)
    render(target, raw // 256)
  end)
end

bind("systemStatusBattery", batteryLabel, function(obj, value)
  obj:set { text = string.format("%d%%", value) }
end)

function pageOnPause
  for _, token in ipairs(tokens) do dataman.pause(token) end
end

function pageOnResume
  for _, token in ipairs(tokens) do dataman.resume(token) end
end

机型可用性

机型可用性
Xiaomi Smart Band 10 Pro✅
Xiaomi Watch S4 41mm✅

参考来源:官方 docs-lua / 洛汐文档库

大纲