Forráskód Böngészése

增加了定时功能

chenyuanjie 2 éve%!(EXTRA string=óta)
szülő
commit
8011862184
8 módosított fájl, 460 hozzáadás és 33 törlés
  1. 36 2
      pro/src/app.c
  2. 96 14
      pro/src/appweb_handle.c
  3. 25 0
      pro/src/common.c
  4. 11 3
      pro/src/common.h
  5. 123 14
      pro/src/json_handle.c
  6. 5 0
      pro/src/json_handle.h
  7. 159 0
      pro/src/sqlite_handle.c
  8. 5 0
      pro/src/sqlite_handle.h

+ 36 - 2
pro/src/app.c

@@ -48,7 +48,7 @@ void* power_thread(void* arg)
     int nSaveTacIndex = 0;
     thread_handle_t *h=(thread_handle_t*)arg;
     GlobalDeviceManager* _globalDeviceManager = (GlobalDeviceManager*)h->arg;
-    
+    _PowerDSManage_t* _powerDsManageTemp = NULL ;
     dev_search_last_Index(_globalDeviceManager->db, &nSaveIndex, strPower);       // 查询日志最大点
     dev_search_last_Index(_globalDeviceManager->db, &nSaveTacIndex, strTacPower); // 查询日志最大点
     _globalDeviceManager->_global_alarmLanguage=create_hash_table(13);
@@ -531,7 +531,39 @@ void* power_thread(void* arg)
                 memcpy(&_globalPowerMangerTemp->_PowerWarninginfo,&_globalPowerWarningInfo,sizeof(PowerWarningInfo)); 
             }
             isErrorBreak=false;
-
+            //判断是否存在定时需要触发
+            time_t now;
+            time(&now);
+            t = localtime(&now);
+            char buffer[80];
+            int nNowt=t->tm_hour*3600+t->tm_min*60+t->tm_sec;
+            int nDst=0,nHours=0,nMin=0,nSecond=0;
+            strftime(buffer, sizeof(buffer), "%Y-%m-%d", t);
+            list_for_each_entry(_powerDsManageTemp, &_globalPowerMangerTemp->list_DS, list)
+            {
+                if (strcmp(_powerDsManageTemp->ds_date, buffer) == 0&&_powerDsManageTemp->ds_is_triggered==0)
+                {
+                    sscanf(_powerDsManageTemp->ds_time, "%d:%d:%d", &nHours, &nMin, &nSecond);
+                    nDst=nHours *3600+nMin*60+nSecond;
+                    int ntoSec=abs(nNowt-nDst);
+                    if (ntoSec < 5) // 小于3秒则触发
+                    {
+                        ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger, _globalPowerMangerTemp,
+                                                        _globalPowerMangerTemp->product_saddr,
+                                                        _globalPowerMangerTemp->product_ch_addr,
+                                                        _powerDsManageTemp->set_stats, false);
+                        if (ret < 0)
+                        {
+                            ret = g_switch_set_all_chn_ctrl(&__globalDeviceManage._globalRelaySampManger, _globalPowerMangerTemp,
+                                                            _globalPowerMangerTemp->product_saddr,
+                                                            _globalPowerMangerTemp->product_ch_addr,
+                                                            _powerDsManageTemp->set_stats, false);
+                        }
+                        _powerDsManageTemp->ds_is_triggered = 1;
+                        dev_update_power_ds(__globalDeviceManage.db, _globalPowerMangerTemp->product_id, _globalPowerMangerTemp->product_ch_id, _powerDsManageTemp);
+                    }
+                }                
+            }
         }
 
         //all 
@@ -1679,6 +1711,8 @@ int _global_device_manage_init(GlobalDeviceManager* _globalDeviceManager)
             }
         }
     }
+    //获取数据库中的定时数据
+    ret=dev_get_power_ds(_globalDeviceManager->db,&_globalDeviceManager->_globalPowerManger,NULL);
 
     //初始化传感器列表
     INIT_LIST_HEAD(&_globalDeviceManager->_globalSensorManger.list);

