mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-06 10:22:50 +08:00
开发本地ota上传用的
This commit is contained in:
@@ -255,6 +255,64 @@ static int HttpSetButtonEvent(httpd_request_t *req) {
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpSetOTAFile(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
uint32_t total = 0, ota_offset = 0;
|
||||
char *buffer = malloc(OTA_BUFFER_SIZE);
|
||||
if (!buffer) return kGeneralErr;
|
||||
|
||||
// mico_logic_partition_t* ota_partition = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP);
|
||||
// MicoFlashErase(MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length);
|
||||
|
||||
CRC16_Context crc_context;
|
||||
CRC16_Init(&crc_context);
|
||||
|
||||
tc1_log("開始接收 OTA 數據...");
|
||||
struct timeval timeout = {60, 0}; // 60秒
|
||||
setsockopt(req->sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||
int remaining = -1;
|
||||
while (remaining!=0) {
|
||||
int readSize = remaining>OTA_BUFFER_SIZE?OTA_BUFFER_SIZE:(remaining>0?remaining:OTA_BUFFER_SIZE);
|
||||
remaining = httpd_get_data(req, buffer, readSize);
|
||||
if (remaining < 0) {
|
||||
err = kConnectionErr;
|
||||
tc1_log("httpd_get_data 失敗");
|
||||
goto exit;
|
||||
break;
|
||||
}
|
||||
|
||||
// CRC16_Update(&crc_context, buffer, readSize);
|
||||
|
||||
// err = MicoFlashWrite(MICO_PARTITION_OTA_TEMP, &crc_context->offset, (uint8_t *)buffer, readSize);
|
||||
// require_noerr_quiet(err, exit);
|
||||
total+=readSize;
|
||||
mico_thread_msleep(10);
|
||||
}
|
||||
|
||||
uint16_t crc16;
|
||||
// CRC16_Final(&crc_context, &crc16);
|
||||
char response[64];
|
||||
|
||||
snprintf(response, sizeof(response), "OK, total: %ld bytes, CRC: 0x%04X", total, crc16);
|
||||
send_http(response, strlen(response), exit, &err);
|
||||
return 0;
|
||||
|
||||
err = mico_ota_switch_to_new_fw(ota_offset, crc16);
|
||||
require_noerr(err, exit);
|
||||
|
||||
tc1_log("OTA 完成,重啟系統");
|
||||
mico_system_power_perform(mico_system_context_get(), eState_Software_Reset);
|
||||
|
||||
exit:
|
||||
if (req->sock >= 0) {
|
||||
close(req->sock);
|
||||
req->sock = -1;
|
||||
}
|
||||
if (buffer) free(buffer);
|
||||
tc1_log("OTA 結束,狀態: %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpSetDeviceName(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
|
||||
@@ -40,20 +40,20 @@
|
||||
</button>
|
||||
<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 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">
|
||||
@@ -113,7 +113,7 @@
|
||||
<use xlink:href="#icon-cloud_download"/>
|
||||
</svg>
|
||||
</i>
|
||||
<span class="lang" langKey="OTA">在线升级</span>
|
||||
<span class="lang" langKey="OTA">固件升级</span>
|
||||
</a>
|
||||
<a class="mdl-navigation__link" href="javascript:ShowPage(7);">
|
||||
<i class="mdl-color-text--blue-grey-400 material-icons" role="presentation">
|
||||
@@ -291,30 +291,30 @@
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="page page1 mdl-cell mdl-cell--4-col mdl-cell--8-col-tablet mdl-grid mdl-grid--no-spacing">
|
||||
<div class="demo-options mdl-card mdl-shadow--2dp mdl-cell mdl-cell--4-col mdl-cell--3-col-tablet mdl-cell--12-col-desktop">
|
||||
<div class="mdl-card__supporting-text">
|
||||
<div class="mdl-card__title mdl-card--expand">
|
||||
<h2 class="mdl-card__title-text">功率图</h2>
|
||||
</div>
|
||||
<div class="mdl-card__title mdl-card--expand">
|
||||
<h2 class="mdl-card__title-text">功率图</h2>
|
||||
</div>
|
||||
|
||||
<div id="ct-chart-par"
|
||||
class="page page1 mdl-card__supporting-text mdl-shadow--2dp"
|
||||
style="height:315px;overflow-x:scroll;overflow-y:hidden;">
|
||||
<table class="pw">
|
||||
<tr>
|
||||
<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"
|
||||
style="height:280px;margin-top:40px;"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="ct-chart-par"
|
||||
class="page page1 mdl-card__supporting-text mdl-shadow--2dp"
|
||||
style="height:315px;overflow-x:scroll;overflow-y:hidden;">
|
||||
<table class="pw">
|
||||
<tr>
|
||||
<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"
|
||||
style="height:280px;margin-top:40px;"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -539,7 +539,7 @@
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="page page4 over-unset mdl-cell mdl-cell--5-col demo-card-square mdl-card mdl-shadow--2dp"
|
||||
style="z-index: unset;">
|
||||
@@ -663,6 +663,24 @@
|
||||
<div id="ota_status" class="mdl-progress mdl-js-progress"></div>
|
||||
</div>
|
||||
|
||||
<div class="page page6 mdl-cell mdl-cell--12-col demo-card-square mdl-card mdl-shadow--2dp">
|
||||
<div class="mdl-card__title mdl-card--expand">
|
||||
<h2 class="mdl-card__title-text">本地升级</h2>
|
||||
</div>
|
||||
<div class="mdl-card__supporting-text">
|
||||
<form id="otaForm" action="/ota/fileUpload" method="post" enctype="multipart/form-data">
|
||||
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
|
||||
<input class="mdl-textfield__input" type="file" id="ota_file" name="ota_file">
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="mdl-card__actions mdl-card--border">
|
||||
<a href="javascript:OtaFileUpload();" class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
|
||||
上传 OTA 文件
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="page page7 mdl-cell mdl-cell--12-col demo-card-square mdl-card mdl-shadow--2dp">
|
||||
<div class="mdl-card__title mdl-card--expand">
|
||||
<h2 class="mdl-card__title-text">系统日志</h2>
|
||||
@@ -777,7 +795,7 @@
|
||||
Config: {en: "Config", cn: "设置"},
|
||||
TimedTask: {en: "Timed Task", cn: "定时任务"},
|
||||
SystemStatus: {en: "System Status", cn: "系统状态"},
|
||||
OTA: {en: "OTA", cn: "在线升级"},
|
||||
OTA: {en: "OTA", cn: "固件升级"},
|
||||
SystemLog: {en: "System Log", cn: "系统日志"},
|
||||
About: {en: "About", cn: "关于"},
|
||||
//主页
|
||||
@@ -1240,6 +1258,7 @@ componentHandler.upgradeDom();
|
||||
var w = document.getElementById("w");
|
||||
var w_t = document.getElementById("w_t");
|
||||
var w_y = document.getElementById("w_y");
|
||||
var powerTimerId;
|
||||
|
||||
function GetPowerRecord() {
|
||||
HttpPost("/power", function (re) {
|
||||
@@ -1305,7 +1324,7 @@ componentHandler.upgradeDom();
|
||||
switch_lables[i+3].MaterialSwitch.off();
|
||||
}
|
||||
}
|
||||
window.powerTimeout= window.setTimeout(GetPowerRecord, 3000);
|
||||
powerTimerId= window.setTimeout(GetPowerRecord, 3000);
|
||||
}, power_idx.toString());
|
||||
}
|
||||
$(document).ready(function(){
|
||||
@@ -1485,11 +1504,27 @@ HttpPost("/shortClickEvent", function (re) {
|
||||
var protocol = window.location.protocol;
|
||||
var baseUrl = protocol+"//"+window.location.host;
|
||||
|
||||
clearTimeout(window.powerTimeout);
|
||||
HttpPost("/ota", function (re) {
|
||||
OtaStatus();
|
||||
}, ota_url);
|
||||
}
|
||||
|
||||
function OtaFileUpload() {
|
||||
//alert("假的假的是假的,忽略");
|
||||
//return;
|
||||
var fileInput = document.getElementById("ota_file");
|
||||
if (fileInput.files.length === 0) {
|
||||
alert("请选择要上传的 OTA 文件");
|
||||
return;
|
||||
}
|
||||
clearTimeout(powerTimerId);
|
||||
fetch("/ota/fileUpload", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/octet-stream" },
|
||||
body: fileInput.files[0]
|
||||
}).then(r => r.text()).then(alert).catch(alert);
|
||||
}
|
||||
|
||||
var ota_status = document.querySelector('#ota_status');
|
||||
ota_status.addEventListener('mdl-componentupgraded', function() {
|
||||
this.MaterialProgress.setProgress(0);
|
||||
|
||||
@@ -199,12 +199,12 @@ int application_start(void) {
|
||||
|
||||
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "p_count",
|
||||
(mico_thread_function_t) schedule_p_count_task,
|
||||
0x2000, 0);
|
||||
0x1000, 0);
|
||||
require_noerr_string(err, exit, "ERROR: Unable to start the p_count thread.");
|
||||
|
||||
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_power_report",
|
||||
(mico_thread_function_t) reportMqttPowerInfoThread,
|
||||
0x2000, 0);
|
||||
0x1000, 0);
|
||||
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt_power_report thread.");
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user