714 lines
24 KiB
C
714 lines
24 KiB
C
/**
|
||
* @file device.c
|
||
* @brief 设备与区域管理实现:编号分配、增删改查、状态位控制、能耗与统计报表
|
||
*
|
||
* 教材对应:项目二(数据类型与类型转换)·项目三(分支结构)·项目四(循环)·
|
||
* 项目五(数组与字符数组)·项目六(函数、递归、static 变量生命周期)·
|
||
* 项目七(指针与指针出参)·项目八(结构体)·项目九(位运算与位标志)
|
||
*
|
||
* 实现约定:
|
||
* 1) 全部静态存储,不使用 malloc(NFR-04);
|
||
* 2) 字符串一律走 shs_str* 安全接口或 snprintf 定长格式化,越界只“截断/报错”,绝不越界写;
|
||
* 3) 库代码不向 stdout 打印,需要留痕时调用 logger_write(返回值用 (void) 包住);
|
||
* 4) 不修改传入数组的有效区之外的位置(数组容量按 SHS_MAX_DEVICES 约定,函数只写前 *count 个元素)。
|
||
*/
|
||
#include "shs/device.h"
|
||
#include "shs/bitops.h"
|
||
#include "shs/logger.h"
|
||
#include "shs/string_utils.h"
|
||
|
||
#include <stdio.h>
|
||
#include <string.h>
|
||
|
||
/* ---------------------------------------------------------------- 模块内常量 */
|
||
#define DEVICE_LOG_TAG "device" /**< 日志标签(项目五 字符数组) */
|
||
|
||
#define DEVICE_TEMP_MIN_C (-40.0f) /**< 合理温度下限(与传感器量程一致) */
|
||
#define DEVICE_TEMP_MAX_C 125.0f /**< 合理温度上限 */
|
||
#define DEVICE_BRIGHT_MIN 0 /**< 亮度下限 */
|
||
#define DEVICE_BRIGHT_MAX 100 /**< 亮度上限 */
|
||
#define DEVICE_REPORT_HOURS 24.0f /**< 能耗报表估算时长(小时) */
|
||
|
||
/**
|
||
* @brief 设备编号序列(文件作用域 static)
|
||
*
|
||
* 项目六 变量作用域与生命周期:static 变量存放在静态存储区,
|
||
* 函数返回后值依然保留,下次调用接着自增 —— 这正是“设备编号不重复”的实现基础。
|
||
*/
|
||
static uint16_t s_device_id_seq = 1u;
|
||
|
||
/* ---------------------------------------------------------------- 类型名表 */
|
||
/** 类型名表(项目五 数组 + 项目八 结构体数组),供 device_type_name / device_type_from_name 共用 */
|
||
static const struct {
|
||
uint8_t type; /**< 类型编号 */
|
||
const char *name; /**< 中文名(CLI 展示) */
|
||
const char *alias; /**< ASCII 别名(文本命令/网页参数用) */
|
||
} k_device_types[] = {
|
||
{ SHS_DEV_UNKNOWN, "未知", "unknown" },
|
||
{ SHS_DEV_LIGHT, "灯", "light" },
|
||
{ SHS_DEV_AC, "空调", "ac" },
|
||
{ SHS_DEV_LOCK, "智能门锁", "lock" },
|
||
{ SHS_DEV_SENSOR, "温湿度传感器", "sensor" },
|
||
{ SHS_DEV_FAN, "风扇", "fan" },
|
||
{ SHS_DEV_CURTAIN, "窗帘", "curtain" }
|
||
};
|
||
|
||
/* ---------------------------------------------------------------- 编号分配 */
|
||
/**
|
||
* @brief 取下一个可用设备编号
|
||
* 项目六 变量生命周期(static)+ 项目二 运算符与类型转换
|
||
*/
|
||
uint16_t device_next_id(void)
|
||
{
|
||
const uint16_t id = s_device_id_seq;
|
||
|
||
s_device_id_seq = (uint16_t)(s_device_id_seq + 1u);
|
||
if (s_device_id_seq == 0u) {
|
||
s_device_id_seq = 1u; /* 回绕保护:编号 0 保留为“非法编号” */
|
||
}
|
||
return id;
|
||
}
|
||
|
||
/**
|
||
* @brief 重置编号序列(测试隔离用)
|
||
* 项目六 static 变量:只有本文件的函数能改写它,外部只能经此入口复位
|
||
*/
|
||
void device_reset_id_sequence(void)
|
||
{
|
||
s_device_id_seq = 1u;
|
||
}
|
||
|
||
/* ---------------------------------------------------------------- 增删改查 */
|
||
/**
|
||
* @brief 追加设备
|
||
* 项目五 一维数组 + 项目八 结构体整体赋值 + 项目九 位标志
|
||
*/
|
||
shs_err_t device_add(Device devices[], int *count, const Device *dev)
|
||
{
|
||
if (devices == NULL || count == NULL || dev == NULL) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
if (*count < 0) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
if (*count >= SHS_MAX_DEVICES) {
|
||
return SHS_ERR_FULL; /* 项目三 边界分支 */
|
||
}
|
||
|
||
Device *slot = &devices[*count]; /* 项目七 指针定位数组元素 */
|
||
*slot = *dev; /* 项目八 结构体整体赋值(含字符数组) */
|
||
(void)shs_strcpy(slot->name, sizeof(slot->name), dev->name); /* 兜底保证 '\0' 结尾 */
|
||
|
||
slot->id = device_next_id(); /* 自动分配编号 */
|
||
slot->status = shs_bits_set_mask(slot->status, (uint16_t)SHS_ST_ONLINE); /* 项目九 掩码置位 */
|
||
|
||
(*count)++;
|
||
return SHS_OK;
|
||
}
|
||
|
||
/**
|
||
* @brief 按 id 删除并保持数组紧凑
|
||
* 项目五 数组元素搬移 + 项目四 循环
|
||
*/
|
||
shs_err_t device_remove(Device devices[], int *count, uint16_t id)
|
||
{
|
||
if (devices == NULL || count == NULL || *count <= 0 || *count > SHS_MAX_DEVICES) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
|
||
int index = -1;
|
||
for (int i = 0; i < *count; i++) {
|
||
if (devices[i].id == id) {
|
||
index = i;
|
||
break;
|
||
}
|
||
}
|
||
if (index < 0) {
|
||
return SHS_ERR_NOT_FOUND;
|
||
}
|
||
|
||
/* 左移填补:只搬动有效区内的元素,不动数组容量之外的位置 */
|
||
for (int i = index; i < *count - 1; i++) {
|
||
devices[i] = devices[i + 1];
|
||
}
|
||
(*count)--;
|
||
return SHS_OK;
|
||
}
|
||
|
||
/**
|
||
* @brief 按 id 线性查找(可写指针)
|
||
* 项目七 返回指针的函数:找到返回元素地址,未找到返回 NULL
|
||
*/
|
||
Device *device_find_by_id(Device devices[], int count, uint16_t id)
|
||
{
|
||
if (devices == NULL || count <= 0 || count > SHS_MAX_DEVICES) {
|
||
return NULL;
|
||
}
|
||
for (int i = 0; i < count; i++) {
|
||
if (devices[i].id == id) {
|
||
return &devices[i]; /* 返回数组元素地址 */
|
||
}
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
/**
|
||
* @brief 按 id 只读查找
|
||
* 项目七 const 指针:返回 const 限定指针,调用方无法经它修改设备
|
||
*/
|
||
const Device *device_find_by_id_const(const Device devices[], int count, uint16_t id)
|
||
{
|
||
if (devices == NULL || count <= 0 || count > SHS_MAX_DEVICES) {
|
||
return NULL;
|
||
}
|
||
for (int i = 0; i < count; i++) {
|
||
if (devices[i].id == id) {
|
||
return &devices[i];
|
||
}
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
/**
|
||
* @brief 按类型查找第一个匹配设备
|
||
* 项目七 返回指针 + 项目三 分支
|
||
*/
|
||
Device *device_find_first_by_type(Device devices[], int count, shs_dev_type_t type)
|
||
{
|
||
if (devices == NULL || count <= 0 || count > SHS_MAX_DEVICES) {
|
||
return NULL;
|
||
}
|
||
for (int i = 0; i < count; i++) {
|
||
if (devices[i].type == (uint8_t)type) {
|
||
return &devices[i];
|
||
}
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
/**
|
||
* @brief 按名称查找(精确匹配)
|
||
* 项目五 字符串比较 + 项目七 指针遍历
|
||
*/
|
||
Device *device_find_by_name(Device devices[], int count, const char *name)
|
||
{
|
||
if (devices == NULL || name == NULL || count <= 0 || count > SHS_MAX_DEVICES) {
|
||
return NULL;
|
||
}
|
||
for (int i = 0; i < count; i++) {
|
||
if (shs_strcmp(devices[i].name, name) == 0) {
|
||
return &devices[i];
|
||
}
|
||
}
|
||
return NULL;
|
||
}
|
||
|
||
/**
|
||
* @brief 递归二分查找(要求数组已按 id 升序)
|
||
* @return 命中的数组下标;未找到返回 -1
|
||
* 项目六 递归调用 + 项目三 区间分支
|
||
*/
|
||
int device_find_by_id_recursive(const Device devices[], int low, int high, uint16_t id)
|
||
{
|
||
if (devices == NULL || low < 0 || high < low) {
|
||
return -1; /* 递归出口 1:区间为空或参数非法 */
|
||
}
|
||
|
||
const int mid = low + (high - low) / 2; /* 写成加法形式避免 (low+high) 溢出(项目二) */
|
||
if (devices[mid].id == id) {
|
||
return mid; /* 递归出口 2:命中 */
|
||
}
|
||
if (id < devices[mid].id) {
|
||
return device_find_by_id_recursive(devices, low, mid - 1, id); /* 递归:左半区 */
|
||
}
|
||
return device_find_by_id_recursive(devices, mid + 1, high, id); /* 递归:右半区 */
|
||
}
|
||
|
||
/**
|
||
* @brief 区域树递归统计:统计 zone_id 及其所有子区域下的设备数
|
||
*
|
||
* 项目六 递归 + 项目五 数组遍历。
|
||
* 语义约定(冻结头文件未传 Room 表,故设备与区域的关联字段取 Device.room_id):
|
||
* 1) 设备归属:devices[i].room_id == zone_id 即视为挂在该区域节点下;
|
||
* 2) 子树:zones[i].parent_id == zone_id 且 zones[i].id != zone_id 的节点递归累加;
|
||
* 3) parent_id == id 表示根节点(自环),不计入任何父节点的子集合;
|
||
* 4) 防环保护:区域编号空间为 [0, SHS_MAX_ZONES),用静态访问标记避免父链成环时无限递归。
|
||
*/
|
||
int zone_count_devices_recursive(const Device devices[], int count,
|
||
const Zone zones[], int zone_count, uint8_t zone_id)
|
||
{
|
||
/* 访问标记 + 递归深度:仅当本文件为单线程使用时成立(本工程为单线程主循环) */
|
||
static uint8_t s_zone_visited[SHS_MAX_ZONES];
|
||
static int s_zone_depth;
|
||
|
||
if (devices == NULL || count <= 0 || zones == NULL || zone_count <= 0) {
|
||
return 0;
|
||
}
|
||
if (zone_count > SHS_MAX_ZONES) {
|
||
zone_count = SHS_MAX_ZONES; /* 越界保护:不超过标记数组容量 */
|
||
}
|
||
if (zone_id >= SHS_MAX_ZONES) {
|
||
return 0;
|
||
}
|
||
|
||
if (s_zone_depth == 0) {
|
||
memset(s_zone_visited, 0, sizeof(s_zone_visited)); /* 顶层调用:清空标记 */
|
||
}
|
||
if (s_zone_visited[zone_id] != 0u) {
|
||
return 0; /* 该节点已统计过(父链成环的兜底出口) */
|
||
}
|
||
s_zone_visited[zone_id] = 1u;
|
||
s_zone_depth++;
|
||
|
||
int total = 0;
|
||
|
||
/* 第一层:本区域直属设备 */
|
||
for (int i = 0; i < count; i++) {
|
||
if (devices[i].room_id == zone_id) {
|
||
total++;
|
||
}
|
||
}
|
||
|
||
/* 第二层:所有以本节点为父的子区域,递归累加 */
|
||
for (int i = 0; i < zone_count; i++) {
|
||
if (zones[i].parent_id == zone_id && zones[i].id != zone_id) {
|
||
total += zone_count_devices_recursive(devices, count, zones, zone_count, zones[i].id);
|
||
}
|
||
}
|
||
|
||
s_zone_depth--;
|
||
return total;
|
||
}
|
||
|
||
/**
|
||
* @brief 按 id 升序冒泡排序
|
||
* 项目四 循环嵌套 + 项目八 结构体整体交换
|
||
*/
|
||
shs_err_t device_sort_by_id(Device devices[], int count)
|
||
{
|
||
if (devices == NULL || count < 0 || count > SHS_MAX_DEVICES) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
if (count < 2) {
|
||
return SHS_OK;
|
||
}
|
||
|
||
for (int pass = 0; pass < count - 1; pass++) {
|
||
bool swapped = false;
|
||
for (int i = 0; i < count - 1 - pass; i++) {
|
||
if (devices[i].id > devices[i + 1].id) {
|
||
const Device tmp = devices[i]; /* 项目八 结构体赋值(含字符数组) */
|
||
devices[i] = devices[i + 1];
|
||
devices[i + 1] = tmp;
|
||
swapped = true;
|
||
}
|
||
}
|
||
if (!swapped) {
|
||
break; /* 项目四:本轮无交换说明已有序,提前结束 */
|
||
}
|
||
}
|
||
return SHS_OK;
|
||
}
|
||
|
||
/**
|
||
* @brief 统计指定类型的设备数量
|
||
* 项目四 循环 + 项目三 分支计数
|
||
*/
|
||
int device_count_by_type(const Device devices[], int count, shs_dev_type_t type)
|
||
{
|
||
if (devices == NULL || count <= 0) {
|
||
return 0;
|
||
}
|
||
if (count > SHS_MAX_DEVICES) {
|
||
count = SHS_MAX_DEVICES;
|
||
}
|
||
|
||
int n = 0;
|
||
for (int i = 0; i < count; i++) {
|
||
if (devices[i].type == (uint8_t)type) {
|
||
n++;
|
||
}
|
||
}
|
||
return n;
|
||
}
|
||
|
||
/* ---------------------------------------------------------------- 状态与控制 */
|
||
/**
|
||
* @brief 批量置位/清位
|
||
* 项目九 位标志批量操作(内部走 bitops 掩码函数)
|
||
*/
|
||
shs_err_t device_set_flag(Device *dev, uint16_t mask, bool on)
|
||
{
|
||
if (dev == NULL) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
|
||
if (on) {
|
||
dev->status = shs_bits_set_mask(dev->status, mask);
|
||
} else {
|
||
dev->status = shs_bits_clear_mask(dev->status, mask);
|
||
}
|
||
return SHS_OK;
|
||
}
|
||
|
||
/**
|
||
* @brief 查询标志(掩码全中才算命中)
|
||
* 项目九 位测试
|
||
*/
|
||
bool device_has_flag(const Device *dev, uint16_t mask)
|
||
{
|
||
if (dev == NULL) {
|
||
return false;
|
||
}
|
||
return (dev->status & mask) == mask; /* 项目九 按位与 */
|
||
}
|
||
|
||
/**
|
||
* @brief 开/关设备(含电源与在线状态校验)
|
||
* 项目三 双分支 + 项目九 位标志
|
||
* 说明:刷新 last_active_ts 需要时间源,而本接口无时间参数(时间由 system_tick 注入),
|
||
* 故时间戳统一由调用方维护。
|
||
*/
|
||
shs_err_t device_set_power(Device *dev, bool on)
|
||
{
|
||
if (dev == NULL) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
|
||
if (on) {
|
||
if (!device_has_flag(dev, (uint16_t)SHS_ST_ONLINE)) {
|
||
(void)logger_write(SHS_LOG_WARN, DEVICE_LOG_TAG,
|
||
"设备 %u 离线,拒绝开机", (unsigned)dev->id);
|
||
return SHS_ERR_STATE; /* 离线设备不允许开机 */
|
||
}
|
||
dev->status = shs_bits_set_mask(dev->status, (uint16_t)SHS_ST_POWER_ON);
|
||
dev->status = shs_bits_clear_mask(dev->status, (uint16_t)SHS_ST_FAULT);
|
||
} else {
|
||
dev->status = shs_bits_clear_mask(dev->status, (uint16_t)SHS_ST_POWER_ON);
|
||
dev->status = shs_bits_clear_mask(dev->status, (uint16_t)SHS_ST_DIMMING);
|
||
}
|
||
return SHS_OK;
|
||
}
|
||
|
||
/**
|
||
* @brief 翻转电源
|
||
* 项目六 函数嵌套调用(本函数 → device_has_flag → device_set_power)
|
||
*/
|
||
shs_err_t device_toggle_power(Device *dev)
|
||
{
|
||
if (dev == NULL) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
const bool powered = device_has_flag(dev, (uint16_t)SHS_ST_POWER_ON);
|
||
return device_set_power(dev, !powered);
|
||
}
|
||
|
||
/**
|
||
* @brief 设置亮度 0~100(0 表示关闭)
|
||
* 项目三 多分支 + 项目九 位标志
|
||
*/
|
||
shs_err_t device_set_brightness(Device *dev, int brightness)
|
||
{
|
||
if (dev == NULL) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
if (brightness < DEVICE_BRIGHT_MIN || brightness > DEVICE_BRIGHT_MAX) {
|
||
(void)logger_write(SHS_LOG_WARN, DEVICE_LOG_TAG,
|
||
"设备 %u 亮度 %d 越界(0~100),已拒绝", (unsigned)dev->id, brightness);
|
||
return SHS_ERR_RANGE;
|
||
}
|
||
|
||
dev->brightness = (int8_t)brightness; /* 项目二 强制类型转换(int → int8_t,已校验范围) */
|
||
|
||
if (brightness == 0) {
|
||
dev->status = shs_bits_clear_mask(dev->status, (uint16_t)SHS_ST_POWER_ON);
|
||
dev->status = shs_bits_clear_mask(dev->status, (uint16_t)SHS_ST_DIMMING);
|
||
} else if (brightness < DEVICE_BRIGHT_MAX) {
|
||
dev->status = shs_bits_set_mask(dev->status, (uint16_t)SHS_ST_POWER_ON);
|
||
dev->status = shs_bits_set_mask(dev->status, (uint16_t)SHS_ST_DIMMING); /* 1~99 处于调光状态 */
|
||
} else {
|
||
dev->status = shs_bits_set_mask(dev->status, (uint16_t)SHS_ST_POWER_ON);
|
||
dev->status = shs_bits_clear_mask(dev->status, (uint16_t)SHS_ST_DIMMING);
|
||
}
|
||
return SHS_OK;
|
||
}
|
||
|
||
/**
|
||
* @brief 设置关联温度(合法性校验)
|
||
* 项目二 浮点类型 + 项目三 边界分支
|
||
*/
|
||
shs_err_t device_set_temperature(Device *dev, float celsius)
|
||
{
|
||
if (dev == NULL) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
if (!(celsius >= DEVICE_TEMP_MIN_C && celsius <= DEVICE_TEMP_MAX_C)) {
|
||
/* NaN 与越界值都会走到这里:取反写法可同时拦住 NaN(项目二 关系运算符) */
|
||
return SHS_ERR_RANGE;
|
||
}
|
||
dev->temperature_c = celsius;
|
||
return SHS_OK;
|
||
}
|
||
|
||
/**
|
||
* @brief 门锁上锁/开锁(仅门锁类型支持)
|
||
* 项目三 双分支 + 项目九 位标志
|
||
*/
|
||
shs_err_t device_set_lock(Device *dev, bool locked)
|
||
{
|
||
if (dev == NULL) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
if (dev->type != (uint8_t)SHS_DEV_LOCK) {
|
||
return SHS_ERR_UNSUPPORTED; /* 非门锁设备不支持上锁操作 */
|
||
}
|
||
return device_set_flag(dev, (uint16_t)SHS_ST_LOCKED, locked);
|
||
}
|
||
|
||
/**
|
||
* @brief 门锁状态文本
|
||
* 项目三 双分支 + 项目五 字符串常量
|
||
*/
|
||
const char *device_lock_state_text(const Device *dev)
|
||
{
|
||
if (dev == NULL || dev->type != (uint8_t)SHS_DEV_LOCK) {
|
||
return "未知";
|
||
}
|
||
if (device_has_flag(dev, (uint16_t)SHS_ST_LOCKED)) {
|
||
return "已锁定";
|
||
}
|
||
return "未锁定";
|
||
}
|
||
|
||
/* ---------------------------------------------------------------- 统计与报表 */
|
||
/**
|
||
* @brief 能耗估算:功率(W) × 小时 ÷ 1000 = 度数(kWh)
|
||
* 项目二 运算符与类型转换:浮点运算全程按 float/double 提升规则进行,
|
||
* 若写成 power * hours / 1000(整型常量)会先做整型除法 —— 教学对照点。
|
||
*/
|
||
float device_power_cost(const Device *dev, float hours)
|
||
{
|
||
if (dev == NULL || hours <= 0.0f) {
|
||
return 0.0f;
|
||
}
|
||
|
||
const float kwh = dev->rated_power_w * hours / 1000.0f; /* 两个 float 相乘后除以 float 常量 */
|
||
return kwh;
|
||
}
|
||
|
||
/**
|
||
* @brief 格式化一行设备信息(不超 buf,容量不足返回 SHS_ERR_FULL)
|
||
* 项目一 格式化输出 + 项目二 转义字符(\t)+ 项目五 字符数组
|
||
*/
|
||
shs_err_t device_format_line(const Device *dev, char *buf, size_t buf_size)
|
||
{
|
||
if (dev == NULL || buf == NULL || buf_size == 0u) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
|
||
const int n = snprintf(buf, buf_size,
|
||
"#%u\t%-14s\t%s\t电源:%s 在线:%s 故障:%s\t亮度:%d\t温度:%.1f℃\t日耗:%.3f度\t锁:%s",
|
||
(unsigned)dev->id,
|
||
dev->name,
|
||
device_type_name(dev->type),
|
||
device_has_flag(dev, (uint16_t)SHS_ST_POWER_ON) ? "开" : "关",
|
||
device_has_flag(dev, (uint16_t)SHS_ST_ONLINE) ? "是" : "否",
|
||
device_has_flag(dev, (uint16_t)SHS_ST_FAULT) ? "是" : "否",
|
||
(int)dev->brightness,
|
||
(double)dev->temperature_c,
|
||
(double)device_power_cost(dev, DEVICE_REPORT_HOURS),
|
||
device_lock_state_text(dev));
|
||
if (n < 0) {
|
||
return SHS_ERR_INTERNAL;
|
||
}
|
||
if ((size_t)n >= buf_size) {
|
||
return SHS_ERR_FULL; /* snprintf 已保证 '\0' 结尾且未越界 */
|
||
}
|
||
return SHS_OK;
|
||
}
|
||
|
||
/**
|
||
* @brief 设备类型名(数组下标法 + switch 兜底,两种写法对照)
|
||
* 项目五 数组 + 项目三 switch
|
||
*/
|
||
const char *device_type_name(uint8_t type)
|
||
{
|
||
if (type < (uint8_t)SHS_ARRAY_LEN(k_device_types)) {
|
||
return k_device_types[type].name; /* 数组写法:编号即下标 */
|
||
}
|
||
|
||
switch (type) { /* switch 写法:越界兜底 */
|
||
case SHS_DEV_TYPE_COUNT:
|
||
default:
|
||
return "未知";
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 按类型名/别名解析类型编号
|
||
* 项目三 多分支 + 项目五 字符串比较
|
||
*/
|
||
shs_err_t device_type_from_name(const char *name, uint8_t *out)
|
||
{
|
||
if (name == NULL || out == NULL) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
|
||
for (int i = 0; i < SHS_ARRAY_LEN(k_device_types); i++) {
|
||
if (shs_strcmp(name, k_device_types[i].name) == 0 ||
|
||
shs_strcasecmp_equals(name, k_device_types[i].alias)) {
|
||
*out = k_device_types[i].type;
|
||
return SHS_OK;
|
||
}
|
||
}
|
||
return SHS_ERR_NOT_FOUND;
|
||
}
|
||
|
||
/**
|
||
* @brief 故障智能诊断(嵌套分支)
|
||
* 项目三 嵌套分支:离线 → 故障 → 亮度异常 → 温度越界 → 正常
|
||
*/
|
||
shs_err_t device_fault_diagnose(const Device *dev, char *advice, size_t advice_size)
|
||
{
|
||
if (dev == NULL || advice == NULL || advice_size == 0u) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
|
||
if (!device_has_flag(dev, (uint16_t)SHS_ST_ONLINE)) {
|
||
return shs_strcpy(advice, advice_size, "设备离线:检查供电、继电器与通信线");
|
||
} else {
|
||
if (device_has_flag(dev, (uint16_t)SHS_ST_FAULT)) {
|
||
return shs_strcpy(advice, advice_size, "设备故障:继电器自检失败,请断电复位或更换模块");
|
||
} else {
|
||
if (dev->type == (uint8_t)SHS_DEV_LIGHT &&
|
||
(dev->brightness < (int8_t)DEVICE_BRIGHT_MIN || dev->brightness > (int8_t)DEVICE_BRIGHT_MAX)) {
|
||
return shs_strcpy(advice, advice_size, "亮度异常:参数超出 0~100,请重新下发调光指令");
|
||
} else {
|
||
if (dev->temperature_c < DEVICE_TEMP_MIN_C || dev->temperature_c > DEVICE_TEMP_MAX_C) {
|
||
return shs_strcpy(advice, advice_size, "温度异常:采样值超出 -40~125℃,检查传感器接线");
|
||
} else {
|
||
return shs_strcpy(advice, advice_size, "设备状态正常,无需处理");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief 全屋能耗报表(嵌套循环:外层按类型,内层遍历设备)
|
||
* 项目四 循环嵌套 + 项目五 字符串连接 + 项目九 文件输出(报表文本交由 storage 落盘)
|
||
*/
|
||
shs_err_t device_energy_report(const Device devices[], int count, char *buf, size_t buf_size)
|
||
{
|
||
if (devices == NULL || buf == NULL || buf_size == 0u) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
if (count < 0) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
if (count > SHS_MAX_DEVICES) {
|
||
count = SHS_MAX_DEVICES;
|
||
}
|
||
|
||
buf[0] = '\0';
|
||
char line[SHS_LINE_MAX];
|
||
|
||
int n = snprintf(line, sizeof(line), "===== 全屋能耗报表(估算 24 小时)=====\n");
|
||
if (n < 0 || shs_strcat(buf, buf_size, line) != SHS_OK) {
|
||
return SHS_ERR_FULL;
|
||
}
|
||
if (shs_strcat(buf, buf_size, "按类型统计:\n") != SHS_OK) {
|
||
return SHS_ERR_FULL;
|
||
}
|
||
|
||
double total_kwh = 0.0;
|
||
float total_watt = 0.0f;
|
||
|
||
for (int t = 0; t < (int)SHS_DEV_TYPE_COUNT; t++) { /* 外层循环:设备类型 */
|
||
int type_count = 0;
|
||
float type_watt = 0.0f;
|
||
|
||
for (int i = 0; i < count; i++) { /* 内层循环:设备表 */
|
||
if (devices[i].type == (uint8_t)t) {
|
||
type_count++;
|
||
type_watt += devices[i].rated_power_w;
|
||
}
|
||
}
|
||
if (type_count == 0) {
|
||
continue; /* 该类型无设备:跳过,不出行 */
|
||
}
|
||
|
||
const double type_kwh = (double)type_watt * (double)DEVICE_REPORT_HOURS / 1000.0;
|
||
total_watt += type_watt;
|
||
total_kwh += type_kwh;
|
||
|
||
n = snprintf(line, sizeof(line), " %-12s 数量 %2d 台,额定功率 %8.1f W,日耗 %7.3f 度\n",
|
||
device_type_name((uint8_t)t), type_count,
|
||
(double)type_watt, type_kwh);
|
||
if (n < 0 || shs_strcat(buf, buf_size, line) != SHS_OK) {
|
||
return SHS_ERR_FULL;
|
||
}
|
||
}
|
||
|
||
n = snprintf(line, sizeof(line), "合计:设备 %d 台,额定总功率 %.1f W,日耗 %.3f 度\n",
|
||
count, (double)total_watt, total_kwh);
|
||
if (n < 0 || shs_strcat(buf, buf_size, line) != SHS_OK) {
|
||
return SHS_ERR_FULL;
|
||
}
|
||
return SHS_OK;
|
||
}
|
||
|
||
/**
|
||
* @brief 一次统计多项结果(指针出参)
|
||
* 项目七 指针出参:一次调用返回 数量 / 最低温度 / 最高温度 / 平均温度 / 总功率
|
||
*/
|
||
shs_err_t device_stats(const Device devices[], int count,
|
||
int *out_count, float *out_min_temp, float *out_max_temp,
|
||
double *out_avg_temp, float *out_total_power)
|
||
{
|
||
if (devices == NULL || out_count == NULL || out_min_temp == NULL ||
|
||
out_max_temp == NULL || out_avg_temp == NULL || out_total_power == NULL) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
if (count < 0) {
|
||
return SHS_ERR_PARAM;
|
||
}
|
||
if (count > SHS_MAX_DEVICES) {
|
||
count = SHS_MAX_DEVICES;
|
||
}
|
||
|
||
/* 先给出空集合兜底值,再逐台累加 */
|
||
*out_count = 0;
|
||
*out_min_temp = 0.0f;
|
||
*out_max_temp = 0.0f;
|
||
*out_avg_temp = 0.0;
|
||
*out_total_power = 0.0f;
|
||
if (count == 0) {
|
||
return SHS_OK;
|
||
}
|
||
|
||
float min_t = 0.0f;
|
||
float max_t = 0.0f;
|
||
double sum_t = 0.0;
|
||
|
||
for (int i = 0; i < count; i++) {
|
||
const float temp = devices[i].temperature_c;
|
||
|
||
if (*out_count == 0) {
|
||
min_t = temp;
|
||
max_t = temp;
|
||
} else {
|
||
min_t = SHS_MIN(min_t, temp);
|
||
max_t = SHS_MAX(max_t, temp);
|
||
}
|
||
sum_t += (double)temp; /* 项目二 浮点提升 */
|
||
*out_total_power += devices[i].rated_power_w;
|
||
(*out_count)++;
|
||
}
|
||
|
||
*out_min_temp = min_t;
|
||
*out_max_temp = max_t;
|
||
*out_avg_temp = (double)sum_t / (double)(*out_count); /* 已有 count>0 保护,无除零 */
|
||
return SHS_OK;
|
||
}
|