+ 96 - 14
pro/src/appweb_handle.c

@@ -2690,36 +2690,118 @@ static void serviceManagement(HttpStream *stream)
         if (_serviceManageRequestInfo._ntp_info.weekday) free(_serviceManageRequestInfo._ntp_info.weekday);
     }
     // 定时设置
-    else if (strcmp(_serviceManageRequestInfo.type, "listDsQuery") == 0)
+    else if (strcmp(_serviceManageRequestInfo.type, "listDsQuery") == 0)//||strcmp(_serviceManageRequestInfo.type, "listRlQuery") == 0)
     {
-        int chn_id = atoi(_serviceManageRequestInfo._ds_info.productChId);
         // 查询通道信息
+        /*
         GlobalPowerManger *_globalPowerMangerTemp = NULL;
         list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
         {
             if (_globalPowerMangerTemp->product_ch_id == chn_id)
                 break;
         }
+        */
         // 返回定时信息
-        ack_str = service_ds_ack_to_json(_serviceManageRequestInfo._ds_info.productChName,
-                                         _serviceManageRequestInfo._ds_info.productChId,
-                                         _globalPowerMangerTemp->_powerDsManage);
+        pthread_mutex_lock(&__globalDeviceManage._power_ds);             
+        ack_str = service_ds_all_ack_to_json(_serviceManageRequestInfo._ds_info.channelDate,
+                                        _serviceManageRequestInfo._ds_info.productChName,
+                                        _serviceManageRequestInfo._ds_info.productChId,
+                                        &__globalDeviceManage._globalPowerManger);
+        pthread_mutex_unlock(&__globalDeviceManage._power_ds);
     }
     else if (strcmp(_serviceManageRequestInfo.type, "dsSave") == 0)
     {
-        int chn_id = atoi(_serviceManageRequestInfo._ds_info.productChId);
-        // 查询通道信息
+        pthread_mutex_lock(&__globalDeviceManage._power_ds);         
+        int chn_id = -1;
+        for (int i = 0; i < 64; i++)
+        {
+            chn_id = _serviceManageRequestInfo._ds_info.productChIds[i];
+            if (chn_id <= 0)
+            {
+                break;
+            }
+            // 查询通道信息
+            GlobalPowerManger *_globalPowerMangerTemp = NULL;
+            list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
+            {
+                if (_globalPowerMangerTemp->product_ch_id == chn_id)
+                    break;
+            }
+            int start_year, start_month, start_day;
+            int end_year, end_month, end_day;
+            char current_date[11]; // 足够存储 "YYYY-MM-DD\0"
+            // 解析起始和结束日期
+            sscanf(_serviceManageRequestInfo._ds_info.StartDate, "%d-%d-%d", &start_year, &start_month, &start_day);
+            sscanf(_serviceManageRequestInfo._ds_info.Enddate, "%d-%d-%d", &end_year, &end_month, &end_day);
+            // 复制起始日期到current_date
+            strcpy(current_date, _serviceManageRequestInfo._ds_info.StartDate);
+            // 循环直到或超过结束日期
+            int done = 0;
+            while (!done)
+            {
+                // 当前日期
+
+                _PowerDSManage_t *new_data = (_PowerDSManage_t *)malloc(sizeof(_PowerDSManage_t));
+                // 清空数据
+                memset(new_data, 0, sizeof(new_data));
+                strcpy(new_data->ds_date, current_date);
+                strcpy(new_data->ds_time, _serviceManageRequestInfo._ds_info.channelTime);
+                new_data->set_stats = atoi(_serviceManageRequestInfo._ds_info.setStatus);
+                new_data->ds_is_triggered = 0;
+                list_add_tail(&new_data->list, &_globalPowerMangerTemp->list_DS);
+                dev_insert_power_ds(__globalDeviceManage.db, 1, chn_id, new_data);
+                // 解析当前日期为年、月、日
+                sscanf(current_date, "%d-%d-%d", &start_year, &start_month, &start_day);
+                // 增加一天
+                add_day_to_date(current_date, &start_year, &start_month, &start_day);
+                // 检查是否超过结束日期
+                if (start_year > end_year ||
+                    (start_year == end_year && start_month > end_month) ||
+                    (start_year == end_year && start_month == end_month && start_day > end_day))
+                {
+                    done = 1; // 超过结束日期,退出循环
+                }
+            }
+        }
+        pthread_mutex_unlock(&__globalDeviceManage._power_ds);
+        ack_str = ctrl_sts_ack_to_json();
+    }
+    //批量删除
+    else if (strcmp(_serviceManageRequestInfo.type, "deleteDs")==0)
+    {
+        pthread_mutex_lock(&__globalDeviceManage._power_ds);     
+        int nChID = 0;
+        char pcDate[16];
+        char pcTime[16];
         GlobalPowerManger *_globalPowerMangerTemp = NULL;
-        list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
+        _PowerDSManage_t* _powerDsManageTemp=NULL;;
+        char *pCharData;
+        for (int i = 0; i < 64; i++)
         {
-            if (_globalPowerMangerTemp->product_ch_id == chn_id)
+            pCharData = _serviceManageRequestInfo._ds_info.idx[i];
+            if (pCharData == NULL || *pCharData == '\0')
+            {
                 break;
+            }
+            sscanf(pCharData, "%d %s %s", &nChID, &pcDate, &pcTime);
+            list_for_each_entry(_globalPowerMangerTemp, &__globalDeviceManage._globalPowerManger.list, list)
+            {
+                if (_globalPowerMangerTemp->product_ch_id == nChID)
+                    break;
+            }
+            list_for_each_entry(_powerDsManageTemp, &_globalPowerMangerTemp->list_DS, list)
+            {
+                if (strcmp(_powerDsManageTemp->ds_date, pcDate) == 0&&strcmp(_powerDsManageTemp->ds_time, pcTime) == 0)
+                {
+                    dev_delete_power_ds(__globalDeviceManage.db, 1, _globalPowerMangerTemp->product_ch_id, _powerDsManageTemp);
+                    list_del(&_powerDsManageTemp->list);  
+                    free(_powerDsManageTemp);
+                    _powerDsManageTemp=NULL;
+                    break;
+                }                
+            }
         }
-        // 记录参数
-        strcpy(_globalPowerMangerTemp->_powerDsManage->ds_date, _serviceManageRequestInfo._ds_info.channelDate);
-        strcpy(_globalPowerMangerTemp->_powerDsManage->ds_time, _serviceManageRequestInfo._ds_info.channelTime);
-        _serviceManageRequestInfo._ds_info.setStatus = atoi(_serviceManageRequestInfo._ds_info.setStatus);
-        //
+        pthread_mutex_unlock(&__globalDeviceManage._power_ds);
         ack_str = ctrl_sts_ack_to_json();
     }
     // 时序设置

