mirror of
https://github.com/oopuuu/zTC1.git
synced 2026-03-23 12:39:53 +08:00
Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
caffddfb43 | ||
|
|
11d3d50265 | ||
|
|
66d2863fa4 | ||
|
|
d925c7d805 | ||
|
|
981c33e3ad | ||
|
|
840b0eb374 | ||
|
|
78dbc6209b | ||
|
|
9d80970e9e | ||
|
|
5f81f2ca71 | ||
|
|
57dbe17175 | ||
|
|
f41a20f8d0 | ||
|
|
52da5983b5 | ||
|
|
758abf123b | ||
|
|
7f0e5d15c8 | ||
|
|
3aec445e57 | ||
|
|
bfae22ddd6 | ||
|
|
1f92b30ea0 | ||
|
|
4a20713693 | ||
|
|
b36fce48f5 | ||
|
|
1b7d86f1f7 | ||
|
|
44ed1d2604 | ||
|
|
b5cb7b8218 | ||
|
|
b099618b91 | ||
|
|
160f4f5b7f | ||
|
|
168f89cc82 |
@@ -8,10 +8,10 @@
|
||||
|
||||
# 固件web界面
|
||||
|
||||
<img src="doc/2.png"><img src="doc/1.png">
|
||||
<img src="doc/IMG_0863.png"><img src="doc/1.png">
|
||||
|
||||
# HASS接入效果
|
||||
<img src="doc/3.png">
|
||||
<img src="doc/IMG_0862.png">
|
||||
|
||||
固件启动后, 会开启一个热点 TC1-AP-XXXXXX,连接热点后, 直接用浏览器访问: http://192.168.0.1 即可看到如上web界面.
|
||||
|
||||
@@ -53,6 +53,10 @@ TC1 排插硬件分 A1 A2 两个版本, 本固件仅支持 **A1 版本**. A1 A2
|
||||
- [x] HomeAssistant中增加总耗电量传感器,今日耗电量传感器,昨日耗电量传感器,数据来自于插座历史统计
|
||||
- [x] 可以设置mqtt数据上报频率,默认2秒
|
||||
- [x] 可以设置电源 led 是否打开,默认打开(系统自检以及错误指示灯仍会工作)
|
||||
- [x] 后台和ha mqtt增加总开关控制
|
||||
- [x] 后台可以自定义6个插座的名称,名称将会自动同步到ha mqtt
|
||||
- [x] 修复了原代码中电源按钮长按 (现在是5秒)重新配网和(10秒)恢复出厂的逻辑错误
|
||||
- [x] 后台和ha mqtt中增加了童锁开关,开启童锁则上电以后不再响应任何电源按钮的事件,上电时同时按住按钮的话会开启配网热点(防止开了童锁,wifi又嗝屁,导致插座功能暴毙的救命逻辑)
|
||||
|
||||
# 编译固件
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@
|
||||
static bool is_http_init;
|
||||
static bool is_handlers_registered;
|
||||
const struct httpd_wsgi_call g_app_handlers[];
|
||||
char power_info_json[1552] = {0};
|
||||
char power_info_json[1952] = {0};
|
||||
char up_time[16] = "00:00:00";
|
||||
|
||||
/*
|
||||
@@ -149,18 +149,27 @@ static int HttpGetAssets(httpd_request_t *req) {
|
||||
|
||||
static int HttpGetTc1Status(httpd_request_t *req) {
|
||||
char *sockets = GetSocketStatus();
|
||||
char *tc1_status = malloc(512);
|
||||
char *tc1_status = malloc(1024);
|
||||
char *socket_names = malloc(512);
|
||||
sprintf(socket_names, "%s,%s,%s,%s,%s,%s",
|
||||
user_config->socket_names[0],
|
||||
user_config->socket_names[1],
|
||||
user_config->socket_names[2],
|
||||
user_config->socket_names[3],
|
||||
user_config->socket_names[4],
|
||||
user_config->socket_names[5]);
|
||||
sprintf(tc1_status, TC1_STATUS_JSON, sockets, ip_status.mode,
|
||||
sys_config->micoSystemConfig.ssid, sys_config->micoSystemConfig.user_key,
|
||||
user_config->ap_name, user_config->ap_key, MQTT_SERVER, MQTT_SERVER_PORT,
|
||||
MQTT_SERVER_USR, MQTT_SERVER_PWD,
|
||||
VERSION, ip_status.ip, ip_status.mask, ip_status.gateway, user_config->mqtt_report_freq,
|
||||
user_config->power_led_enabled, 0L);
|
||||
user_config->power_led_enabled, 0L,socket_names,childLockEnabled,sys_config->micoSystemConfig.name);
|
||||
|
||||
OSStatus err = kNoErr;
|
||||
send_http(tc1_status, strlen(tc1_status), exit, &err);
|
||||
|
||||
exit:
|
||||
if (socket_names) free(socket_names);
|
||||
if (tc1_status) free(tc1_status);
|
||||
return err;
|
||||
}
|
||||
@@ -183,6 +192,68 @@ static int HttpSetSocketStatus(httpd_request_t *req) {
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpSetSocketName(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
int buf_size = 70;
|
||||
char *buf = malloc(buf_size);
|
||||
|
||||
err = httpd_get_data(req, buf, buf_size);
|
||||
require_noerr(err, exit);
|
||||
int index;
|
||||
char name[64];
|
||||
sscanf(buf, "%d %s",&index,name);
|
||||
strcpy(user_config->socket_names[index],name);
|
||||
mico_system_context_update(sys_config);
|
||||
registerMqttEvents();
|
||||
send_http("OK", 2, exit, &err);
|
||||
|
||||
exit:
|
||||
if (buf) free(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpSetDeviceName(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
int buf_size = 70;
|
||||
char *buf = malloc(buf_size);
|
||||
|
||||
err = httpd_get_data(req, buf, buf_size);
|
||||
require_noerr(err, exit);
|
||||
char name[64];
|
||||
sscanf(buf, "%s",name);
|
||||
strcpy(sys_config->micoSystemConfig.name,name);
|
||||
mico_system_context_update(sys_config);
|
||||
registerMqttEvents();
|
||||
send_http("OK", 2, exit, &err);
|
||||
|
||||
exit:
|
||||
if (buf) free(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpSetChildLock(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
int buf_size = 32;
|
||||
char *buf = malloc(buf_size);
|
||||
|
||||
err = httpd_get_data(req, buf, buf_size);
|
||||
require_noerr(err, exit);
|
||||
int enableLock;
|
||||
sscanf(buf, "%d",&enableLock);
|
||||
user_config->user[0] = enableLock;
|
||||
childLockEnabled = enableLock;
|
||||
mico_system_context_update(sys_config);
|
||||
UserMqttSendChildLockState();
|
||||
send_http("OK", 2, exit, &err);
|
||||
|
||||
exit:
|
||||
if (buf) free(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpGetPowerInfo(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
char buf[16];
|
||||
@@ -204,9 +275,18 @@ static int HttpGetPowerInfo(httpd_request_t *req) {
|
||||
|
||||
char *powers = GetPowerRecord(idx);
|
||||
char *sockets = GetSocketStatus();
|
||||
char *socket_names = malloc(512);
|
||||
sprintf(socket_names, "%s,%s,%s,%s,%s,%s",
|
||||
user_config->socket_names[0],
|
||||
user_config->socket_names[1],
|
||||
user_config->socket_names[2],
|
||||
user_config->socket_names[3],
|
||||
user_config->socket_names[4],
|
||||
user_config->socket_names[5]);
|
||||
sprintf(power_info_json, POWER_INFO_JSON, sockets, power_record.idx, PW_NUM, p_count, powers,
|
||||
up_time,user_config->power_led_enabled,RelayOut()?1:0);
|
||||
up_time,user_config->power_led_enabled,RelayOut()?1:0,socket_names,user_config->p_count_1_day_ago,user_config->p_count_2_days_ago,childLockEnabled,sys_config->micoSystemConfig.name);
|
||||
send_http(power_info_json, strlen(power_info_json), exit, &err);
|
||||
if (socket_names) free(socket_names);
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
@@ -328,6 +408,7 @@ static int HttpGetMqttReportFreq(httpd_request_t *req) {
|
||||
send_http(freq, strlen(freq), exit, &err);
|
||||
|
||||
exit:
|
||||
if(freq) free(freq);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -378,6 +459,7 @@ static int HttpAddTask(httpd_request_t *req) {
|
||||
char *mess = (re == 4 && AddTask(task)) ? "OK" : "NO";
|
||||
|
||||
send_http(mess, strlen(mess), exit, &err);
|
||||
if(mess) free(mess);
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
@@ -397,6 +479,7 @@ static int HttpDelTask(httpd_request_t *req) {
|
||||
|
||||
send_http(mess, strlen(mess), exit, &err);
|
||||
exit:
|
||||
if(time_str) free(time_str);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -409,6 +492,7 @@ static int LedStatus(httpd_request_t *req) {
|
||||
send_http(led, strlen(led), exit, &err);
|
||||
|
||||
exit:
|
||||
if(led) free(led);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -502,6 +586,9 @@ const struct httpd_wsgi_call g_app_handlers[] = {
|
||||
{"/ota", HTTPD_HDR_DEFORT, 0, Otastatus, OtaStart, NULL, NULL},
|
||||
{"/led", HTTPD_HDR_DEFORT, 0, LedStatus, LedSetEnabled, NULL, NULL},
|
||||
{"/socketAll", HTTPD_HDR_DEFORT, 0, NULL, TotalSocketSetEnabled, NULL, NULL},
|
||||
{"/socketNames", HTTPD_HDR_DEFORT, 0, NULL, HttpSetSocketName, NULL, NULL},
|
||||
{"/childLock", HTTPD_HDR_DEFORT, 0, NULL, HttpSetChildLock, NULL, NULL},
|
||||
{"/deviceName", HTTPD_HDR_DEFORT, 0, NULL, HttpSetDeviceName, NULL, NULL},
|
||||
};
|
||||
|
||||
static int g_app_handlers_no = sizeof(g_app_handlers) / sizeof(struct httpd_wsgi_call);
|
||||
|
||||
@@ -64,10 +64,13 @@
|
||||
'gateway':'%s',\
|
||||
'reportFreq':'%d',\
|
||||
'ledEnabled':%d,\
|
||||
'up_time':%ld\
|
||||
'up_time':%ld,\
|
||||
'socketNames':'%s',\
|
||||
'child_lock_enabled':%d,\
|
||||
'deviceName':'%s'\
|
||||
}"
|
||||
|
||||
#define POWER_INFO_JSON "{'sockets':'%s','idx':%d,'len':%d,'p_count':%ld,'powers':[%s],'up_time':'%s','led_enabled':%d,'total_switch_on':%d}"
|
||||
#define POWER_INFO_JSON "{'sockets':'%s','idx':%d,'len':%d,'p_count':%ld,'powers':[%s],'up_time':'%s','led_enabled':%d,'total_switch_on':%d,'socketNames':'%s','p_count_1_day_ago':%d,'p_count_2_days_ago':%d,'child_lock_enabled':%d,'deviceName':'%s'}"
|
||||
|
||||
int AppHttpdStart(void);
|
||||
|
||||
|
||||
@@ -25,22 +25,29 @@
|
||||
<div class="demo-layout mdl-layout mdl-js-layout mdl-layout--fixed-drawer mdl-layout--fixed-header">
|
||||
<header class="demo-header mdl-layout__header">
|
||||
<div class="mdl-layout__header-row">
|
||||
<span class="mdl-layout-title">TC1智能插座</span>
|
||||
<div class="mdl-layout-spacer"></div>
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon"
|
||||
id="hdrbtn">
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-device-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-translate"/>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
</svg>
|
||||
</i>
|
||||
</button>
|
||||
<ul class="mdl-menu mdl-js-menu mdl-js-ripple-effect mdl-menu--bottom-right"
|
||||
for="hdrbtn">
|
||||
<li class="mdl-menu__item" onclick="ChangeLanguage('en')">English</li>
|
||||
<li class="mdl-menu__item" onclick="ChangeLanguage('cn')">中文</li>
|
||||
<li class="mdl-menu__item" onclick="ChangeLanguage('jp')">日本語</li>
|
||||
</ul>
|
||||
<span class="mdl-layout-title">TC1智能插座</span>
|
||||
<div class="mdl-layout-spacer"></div>
|
||||
<!-- <button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon"-->
|
||||
<!-- id="hdrbtn">-->
|
||||
<!-- <i class="material-icons">-->
|
||||
<!-- <svg>-->
|
||||
<!-- <use xlink:href="#icon-translate"/>-->
|
||||
<!-- </svg>-->
|
||||
<!-- </i>-->
|
||||
<!-- </button>-->
|
||||
<!-- <ul class="mdl-menu mdl-js-menu mdl-js-ripple-effect mdl-menu--bottom-right"-->
|
||||
<!-- for="hdrbtn">-->
|
||||
<!-- <li class="mdl-menu__item" onclick="ChangeLanguage('en')">English</li>-->
|
||||
<!-- <li class="mdl-menu__item" onclick="ChangeLanguage('cn')">中文</li>-->
|
||||
<!-- <li class="mdl-menu__item" onclick="ChangeLanguage('jp')">日本語</li>-->
|
||||
<!-- </ul>-->
|
||||
<button onclick="reboot()"
|
||||
class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon"
|
||||
id="rebootbtn">
|
||||
@@ -122,6 +129,18 @@
|
||||
-->
|
||||
<ul id="socket_ul" class="demo-list-control mdl-list">
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<span class="lang" langKey="ChildLock">启用童锁</span>
|
||||
</span>
|
||||
<span class="mdl-list__item-secondary-action">
|
||||
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
|
||||
for="list-switch-child-lock">
|
||||
<input type="checkbox" id="list-switch-child-lock" class="mdl-switch__input"
|
||||
checked/>
|
||||
</label>
|
||||
</span>
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<span class="lang" langKey="Led">电源指示灯</span>
|
||||
</span>
|
||||
@@ -147,7 +166,14 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<span class="lang" langKey="Socket">插座</span>-1
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
</svg>
|
||||
</i>
|
||||
</button>
|
||||
<span class="lang" langKey="Socket">插座</span><span class="socket-index">-1</span>
|
||||
</span>
|
||||
<span class="mdl-list__item-secondary-action">
|
||||
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
|
||||
@@ -159,7 +185,14 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<span class="lang" langKey="Socket">插座</span>-2
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
</svg>
|
||||
</i>
|
||||
</button>
|
||||
<span class="lang" langKey="Socket">插座</span><span class="socket-index">-2</span>
|
||||
</span>
|
||||
<span class="mdl-list__item-secondary-action">
|
||||
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
|
||||
@@ -171,7 +204,14 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<span class="lang" langKey="Socket">插座</span>-3
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
</svg>
|
||||
</i>
|
||||
</button>
|
||||
<span class="lang" langKey="Socket">插座</span><span class="socket-index">-3</span>
|
||||
</span>
|
||||
<span class="mdl-list__item-secondary-action">
|
||||
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
|
||||
@@ -183,7 +223,14 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<span class="lang" langKey="Socket">插座</span>-4
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
</svg>
|
||||
</i>
|
||||
</button>
|
||||
<span class="lang" langKey="Socket">插座</span><span class="socket-index">-4</span>
|
||||
</span>
|
||||
<span class="mdl-list__item-secondary-action">
|
||||
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
|
||||
@@ -195,7 +242,14 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<span class="lang" langKey="Socket">插座</span>-5
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
</svg>
|
||||
</i>
|
||||
</button>
|
||||
<span class="lang" langKey="Socket">插座</span><span class="socket-index">-5</span>
|
||||
</span>
|
||||
<span class="mdl-list__item-secondary-action">
|
||||
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
|
||||
@@ -207,7 +261,14 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<span class="lang" langKey="Socket">插座</span>-6
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
</svg>
|
||||
</i>
|
||||
</button>
|
||||
<span class="lang" langKey="Socket">插座</span><span class="socket-index">-6</span>
|
||||
</span>
|
||||
<span class="mdl-list__item-secondary-action">
|
||||
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
|
||||
@@ -228,8 +289,10 @@
|
||||
style="height:315px;overflow-x:scroll;overflow-y:hidden;">
|
||||
<table class="pw">
|
||||
<tr>
|
||||
<td>P: <span id="p" class="success">0</span> W</td>
|
||||
<td>W: <span id="w" class="error">0</span> kW·h</td>
|
||||
<td>当前功率: <span id="p" class="success">0</span> W</td>
|
||||
<td>今日电量: <span id="w_t" class="error">0</span> kW·h</td>
|
||||
<td>昨日电量: <span id="w_y" class="error">0</span> kW·h</td>
|
||||
<td>总电量: <span id="w" class="error">0</span> kW·h</td>
|
||||
</tr>
|
||||
</table>
|
||||
<div id="ct-chart" class="ct-chart ct-perfect-fourth"
|
||||
@@ -477,6 +540,7 @@
|
||||
</tr>
|
||||
</table>
|
||||
<div class="mdl-card__actions mdl-card--border">
|
||||
<span>提示:长按电源按钮5s,指示灯闪烁3次可开启热点重新配网,10s指示灯保持闪烁恢复出厂设置</span>
|
||||
<div class="mdl-layout-spacer"></div>
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
@@ -544,6 +608,10 @@
|
||||
<path d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
|
||||
<path d="M0 0h24v24H0z" fill="none"/>
|
||||
</g>
|
||||
<g id="icon-edit">
|
||||
<path d="M5,3C3.89,3 3,3.89 3,5V19A2,2 0 0,0 5,21H19A2,2 0 0,0 21,19V12H19V19H5V5H12V3H5M17.78,4C17.61,4 17.43,4.07 17.3,4.2L16.08,5.41L18.58,7.91L19.8,6.7C20.06,6.44 20.06,6 19.8,5.75L18.25,4.2C18.12,4.07 17.95,4 17.78,4M15.37,6.12L8,13.5V16H10.5L17.87,8.62L15.37,6.12Z" />
|
||||
<path d="M0 0h24v24H0z" fill="none"/>
|
||||
</g>
|
||||
<g id="icon-translate">
|
||||
<path d="M0 0h24v24H0z" fill="none"/>
|
||||
<path d="M12.87 15.07l-2.54-2.51.03-.03c1.74-1.94 2.98-4.17 3.71-6.53H17V4h-7V2H8v2H1v1.99h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04zM18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12zm-2.62 7l1.62-4.33L19.12 17h-3.24z"/>
|
||||
@@ -647,7 +715,8 @@
|
||||
//主页
|
||||
Socket: {en: "Socket", cn: "插座"},
|
||||
Led:{en: "PowerLed", cn: "电源指示灯"},
|
||||
SocketAll:{en: "Total switch", cn: "总开关"}
|
||||
SocketAll:{en: "Total Switch", cn: "总开关"},
|
||||
ChildLock:{en: "Child Lock", cn: "童锁"}
|
||||
}
|
||||
function ChangeLanguage(lang) {
|
||||
if (lang == "jp") {
|
||||
@@ -731,12 +800,20 @@
|
||||
HttpGet("/status", function (re) {
|
||||
var status = JSON.parse(re);
|
||||
var status_arr = status.sockets.split(",");
|
||||
var names_arr = status.socketNames.split(",");
|
||||
var switchTexts = document.querySelectorAll('.mdl-list__item-primary-content');
|
||||
$(".mdl-layout-title").text(status.deviceName);
|
||||
document.title=status.deviceName;
|
||||
for (var i = 0; i < status_arr.length; i++) {
|
||||
var langSpan = switchTexts[i+3].querySelector('.lang');
|
||||
var indexSpan = switchTexts[i+3].querySelector('.socket-index');
|
||||
if (langSpan) langSpan.textContent = names_arr[i]; // 修改插座名称
|
||||
if (indexSpan) indexSpan.textContent = ""; // 修改序号,可自定义起始数字
|
||||
//checkboxs[i].checked = status_arr[i] == "1";
|
||||
if (status_arr[i] == "1") {
|
||||
switch_lables[i].MaterialSwitch.on();
|
||||
switch_lables[i+3].MaterialSwitch.on();
|
||||
} else {
|
||||
switch_lables[i].MaterialSwitch.off();
|
||||
switch_lables[i+3].MaterialSwitch.off();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -801,22 +878,81 @@
|
||||
var checkboxs = socket_ul.getElementsByClassName("mdl-switch__input");
|
||||
var switch_lables = socket_ul.getElementsByClassName("mdl-switch");
|
||||
$(".mdl-switch__input").on("click", function() {
|
||||
if("list-switch-all" == this.id){
|
||||
if("list-switch-all" == this.id||"list-switch-child-lock"== this.id){
|
||||
return;
|
||||
}
|
||||
var sockets_st = "";
|
||||
for (var i = 2; i < checkboxs.length; i++) {
|
||||
for (var i = 3; i < checkboxs.length; i++) {
|
||||
sockets_st += (checkboxs[i].checked ? "1," : "0,");
|
||||
}
|
||||
HttpPost("/socket", function (re) {
|
||||
}, sockets_st);
|
||||
HttpPost("/led", function (re) {
|
||||
}, checkboxs[0].checked ? "1" : "0");
|
||||
}, checkboxs[1].checked ? "1" : "0");
|
||||
|
||||
});
|
||||
|
||||
$(".edit-socket-name").on("click", function () {
|
||||
// 获取当前按钮所在的 li 元素
|
||||
var $li = $(this).closest(".mdl-list__item");
|
||||
|
||||
// 获取当前索引
|
||||
var index = $(".edit-socket-name").index(this);
|
||||
|
||||
// 获取 lang 名称和 socket-index 元素
|
||||
var $lang = $li.find(".lang");
|
||||
var $index = $li.find(".socket-index");
|
||||
|
||||
// 原名称
|
||||
var oldName = $lang.text().trim();
|
||||
|
||||
// 弹窗编辑
|
||||
var newName = prompt("请输入新的插座名称", oldName);
|
||||
if (newName && newName.trim() !== "") {
|
||||
if(newName.trim().includes(" ")){
|
||||
ShowToast("名称不能包含空格");
|
||||
return;
|
||||
}
|
||||
if(newName.trim().length>16){
|
||||
ShowToast("名称不能超过16个字");
|
||||
return;
|
||||
}
|
||||
$lang.text(newName.trim());
|
||||
// 可选:更新序号
|
||||
$index.text("");
|
||||
HttpPost("/socketNames", function (re) {
|
||||
}, index+' '+newName.trim());}
|
||||
});
|
||||
|
||||
$(".edit-device-name").on("click", function () {
|
||||
var title = $(".mdl-layout-title");
|
||||
// 原名称
|
||||
var oldName = title.text().trim();
|
||||
|
||||
// 弹窗编辑
|
||||
var newName = prompt("请输入新的设备名称", oldName);
|
||||
if (newName && newName.trim() !== "") {
|
||||
if(newName.trim().includes(" ")){
|
||||
ShowToast("名称不能包含空格");
|
||||
return;
|
||||
}
|
||||
if(newName.trim().length>16){
|
||||
ShowToast("名称不能超过16个字");
|
||||
return;
|
||||
}
|
||||
title.text(newName.trim());
|
||||
|
||||
HttpPost("/deviceName", function (re) {
|
||||
}, newName.trim());}
|
||||
});
|
||||
|
||||
document.getElementById("list-switch-all").addEventListener("click", function() {
|
||||
HttpPost("/socketAll", function (re) {
|
||||
}, checkboxs[1].checked ? "1" : "0");});
|
||||
}, checkboxs[2].checked ? "1" : "0");});
|
||||
|
||||
document.getElementById("list-switch-child-lock").addEventListener("click", function() {
|
||||
HttpPost("/childLock", function (re) {
|
||||
}, checkboxs[0].checked ? "1" : "0");});
|
||||
|
||||
//Socket-end
|
||||
|
||||
@@ -942,6 +1078,9 @@
|
||||
var chart_wth = 0;
|
||||
var p = document.getElementById("p");
|
||||
var w = document.getElementById("w");
|
||||
var w_t = document.getElementById("w_t");
|
||||
var w_y = document.getElementById("w_y");
|
||||
|
||||
function GetPowerRecord() {
|
||||
HttpPost("/power", function (re) {
|
||||
re = re.replace(/'/g, '"');
|
||||
@@ -967,24 +1106,43 @@
|
||||
$("#uptime").html(power.up_time);
|
||||
|
||||
var w_v = (17.1 * power.p_count) / 1000 / 36000;
|
||||
w.innerHTML = w_v.toFixed(2);
|
||||
if (power.led_enabled == 1) {
|
||||
w.innerHTML = w_v.toFixed(3);
|
||||
var w_v_t = (17.1 * (power.p_count - power.p_count_1_day_ago)) / 1000 / 36000;
|
||||
w_v_t = w_v_t < 0 ? 0 : w_v_t;
|
||||
w_t.innerHTML = w_v_t.toFixed(3);
|
||||
var w_v_y = (17.1 * (power.p_count_1_day_ago - power.p_count_2_days_ago)) / 1000 / 36000;
|
||||
w_v_y = w_v_y < 0 ? 0 : w_v_y;
|
||||
w_y.innerHTML = w_v_y.toFixed(3);
|
||||
if (power.child_lock_enabled == 1) {
|
||||
switch_lables[0].MaterialSwitch.on();
|
||||
} else {
|
||||
switch_lables[0].MaterialSwitch.off();
|
||||
}
|
||||
if (power.total_switch_on == 1) {
|
||||
if (power.led_enabled == 1) {
|
||||
switch_lables[1].MaterialSwitch.on();
|
||||
} else {
|
||||
switch_lables[1].MaterialSwitch.off();
|
||||
}
|
||||
if (power.total_switch_on == 1) {
|
||||
switch_lables[2].MaterialSwitch.on();
|
||||
} else {
|
||||
switch_lables[2].MaterialSwitch.off();
|
||||
}
|
||||
var status_arr = power.sockets.split(",");
|
||||
var names_arr = power.socketNames.split(",");
|
||||
var switchTexts = document.querySelectorAll('.mdl-list__item-primary-content');
|
||||
$(".mdl-layout-title").text(power.deviceName);
|
||||
document.title=power.deviceName;
|
||||
for (var i = 0; i < status_arr.length; i++) {
|
||||
//checkboxs[i].checked = status_arr[i] == "1";
|
||||
var langSpan = switchTexts[i+3].querySelector('.lang');
|
||||
var indexSpan = switchTexts[i+3].querySelector('.socket-index');
|
||||
if (langSpan) langSpan.textContent = names_arr[i]; // 修改插座名称
|
||||
if (indexSpan) indexSpan.textContent = ""; // 修改序号,可自定义起始数字
|
||||
if (status_arr[i] == "1") {
|
||||
switch_lables[i+2].MaterialSwitch.on();
|
||||
switch_lables[i+3].MaterialSwitch.on();
|
||||
} else {
|
||||
switch_lables[i+2].MaterialSwitch.off();
|
||||
switch_lables[i+3].MaterialSwitch.off();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
24
TC1/main.c
24
TC1/main.c
@@ -4,6 +4,7 @@
|
||||
#include "time.h"
|
||||
#include "unistd.h"
|
||||
#include "TimeUtils.h"
|
||||
#include "mico_system.h"
|
||||
|
||||
#include "user_gpio.h"
|
||||
#include "user_wifi.h"
|
||||
@@ -17,6 +18,7 @@ char rtc_init = 0; //sntp校时成功标志位
|
||||
uint32_t total_time = 0;
|
||||
char str_mac[16] = {0};
|
||||
int last_check_day = 0;
|
||||
int childLockEnabled = 0;
|
||||
|
||||
system_config_t *sys_config;
|
||||
user_config_t *user_config;
|
||||
@@ -47,6 +49,7 @@ void appRestoreDefault_callback(void *const user_config_data, uint32_t size) {
|
||||
int i;
|
||||
for (i = 0; i < SOCKET_NUM; i++) {
|
||||
userConfigDefault->socket_status[i] = 1;
|
||||
snprintf(userConfigDefault->socket_names[i], SOCKET_NAME_LENGTH, "插座-%d", i + 1);
|
||||
}
|
||||
for (i = 0; i < MAX_TASK_NUM; i++) {
|
||||
userConfigDefault->timed_tasks[i].on_use = false;
|
||||
@@ -56,15 +59,17 @@ void appRestoreDefault_callback(void *const user_config_data, uint32_t size) {
|
||||
|
||||
void recordDailyPCount() {
|
||||
// 获取当前时间
|
||||
mico_utc_time_t utc_time;
|
||||
mico_time_get_utc_time(&utc_time);
|
||||
utc_time += 28800;
|
||||
struct tm * current_time = localtime((const time_t *) &utc_time);
|
||||
mico_utc_time_t utc_time;
|
||||
mico_time_get_utc_time(&utc_time);
|
||||
utc_time += 28800;
|
||||
struct tm *current_time = localtime((const time_t *) &utc_time);
|
||||
// 判断上次检查的时间与当前时间的日期是否不同
|
||||
if (last_check_day != 0) {
|
||||
// 如果日期发生变化(即跨天了),则进行记录
|
||||
if (current_time->tm_mday != last_check_day) {
|
||||
tc1_log("WARNGIN: pcount day changed! now day %d hour %d min %d ,lastCheck day %d",current_time->tm_mday,current_time->tm_hour,current_time->tm_min,last_check_day);
|
||||
if (current_time->tm_mday != last_check_day) { tc1_log(
|
||||
"WARNGIN: pcount day changed! now day %d hour %d min %d ,lastCheck day %d",
|
||||
current_time->tm_mday, current_time->tm_hour, current_time->tm_min,
|
||||
last_check_day);
|
||||
|
||||
// tc1_log("WARNGIN: pcount day changed! ");
|
||||
// 记录数据
|
||||
@@ -81,8 +86,8 @@ void recordDailyPCount() {
|
||||
} else {
|
||||
// tc1_log("WARNGIN: pcount day not changed , waiting for next run! ");
|
||||
}
|
||||
}else{
|
||||
tc1_log("WARNGIN: now day %d hour %d min %d ,lastCheck day %d",current_time->tm_mday,current_time->tm_hour,current_time->tm_min,last_check_day);
|
||||
} else { tc1_log("WARNGIN: now day %d hour %d min %d ,lastCheck day %d", current_time->tm_mday,
|
||||
current_time->tm_hour, current_time->tm_min, last_check_day);
|
||||
}
|
||||
// 更新上次检查时间
|
||||
last_check_day = current_time->tm_mday;
|
||||
@@ -132,7 +137,7 @@ int application_start(void) {
|
||||
MicoGpioInitialize((mico_gpio_t) Button, INPUT_PULL_UP);
|
||||
if (!MicoGpioInputGet(Button)) { //开机时按钮状态
|
||||
tc1_log("press ap_init");
|
||||
ApInit(false);
|
||||
ApInit(true);
|
||||
open_ap = true;
|
||||
}
|
||||
|
||||
@@ -143,6 +148,7 @@ int application_start(void) {
|
||||
}
|
||||
MicoSysLed(0);
|
||||
|
||||
childLockEnabled = (int) user_config->user[0];
|
||||
if (user_config->version != USER_CONFIG_VERSION) { tc1_log("WARNGIN: user params restored!");
|
||||
err = mico_system_context_restore(sys_config);
|
||||
require_noerr(err, exit);
|
||||
|
||||
11
TC1/main.h
11
TC1/main.h
@@ -19,14 +19,14 @@
|
||||
#define VERSION "v2.1.6"
|
||||
|
||||
#define TYPE 1
|
||||
#define TYPE_NAME "zTC1"
|
||||
#define TYPE_NAME "TC1"
|
||||
|
||||
#define ZTC1_NAME "zTC1-%s"
|
||||
#define ZTC1_NAME "TC1-%s"
|
||||
|
||||
#define USER_CONFIG_VERSION 8
|
||||
#define USER_CONFIG_VERSION 9
|
||||
#define SETTING_MQTT_STRING_LENGTH_MAX 32 //必须4字节对齐。
|
||||
|
||||
#define SOCKET_NAME_LENGTH 32
|
||||
#define SOCKET_NAME_LENGTH 64
|
||||
#define SOCKET_NUM 6 //插座数量
|
||||
|
||||
#define Led MICO_GPIO_5
|
||||
@@ -51,6 +51,7 @@ typedef struct
|
||||
{
|
||||
char version;
|
||||
char mqtt_ip[SETTING_MQTT_STRING_LENGTH_MAX];
|
||||
char socket_names[SOCKET_NUM][SOCKET_NAME_LENGTH];
|
||||
int mqtt_port;
|
||||
int mqtt_report_freq;
|
||||
char mqtt_user[SETTING_MQTT_STRING_LENGTH_MAX];
|
||||
@@ -74,5 +75,7 @@ extern char str_mac[16];
|
||||
extern system_config_t* sys_config;
|
||||
extern user_config_t* user_config;
|
||||
extern mico_gpio_t Relay[Relay_NUM];
|
||||
extern int childLockEnabled;
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -76,6 +76,7 @@ void UserMqttTimerFunc(void *arg) {
|
||||
case 0:
|
||||
UserMqttHassAutoLed();
|
||||
UserMqttHassAutoTotalSocket();
|
||||
UserMqttHassAutoChildLock();
|
||||
break;
|
||||
case 1:
|
||||
case 2:
|
||||
@@ -174,6 +175,10 @@ static OSStatus MqttMsgPublish(Client *c, const char *topic, char qos, char reta
|
||||
return err;
|
||||
}
|
||||
|
||||
void registerMqttEvents(void) {
|
||||
mico_start_timer(&timer_handle);
|
||||
}
|
||||
|
||||
void MqttClientThread(mico_thread_arg_t arg) {
|
||||
OSStatus err = kUnknownErr;
|
||||
|
||||
@@ -259,9 +264,11 @@ void MqttClientThread(mico_thread_arg_t arg) {
|
||||
}
|
||||
|
||||
UserMqttSendLedState();
|
||||
UserMqttSendTotalSocketState();
|
||||
UserMqttSendChildLockState();
|
||||
|
||||
mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg);
|
||||
mico_start_timer(&timer_handle);
|
||||
registerMqttEvents();
|
||||
/* 5. client loop for recv msg && keepalive */
|
||||
while (1) {
|
||||
isconnect = true;
|
||||
@@ -406,6 +413,14 @@ void ProcessHaCmd(char *cmd) {
|
||||
UserMqttSendSocketState(i);
|
||||
}
|
||||
UserMqttSendTotalSocketState();
|
||||
}else if (strcmp(cmd, "set childLock") == ' ') {
|
||||
int on;
|
||||
sscanf(cmd, "set childLock %s %d", mac, &on);
|
||||
if (strcmp(mac, str_mac)) return;mqtt_log("set childLock on[%d]", on);
|
||||
user_config->user[0] = on;
|
||||
childLockEnabled = on;
|
||||
UserMqttSendChildLockState();
|
||||
mico_system_context_update(sys_config);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -494,29 +509,46 @@ OSStatus UserMqttSendLedState(void) {
|
||||
return oss_status;
|
||||
}
|
||||
|
||||
OSStatus UserMqttSendChildLockState(void) {
|
||||
char *send_buf = malloc(64);
|
||||
char *topic_buf = malloc(64);
|
||||
OSStatus oss_status = kUnknownErr;
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/childLock/state", str_mac);
|
||||
sprintf(send_buf, "set childLock %s %d", str_mac, childLockEnabled);
|
||||
oss_status = UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf) free(send_buf);
|
||||
if (topic_buf) free(topic_buf);
|
||||
|
||||
return oss_status;
|
||||
}
|
||||
|
||||
//hass mqtt鑷姩鍙戠幇鏁版嵁寮<E5B581>鍏冲彂閫<E5BD82>
|
||||
void UserMqttHassAuto(char socket_id) {
|
||||
socket_id--;
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = (char *) malloc(1024);
|
||||
send_buf = (char *) malloc(600);
|
||||
topic_buf = (char *) malloc(64);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/socket_%d/config", str_mac, socket_id);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_Socket_%d\","
|
||||
"\"uniq_id\":\"%s_s%d\","
|
||||
"{\"name\":\"%s\","
|
||||
"\"uniq_id\":\"tc1_%s_s%d\","
|
||||
"\"object_id\":\"tc1_%s_s%d\","
|
||||
"\"stat_t\":\"homeassistant/switch/%s/socket_%d/state\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set socket %s %d 1\","
|
||||
"\"pl_off\":\"set socket %s %d 0\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"TC1_%s\","
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac + 8, socket_id + 1, str_mac, socket_id, str_mac, socket_id, str_mac,
|
||||
socket_id, str_mac, socket_id, str_mac, str_mac);
|
||||
user_config->socket_names[(int)socket_id], str_mac, socket_id,str_mac, socket_id, str_mac, socket_id,
|
||||
str_mac,
|
||||
socket_id, str_mac, socket_id, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf)
|
||||
@@ -528,23 +560,53 @@ void UserMqttHassAuto(char socket_id) {
|
||||
void UserMqttHassAutoLed(void) {
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = (char *) malloc(1024);
|
||||
send_buf = (char *) malloc(600);
|
||||
topic_buf = (char *) malloc(64);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/led/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_Led\","
|
||||
"\"uniq_id\":\"%s_led\","
|
||||
"{\"name\":\"LED指示灯\","
|
||||
"\"uniq_id\":\"tc1_%s_led\","
|
||||
"\"object_id\":\"tc1_%s_led\","
|
||||
"\"stat_t\":\"homeassistant/switch/%s/led/state\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set led %s 1\","
|
||||
"\"pl_off\":\"set led %s 0\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"TC1_%s\","
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac + 8, str_mac, str_mac, str_mac, str_mac, str_mac, str_mac);
|
||||
str_mac,str_mac,str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf)
|
||||
free(send_buf);
|
||||
if (topic_buf)
|
||||
free(topic_buf);
|
||||
}
|
||||
|
||||
void UserMqttHassAutoChildLock(void) {
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = (char *) malloc(600);
|
||||
topic_buf = (char *) malloc(64);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/childLock/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"童锁\","
|
||||
"\"uniq_id\":\"tc1_%s_child_lock\","
|
||||
"\"object_id\":\"tc1_%s_child_lock\","
|
||||
"\"stat_t\":\"homeassistant/switch/%s/childLock/state\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set childLock %s 1\","
|
||||
"\"pl_off\":\"set childLock %s 0\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac,str_mac,str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf)
|
||||
@@ -556,23 +618,24 @@ void UserMqttHassAutoLed(void) {
|
||||
void UserMqttHassAutoTotalSocket(void) {
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = (char *) malloc(1024);
|
||||
send_buf = (char *) malloc(600);
|
||||
topic_buf = (char *) malloc(64);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/total_socket/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_TotalSocket\","
|
||||
"\"uniq_id\":\"%s_total_socket\","
|
||||
"{\"name\":\"总开关\","
|
||||
"\"uniq_id\":\"tc1_%s_total_socket\","
|
||||
"\"object_id\":\"tc1_%s_total_socket\","
|
||||
"\"stat_t\":\"homeassistant/switch/%s/total_socket/state\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set total_socket %s 1\","
|
||||
"\"pl_off\":\"set total_socket %s 0\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"TC1_%s\","
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac + 8, str_mac, str_mac, str_mac, str_mac, str_mac, str_mac);
|
||||
str_mac, str_mac, str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf)
|
||||
@@ -585,66 +648,70 @@ void UserMqttHassAutoTotalSocket(void) {
|
||||
void UserMqttHassAutoPower(void) {
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = malloc(1024);
|
||||
send_buf = malloc(600);
|
||||
topic_buf = malloc(128);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/power/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_Power\","
|
||||
"\"uniq_id\":\"%s_p\","
|
||||
"{\"name\":\"功率\","
|
||||
"\"uniq_id\":\"tc1_%s_p\","
|
||||
"\"object_id\":\"tc1_%s_p\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/power/state\","
|
||||
"\"unit_of_measurement\":\"W\","
|
||||
"\"icon\":\"mdi:gauge\","
|
||||
"\"value_template\":\"{{ value_json.power }}\",""\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"TC1_%s\","
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac + 8, str_mac, str_mac, str_mac, str_mac);
|
||||
str_mac,str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumption/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_PowerConsumption\","
|
||||
"\"uniq_id\":\"%s_pc\","
|
||||
"{\"name\":\"总耗电量\","
|
||||
"\"uniq_id\":\"tc1_%s_pc\","
|
||||
"\"object_id\":\"tc1_%s_pc\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumption/state\","
|
||||
"\"unit_of_measurement\":\"kWh\","
|
||||
"\"icon\":\"mdi:fence-electric\","
|
||||
"\"value_template\":\"{{ value_json.powerConsumption }}\",""\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"TC1_%s\","
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac + 8, str_mac, str_mac, str_mac, str_mac);
|
||||
str_mac, str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionToday/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_powerConsumptionToday\","
|
||||
"\"uniq_id\":\"%s_pc_today\","
|
||||
"{\"name\":\"今日耗电量\","
|
||||
"\"uniq_id\":\"tc1_%s_pc_today\","
|
||||
"\"object_id\":\"tc1_%s_pc_today\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumptionToday/state\","
|
||||
"\"unit_of_measurement\":\"kWh\","
|
||||
"\"icon\":\"mdi:fence-electric\","
|
||||
"\"value_template\":\"{{ value_json.powerConsumptionToday }}\",""\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"TC1_%s\","
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac + 8, str_mac, str_mac, str_mac, str_mac);
|
||||
str_mac,str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionYesterday/config", str_mac);
|
||||
sprintf(send_buf,
|
||||
"{\"name\":\"TC1_%s_powerConsumptionYesterday\","
|
||||
"\"uniq_id\":\"%s_pc_yesterday\","
|
||||
"{\"name\":\"昨日耗电量\","
|
||||
"\"uniq_id\":\"tc1_%s_pc_yesterday\","
|
||||
"\"object_id\":\"tc1_%s_pc_yesterday\","
|
||||
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumptionYesterday/state\","
|
||||
"\"unit_of_measurement\":\"kWh\","
|
||||
"\"icon\":\"mdi:fence-electric\","
|
||||
"\"value_template\":\"{{ value_json.powerConsumptionYesterday }}\",""\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"TC1_%s\","
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac + 8, str_mac, str_mac, str_mac, str_mac);
|
||||
str_mac,str_mac, str_mac, str_mac,sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf) free(send_buf);
|
||||
|
||||
@@ -31,6 +31,8 @@ extern OSStatus UserMqttSendSocketState(char socket_id);
|
||||
|
||||
extern OSStatus UserMqttSendLedState(void);
|
||||
|
||||
extern OSStatus UserMqttSendChildLockState(void);
|
||||
|
||||
extern OSStatus UserMqttSendTotalSocketState(void);
|
||||
|
||||
extern void UserMqttHassAuto(char socket_id);
|
||||
@@ -41,6 +43,10 @@ extern void UserMqttHassAutoPower(void);
|
||||
|
||||
extern void UserMqttHassAutoLed(void);
|
||||
|
||||
extern void UserMqttHassAutoChildLock(void);
|
||||
|
||||
extern void UserMqttHassAutoTotalSocket(void);
|
||||
|
||||
extern void registerMqttEvents(void);
|
||||
|
||||
#endif
|
||||
|
||||
176
TC1/user_gpio.c
176
TC1/user_gpio.c
@@ -2,13 +2,13 @@
|
||||
|
||||
#include "main.h"
|
||||
#include "user_gpio.h"
|
||||
#include "user_wifi.h"
|
||||
#include "mqtt_server/user_mqtt_client.h"
|
||||
|
||||
mico_gpio_t relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 };
|
||||
char socket_status[32] = { 0 };
|
||||
mico_gpio_t relay[Relay_NUM] = {Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5};
|
||||
char socket_status[32] = {0};
|
||||
|
||||
void UserLedSet(char x)
|
||||
{
|
||||
void UserLedSet(char x) {
|
||||
if (x == -1)
|
||||
MicoGpioOutputTrigger(Led);
|
||||
else if (x)
|
||||
@@ -17,43 +17,37 @@ void UserLedSet(char x)
|
||||
MicoGpioOutputLow(Led);
|
||||
}
|
||||
|
||||
bool RelayOut(void)
|
||||
{
|
||||
bool RelayOut(void) {
|
||||
int i;
|
||||
for (i = 0; i < SOCKET_NUM; i++)
|
||||
{
|
||||
if (user_config->socket_status[i] != 0)
|
||||
{
|
||||
for (i = 0; i < SOCKET_NUM; i++) {
|
||||
if (user_config->socket_status[i] != 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
char* GetSocketStatus()
|
||||
{
|
||||
char *GetSocketStatus() {
|
||||
sprintf(socket_status, "%d,%d,%d,%d,%d,%d",
|
||||
user_config->socket_status[0],
|
||||
user_config->socket_status[1],
|
||||
user_config->socket_status[2],
|
||||
user_config->socket_status[3],
|
||||
user_config->socket_status[4],
|
||||
user_config->socket_status[5]);
|
||||
user_config->socket_status[0],
|
||||
user_config->socket_status[1],
|
||||
user_config->socket_status[2],
|
||||
user_config->socket_status[3],
|
||||
user_config->socket_status[4],
|
||||
user_config->socket_status[5]);
|
||||
return socket_status;
|
||||
}
|
||||
|
||||
void SetSocketStatus(char* socket_status)
|
||||
{
|
||||
void SetSocketStatus(char *socket_status) {
|
||||
sscanf(socket_status, "%d,%d,%d,%d,%d,%d,",
|
||||
(int*)&user_config->socket_status[0],
|
||||
(int*)&user_config->socket_status[1],
|
||||
(int*)&user_config->socket_status[2],
|
||||
(int*)&user_config->socket_status[3],
|
||||
(int*)&user_config->socket_status[4],
|
||||
(int*)&user_config->socket_status[5]);
|
||||
(int *) &user_config->socket_status[0],
|
||||
(int *) &user_config->socket_status[1],
|
||||
(int *) &user_config->socket_status[2],
|
||||
(int *) &user_config->socket_status[3],
|
||||
(int *) &user_config->socket_status[4],
|
||||
(int *) &user_config->socket_status[5]);
|
||||
int i = 0;
|
||||
for (i = 0; i < SOCKET_NUM; i++)
|
||||
{
|
||||
for (i = 0; i < SOCKET_NUM; i++) {
|
||||
UserRelaySet(i, user_config->socket_status[i]);
|
||||
UserMqttSendSocketState(i);
|
||||
}
|
||||
@@ -66,27 +60,20 @@ void SetSocketStatus(char* socket_status)
|
||||
* i:编号 0-5
|
||||
* on:开关 0:关 1:开
|
||||
*/
|
||||
void UserRelaySet(unsigned char i, unsigned char on)
|
||||
{
|
||||
void UserRelaySet(unsigned char i, unsigned char on) {
|
||||
if (i < 0 || i >= SOCKET_NUM) return;
|
||||
|
||||
if (on == Relay_ON)
|
||||
{
|
||||
if (on == Relay_ON) {
|
||||
MicoGpioOutputHigh(relay[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
MicoGpioOutputLow(relay[i]);
|
||||
}
|
||||
|
||||
user_config->socket_status[i] = on;
|
||||
|
||||
if (RelayOut()&&user_config->power_led_enabled)
|
||||
{
|
||||
if (RelayOut() && user_config->power_led_enabled) {
|
||||
UserLedSet(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
UserLedSet(0);
|
||||
}
|
||||
}
|
||||
@@ -96,109 +83,110 @@ void UserRelaySet(unsigned char i, unsigned char on)
|
||||
* y: 0:全部关 1:全部开
|
||||
*
|
||||
*/
|
||||
void UserRelaySetAll(char y)
|
||||
{
|
||||
void UserRelaySetAll(char y) {
|
||||
int i;
|
||||
for (i = 0; i < SOCKET_NUM; i++)
|
||||
UserRelaySet(i, y);
|
||||
}
|
||||
|
||||
static void KeyLongPress(void)
|
||||
{
|
||||
// key_log("KeyLongPress");
|
||||
// UserLedSet(1);
|
||||
// UserMqttSend("mqtt test");
|
||||
static void KeyLong5sPress(void) {
|
||||
key_log("WARNGIN: wifi ap started!");
|
||||
micoWlanSuspendStation();
|
||||
ApInit(true);
|
||||
}
|
||||
|
||||
static void KeyLong10sPress(void)
|
||||
{
|
||||
static void KeyLong10sPress(void) {
|
||||
key_log("WARNGIN: user params restored!");
|
||||
appRestoreDefault_callback(user_config, sizeof(user_config_t));
|
||||
sys_config->micoSystemConfig.ssid[0] = 0;
|
||||
mico_system_context_update(mico_system_context_get());
|
||||
mico_system_context_restore(sys_config);
|
||||
// appRestoreDefault_callback(user_config, sizeof(user_config_t));
|
||||
// sys_config->micoSystemConfig.ssid[0] = 0;
|
||||
// mico_system_context_update(mico_system_context_get());
|
||||
}
|
||||
static void KeyShortPress(void)
|
||||
{
|
||||
|
||||
static void KeyShortPress(void) {
|
||||
char i;
|
||||
|
||||
if (RelayOut())
|
||||
{
|
||||
if (RelayOut()) {
|
||||
UserRelaySetAll(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
UserRelaySetAll(1);
|
||||
}
|
||||
|
||||
for (i = 0; i < SOCKET_NUM; i++)
|
||||
{
|
||||
for (i = 0; i < SOCKET_NUM; i++) {
|
||||
UserMqttSendSocketState(i);
|
||||
}
|
||||
UserMqttSendTotalSocketState();
|
||||
}
|
||||
|
||||
mico_timer_t user_key_timer;
|
||||
uint16_t key_time = 0;
|
||||
#define BUTTON_LONG_PRESS_TIME 10 //100ms*10=1s
|
||||
|
||||
static void KeyTimeoutHandler(void* arg)
|
||||
{
|
||||
static void KeyTimeoutHandler(void *arg) {
|
||||
if(childLockEnabled)
|
||||
return;
|
||||
static char key_trigger, key_continue;
|
||||
//按键扫描程序
|
||||
char tmp = ~(0xfe | MicoGpioInputGet(Button));
|
||||
key_trigger = tmp & (tmp ^ key_continue);
|
||||
key_continue = tmp;
|
||||
if (key_trigger != 0) key_time = 0; //新按键按下时,重新开始按键计时
|
||||
if (key_continue != 0)
|
||||
{
|
||||
if (key_continue != 0) {
|
||||
//any button pressed
|
||||
key_time++;
|
||||
if (key_time <= BUTTON_LONG_PRESS_TIME)
|
||||
{
|
||||
key_log("button long pressed:%d",key_time);
|
||||
if (key_time > BUTTON_LONG_PRESS_TIME) { key_log("button long pressed:%d", key_time);
|
||||
|
||||
if (key_time == 30)
|
||||
{
|
||||
KeyLongPress();
|
||||
}
|
||||
else if (key_time == 100)
|
||||
{
|
||||
if (key_time == 50) {
|
||||
KeyLong5sPress();
|
||||
} else if (key_time > 50 && key_time < 57) {
|
||||
switch (key_time) {
|
||||
case 51:
|
||||
UserLedSet(1);
|
||||
break;
|
||||
case 52:
|
||||
UserLedSet(0);
|
||||
break;
|
||||
case 53:
|
||||
UserLedSet(1);
|
||||
break;
|
||||
case 54:
|
||||
UserLedSet(0);
|
||||
break;
|
||||
case 55:
|
||||
UserLedSet(1);
|
||||
break;
|
||||
case 56:
|
||||
UserLedSet(0);
|
||||
break;
|
||||
}
|
||||
} else if (key_time == 57) {
|
||||
UserLedSet(RelayOut() && user_config->power_led_enabled);
|
||||
} else if (key_time == 100) {
|
||||
KeyLong10sPress();
|
||||
}
|
||||
else if (key_time == 102)
|
||||
{
|
||||
} else if (key_time == 102) {
|
||||
UserLedSet(1);
|
||||
}
|
||||
else if (key_time == 103)
|
||||
{
|
||||
} else if (key_time == 103) {
|
||||
UserLedSet(0);
|
||||
key_time = 101;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
//button released
|
||||
if (key_time < BUTTON_LONG_PRESS_TIME)
|
||||
{ //100ms*10=1s 大于1s为长按
|
||||
key_time = 0;
|
||||
key_log("button short pressed:%d",key_time);
|
||||
if (key_time < BUTTON_LONG_PRESS_TIME) { //100ms*10=1s 大于1s为长按
|
||||
key_time = 0;key_log("button short pressed:%d", key_time);
|
||||
KeyShortPress();
|
||||
}
|
||||
else if (key_time > 100)
|
||||
{
|
||||
} else if (key_time > 100) {
|
||||
MicoSystemReboot();
|
||||
}
|
||||
mico_rtos_stop_timer(&user_key_timer);
|
||||
}
|
||||
}
|
||||
|
||||
static void KeyFallingIrqHandler(void* arg)
|
||||
{
|
||||
static void KeyFallingIrqHandler(void *arg) {
|
||||
mico_rtos_start_timer(&user_key_timer);
|
||||
}
|
||||
|
||||
void KeyInit(void)
|
||||
{
|
||||
void KeyInit(void) {
|
||||
MicoGpioInitialize(Button, INPUT_PULL_UP);
|
||||
mico_rtos_init_timer(&user_key_timer, 100, KeyTimeoutHandler, NULL);
|
||||
|
||||
|
||||
BIN
doc/IMG_0862.png
Normal file
BIN
doc/IMG_0862.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 396 KiB |
BIN
doc/IMG_0863.png
Normal file
BIN
doc/IMG_0863.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 286 KiB |
Reference in New Issue
Block a user