diff --git a/stm32basic b/stm32basic index d830dc9..cba5845 160000 --- a/stm32basic +++ b/stm32basic @@ -1 +1 @@ -Subproject commit d830dc9ba6bbcae328723d94f6b8a0c1829fe850 +Subproject commit cba5845768a21a847d3e2b528107f65516399c90 diff --git a/uappbase/bean/appevent.hpp b/uappbase/bean/appevent.hpp index 8084987..a7ec0b6 100644 --- a/uappbase/bean/appevent.hpp +++ b/uappbase/bean/appevent.hpp @@ -20,28 +20,28 @@ extern "C" { class AppEvent { public: AppEventType_t type; - std::function onfnc; + std::function onfnc = nullptr; uint8_t buf[sizeof(UIEvent) + 10]; - char* getBleName() { return (char*)buf; } + public: + AppEvent() { memset(buf, 0, sizeof(buf)); } - char* getStateDisplayInfo() { return (char*)buf; } + public: + int getBufSize() { return sizeof(buf); } + + void setBleName(const char* name) { strcpy((char*)buf, name); } + char* getBleName() { return (char*)buf; } UIEvent* getUIEvent() { return (UIEvent*)buf; } + void setUIEvent(const UIEvent& event) { memcpy(buf, &event, sizeof(UIEvent)); } + void setPageChangeTo(int32_t page) { *(int32_t*)buf = page; } int32_t getPageChangeTo() { return *(int32_t*)buf; } - void setBleName(const char* name) { strcpy((char*)buf, name); } - - void setStateDisplayInfo(const char* info) { strcpy((char*)buf, info); } - - void setUIEvent(const UIEvent& event) { memcpy(buf, &event, sizeof(UIEvent)); } + void setStateDisplayInfo(const char* info) { strcpy((char*)buf, info); } + char* getStateDisplayInfo() { return (char*)buf; } - void setPageChangeTo(int32_t page) { *(int32_t*)buf = page; } + AcidStateChangeEvent_t* getAcidStateChangeEvent() { return (AcidStateChangeEvent_t*)buf; } void setOnFnc(std::function fnc) { onfnc = fnc; } - - int getBufSize() { return sizeof(buf); } - - AcidStateChangeEvent_t* getAcidStateChangeEvent() { return (AcidStateChangeEvent_t*)buf; } }; diff --git a/uappbase/bean/appevent_type.hpp b/uappbase/bean/appevent_type.hpp index f193df9..8e4581c 100644 --- a/uappbase/bean/appevent_type.hpp +++ b/uappbase/bean/appevent_type.hpp @@ -1,8 +1,6 @@ #pragma once #include - - enum AppEventType { KAE_callOnAppEventBusLoop, @@ -14,7 +12,6 @@ enum AppEventType { // UI_EVENT KAE_UIEvent, kAE_LoginEvent, - kAE_unLoginEvent, KAE_PageChangeEvent, kAE_RemoterConnectedEvent, // 遥控器连接成功 @@ -24,16 +21,13 @@ enum AppEventType { * @brief 统计数据变化事件 */ kAE_AcidStatDisplayChangeEvent, // 统计数据显示数值变化 - kAE_AcidStatChangeEvent, // 使用 + kAE_AcidStatChangeEvent // 使用 }; -enum AcidStatChangeEventType { - kACID_USE, - kACID_STORAGE, -}; +enum AcidStatChangeEventType { kACID_USE, kACID_STORAGE }; struct AcidStateChangeEvent { - enum AcidStatChangeEventType subtype; + enum AcidStatChangeEventType subtype; // 添加 uint8_t addCh; @@ -46,4 +40,4 @@ struct AcidStateChangeEvent { typedef enum AppEventType AppEventType_t; typedef enum AcidStatChangeEventType AcidStatChangeEventType_t; -typedef struct AcidStateChangeEvent AcidStateChangeEvent_t; +typedef struct AcidStateChangeEvent AcidStateChangeEvent_t; diff --git a/uappbase/bean/dbtype/acid_use_record.hpp b/uappbase/bean/dbtype/acid_use_record.hpp index 12818e4..ed2d145 100644 --- a/uappbase/bean/dbtype/acid_use_record.hpp +++ b/uappbase/bean/dbtype/acid_use_record.hpp @@ -8,6 +8,7 @@ typedef struct { uint8_t id; zdate_t date; uint8_t usrid; + uint8_t dirty; // 如果为dirty,说明该条记录用户并没有正常登出 uint16_t ch0take0p1ml; // 0.1ml uint16_t ch1take0p1ml; // 0.1ml diff --git a/uappbase/service/app_event_bus.cpp b/uappbase/service/app_event_bus.cpp index 5473e7c..f16d89d 100644 --- a/uappbase/service/app_event_bus.cpp +++ b/uappbase/service/app_event_bus.cpp @@ -43,8 +43,14 @@ void AppEventBus::regOnEvent(onAppEventCB_t onEvent) { } void AppEventBus::pushEvent(const AppEvent& event) { // ZLOGI(TAG, "pushEvent %d", event.type); - if (xQueueSend(xQueue, &event, 100) != pdPASS) { + if (xQueueSend(xQueue, &event, 1000) != pdPASS) { ZLOGE(TAG, "xQueueSend failed"); + + for (int i = 0; i < 3; i++) { + ZLOGE(TAG, "pushEvent fail"); + osDelay(1000); + } + NVIC_SystemReset(); } } @@ -74,6 +80,7 @@ void AppEventBus::pushStateDisplayInfoEvent(const char* info) { eventtxcache.setStateDisplayInfo(info); pushEvent(eventtxcache); } +bool AppEventBus::isInAppEventThread() { return thread.m_defaultTaskHandle == osThreadGetId(); } void AppEventBus::pushAcidStatStorageEvent(uint8_t ch, float chVal) { zlock_guard lck(lock); diff --git a/uappbase/service/app_event_bus.hpp b/uappbase/service/app_event_bus.hpp index 9b3889b..864c985 100644 --- a/uappbase/service/app_event_bus.hpp +++ b/uappbase/service/app_event_bus.hpp @@ -37,6 +37,8 @@ class AppEventBus { void pushAcidStatUseEvent(float ch1, float ch2, float ch3, float ch4); void pushAcidStatStorageEvent(uint8_t ch, float chVal); void pushAcidStatDisplayChangeEvent(); + + bool isInAppEventThread(); }; } // namespace iflytop diff --git a/uappbase/service/gstate_mgr.cpp b/uappbase/service/gstate_mgr.cpp index 0a3b4a4..0508c4a 100644 --- a/uappbase/service/gstate_mgr.cpp +++ b/uappbase/service/gstate_mgr.cpp @@ -46,7 +46,6 @@ int GStateMgr::getUserId() { // - void GStateMgr::setRemoterS(int32_t state, const char* name) { zlock_guard l(m_mutex); AppEvent appevent; @@ -99,30 +98,14 @@ bool GStateMgr::getPumpSelectState(int32_t index) { zlock_guard l(m_mutex); return m_pumpSelectState[index]; } - -void GStateMgr::resetAcidUsed() { +float GStateMgr::getAcidRemain(int32_t index) { zlock_guard l(m_mutex); - for (int i = 0; i < 4; i++) { - acidUsed[i] = 0.0; - } + return acidRemain[index]; // add 0.5 for force change to int } float GStateMgr::getAcidUsed(int32_t index) { zlock_guard l(m_mutex); return acidUsed[index]; // add 0.5 for force change to int } -void GStateMgr::appendAcidUsed(int32_t index, float val) { - zlock_guard l(m_mutex); - acidUsed[index] += val; -} - -void GStateMgr::setAcidRemain(int32_t index, float val) { - zlock_guard l(m_mutex); - acidRemain[index] = val; -} -float GStateMgr::getAcidRemain(int32_t index) { - zlock_guard l(m_mutex); - return acidRemain[index]; // add 0.5 for force change to int -} bool GStateMgr::isHasPumpSelect() { zlock_guard l(m_mutex); @@ -134,11 +117,11 @@ bool GStateMgr::isHasPumpSelect() { return false; } - - - - -void GStateMgr::decreaseAcidRemain(int32_t index, float val) { +void GStateMgr::setAcidRemain(int32_t index, float val) { + zlock_guard l(m_mutex); + acidRemain[index] = val; +} +void GStateMgr::setAcidUsed(int32_t index, float val) { zlock_guard l(m_mutex); - acidRemain[index] -= val; + acidUsed[index] = val; } diff --git a/uappbase/service/gstate_mgr.hpp b/uappbase/service/gstate_mgr.hpp index 03b8d40..0533b60 100644 --- a/uappbase/service/gstate_mgr.hpp +++ b/uappbase/service/gstate_mgr.hpp @@ -13,6 +13,8 @@ using namespace std; class GStateMgr { private: + bool m_powerOn = true; + /** * @brief 用户状态 */ @@ -50,6 +52,14 @@ class GStateMgr { void initialize(); /** + * @brief powerOn + * + */ + + void setPowerOn(bool state) { m_powerOn = state; } + bool getPowerOn() { return m_powerOn; } + + /** * @brief 用户状态 */ @@ -84,35 +94,34 @@ class GStateMgr { */ /** - * @brief - * - * + * @brief + * + * * 1.剩余量: * .补酸时修改 * .用酸时修改 - * + * * 2.使用量: * .登录时初始化为零 * .使用时候修改,实时存储 * .登出时清零,并存储 - * + * * 相关数据库 * .总量 * .使用记录 - * - * 1. - * + * + * 1. + * */ - void initAcidStat(); + void setAcidRemain(int32_t index, float val); + void setAcidUsed(int32_t index, float val); - void resetAcidUsed(); - void appendAcidUsed(int32_t index, float val); - void setAcidRemain(int32_t index, float val); - void decreaseAcidRemain(int32_t index, float val); - - float getAcidUsed(int32_t index); float getAcidRemain(int32_t index); + float getAcidUsed(int32_t index); + + // + void placeHolder() {} }; } // namespace iflytop \ No newline at end of file diff --git a/usrc/db/dao/acid_ch_cfg_dao.cpp b/usrc/db/dao/acid_ch_cfg_dao.cpp index b0dda6b..ffa0eb8 100644 --- a/usrc/db/dao/acid_ch_cfg_dao.cpp +++ b/usrc/db/dao/acid_ch_cfg_dao.cpp @@ -23,10 +23,12 @@ static struct { uint32_t maxid; AcidChannelCfg_t item[4]; uint32_t end; -}* tabledata; +}* tabledata; +static zmutex lock(TABLE_NAME); static void storgeTableData() { ZHDB::storageData((uint8_t*)tabledata, sizeof(*tabledata)); } void AcidChCfgDao::init() { + lock.init(); // table = ZHDB::allocTable(TABLE_NAME, sizeof(*tabledata)); ASSERT(table != NULL, "alloc table failed"); @@ -68,25 +70,36 @@ static void checkIndex(int chIndex) { } AcidChannelCfg_t* AcidChCfgDao::getCfg(int chIndex) { + zlock_guard l(lock); checkIndex(chIndex); return &tabledata->item[chIndex]; } void AcidChCfgDao::updatekCfgAcidChooseName(int chIndex, const char* acidChooseName) { + zlock_guard l(lock); + checkIndex(chIndex); strcpy(tabledata->item[chIndex].acidChooseName, acidChooseName); storgeTableData(); } -void AcidChCfgDao::syncCfg() { storgeTableData(); } +void AcidChCfgDao::syncCfg() { + zlock_guard l(lock); + + storgeTableData(); +} void AcidChCfgDao::updateAcidEachDistriVal(int chIndex, float acidEachDistriVal) { + zlock_guard l(lock); + checkIndex(chIndex); tabledata->item[chIndex].acidEachDistriVal = acidEachDistriVal; storgeTableData(); } const char** AcidChCfgDao::getAcidCHNameList() { + zlock_guard l(lock); + static char nameTableStr[4][30]; static const char* nameTable[4 + 1]; memset(nameTable, 0, sizeof(nameTable)); diff --git a/usrc/db/dao/acid_distrubt_record_dao.cpp b/usrc/db/dao/acid_distrubt_record_dao.cpp index 8eaabe0..9ad31a0 100644 --- a/usrc/db/dao/acid_distrubt_record_dao.cpp +++ b/usrc/db/dao/acid_distrubt_record_dao.cpp @@ -19,6 +19,7 @@ using namespace iflytop; static zhdb_table_t* table; static bool module_inited; static acid_distrubt_record_table_t* acid_distrubt_record_table; +static zmutex lock(TABLE_NAME); /*********************************************************************************************************************** * FUNCTION * @@ -30,6 +31,7 @@ static void storgeItem(int offset) { } // void AcidDistrubtRecordDao::init() { + lock.init(); // table = ZHDB::allocTable(TABLE_NAME, sizeof(*acid_distrubt_record_table)); ASSERT(table != NULL, "alloc table failed"); @@ -48,7 +50,8 @@ void AcidDistrubtRecordDao::init() { ZLOGI(TAG, "init success"); } -void AcidDistrubtRecordDao::addRecord(const acid_distrubt_record_t& record) { +void AcidDistrubtRecordDao::addRecordSync(const acid_distrubt_record_t& record) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "%s not inited", TABLE_NAME); if (acid_distrubt_record_table->recordNum >= ACID_DISTRUBT_RECORD_NUM) { @@ -57,7 +60,7 @@ void AcidDistrubtRecordDao::addRecord(const acid_distrubt_record_t& record) { } int recordNum = acid_distrubt_record_table->recordNum; - + acid_distrubt_record_table->record[recordNum] = record; acid_distrubt_record_table->maxid++; acid_distrubt_record_table->recordNum++; @@ -72,11 +75,13 @@ void AcidDistrubtRecordDao::addRecord(const acid_distrubt_record_t& record) { acid_distrubt_record_table_t* AcidDistrubtRecordDao::getRecordTable() { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "%s not inited", TABLE_NAME); return acid_distrubt_record_table; } void AcidDistrubtRecordDao::getRecord(int pageNum, AcidDistrubtRcordPage* page) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "%s not inited", TABLE_NAME); // 获取最新的记录 @@ -110,10 +115,15 @@ void AcidDistrubtRecordDao::clearRecord() { acid_distrubt_record_table->recordNum = 0; storgeTableData(); } -bool AcidDistrubtRecordDao::isAlmostFull() { return acid_distrubt_record_table->recordNum >= ACID_DISTRUBT_ALMOST_FULL; } -bool AcidDistrubtRecordDao::isFull() { return acid_distrubt_record_table->recordNum >= ACID_DISTRUBT_RECORD_NUM; } +bool AcidDistrubtRecordDao::isAlmostFull() { + zlock_guard l(lock); + return acid_distrubt_record_table->recordNum >= ACID_DISTRUBT_ALMOST_FULL; } +bool AcidDistrubtRecordDao::isFull() { + zlock_guard l(lock); + return acid_distrubt_record_table->recordNum >= ACID_DISTRUBT_RECORD_NUM; } int AcidDistrubtRecordDao::getRecordNum() { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "%s not inited", TABLE_NAME); return acid_distrubt_record_table->recordNum; } diff --git a/usrc/db/dao/acid_distrubt_record_dao.hpp b/usrc/db/dao/acid_distrubt_record_dao.hpp index 979d4c3..5a369d6 100644 --- a/usrc/db/dao/acid_distrubt_record_dao.hpp +++ b/usrc/db/dao/acid_distrubt_record_dao.hpp @@ -30,7 +30,7 @@ class AcidDistrubtRecordDao { static void init(); static acid_distrubt_record_table_t* getRecordTable(); - static void addRecord(const acid_distrubt_record_t& record); + static void addRecordSync(const acid_distrubt_record_t& record); static void getRecord(int pageNum, AcidDistrubtRcordPage* page); static bool isAlmostFull(); static bool isFull(); diff --git a/usrc/db/dao/acid_name_dao.cpp b/usrc/db/dao/acid_name_dao.cpp index e36cb60..9011a5f 100644 --- a/usrc/db/dao/acid_name_dao.cpp +++ b/usrc/db/dao/acid_name_dao.cpp @@ -24,6 +24,7 @@ static struct { acid_name_t acidName[1]; uint32_t end; }* tabledata; +static zmutex lock(TABLE_NAME); static const char* defaultAcidName[MAX_ACID_NUM] = { "浓硫酸", // @@ -48,6 +49,7 @@ static const char* defaultAcidName[MAX_ACID_NUM] = { ***********************************************************************************************************************/ static void storgeTableData() { ZHDB::storageData((uint8_t*)tabledata, sizeof(*tabledata)); } void AcidNameDao::init() { + lock.init(); // table = ZHDB::allocTable(TABLE_NAME, sizeof(*tabledata)); ASSERT(table != NULL, "alloc table failed"); @@ -72,11 +74,13 @@ void AcidNameDao::init() { ZLOGI(TAG, "init success"); } acid_name_t* AcidNameDao::getAcidName() { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "acid name dao not inited"); return &tabledata->acidName[0]; } void AcidNameDao::updateAcidName(int index, const char* name) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "acid name dao not inited"); ZASSERT_INFO(index < MAX_ACID_NUM, "index out of range"); @@ -85,6 +89,7 @@ void AcidNameDao::updateAcidName(int index, const char* name) { } const char* AcidNameDao::getAcidName(int index) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "acid name dao not inited"); if (index >= MAX_ACID_NUM) { return "FAKE_INDEX"; @@ -94,6 +99,7 @@ const char* AcidNameDao::getAcidName(int index) { } const char** AcidNameDao::getAcidNameList() { + zlock_guard l(lock); static const char* acidNameList[MAX_ACID_NUM + 1] = {0}; for (int i = 0; i < MAX_ACID_NUM; i++) { acidNameList[i] = tabledata->acidName[0].acidName[i]; diff --git a/usrc/db/dao/acid_use_record_dao.cpp b/usrc/db/dao/acid_use_record_dao.cpp index 959cd4c..4f13a61 100644 --- a/usrc/db/dao/acid_use_record_dao.cpp +++ b/usrc/db/dao/acid_use_record_dao.cpp @@ -10,7 +10,7 @@ using namespace iflytop; * MARCO * ***********************************************************************************************************************/ #define TABLE_VERSION (TABLE_BASE_VERSION + 6) -#define TABLE_NAME "acid_use_record_table" +#define TABLE_NAME "acid_use_record_taxxble" /*********************************************************************************************************************** * STATIC VAR * @@ -18,77 +18,129 @@ using namespace iflytop; static zhdb_table_t* table; static bool module_inited; -static acid_use_record_table_t* acid_use_record_table; +static acid_use_record_table_t* m_table; +static zmutex lock(TABLE_NAME); /*********************************************************************************************************************** * FUNCTION * ***********************************************************************************************************************/ -static void storgeTableData() { ZHDB::storageData((uint8_t*)acid_use_record_table, sizeof(*acid_use_record_table)); } +static void storgeTableData() { ZHDB::storageData((uint8_t*)m_table, sizeof(*m_table)); } +static void storgeTableHeader() { ZHDB::storageData((uint8_t*)m_table, sizeof(*m_table) - sizeof(m_table->record) - sizeof(m_table->end)); } +static void storgeItem(int offset) { + ZHDB::storageData((uint8_t*)&(m_table->record[offset]), sizeof(acid_use_record_t)); + ZHDB::storageData((uint8_t*)m_table, sizeof(*m_table) - sizeof(m_table->record) - sizeof(m_table->end)); +} // void AcidUseRecordDao::init() { + lock.init(); // - table = ZHDB::allocTable(TABLE_NAME, sizeof(*acid_use_record_table)); + table = ZHDB::allocTable(TABLE_NAME, sizeof(*m_table)); ASSERT(table != NULL, "alloc table failed"); - acid_use_record_table = (decltype(acid_use_record_table))table->add; + m_table = (decltype(m_table))table->add; - if (TABLE_VERSION != acid_use_record_table->version || TABLE_VERSION != acid_use_record_table->end) { + if (TABLE_VERSION != m_table->version || TABLE_VERSION != m_table->end) { ZLOGI(TAG, "%s table version not match, reset table", TABLE_NAME); - acid_use_record_table->version = TABLE_VERSION; - acid_use_record_table->end = TABLE_VERSION; - acid_use_record_table->maxid = 0; - acid_use_record_table->recordNum = 0; + m_table->version = TABLE_VERSION; + m_table->end = TABLE_VERSION; + m_table->maxid = 0; + m_table->recordNum = 0; } storgeTableData(); module_inited = true; - // for (int i = 0; i < acid_use_record_table->recordNum; i++) { - // auto *record = &acid_use_record_table->record[i]; - - // ZLOGI(TAG, "record: %d/%d/%d %02d:%02d:%02d %d %d %d %d %d", // - // record->date.year, record->date.month, record->date.day, record->date.hours, record->date.minutes, record->date.seconds, // - // record->usrid, record->ch0take0p1ml, record->ch1take0p1ml, record->ch2take0p1ml, record->ch3take0p1ml); - // } - ZLOGI(TAG, "init success"); } void AcidUseRecordDao::addRecord(const acid_use_record_t& record) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "acid use record dao not inited"); - if (acid_use_record_table->recordNum >= ACIDUSERCORD_RECORD_NUM) { + if (m_table->recordNum >= ACIDUSERCORD_RECORD_NUM) { ZLOGE(TAG, "no space for new record"); return; } - int recordNum = acid_use_record_table->recordNum; + int recordNum = m_table->recordNum; - acid_use_record_table->record[recordNum] = record; - acid_use_record_table->maxid++; - acid_use_record_table->recordNum++; + m_table->record[recordNum] = record; + m_table->record[recordNum].dirty = 1; + m_table->maxid++; + m_table->recordNum++; ZLOGI(TAG, "add record %d/%d/%d %02d:%02d:%02d %d %d %d %d %d (%d/%d)", // record.date.year, record.date.month, record.date.day, record.date.hours, record.date.minutes, record.date.seconds, // - record.usrid, record.ch0take0p1ml, record.ch1take0p1ml, record.ch2take0p1ml, record.ch3take0p1ml, // - acid_use_record_table->recordNum, ACIDUSERCORD_RECORD_NUM); + record.usrid, record.ch0take0p1ml, record.ch1take0p1ml, record.ch2take0p1ml, record.ch3take0p1ml, // + m_table->recordNum, ACIDUSERCORD_RECORD_NUM); } -void AcidUseRecordDao::sync() { - ZASSERT_INFO(module_inited, "acid use record dao not inited"); - storgeTableData(); +void AcidUseRecordDao::updateLastRecord(uint16_t val0, uint16_t val1, uint16_t val2, uint16_t val3) { + zlock_guard l(lock); + int recordNum = m_table->recordNum - 1; + if (recordNum < 0) { + ZLOGW(TAG, "no record to update"); + return; + } + + if (m_table->record[recordNum].dirty == 0) { + ZLOGW(TAG, "record not dirty"); + return; + } + + m_table->record[recordNum].ch0take0p1ml = val0; + m_table->record[recordNum].ch1take0p1ml = val1; + m_table->record[recordNum].ch2take0p1ml = val2; + m_table->record[recordNum].ch3take0p1ml = val3; +} + +void AcidUseRecordDao::syncTheLastRecord() { + zlock_guard l(lock); + int recordNum = m_table->recordNum - 1; + if (recordNum < 0) { + ZLOGW(TAG, "no record to sync"); + return; + } + storgeItem(recordNum); +} + +void AcidUseRecordDao::setDirtyRecordClean() { + zlock_guard l(lock); + int recordNum = m_table->recordNum - 1; + if (recordNum < 0) { + ZLOGW(TAG, "no record to clean"); + return; + } + + m_table->record[recordNum].dirty = 0; +} +void AcidUseRecordDao::removeTheLastDirtyRecordSync() { + zlock_guard l(lock); + int recordNum = m_table->recordNum - 1; + if (recordNum < 0) { + ZLOGW(TAG, "no record to remove"); + return; + } + + m_table->recordNum--; + if (m_table->maxid > 0) { + m_table->maxid--; + } + storgeTableHeader(); } acid_use_record_table_t* AcidUseRecordDao::getRecordTable() { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "acid use record dao not inited"); - return acid_use_record_table; + return m_table; } void AcidUseRecordDao::getRecord(int pageNum, AcidUseRcordPage* page) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "acid use record dao not inited"); // 获取最新的记录 memset(page, 0, sizeof(AcidUseRcordPage)); - int recordNum = acid_use_record_table->recordNum; + int recordNum = m_table->recordNum; int start = recordNum - ACIDUSERCORD_PAGE_SIZE * pageNum - 1; if (start < 0) { page->pageNum = pageNum; @@ -103,7 +155,7 @@ void AcidUseRecordDao::getRecord(int pageNum, AcidUseRcordPage* page) { int i = 0; for (int j = start; j > end; j--) { - page->record[i++] = &acid_use_record_table->record[j]; + page->record[i++] = &m_table->record[j]; } page->pageNum = pageNum; @@ -111,15 +163,23 @@ void AcidUseRecordDao::getRecord(int pageNum, AcidUseRcordPage* page) { return; } void AcidUseRecordDao::clearRecord() { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "acid use record dao not inited"); - acid_use_record_table->recordNum = 0; + m_table->recordNum = 0; storgeTableData(); } -bool AcidUseRecordDao::isAlmostFull() { return acid_use_record_table->recordNum >= ACIDUSERCORD_ALMOST_FULL; } -bool AcidUseRecordDao::isFull() { return acid_use_record_table->recordNum >= ACIDUSERCORD_RECORD_NUM; } +bool AcidUseRecordDao::isAlmostFull() { + zlock_guard l(lock); + + return m_table->recordNum >= ACIDUSERCORD_ALMOST_FULL; } +bool AcidUseRecordDao::isFull() { + zlock_guard l(lock); + + return m_table->recordNum >= ACIDUSERCORD_RECORD_NUM; } int AcidUseRecordDao::getRecordNum() { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "acid use record dao not inited"); - return acid_use_record_table->recordNum; + return m_table->recordNum; } diff --git a/usrc/db/dao/acid_use_record_dao.hpp b/usrc/db/dao/acid_use_record_dao.hpp index d4e7bfd..cd97cef 100644 --- a/usrc/db/dao/acid_use_record_dao.hpp +++ b/usrc/db/dao/acid_use_record_dao.hpp @@ -31,7 +31,12 @@ class AcidUseRecordDao { static acid_use_record_table_t* getRecordTable(); static void addRecord(const acid_use_record_t& record); - static void sync(); + static void updateLastRecord(uint16_t val0, uint16_t val1, uint16_t val2, uint16_t val3); + static void setDirtyRecordClean(); + static void syncTheLastRecord(); + + static void removeTheLastDirtyRecordSync(); + static void getRecord(int pageNum, AcidUseRcordPage* page); static bool isAlmostFull(); static bool isFull(); diff --git a/usrc/db/dao/device_acid_volume_dao.cpp b/usrc/db/dao/device_acid_volume_dao.cpp index f5a282f..4b23d7f 100644 --- a/usrc/db/dao/device_acid_volume_dao.cpp +++ b/usrc/db/dao/device_acid_volume_dao.cpp @@ -25,11 +25,14 @@ static struct { uint32_t end; }* tabledata; +static zmutex lock(TABLE_NAME); + /*********************************************************************************************************************** * FUNCTION * ***********************************************************************************************************************/ static void storgeTableData() { ZHDB::storageData((uint8_t*)tabledata, sizeof(*tabledata)); } void DeviceAcidVolume::init() { + lock.init(); // table = ZHDB::allocTable(TABLE_NAME, sizeof(*tabledata)); ASSERT(table != NULL, "alloc table failed"); @@ -52,15 +55,28 @@ void DeviceAcidVolume::init() { } void DeviceAcidVolume::updateAcidVolume(int index, float volume) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "device acid volume dao not inited"); if (index < 0 || index >= sizeof(tabledata->item[0].acidVolume) / sizeof(tabledata->item[0].acidVolume[0])) { ZLOGE(TAG, "updateAcidVolume fail,index out of range index=%d", index); return; } tabledata->item[0].acidVolume[index] = volume; +} +void DeviceAcidVolume::sync() { + zlock_guard l(lock); + ZASSERT_INFO(module_inited, "device acid volume dao not inited"); storgeTableData(); } + +void DeviceAcidVolume::updateAcidVolumeSync(int index, float volume) { + zlock_guard l(lock); + updateAcidVolume(index, volume); + sync(); +} + float DeviceAcidVolume::getAcidVolume(int index) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "device acid volume dao not inited"); if (index < 0 || index >= sizeof(tabledata->item[0].acidVolume) / sizeof(tabledata->item[0].acidVolume[0])) { ZLOGE(TAG, "getAcidVolume fail,index out of range index=%d", index); diff --git a/usrc/db/dao/device_acid_volume_dao.hpp b/usrc/db/dao/device_acid_volume_dao.hpp index 3582d1e..b66f458 100644 --- a/usrc/db/dao/device_acid_volume_dao.hpp +++ b/usrc/db/dao/device_acid_volume_dao.hpp @@ -7,8 +7,12 @@ namespace iflytop { class DeviceAcidVolume { public: - static void init(); - static void updateAcidVolume(int index, float volume); + static void init(); + + static void updateAcidVolume(int index, float volume); + static void sync(); + + static void updateAcidVolumeSync(int index, float volume); static float getAcidVolume(int index); }; diff --git a/usrc/db/dao/device_setting_dao.cpp b/usrc/db/dao/device_setting_dao.cpp index ddb836b..1c0ab6f 100644 --- a/usrc/db/dao/device_setting_dao.cpp +++ b/usrc/db/dao/device_setting_dao.cpp @@ -24,12 +24,14 @@ static struct { device_setting_t item[1]; uint32_t end; }* tabledata; +static zmutex lock(TABLE_NAME); /*********************************************************************************************************************** * FUNCTION * ***********************************************************************************************************************/ static void storgeTableData() { ZHDB::storageData((uint8_t*)tabledata, sizeof(*tabledata)); } void DeviceSettingDao::init() { + lock.init(); // table = ZHDB::allocTable(TABLE_NAME, sizeof(*tabledata)); ASSERT(table != NULL, "alloc table failed"); @@ -56,16 +58,19 @@ void DeviceSettingDao::init() { device_setting_t* DeviceSettingDao::get() { return &tabledata->item[0]; } void DeviceSettingDao::updateBleClientName(const char* name) { + zlock_guard l(lock); ZLOGI(TAG, "updateBleClientName name:%s", name); strncpy(tabledata->item[0].bleClientName, name, sizeof(tabledata->item[0].bleClientName)); storgeTableData(); } void DeviceSettingDao::updateEchDitrUpLi(int value) { + zlock_guard l(lock); ZLOGI(TAG, "updateEchDitrUpLi value:%d", value); tabledata->item[0].echDitrUpLimit = value; storgeTableData(); } void DeviceSettingDao::updatedistrIntervalS(float value) { + zlock_guard l(lock); ZLOGI(TAG, "updatedistrIntervalS value:%f", value); tabledata->item[0].distrIntervalS = value; storgeTableData(); diff --git a/usrc/db/dao/user_dao.cpp b/usrc/db/dao/user_dao.cpp index 0612293..e8b48c4 100644 --- a/usrc/db/dao/user_dao.cpp +++ b/usrc/db/dao/user_dao.cpp @@ -26,6 +26,7 @@ typedef struct { static zhdb_table_t* table; static bool module_inited; static user_table_t* usr_table_data; +static zmutex lock(TABLE_NAME); /*********************************************************************************************************************** * FUNCTION * @@ -61,6 +62,8 @@ static void sortUsr() { // void UserDao::init() { // + lock.init(); + table = ZHDB::allocTable(TABLE_NAME, sizeof(*usr_table_data)); ASSERT(table != NULL, "alloc table failed"); usr_table_data = (decltype(usr_table_data))table->add; @@ -107,6 +110,7 @@ void UserDao::init() { // } void UserDao::getUsers(user_t** user, int* numUser) { + zlock_guard l(lock); *user = usr_table_data->user; int count = 0; for (int i = 0; i < MAX_USR_NUM; i++) { @@ -117,9 +121,13 @@ void UserDao::getUsers(user_t** user, int* numUser) { *numUser = count; } -user_t* UserDao::getUsersTable() { return usr_table_data->user; } -int UserDao::getUsersTableNum() { - int count = 0; +user_t* UserDao::getUsersTable() { + zlock_guard l(lock); + return usr_table_data->user; +} +int UserDao::getUsersTableNum() { + zlock_guard l(lock); + int count = 0; for (int i = 0; i < MAX_USR_NUM; i++) { if (usr_table_data->user[i].effective) { count++; @@ -129,6 +137,7 @@ int UserDao::getUsersTableNum() { } user_t* UserDao::getUserByName(const char* name) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "user dao not inited"); for (int i = 0; i < MAX_USR_NUM; i++) { if (usr_table_data->user[i].effective && strcmp(usr_table_data->user[i].name, name) == 0) { @@ -138,6 +147,7 @@ user_t* UserDao::getUserByName(const char* name) { return nullptr; } user_t* UserDao::getUserById(int32_t id) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "user dao not inited"); for (int i = 0; i < MAX_USR_NUM; i++) { if (usr_table_data->user[i].effective && usr_table_data->user[i].id == id) { @@ -148,6 +158,7 @@ user_t* UserDao::getUserById(int32_t id) { } user_t* UserDao::getUserByIdNotNull(int32_t id) { + zlock_guard l(lock); user_t* user = UserDao::getUserById(id); static user_t nullUser; nullUser.id = -1; @@ -160,6 +171,7 @@ user_t* UserDao::getUserByIdNotNull(int32_t id) { // Errno UserDao::addUser(const char* name, const char* passwd, user_role_t role) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "user dao not inited"); user_t* user = nullptr; @@ -205,9 +217,13 @@ const char* createNewUsrName() { return name; } -Errno UserDao::addUser() { return UserDao::addUser(createNewUsrName(), "888888", kuser); } +Errno UserDao::addUser() { + zlock_guard l(lock); + return UserDao::addUser(createNewUsrName(), "888888", kuser); +} void UserDao::delUser(uint8_t userId) { + zlock_guard l(lock); ZLOGI(TAG, "del user %d", userId); ZASSERT_INFO(module_inited, "user dao not inited"); @@ -222,6 +238,7 @@ void UserDao::delUser(uint8_t userId) { } void UserDao::updateUser(uint8_t userId, const char* name, const char* passwd, user_role_t role) { + zlock_guard l(lock); ZASSERT_INFO(module_inited, "user dao not inited"); user_t* user = nullptr; @@ -249,6 +266,7 @@ void UserDao::updateUser(uint8_t userId, const char* name, const char* passwd, u } const char** UserDao::getUsrNameTable() { + zlock_guard l(lock); static const char* nameTable[MAX_USR_NUM + 1]; memset(nameTable, 0, sizeof(nameTable)); for (int i = 0; i < MAX_USR_NUM; i++) { diff --git a/usrc/service/statistics_sync_service.cpp b/usrc/service/statistics_sync_service.cpp new file mode 100644 index 0000000..893445e --- /dev/null +++ b/usrc/service/statistics_sync_service.cpp @@ -0,0 +1,148 @@ +#include "statistics_sync_service.hpp" + +#include "db\dao\acid_ch_cfg_dao.hpp" +#include "db\dao\acid_distrubt_record_dao.hpp" +#include "db\dao\acid_name_dao.hpp" +#include "db\dao\acid_use_record_dao.hpp" +#include "db\dao\device_acid_volume_dao.hpp" +#include "db\dao\device_setting_dao.hpp" +#include "db\dao\user_dao.hpp" + +#define TAG "StatisticsSyncService" +using namespace iflytop; +static ZThread m_thread; +static osTimerId statiUpdateTimerId; // + +static bool m_dataIsDirty = false; +static void syncTimer(const void* tid); + +void StatisticsSyncService::initialize() { + m_thread.init("StatiSync"); + AppEventBus::ins()->regOnEvent([](AppEvent* event) { + if (event->type == kAE_LoginEvent) { + onLogin(); + } + + else if (event->type == kAE_AcidStatChangeEvent) { + auto* acidStateChangeEvent = event->getAcidStateChangeEvent(); + if (acidStateChangeEvent->subtype == kACID_STORAGE) { + onAddAcidStorage(acidStateChangeEvent->addCh, acidStateChangeEvent->addChVal); + } else if (acidStateChangeEvent->subtype == kACID_USE) { + onUseAcid(acidStateChangeEvent->useVal[0], acidStateChangeEvent->useVal[1], acidStateChangeEvent->useVal[2], acidStateChangeEvent->useVal[3]); + } + } + }); + + osTimerDef(statiUpdateTimer, syncTimer); + osTimerStart(statiUpdateTimerId, 30 * 1000); +} + +void StatisticsSyncService::onLogin() { + /** + * @brief + * 1.同步数据库中当前剩余酸液到全局 + * 2.清空全局用户酸液使用量 + */ + + GStateMgr::ins()->setAcidRemain(0, DeviceAcidVolume::getAcidVolume(0)); + GStateMgr::ins()->setAcidRemain(1, DeviceAcidVolume::getAcidVolume(1)); + GStateMgr::ins()->setAcidRemain(2, DeviceAcidVolume::getAcidVolume(2)); + GStateMgr::ins()->setAcidRemain(3, DeviceAcidVolume::getAcidVolume(3)); + + GStateMgr::ins()->setAcidUsed(0, 0); + GStateMgr::ins()->setAcidUsed(1, 0); + GStateMgr::ins()->setAcidUsed(2, 0); + GStateMgr::ins()->setAcidUsed(3, 0); + + acid_use_record_t record = {0}; + AppHal::rtc_set(&record.date); + record.usrid = GStateMgr::ins()->getUserId(); + record.dirty = 1; + + AcidUseRecordDao::addRecord(record); + AcidUseRecordDao::syncTheLastRecord(); +} +void StatisticsSyncService::doSyncOnUnLoginOrPowerOff() { + /** + * @brief 切换到事件上下文 + */ + if (!AppEventBus::ins()->isInAppEventThread()) { + bool callfinished = false; + AppEventBus::ins()->callFnInEventBus([&]() { + doSyncOnUnLoginOrPowerOff(); + callfinished = true; + }); + + while (!callfinished) { + osDelay(30); + } + } + + float used0 = GStateMgr::ins()->getAcidUsed(0); + float used1 = GStateMgr::ins()->getAcidUsed(1); + float used2 = GStateMgr::ins()->getAcidUsed(2); + float used3 = GStateMgr::ins()->getAcidUsed(3); + + /** + * @brief 同步数据库中当前剩余酸液到全局 + */ + DeviceAcidVolume::updateAcidVolume(0, GStateMgr::ins()->getAcidRemain(0)); + DeviceAcidVolume::updateAcidVolume(1, GStateMgr::ins()->getAcidRemain(1)); + DeviceAcidVolume::updateAcidVolume(2, GStateMgr::ins()->getAcidRemain(2)); + DeviceAcidVolume::updateAcidVolume(3, GStateMgr::ins()->getAcidRemain(3)); + DeviceAcidVolume::sync(); + + // 如果酸液使用量小于0.01,则删除最后一条记录 + if (used0 < 0.01 && used1 < 0.01 && used2 < 0.01 && used3 < 0.01) { + AcidUseRecordDao::removeTheLastDirtyRecordSync(); + } else { + ZLOGI(TAG, "updateLastRecordSync : %f %f %f %f", used0, used1, used2, used3); + AcidUseRecordDao::updateLastRecord(used0, used1, used2, used3); + AcidUseRecordDao::syncTheLastRecord(); + } + + /** + * @brief 清空全局用户酸液使用量 + */ + + GStateMgr::ins()->setAcidUsed(0, 0); + GStateMgr::ins()->setAcidUsed(1, 0); + GStateMgr::ins()->setAcidUsed(2, 0); + GStateMgr::ins()->setAcidUsed(3, 0); + m_dataIsDirty = false; +} + +void StatisticsSyncService::onAddAcidStorage(int ch, float addChVal) { + /** + * @brief 添加酸液 + * + * 1. 修正酸液总量 + * 2. 修正酸液剩余量 + * + */ + GStateMgr::ins()->setAcidRemain(ch, addChVal); + DeviceAcidVolume::updateAcidVolumeSync(ch, addChVal); +} +void StatisticsSyncService::onUseAcid(float useVal0, float useVal1, float useVal2, float useVal3) { + float now0 = GStateMgr::ins()->getAcidUsed(0); + float now1 = GStateMgr::ins()->getAcidUsed(1); + float now2 = GStateMgr::ins()->getAcidUsed(2); + float now3 = GStateMgr::ins()->getAcidUsed(3); + + GStateMgr::ins()->setAcidUsed(0, now0 + useVal0); + GStateMgr::ins()->setAcidUsed(1, now1 + useVal1); + GStateMgr::ins()->setAcidUsed(2, now2 + useVal2); + GStateMgr::ins()->setAcidUsed(3, now3 + useVal3); + + AcidUseRecordDao::updateLastRecord(now0 + useVal0, now1 + useVal1, now2 + useVal2, now3 + useVal3); + m_dataIsDirty = true; +} + +static void syncTimer(const void* tid) { + AppEventBus::ins()->callFnInEventBus([]() { + if (m_dataIsDirty) { + AcidUseRecordDao::syncTheLastRecord(); + m_dataIsDirty = false; + } + }); +} diff --git a/usrc/service/statistics_sync_service.hpp b/usrc/service/statistics_sync_service.hpp new file mode 100644 index 0000000..35ceea3 --- /dev/null +++ b/usrc/service/statistics_sync_service.hpp @@ -0,0 +1,16 @@ +#pragma once +#include "uappbase/base.hpp" +namespace iflytop { +using namespace std; +class StatisticsSyncService { + public: + static void initialize(); + static void doSyncOnUnLoginOrPowerOff(); + + private: + static void onLogin(); + static void onAddAcidStorage(int ch, float addChVal); + static void onUseAcid(float useVal0, float useVal1, float useVal2, float useVal3); +}; + +} // namespace iflytop diff --git a/usrc/uicontroler/page/mupage/MuAcidDistMgrPage.cpp b/usrc/uicontroler/page/mupage/MuAcidDistMgrPage.cpp index 61292e5..5d0b8c7 100644 --- a/usrc/uicontroler/page/mupage/MuAcidDistMgrPage.cpp +++ b/usrc/uicontroler/page/mupage/MuAcidDistMgrPage.cpp @@ -123,9 +123,9 @@ class MuAcidDistMgrPage : public IPageProcesser { record.takeAcidUsrid = m_takePers; record.distribuCh = m_chId; record.distribuVolume = m_volumeVal; - AcidDistrubtRecordDao::addRecord(record); + AcidDistrubtRecordDao::addRecordSync(record); - DeviceAcidVolume::updateAcidVolume(m_chId, m_volumeVal); + DeviceAcidVolume::updateAcidVolumeSync(m_chId, m_volumeVal); GStateMgr::ins()->setAcidRemain(m_chId, m_volumeVal); AppEventBus::ins()->pushAcidStatStorageEvent(m_chId, m_volumeVal); resetForm(); diff --git a/usrc/uicontroler/page/mupage/muAcidUseMgr_page.cpp b/usrc/uicontroler/page/mupage/muAcidUseMgr_page.cpp index d822932..4db6a49 100644 --- a/usrc/uicontroler/page/mupage/muAcidUseMgr_page.cpp +++ b/usrc/uicontroler/page/mupage/muAcidUseMgr_page.cpp @@ -92,7 +92,7 @@ class MuAcidUseMgrPage : public IPageProcesser { record.ch1take0p1ml = 110; record.ch2take0p1ml = 120; record.ch3take0p1ml = 130; - AcidUseRecordDao::addRecord(record); + AcidUseRecordDao::addRecordSync(record); } AcidUseRecordDao::sync(); #endif @@ -124,7 +124,6 @@ class MuAcidUseMgrPage : public IPageProcesser { if (isOk) { UILoadingCxt loadingCxt; AcidUseRecordDao::clearRecord(); - AcidUseRecordDao::sync(); UIControler::ins()->chpage(pg_muAcidUseRecd, true); } diff --git a/usrc/uicontroler/tjc/tjc_base_type.h b/usrc/uicontroler/tjc/tjc_base_type.h index 2a3bca0..d0b5054 100644 --- a/usrc/uicontroler/tjc/tjc_base_type.h +++ b/usrc/uicontroler/tjc/tjc_base_type.h @@ -1,33 +1,5 @@ #pragma once #include -/** - * @brief - * - * 约定 - * 1.用户名最大长度为15 - * 2.组件名称最大长度为14 - * 3.页面名称最大长度为14 - * 4.密码为纯数字6位数密码 - * - * - * 1. 键盘最终不修改页面内容,页面内容由单片机进行修改 - * 2. 每进入一个页面均要发送sendme指令 - * 2. 可用组件 - * 按键 - * 双态按键 - * 键盘 - * 文本输入框(页面一律使用文本输入框) - * 图片 - * 返回按键 - * 下拉框组件 - * - */ - - - -/*********************************************************************************************************************** - * event * - ***********************************************************************************************************************/ #include "ui_event.h"