+ 25 - 0
pro/src/common.c

@@ -450,3 +450,28 @@ bool CheckLoginStatus(char* Token)
     // return __globalDeviceManage._global_device_info->login_status;
 }
 
+// 辅助函数:判断是否是闰年  
+int is_leap_year(int year) {  
+    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);  
+}  
+  
+// 辅助函数:获取某月的天数  
+int days_in_month(int year, int month) {  
+    int days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  
+    if (month == 2 && is_leap_year(year)) return 29;  
+    return days_per_month[month];  
+}  
+  
+// 函数:增加一天到日期  
+void add_day_to_date(char *date_str, int *year, int *month, int *day) {  
+    (*day)++;  
+    if (*day > days_in_month(*year, *month)) {  
+        (*day) = 1;  
+        (*month)++;  
+        if (*month > 12) {  
+            (*month) = 1;  
+            (*year)++;  
+        }  
+    }  
+    sprintf(date_str, "%04d-%02d-%02d", *year, *month, *day);  
+}  

+ 11 - 3
pro/src/common.h

@@ -275,7 +275,8 @@ typedef struct
     char                set_date[16]; //设置日期
     char                ds_date[16]; //设置时间
     char                ds_time[16]; //设置时间
-    char                set_stats;    //状态
+    int                 set_stats;    //状态
+    int                 ds_is_triggered;   //是否已触发
     struct list_head    list;   
 }_PowerDSManage_t;
 
