更新时间:2025-11-14 10:51:06
专为图表数据可视化设计的统计API套件,提供四种不同时间粒度的设备数据统计接口,帮助开发者快速实现:
统计能力使用需要提交工单开通产品数据统计,请在使用前 提交快速工单。
✅ 四种时间粒度 - 15分钟、小时、天、月,满足各类图表需求
✅ 灵活统计方式 - 支持求和、最大值、最小值、平均值等7种统计类型
✅ 智能数据补全 - 自动填充空缺数据,保证图表连续性
✅ 开箱即用 - 单例模式设计,无需初始化直接调用
| 接口 | 时间粒度 | 典型应用 | 数据点数 |
|---|---|---|---|
| get15MinData | 15分钟 | 实时监控、精细化分析 | 96点/天 |
| getHourData | 小时 | 日内趋势、小时对比 | 24点/天 |
| getDayData | 天 | 周报表、月报表 | 自定义范围 |
| getMonthData | 月 | 年度报表、长期趋势 | 自定义范围 |
| 统计类型 | 枚举值 | 说明 | 适用场景 |
|---|---|---|---|
| SUM | sum | 求和 | 能耗累计、用量统计 |
| MAX | max | 最大值 | 峰值分析、最高温度 |
| MIN | min | 最小值 | 谷值分析、最低温度 |
| AVG | avg | 平均值 | 平均功率、平均温度 |
| MINUX | minux | 差值 | 增量计算 |
| COUNT | count | 次数 | 开关次数、触发次数 |
| RECENT | recent | 最近值 | 实时状态(仅小时) |
使用建议:
// 能耗统计用sum
type = StatType.SUM.value
// 温度分析用avg
type = StatType.AVG.value
// 峰值功率用max
type = StatType.MAX.value
获取单日内以15分钟为间隔的设备数据统计,适合实时监控图表和精细化数据分析。
数据特点:
YYYYMMDDhhmm (如:202411130000、202411130015)fun get15MinData(
devId: String, // 设备ID
dpId: String, // 功能点ID(整数)
date: String, // 日期:YYYYMMDD
type: String = "sum", // 统计类型(默认sum)
auto: Int = 0, // 数据补全策略
keepScalaPoint: Boolean = false, // 保留小数点
callback: IThingDataCallback<String?>
)
参数详解
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| devId | String | ✅ | - | 设备ID(从设备对象获取) |
| dpId | String | ✅ | - | 功能点ID |
| date | String | ✅ | - | 查询日期,格式:20241113 |
| type | String | ❌ | “sum” | 统计类型,建议使用StatType枚举 |
| auto | Int | ❌ | 0 | 0=补0,1=补前值,2=补# |
| keepScalaPoint | Boolean | ❌ | false | 是否保留小数点 |
| callback | IThingDataCallback | ✅ | - | 回调接口 |
场景:绘制今日用电量15分钟级别折线图
// Kotlin示例
class PowerConsumptionChart : Fragment() {
private fun loadChartData() {
val today = SimpleDateFormat("yyyyMMdd", Locale.getDefault())
.format(Date())
ThingSmartBusinessRequest.get15MinData(
devId = deviceId,
dpId = 101, // 假设101是电量dp点
date = today,
type = StatType.SUM.value, // 使用枚举更安全
auto = 0, // 无数据时补0
keepScalaPoint = true, // 保留小数位
callback = object : IThingDataCallback<String?> {
override fun onSuccess(result: String?) {
parseAndDrawChart(result)
}
override fun onError(errorCode: String?, errorMessage: String?) {
showError("加载失败: $errorMessage")
}
}
)
}
private fun parseAndDrawChart(json: String?) {
try {
val data = JSONObject(json)
val result = data.getJSONObject("result")
// 解析数据点
val entries = mutableListOf<Entry>()
result.keys().forEach { timeKey ->
val value = result.getString(timeKey).toFloat()
entries.add(Entry(timeKey, value))
}
// 绘制图表(使用MPAndroidChart等库)
drawLineChart(entries)
} catch (e: Exception) {
Log.e(TAG, "数据解析失败", e)
}
}
}
{
"result": {
"202411130000": "12.34",
"202411130015": "11.89",
"202411130030": "13.45",
"202411130045": "12.01",
"202411130100": "14.23",
"202411130115": "13.67",
...
"202411132345": "10.12"
},
"t": 1699862400000,
"success": true,
"status": "ok"
}
数据说明:
获取单日内以小时为间隔的设备数据统计,最常用的图表数据接口。
数据特点:
YYYYMMDDhh (如:2024111300、2024111301)fun getHourData(
devId: String, // 设备ID
dpId: String, // 功能点ID
date: String, // 日期:YYYYMMDD
auto: Int = 0, // 数据补全策略
type: String = "sum", // 统计类型
keepScalaPoint: Boolean = false,
callback: IThingDataCallback<String?>
)
场景:绘制今日24小时温度曲线
class TemperatureChart : Fragment() {
private fun load24HourTemperature() {
ThingSmartBusinessRequest.getHourData(
devId = deviceId,
dpId = "102",
date = "20241113",
type = StatType.AVG.value, // 温度用平均值
auto = 1, // 用前一个值填充空缺
callback = object : IThingDataCallback<String?> {
override fun onSuccess(result: String?) {
drawTemperatureChart(result)
}
override fun onError(errorCode: String?, errorMessage: String?) {
Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show()
}
}
)
}
}
{
"result": {
"2024111300": "22.5",
"2024111301": "22.3",
"2024111302": "22.1",
...
"2024111323": "21.8"
},
"t": 1699862400000,
"success": true,
"status": "ok"
}
获取指定日期范围内以天为间隔的设备数据统计,适合周报表、月报表。
数据特点:
YYYYMMDD (如:20241101、20241102)fun getDayData(
devId: String,
dpId: String,
type: String = "sum",
startDay: String, // 起始日期:YYYYMMDD
endDay: String, // 结束日期:YYYYMMDD
auto: Int? = null,
keepScalaPoint: Boolean = false,
callback: IThingDataCallback<String?>
)
场景:绘制本周7天用电量柱状图
class WeeklyConsumption : Fragment() {
private fun loadWeeklyData() {
val calendar = Calendar.getInstance()
val endDay = SimpleDateFormat("yyyyMMdd").format(calendar.time)
calendar.add(Calendar.DAY_OF_YEAR, -6)
val startDay = SimpleDateFormat("yyyyMMdd").format(calendar.time)
ThingSmartBusinessRequest.getDayData(
devId = deviceId,
dpId = 101,
type = StatType.SUM.value,
startDay = startDay, // 7天前
endDay = endDay, // 今天
auto = 0,
callback = object : IThingDataCallback<String?> {
override fun onSuccess(result: String?) {
drawWeeklyBarChart(result)
}
override fun onError(errorCode: String?, errorMessage: String?) {
Log.e(TAG, "加载周数据失败: $errorMessage")
}
}
)
}
}
{
"result": {
"result": {
"20241107": "45.67",
"20241108": "52.34",
"20241109": "48.90",
"20241110": "51.23",
"20241111": "49.56",
"20241112": "53.78",
"20241113": "47.89"
},
"min": "20241113"
},
"t": 1699862400000,
"success": true,
"status": "ok"
}
获取指定月份范围内以月为间隔的设备数据统计,适合年度报表、长期趋势分析。
数据特点:
YYYYMM (如:202401、202402)fun getMonthData(
devId: String,
dpId: String,
type: String = "sum",
startMonth: String, // 起始月份:YYYYMM
endMonth: String, // 结束月份:YYYYMM
auto: Int? = null,
keepScalaPoint: Boolean = false,
callback: IThingDataCallback<String?>
)
场景:绘制今年12个月用电量对比图
class YearlyConsumption : Fragment() {
private fun loadYearlyData() {
val currentYear = Calendar.getInstance().get(Calendar.YEAR)
ThingSmartBusinessRequest.getMonthData(
devId = deviceId,
dpId = 101,
type = StatType.SUM.value,
startMonth = "${currentYear}01", // 1月
endMonth = "${currentYear}12", // 12月
callback = object : IThingDataCallback<String?> {
override fun onSuccess(result: String?) {
drawYearlyChart(result)
}
override fun onError(errorCode: String?, errorMessage: String?) {
showError("加载年度数据失败")
}
}
)
}
}
{
"result": {
"result": {
"202401": "1234.56",
"202402": "1156.78",
"202403": "1289.90",
"202404": "1345.67",
"202405": "1298.45",
"202406": "1423.89",
"202407": "1567.23",
"202408": "1489.01",
"202409": "1376.54",
"202410": "1298.76",
"202411": "1234.00",
"202412": "0"
},
"min": "202412"
},
"t": 1699862400000,
"success": true,
"status": "ok"
}
该内容对您有帮助吗?
是意见反馈该内容对您有帮助吗?
是意见反馈