Merge branch 'oopuuu:master' into master

This commit is contained in:
wsuow
2025-03-17 11:16:27 +08:00
committed by GitHub
8 changed files with 167 additions and 42 deletions

View File

@@ -205,7 +205,7 @@ static int HttpGetPowerInfo(httpd_request_t *req) {
char *powers = GetPowerRecord(idx);
char *sockets = GetSocketStatus();
sprintf(power_info_json, POWER_INFO_JSON, sockets, power_record.idx, PW_NUM, p_count, powers,
up_time,user_config->power_led_enabled);
up_time,user_config->power_led_enabled,RelayOut()?1:0);
send_http(power_info_json, strlen(power_info_json), exit, &err);
exit:
return err;
@@ -437,6 +437,26 @@ static int LedSetEnabled(httpd_request_t *req) {
return err;
}
static int TotalSocketSetEnabled(httpd_request_t *req){
OSStatus err = kNoErr;
int buf_size = 97;
int on;
char *buf = malloc(buf_size);
err = httpd_get_data(req, buf, buf_size);
require_noerr(err, exit);
sscanf(buf, "%d", &on);
UserRelaySetAll(on);
UserMqttSendTotalSocketState();
send_http("OK", 2, exit, &err);
exit:
if (buf) free(buf);
return err;
}
static int Otastatus(httpd_request_t *req) {
OSStatus err = kNoErr;
char buf[16] = {0};
@@ -477,6 +497,7 @@ const struct httpd_wsgi_call g_app_handlers[] = {
{"/task", HTTPD_HDR_DEFORT, APP_HTTP_FLAGS_NO_EXACT_MATCH, HttpGetTasks, HttpAddTask, NULL, HttpDelTask},
{"/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},
};
static int g_app_handlers_no = sizeof(g_app_handlers) / sizeof(struct httpd_wsgi_call);

View File

@@ -67,7 +67,7 @@
'up_time':%ld\
}"
#define POWER_INFO_JSON "{'sockets':'%s','idx':%d,'len':%d,'p_count':%ld,'powers':[%s],'up_time':'%s','led_enabled':%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}"
int AppHttpdStart(void);

View File

@@ -134,6 +134,18 @@
</span>
</li>
<li class="mdl-list__item">
<span class="mdl-list__item-primary-content">
<span class="lang" langKey="SocketAll">总开关</span>
</span>
<span class="mdl-list__item-secondary-action">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect"
for="list-switch-all">
<input type="checkbox" id="list-switch-all" class="mdl-switch__input"
checked/>
</label>
</span>
</li>
<li class="mdl-list__item">
<span class="mdl-list__item-primary-content">
<span class="lang" langKey="Socket">插座</span>-1
</span>
@@ -634,7 +646,8 @@
About: {en: "About", cn: "关于"},
//主页
Socket: {en: "Socket", cn: "插座"},
Led:{en: "PowerLed", cn: "电源指示灯"}
Led:{en: "PowerLed", cn: "电源指示灯"},
SocketAll:{en: "Total switch", cn: "总开关"}
}
function ChangeLanguage(lang) {
if (lang == "jp") {
@@ -789,13 +802,15 @@
var switch_lables = socket_ul.getElementsByClassName("mdl-switch");
$(".mdl-switch__input").on("click", function() {
var sockets_st = "";
for (var i = 1; i < checkboxs.length; i++) {
for (var i = 2; 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");
HttpPost("/socketAll", function (re) {
}, checkboxs[1].checked ? "1" : "0");
});
//Socket-end
@@ -951,14 +966,19 @@
switch_lables[0].MaterialSwitch.on();
} else {
switch_lables[0].MaterialSwitch.off();
}
if (power.total_switch_on == 1) {
switch_lables[1].MaterialSwitch.on();
} else {
switch_lables[1].MaterialSwitch.off();
}
var status_arr = power.sockets.split(",");
for (var i = 0; i < status_arr.length; i++) {
//checkboxs[i].checked = status_arr[i] == "1";
if (status_arr[i] == "1") {
switch_lables[i+1].MaterialSwitch.on();
switch_lables[i+2].MaterialSwitch.on();
} else {
switch_lables[i+1].MaterialSwitch.off();
switch_lables[i+2].MaterialSwitch.off();
}
}

View File

@@ -1,5 +1,5 @@
const unsigned char js_pack[0xd2be] = {
0x1f,0x8b,0x08,0x00,0xa0,0x55,0xd0,0x67,0x02,0xff,0xd4,0xbd,0xfb,0x72,0xdb,0x46,
0x1f,0x8b,0x08,0x00,0xa5,0x26,0xd3,0x67,0x02,0xff,0xd4,0xbd,0xfb,0x72,0xdb,0x46,
0xd2,0x38,0xfa,0xff,0x56,0xed,0x3b,0x88,0x48,0x96,0x01,0xc4,0x21,0x45,0xca,0xb1,
0x13,0x83,0x1e,0xb1,0x1c,0xdb,0x49,0xbc,0x5f,0x9c,0x64,0xe3,0x5c,0x97,0x62,0x52,
0xb8,0x92,0xa0,0x48,0x82,0xe2,0xc5,0x12,0x23,0x72,0xdf,0xe7,0xf7,0x1a,0xe7,0xc9,
@@ -3372,7 +3372,7 @@ const unsigned char js_pack[0xd2be] = {
0x02,0x0f,0xb7,0x98,0x0c,0x59,0x7f,0x97,0xf2,0x25,0x55,0x7c,0x45,0xad,0x25,0x8e,
0x0f,0x58,0x4f,0xcc,0x9f,0x00,0x32,0xd2,0xf4,0xa5,0xf0,0xee,0x02,0x00};
const unsigned char css_pack[0x6217] = {
0x1f,0x8b,0x08,0x00,0xa0,0x55,0xd0,0x67,0x02,0xff,0xed,0xbd,0x79,0x73,0xdb,0xc8,
0x1f,0x8b,0x08,0x00,0xa5,0x26,0xd3,0x67,0x02,0xff,0xed,0xbd,0x79,0x73,0xdb,0xc8,
0xf5,0x28,0xfa,0x7f,0xaa,0xf2,0x1d,0x70,0x9d,0x9a,0x1a,0x6b,0x42,0xc8,0xd8,0x49,
0x4a,0x95,0x54,0xbc,0x8f,0x26,0x96,0x9c,0x78,0x19,0x8f,0x9d,0x5f,0x9e,0x0b,0x04,
0x9a,0x24,0x24,0x90,0x60,0x00,0x50,0x1b,0x4b,0xb7,0xee,0x07,0xb9,0xef,0xcb,0xbd,
@@ -4943,7 +4943,7 @@ const unsigned char css_pack[0x6217] = {
0xe6,0x54,0x09,0x49,0xbc,0xd0,0xc2,0x1c,0x55,0x23,0x76,0x55,0x21,0xff,0x3f,0x3f,
0xef,0x85,0xce,0x3d,0xbc,0x02,0x00};
const unsigned char web_index_html[0x2c41] = {
0x1f,0x8b,0x08,0x00,0xa0,0x55,0xd0,0x67,0x02,0xff,0xed,0x7d,0x6b,0x73,0xdc,0xc6,
0x1f,0x8b,0x08,0x00,0xa5,0x26,0xd3,0x67,0x02,0xff,0xed,0x7d,0x6b,0x73,0xdc,0xc6,
0x95,0xe8,0xf7,0x5b,0xb5,0xff,0xa1,0x0d,0xa7,0x96,0xc3,0xe5,0x00,0x83,0xc7,0x00,
0x33,0x43,0x91,0xdc,0xd2,0xd2,0x4a,0xe4,0x0d,0x15,0x39,0xa6,0xae,0x7c,0x77,0x1d,
0xd7,0x14,0x38,0x83,0xe1,0xc0,0xc2,0x0c,0xc6,0x00,0x86,0x14,0x95,0xa8,0xca,0xca,

View File

@@ -55,19 +55,18 @@ void appRestoreDefault_callback(void *const user_config_data, uint32_t size) {
}
void recordDailyPCount() {
tc1_log("WARNGIN: enter recordDailyPCount! ");
// 获取当前时间
time_t now;
time(&now);
struct tm *current_time = localtime(&now);
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) { tc1_log(
"WARNGIN: last_check_time day %d ,current_time day %d", last_check_day,
current_time->tm_mday);
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);
tc1_log("WARNGIN: pcount day changed! ");
// tc1_log("WARNGIN: pcount day changed! ");
// 记录数据
if (user_config->p_count_1_day_ago != 0) {
user_config->p_count_2_days_ago = user_config->p_count_1_day_ago;
@@ -79,12 +78,14 @@ void recordDailyPCount() {
tc1_log("WARNGIN: p_count record! p_count_1_day_ago:%d p_count_2_days_ago:%d",
user_config->p_count_1_day_ago, user_config->p_count_2_days_ago);
} else { tc1_log("WARNGIN: pcount day not changed , waiting for next run! ");
} 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);
}
// 更新上次检查时间
last_check_day = current_time->tm_mday;
}
void schedule_p_count_task(mico_thread_arg_t arg) {

View File

@@ -74,6 +74,7 @@ void UserMqttTimerFunc(void *arg)
{
case 0:
UserMqttHassAutoLed();
UserMqttHassAutoTotalSocket();
break;
case 1:
case 2:
@@ -412,6 +413,13 @@ void ProcessHaCmd(char* cmd)
}
UserMqttSendLedState();
mico_system_context_update(sys_config);
}else if(strcmp(cmd, "set total_socket") == ' '){
int on;
sscanf(cmd, "set total_socket %s %d", mac, &on);
if (strcmp(mac, str_mac)) return;
mqtt_log("set total_socket on[%d]", on);
UserRelaySetAll(on);
UserMqttSendTotalSocketState();
}
}
@@ -474,6 +482,23 @@ OSStatus UserMqttSendSocketState(char socket_id)
return oss_status;
}
OSStatus UserMqttSendTotalSocketState()
{
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/total_socket/state", str_mac);
sprintf(send_buf, "set total_socket %s %d", str_mac, RelayOut()?1:0);
oss_status = UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf) free(send_buf);
if (topic_buf) free(topic_buf);
return oss_status;
}
OSStatus UserMqttSendLedState(void)
{
char *send_buf = malloc(64);
@@ -497,19 +522,24 @@ void UserMqttHassAuto(char socket_id)
socket_id--;
char *send_buf = NULL;
char *topic_buf = NULL;
send_buf = (char *) malloc(300);
send_buf = (char *) malloc(1024);
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\","
"\"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\"}",
str_mac+8, socket_id+1, str_mac, socket_id, str_mac, socket_id, str_mac, socket_id, str_mac, socket_id);
"{\"name\":\"TC1_%s_Socket_%d\","
"\"uniq_id\":\"%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\","
"\"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);
UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf)
@@ -522,21 +552,24 @@ void UserMqttHassAutoLed(void)
{
char *send_buf = NULL;
char *topic_buf = NULL;
send_buf = (char *) malloc(300);
send_buf = (char *) malloc(1024);
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, "set led %s %d", str_mac, (int)user_config->power_led_enabled);
sprintf(send_buf,
sprintf(send_buf,
"{\"name\":\"TC1_%s_Led\","
"\"uniq_id\":\"%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\"}",
str_mac+8, str_mac, str_mac, str_mac,str_mac);
"\"pl_off\":\"set led %s 0\","
"\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1_%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac+8, str_mac, str_mac, str_mac,str_mac, str_mac, str_mac);
UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf)
@@ -544,12 +577,42 @@ void UserMqttHassAutoLed(void)
if (topic_buf)
free(topic_buf);
}
void UserMqttHassAutoTotalSocket(void)
{
char *send_buf = NULL;
char *topic_buf = NULL;
send_buf = (char *) malloc(1024);
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\","
"\"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\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac+8, str_mac, str_mac, str_mac,str_mac, str_mac, str_mac);
UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf)
free(send_buf);
if (topic_buf)
free(topic_buf);
}
//hass mqtt鑷姩鍙戠幇鏁版嵁鍔熺巼鍙戦<E98D99><E688A6>
void UserMqttHassAutoPower(void)
{
char *send_buf = NULL;
char *topic_buf = NULL;
send_buf = malloc(512);
send_buf = malloc(1024);
topic_buf = malloc(128);
if (send_buf != NULL && topic_buf != NULL)
{
@@ -560,8 +623,12 @@ void UserMqttHassAutoPower(void)
"\"state_topic\":\"homeassistant/sensor/%s/power/state\","
"\"unit_of_measurement\":\"W\","
"\"icon\":\"mdi:gauge\","
"\"value_template\":\"{{ value_json.power }}\"}",
str_mac+8, str_mac, str_mac);
"\"value_template\":\"{{ value_json.power }}\",""\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1_%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac+8, str_mac, str_mac, str_mac, str_mac);
UserMqttSendTopic(topic_buf, send_buf, 1);
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumption/config", str_mac);
sprintf(send_buf,
@@ -570,8 +637,12 @@ void UserMqttHassAutoPower(void)
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumption/state\","
"\"unit_of_measurement\":\"kWh\","
"\"icon\":\"mdi:fence-electric\","
"\"value_template\":\"{{ value_json.powerConsumption }}\"}",
str_mac+8, str_mac, str_mac);
"\"value_template\":\"{{ value_json.powerConsumption }}\",""\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1_%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac+8, str_mac, str_mac, str_mac, str_mac);
UserMqttSendTopic(topic_buf, send_buf, 1);
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionToday/config", str_mac);
@@ -581,8 +652,12 @@ void UserMqttHassAutoPower(void)
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumptionToday/state\","
"\"unit_of_measurement\":\"kWh\","
"\"icon\":\"mdi:fence-electric\","
"\"value_template\":\"{{ value_json.powerConsumptionToday }}\"}",
str_mac+8, str_mac, str_mac);
"\"value_template\":\"{{ value_json.powerConsumptionToday }}\",""\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1_%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac+8, str_mac, str_mac, str_mac, str_mac);
UserMqttSendTopic(topic_buf, send_buf, 1);
sprintf(topic_buf, "homeassistant/sensor/%s/powerConsumptionYesterday/config", str_mac);
@@ -592,8 +667,12 @@ void UserMqttHassAutoPower(void)
"\"state_topic\":\"homeassistant/sensor/%s/powerConsumptionYesterday/state\","
"\"unit_of_measurement\":\"kWh\","
"\"icon\":\"mdi:fence-electric\","
"\"value_template\":\"{{ value_json.powerConsumptionYesterday }}\"}",
str_mac+8, str_mac, str_mac);
"\"value_template\":\"{{ value_json.powerConsumptionYesterday }}\",""\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"TC1_%s\","
"\"model\":\"TC1\","
"\"manufacturer\":\"PHICOMM\"}}",
str_mac+8, str_mac, str_mac, str_mac, str_mac);
UserMqttSendTopic(topic_buf, send_buf, 1);
}
if (send_buf) free(send_buf);

View File

@@ -31,6 +31,8 @@ extern OSStatus UserMqttSendSocketState(char socket_id);
extern OSStatus UserMqttSendLedState(void);
extern OSStatus UserMqttSendTotalSocketState(void);
extern void UserMqttHassAuto(char socket_id);
extern void UserMqttHassPower(void);

View File

@@ -57,6 +57,7 @@ void SetSocketStatus(char* socket_status)
UserRelaySet(i, user_config->socket_status[i]);
UserMqttSendSocketState(i);
}
UserMqttSendTotalSocketState();
mico_system_context_update(sys_config);
}
@@ -133,6 +134,7 @@ static void KeyShortPress(void)
{
UserMqttSendSocketState(i);
}
UserMqttSendTotalSocketState();
}
mico_timer_t user_key_timer;
uint16_t key_time = 0;