@@ -374,7 +375,7 @@ typedef struct
     unsigned short      product_ch_start_delay                     ;//通道延时 
     unsigned short      product_ch_stop_delay                      ;//通道延时 
     GlobalOverManager*  global_over_manager                        ;//超限管理
-    _PowerDSManage_t*   _powerDsManage                             ;//定时设置      
+    struct list_head    list_DS;                                    //定时设置      
     _PowerSXManage_t*   _PowerSXManage;                             //时序管理      
     PowerInfo           _PowerInfo;                                 //存储最新的数据     
     PowerWarningInfo    _PowerWarninginfo ;                         //存储报警数据     
@@ -713,7 +714,7 @@ typedef struct
     struct modbus_slave_read_reg g_modebus_read;
     struct modbus_slave_write_reg g_modebus_write;
     smartPDU_shm_data* g_shm_Data;
-   
+    pthread_mutex_t     _power_ds;        //定时器锁 
     cascade_dc_mmap_t *dc_mmap;
     cascade_single_mmap_t *single_mmap;
 }GlobalDeviceManager;
@@ -752,4 +753,11 @@ char* getUserNamebyToken(char* Token);
 int is_process_running(const char *process_name);
 bool CheckLoginStatus(char* Token);
 
+// 辅助函数:判断是否是闰年  
+int is_leap_year(int year);  
+// 辅助函数:获取某月的天数  
+int days_in_month(int year, int month);
+// 函数:增加一天到日期  
+void add_day_to_date(char *date_str, int *year, int *month, int *day);
+
 #endif

+ 123 - 14
pro/src/json_handle.c

@@ -2856,8 +2856,83 @@ char* service_ds_ack_to_json(char* chn_name,char* chn_id,_PowerDSManage_t* __pow
     cJSON_Delete(root);
     return str;
 }
-
-
+/// @brief 返回所有定时设置
+/// @param chn_name 
+/// @param chn_id 
+/// @param __powerDSManage_t 
+/// @return 
+char* service_ds_all_ack_to_json(char* pDate,char* strName,char* strchID,GlobalPowerManger* _powerAll)
+{
+    if (!_powerAll)
+    {
+        return NULL;
+    }    
+    char* str = NULL;
+    int nchID=0;
+    char datetemp[32]={0};
+    char temp[64]={0};
+    _PowerDSManage_t* _powerDsManageTemp = NULL ;
+    // 创建 JSON 对象
+    cJSON* root = cJSON_CreateObject();   
+    // 添加基本键值对
+    cJSON_AddNumberToObject(root, "code", 200); 
+    //创建数据
+    cJSON* data_root_array = cJSON_CreateArray();   
+    GlobalPowerManger* _globalPowerMangerTemp=NULL;
+    list_for_each_entry(_globalPowerMangerTemp, &_powerAll->list, list)
+    {
+        if (!(strchID == NULL || *strchID == '\0'))//ID号
+        {
+            nchID=atoi(strchID);
+            if (nchID>0)
+            {
+                if (nchID != _globalPowerMangerTemp->product_ch_id)
+                {
+                    continue;
+                }
+            }
+        }
+        if (!(strName == NULL || *strName == '\0'))//名称
+        {
+            if (strcmp(_globalPowerMangerTemp->product_ch_name, strName) != 0)
+            {
+                continue;
+            }
+        }
+        // 循环添加数据s
+        _powerDsManageTemp=NULL;
+        list_for_each_entry(_powerDsManageTemp, &_globalPowerMangerTemp->list_DS,list)
+        {
+            if (!(pDate == NULL || *pDate == '\0'))
+            {
+                if (strcmp(_powerDsManageTemp->ds_date, pDate) != 0)
+                {
+                    continue;
+                }
+            }
+            cJSON *data_filed = cJSON_CreateObject();
+            sprintf(temp,"%d %s %s",_globalPowerMangerTemp->product_ch_id,_powerDsManageTemp->ds_date,_powerDsManageTemp->ds_time);
+            cJSON_AddStringToObject(data_filed, "idx", temp);
+            cJSON_AddStringToObject(data_filed, "productChName", _globalPowerMangerTemp->product_ch_name);
+            sprintf(temp,"%d",_globalPowerMangerTemp->product_ch_id);
+            cJSON_AddStringToObject(data_filed, "productChId", temp);
+            cJSON_AddStringToObject(data_filed, "setDate", _powerDsManageTemp->ds_date);
+            cJSON_AddStringToObject(data_filed, "setTime", _powerDsManageTemp->ds_time);
+            cJSON_AddNumberToObject(data_filed, "setStatus", _powerDsManageTemp->set_stats);
+            cJSON_AddNumberToObject(data_filed, "triggerStatus", _powerDsManageTemp->ds_is_triggered);
+            cJSON_AddItemToObject(data_root_array, "data", data_filed);
+        }
+    }
+    //添加汇总
+    cJSON_AddItemToObject(root, "data", data_root_array);
+    //添加结果
+    cJSON_AddStringToObject(root, "msg", "操作成功");
+    //转换
+    str = cJSON_Print(root);    
+    //释放资源
+    cJSON_Delete(root);
+    return str;
+}
 
 /// @brief 告警信息请求
 /// @param str 
@@ -3232,11 +3307,22 @@ cJSON* json_to_service_manage(const char* str,_ServiceManageRequestInfo* _servic
     else if(strcmp(_serviceManageRequestInfo->type,"listDsQuery")==0)
     {
         cJSON* cjson_ds_ch_name = cJSON_GetObjectItem(cjson,"productChName");
-        cJSON* cjson_ds_ch_id = cJSON_GetObjectItem(cjson,"productChId");  
-        if(cjson_ds_ch_name!=NULL)
-            _serviceManageRequestInfo->_ds_info.productChName = cjson_ds_ch_name->valuestring ;
-        if(cjson_ds_ch_id!=NULL)
-            _serviceManageRequestInfo->_ds_info.productChId = cjson_ds_ch_id->valuestring ;           
+        cJSON* cjson_ds_ch_id   = cJSON_GetObjectItem(cjson,"productChId");  
+        cJSON* cjson_ds_ch_date = cJSON_GetObjectItem(cjson,"setDate");
+        if (cjson_ds_ch_name != NULL)
+        {
+            _serviceManageRequestInfo->_ds_info.productChName = cjson_ds_ch_name->valuestring;
+        }
+        if (cjson_ds_ch_id != NULL)
+        {
+            _serviceManageRequestInfo->_ds_info.productChId = cjson_ds_ch_id->valuestring;
+        }
+        if (cjson_ds_ch_date != NULL)
+        {
+            _serviceManageRequestInfo->_ds_info.channelDate = cjson_ds_ch_date->valuestring;
+        }else{
+            _serviceManageRequestInfo->_ds_info.channelDate =NULL;
+        }
     }
     else if(strcmp(_serviceManageRequestInfo->type,"listSxQuery")==0)
     {
@@ -3255,20 +3341,43 @@ cJSON* json_to_service_manage(const char* str,_ServiceManageRequestInfo* _servic
     {
         cJSON* cjson_ds_ch_name = cJSON_GetObjectItem(cjson,"productChName");
         cJSON* cjson_ds_ch_id = cJSON_GetObjectItem(cjson,"productChId");  
-        cJSON* cjson_ds_date = cJSON_GetObjectItem(cjson,"channelDate"); 
+        cJSON* cjson_ds_dateS = cJSON_GetObjectItem(cjson,"startDate"); 
+        cJSON* cjson_ds_dateE = cJSON_GetObjectItem(cjson,"endDate"); 
         cJSON* cjson_ds_time = cJSON_GetObjectItem(cjson,"channelTime"); 
         cJSON* cjson_ds_sts = cJSON_GetObjectItem(cjson,"setStatus"); 
         if(cjson_ds_ch_name!=NULL)
             _serviceManageRequestInfo->_ds_info.productChName = cjson_ds_ch_name->valuestring ;
         if(cjson_ds_ch_id!=NULL)
-            _serviceManageRequestInfo->_ds_info.productChId = cjson_ds_ch_id->valuestring ;  
-        if(cjson_ds_date!=NULL)
-            _serviceManageRequestInfo->_ds_info.channelDate = cjson_ds_date->valuestring ;
-        if(cjson_ds_time!=NULL)
-            _serviceManageRequestInfo->_ds_info.channelTime = cjson_ds_time->valuestring ;  
-        if(cjson_ds_sts!=NULL)
+        {
+            memset(_serviceManageRequestInfo->_ds_info.productChIds,-1,sizeof(_serviceManageRequestInfo->_ds_info.productChIds));
+            int nIndex =0;
+            for (cJSON* cjson_ds_ch_id_child = cjson_ds_ch_id->child; cjson_ds_ch_id_child != NULL; cjson_ds_ch_id_child = cjson_ds_ch_id_child->next,nIndex++)
+            {               
+                _serviceManageRequestInfo->_ds_info.productChIds[nIndex]=cjson_ds_ch_id_child->valueint;
+            }
+        }
+        if (cjson_ds_dateS != NULL)
+            _serviceManageRequestInfo->_ds_info.StartDate = cjson_ds_dateS->valuestring;
+        if (cjson_ds_dateE != NULL)
+            _serviceManageRequestInfo->_ds_info.Enddate = cjson_ds_dateE->valuestring;
+        if (cjson_ds_time != NULL)
+            _serviceManageRequestInfo->_ds_info.channelTime = cjson_ds_time->valuestring;
+        if (cjson_ds_sts != NULL)
             _serviceManageRequestInfo->_ds_info.setStatus = cjson_ds_sts->valuestring ;         
     }
+    else if(strcmp(_serviceManageRequestInfo->type,"deleteDs")==0)
+    {
+        cJSON *cjson_ds_ch_id = cJSON_GetObjectItem(cjson, "idx");
+        if (cjson_ds_ch_id != NULL)
+        {
+            memset(_serviceManageRequestInfo->_ds_info.idx, '\0', sizeof(_serviceManageRequestInfo->_ds_info.idx));
+            int nIndex = 0;
+            for (cJSON *cjson_ds_ch_id_child = cjson_ds_ch_id->child; cjson_ds_ch_id_child != NULL&&nIndex<64; cjson_ds_ch_id_child = cjson_ds_ch_id_child->next, nIndex++)
+            {
+                _serviceManageRequestInfo->_ds_info.idx[nIndex] = cjson_ds_ch_id_child->valuestring;
+            }
+        }
+    }
     else if(strcmp(_serviceManageRequestInfo->type,"sXTimeSave")==0)
     {
         cJSON* cjson_sx_ch_name = cJSON_GetObjectItem(cjson,"productChName");

+ 5 - 0
pro/src/json_handle.h

@@ -323,7 +323,11 @@ typedef struct
 {
     char* productChName;
     char* productChId;
+    int productChIds[64];
+    char* idx[64];
     char* channelDate;
+    char* StartDate;
+    char* Enddate;
     char* channelTime;
     char* setStatus;
 }_DS_ServiceRequestInfo;
@@ -506,6 +510,7 @@ char* communication_net_ack_to_json(NetworkInfo_t* _networkInfo_t);
 char* communication_rs_ack_to_json(ModbusInfo_t* _modbusInfo_t);
 
 char* service_ds_ack_to_json(char* chn_name,char* chn_id,_PowerDSManage_t* __powerDSManage_t);
+char* service_ds_all_ack_to_json(char* pDate,char* Name,char* nchID,GlobalPowerManger* __powerDSManage_t);
 char* service_sx_ack_to_json(int product_id, GlobalPowerManger * __PowerManage_t);
 char* service_ntp_ack_to_json(NTPManager* __NTPManager_t);
 char* service_snmp_ack_to_json(SNMPManager* __SNMPManager_t);

+ 159 - 0
pro/src/sqlite_handle.c

@@ -2599,6 +2599,165 @@ int dev_get_role_info(sqlite3 *db, RoleInfo* _globalRoleInfo)
         list_add_tail(&new_role->list, &_globalRoleInfo->list);
     }
 
+    sqlite3_free_table(pResult);
+    return 0;
+}
+
+//定时器设置
+int dev_get_power_ds(sqlite3 *db,GlobalPowerManger* _powerAll,char* dateSelect)
+{
+    int res = 0 ;
+    int rowIndex = 1 ;
+    char *errorMsg = NULL;
+    int nrow = 0;               //满足条件的记录数目
+    int ncolumn = 0;            //每条记录包含的字段数据
+    char** pResult = NULL;  //用来指向sql执行结果的指针
+    char* err_msg=NULL;
+    char select_sql[1024] = {0};
+    GlobalPowerManger* _globalPowerMangerTemp=NULL; 
+    list_for_each_entry(_globalPowerMangerTemp, &_powerAll->list, list)
+    {
+        INIT_LIST_HEAD(&_globalPowerMangerTemp->list_DS);
+    }
+
+    if (dateSelect)
+    {
+        sprintf(select_sql, "SELECT* from Table_ChnTimerManage WHERE product_id = %d and date=date('%s');", __globalDeviceManage._global_device_info->product_id,dateSelect);
+    }else{
+        sprintf(select_sql, "SELECT* from Table_ChnTimerManage WHERE product_id = %d;", 1);
+    }        
+    res = sqlite3_get_table(db, select_sql, &pResult, &nrow, &ncolumn, &err_msg);
+    if(res != SQLITE_OK)
+        return -1 ;   
+    //提取数据
+    if(nrow==0)
+        return -2 ;
+    //初始化定时链表
+
+    for(int i = 1; i <= nrow; i++)
+    {
+        // 获取ID
+        _PowerDSManage_t *new_data = (_PowerDSManage_t *)malloc(sizeof(_PowerDSManage_t));
+        // 清空数据
+        memset(new_data, 0, sizeof(new_data));
+        if (!new_data)
+        {
+            break;
+        }
+        int nProduct_id     = atoi(pResult[i * ncolumn + 0]);
+        int nProduct_chn    = atoi(pResult[i * ncolumn + 1]);
+        strcpy(new_data->ds_date, pResult[i * ncolumn + 2]);
+        strcpy(new_data->ds_time, pResult[i * ncolumn + 3]);
+        new_data->set_stats = atoi(pResult[i * ncolumn + 4]);
+        new_data->ds_is_triggered = atoi(pResult[i * ncolumn + 5]);
+
+        list_for_each_entry(_globalPowerMangerTemp,&_powerAll->list, list)
+        {
+            if (_globalPowerMangerTemp->product_ch_id==nProduct_chn)
+            {
+                break;
+            }
+        }
+        if (!_globalPowerMangerTemp)
+        {
+            continue;
+        }        
+        list_add_tail(&new_data->list, &_globalPowerMangerTemp->list_DS);
+        
+    }
+
+    sqlite3_free_table(pResult);
+    return 0;
+
+}
+
+int dev_update_power_ds(sqlite3 *db,int product_id,int nChn,_PowerDSManage_t* _powerDS)
+{
+    int res ;
+    char select_sql[512] = {0};
+    char *errorMsg = NULL;
+    int nrow = 0;               //满足条件的记录数目
+    int ncolumn = 0;            //每条记录包含的字段数据
+    char** pResult = NULL;  //用来指向sql执行结果的指针
+    char* err_msg=NULL;
+    if (!_powerDS)
+    {
+        return -1;
+    }
+    sprintf(select_sql, "UPDATE Table_ChnTimerManage SET status = %d,is_triggered=%d WHERE product_id=%d and product_ch_id=%d and date=date('%s') and time=time('%s');",
+            _powerDS->set_stats,
+            _powerDS->ds_is_triggered,
+            product_id,
+            nChn,
+            _powerDS->ds_date,
+            _powerDS->ds_time
+            );
+    res = sqlite_SQL_RUN(db, select_sql, &pResult, &nrow, &ncolumn, &err_msg);
+    if(res != SQLITE_OK)
+    {
+        log_e("sqlite handle error: %s \n%s", err_msg,select_sql);
+        return -1 ; 
+    }         
+    sqlite3_free_table(pResult);
+    return 0;
+}
+
+int dev_insert_power_ds(sqlite3 *db,int product_id,int nChn,_PowerDSManage_t* _powerDS)
+{
+    int res ;
+    char select_sql[512] = {0};
+    char *errorMsg = NULL;
+    int nrow = 0;               //满足条件的记录数目
+    int ncolumn = 0;            //每条记录包含的字段数据
+    char** pResult = NULL;  //用来指向sql执行结果的指针
+    char* err_msg=NULL;
+    if (!_powerDS)
+    {
+        return -1;
+    }
+    
+    sprintf(select_sql,"INSERT INTO Table_ChnTimerManage VALUES (%d,%d,date('%s'),time('%s'),%d,%d);",
+                        product_id,
+                        nChn,
+                        _powerDS->ds_date,
+                        _powerDS->ds_time,
+                        _powerDS->set_stats,
+                        _powerDS->ds_is_triggered
+                        );
+    res = sqlite_SQL_RUN(db, select_sql, &pResult, &nrow, &ncolumn, &err_msg);
+    if(res != SQLITE_OK)
+    {
+        log_e("sqlite handle error: %s \n%s", err_msg,select_sql);
+        return -1 ; 
+    }         
+    sqlite3_free_table(pResult);
+    return 0;
+}
+
+int dev_delete_power_ds(sqlite3 *db,int product_id,int nChn,_PowerDSManage_t* _powerDS)
+{
+    int res;
+    char select_sql[512] = {0};
+    char *errorMsg = NULL;
+    int nrow = 0;          // 满足条件的记录数目
+    int ncolumn = 0;       // 每条记录包含的字段数据
+    char **pResult = NULL; // 用来指向sql执行结果的指针
+    char *err_msg = NULL;
+    if (!_powerDS)
+    {
+        return -1;
+    }
+    sprintf(select_sql, "DELETE FROM Table_ChnTimerManage WHERE product_id=%d and product_ch_id=%d and date=date('%s') and time=time('%s');",
+            product_id,
+            nChn,
+            _powerDS->ds_date,
+            _powerDS->ds_time);
+    res = sqlite_SQL_RUN(db, select_sql, &pResult, &nrow, &ncolumn, &err_msg);
+    if (res != SQLITE_OK)
+    {
+        log_e("sqlite handle error: %s \n%s", err_msg, select_sql);
+        return -1;
+    }
     sqlite3_free_table(pResult);
     return 0;
 }

+ 5 - 0
pro/src/sqlite_handle.h

@@ -120,4 +120,9 @@ int dev_update_role_info(sqlite3 *db, RoleInfo* _globalRoleInfo);
 int dev_insert_role_info(sqlite3 *db,RoleInfo* _globalRoleInfo);
 int dev_get_role_info(sqlite3 *db, RoleInfo* _globalRoleInfo);
 
+//¶¨Ê±Æ÷ÉèÖÃ
+int dev_get_power_ds(sqlite3 *db,GlobalPowerManger* _powerAll,char* dateSelect);
+int dev_update_power_ds(sqlite3 *db,int product_id,int nChn,_PowerDSManage_t* _powerDS);
+int dev_insert_power_ds(sqlite3 *db,int product_id,int nChn,_PowerDSManage_t* _powerDS);
+int dev_delete_power_ds(sqlite3 *db,int product_id,int nChn,_PowerDSManage_t* _powerDS);
 #endif