|
|
@@ -8,6 +8,25 @@
|
|
|
#include "smtp.h"
|
|
|
#include "switch_ctrl.h"
|
|
|
#include "pduMIB_trap.h"
|
|
|
+#include "xlist.h"
|
|
|
+#include "file.h"
|
|
|
+
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ int prod_id;
|
|
|
+ int channel_id;
|
|
|
+ int phase_type;
|
|
|
+ int product_status;
|
|
|
+ char samp_time[32];
|
|
|
+ PowerInfo power;
|
|
|
+}power_data_t;
|
|
|
+const char *tabNamePool[TAB_ID_MAX]={
|
|
|
+ "Table_PowerInfo",
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
#define TOTAL_COUNT 60//统计总数
|
|
|
|
|
|
@@ -138,6 +157,247 @@ int sqlite_SQL_RUN(sqlite3 *db,const char *select_sql, char ***pResult, int *nro
|
|
|
pthread_mutex_unlock(&__globalDeviceManage.dbThreadlock);
|
|
|
return res;
|
|
|
}
|
|
|
+
|
|
|
+static int db_exec(sqlite3 *db, int tab_id, int io_type, time_t from, time_t to, handle_t l)
|
|
|
+{
|
|
|
+ int r=-1,cnt=0;
|
|
|
+ char *p,temp[1024];
|
|
|
+ char time_s[64],time_e[64];
|
|
|
+ sqlite3_stmt *stmt=NULL;
|
|
|
+
|
|
|
+ get_time_str(time_s, sizeof(time_s), from);
|
|
|
+ get_time_str(time_e, sizeof(time_e), to);
|
|
|
+
|
|
|
+ switch(tab_id) {
|
|
|
+#if 0
|
|
|
+ case TAB_ID_POWER:
|
|
|
+ {
|
|
|
+ //
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case TAB_ID_POWER3:
|
|
|
+ {
|
|
|
+ //
|
|
|
+ }
|
|
|
+ break;
|
|
|
+#endif
|
|
|
+ case TAB_ID_POWER_INFO:
|
|
|
+ {
|
|
|
+ power_data_t tmp;
|
|
|
+ snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE product_samp_time BETWEEN '%s' AND '%s' ORDER BY product_samp_time DESC;", tabNamePool[tab_id], time_s, time_e);
|
|
|
+
|
|
|
+ r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
|
|
|
+ if (r != SQLITE_OK) {
|
|
|
+ log_e("___db_requery TAB_ID_POWER_INFO, sqlite3_prepare_v2 failed\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (sqlite3_step(stmt)==SQLITE_ROW) { //column start from 0
|
|
|
+ tmp.prod_id = sqlite3_column_int(stmt, 1);
|
|
|
+ tmp.channel_id = sqlite3_column_int(stmt, 2);
|
|
|
+ tmp.product_status = sqlite3_column_int(stmt, 3);
|
|
|
+ tmp.power.voltage = sqlite3_column_double(stmt, 4);
|
|
|
+ tmp.power.current = sqlite3_column_double(stmt, 5);
|
|
|
+ tmp.power.power = sqlite3_column_double(stmt, 6);
|
|
|
+ tmp.power.freq = sqlite3_column_double(stmt, 7);
|
|
|
+ tmp.power.consumption = sqlite3_column_double(stmt, 8);
|
|
|
+ tmp.power.factor = sqlite3_column_double(stmt, 9);
|
|
|
+ strcpy(tmp.samp_time, (char*)sqlite3_column_text(stmt, 0));
|
|
|
+
|
|
|
+ xlist_append(l, tab_id, &tmp, sizeof(tmp));
|
|
|
+ cnt++;
|
|
|
+ }
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+#if 0
|
|
|
+ case TAB_ID_POWER3_INFO:
|
|
|
+ {
|
|
|
+
|
|
|
+ power_data_t tmp;
|
|
|
+
|
|
|
+ snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE (product_ph_samp_time BETWEEN '%s' AND '%s') ORDER BY product_ph_samp_time DESC;", tabNamePool[tab_id], time_s, time_e);
|
|
|
+ r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
|
|
|
+ if (r != SQLITE_OK) {
|
|
|
+ log_e("___db_requery TAB_ID_POWER3_INFO, sqlite3_prepare_v2 failed\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (sqlite3_step(stmt)==SQLITE_ROW) {
|
|
|
+ tmp.prod_id = sqlite3_column_int(stmt, 1);
|
|
|
+ tmp.phase_type = sqlite3_column_int(stmt, 2);
|
|
|
+ tmp.channel_id = sqlite3_column_int(stmt, 3);
|
|
|
+ tmp.power.voltage = sqlite3_column_double(stmt, 4);
|
|
|
+ tmp.power.current = sqlite3_column_double(stmt, 5);
|
|
|
+ tmp.power.power = sqlite3_column_double(stmt, 6);
|
|
|
+ tmp.power.freq = sqlite3_column_double(stmt, 7);
|
|
|
+ tmp.power.consumption = sqlite3_column_double(stmt, 8);
|
|
|
+ tmp.power.factor = sqlite3_column_double(stmt, 9);
|
|
|
+ strcpy(tmp.samp_time, (char*)sqlite3_column_text(stmt, 10));
|
|
|
+
|
|
|
+ xlist_append(l, tab_id, &tmp, sizeof(tmp));
|
|
|
+ cnt++;
|
|
|
+ }
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case TAB_ID_LOG_INFO:
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case TAB_ID_ALARM_INFO:
|
|
|
+ {
|
|
|
+ char *p=NULL;
|
|
|
+ alarm_data_t tmp;
|
|
|
+ GlobalDeviceManager *dm=&__globalDeviceManage;
|
|
|
+
|
|
|
+ snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE ((alarmType=%d) OR (alarmType=%d)) AND (alarmDate BETWEEN '%s' AND '%s') ORDER BY alarmDate DESC;",
|
|
|
+ tabNamePool[tab_id], ALARM_TYPE_OPRATION, ALARM_TYPE_RUNNING, time_s, time_e);
|
|
|
+
|
|
|
+ r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
|
|
|
+ if (r != SQLITE_OK) {
|
|
|
+ log_e("___%s failed, %s\n", temp, sqlite3_errstr(r));
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (sqlite3_step(stmt) == SQLITE_ROW) { //column start from 0
|
|
|
+ tmp.id = sqlite3_column_int(stmt, 0);
|
|
|
+ tmp.type = sqlite3_column_int(stmt, 2);
|
|
|
+
|
|
|
+ p = (char*)sqlite3_column_text(stmt, 1);
|
|
|
+ if(p) strcpy(tmp.date, p);
|
|
|
+ else tmp.date[0] = 0;
|
|
|
+
|
|
|
+ p = (char*)sqlite3_column_text(stmt, 4);
|
|
|
+ if(p) {
|
|
|
+ strcpy(tmp.content, p);
|
|
|
+ replace_sequences(tmp.content, dm->alarmTab);
|
|
|
+ }
|
|
|
+ else tmp.content[0] = 0;
|
|
|
+
|
|
|
+ xlist_append(l, tab_id, &tmp, sizeof(tmp));
|
|
|
+ cnt++;
|
|
|
+ }
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case TAB_ID_SENSOR:
|
|
|
+ {
|
|
|
+ GlobalSensorManger tmp;
|
|
|
+
|
|
|
+ snprintf(temp, sizeof(temp), "SELECT * FROM %s;", tabNamePool[tab_id]);
|
|
|
+ r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
|
|
|
+ if (r != SQLITE_OK) {
|
|
|
+ log_e("___db_requery TAB_ID_SENSOR, sqlite3_prepare_v2 failed\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
|
+ tmp.sensor_id = sqlite3_column_int(stmt, 1);
|
|
|
+ tmp.product_id = sqlite3_column_int(stmt, 2);
|
|
|
+ tmp.sensor_type = sqlite3_column_int(stmt, 3);
|
|
|
+ tmp.sensor_interface_type = sqlite3_column_int(stmt, 4);
|
|
|
+
|
|
|
+ p = (char*)sqlite3_column_text(stmt, 5);
|
|
|
+ tmp.sensor_name[0] = 0;
|
|
|
+ if(p) strcpy(tmp.sensor_name, p);
|
|
|
+
|
|
|
+ p = (char*)sqlite3_column_text(stmt, 6);
|
|
|
+ tmp.sensor_port_name[0] = 0;
|
|
|
+ if(p) strcpy(tmp.sensor_port_name, p);
|
|
|
+
|
|
|
+ tmp.sensor_baud = 0;
|
|
|
+ if(tmp.sensor_interface_type==1) {
|
|
|
+ tmp.sensor_baud = sqlite3_column_int(stmt, 7);
|
|
|
+ }
|
|
|
+ tmp.sensor_addr = sqlite3_column_int(stmt, 8);
|
|
|
+
|
|
|
+ tmp.sensor_status = 1;
|
|
|
+ tmp.sensor_val_count = sqlite3_column_int(stmt, 10);
|
|
|
+
|
|
|
+ tmp.over_manager.sensor_val1_upper_enabled = sqlite3_column_int(stmt, 11);
|
|
|
+ tmp.over_manager.sensor_val1_upper_threshold = sqlite3_column_double(stmt, 12);
|
|
|
+ tmp.over_manager.sensor_val1_upper_threshold_ctrl = sqlite3_column_int(stmt, 13);
|
|
|
+
|
|
|
+ p = (char*)sqlite3_column_text(stmt, 14);
|
|
|
+ tmp.over_manager.sensor_val1_upper_threshold_ctrl_para[0] = 0;
|
|
|
+ if(p) strcpy(tmp.over_manager.sensor_val1_upper_threshold_ctrl_para, p);
|
|
|
+
|
|
|
+ tmp.over_manager.sensor_val1_lower_enabled = sqlite3_column_int(stmt, 15);
|
|
|
+ tmp.over_manager.sensor_val1_lower_threshold = sqlite3_column_double(stmt, 16);
|
|
|
+ tmp.over_manager.sensor_val1_lower_threshold_ctrl = sqlite3_column_int(stmt, 17);
|
|
|
+
|
|
|
+ p = (char*)sqlite3_column_text(stmt, 18);
|
|
|
+ tmp.over_manager.sensor_val1_lower_threshold_ctrl_para[0] = 0;
|
|
|
+ if(p) strcpy(tmp.over_manager.sensor_val1_lower_threshold_ctrl_para, p);
|
|
|
+
|
|
|
+ tmp.over_manager.sensor_val2_upper_enabled = sqlite3_column_int(stmt, 19);
|
|
|
+ tmp.over_manager.sensor_val2_upper_threshold = sqlite3_column_double(stmt, 20);
|
|
|
+ tmp.over_manager.sensor_val2_upper_threshold_ctrl = sqlite3_column_int(stmt, 21);
|
|
|
+
|
|
|
+ p = (char*)sqlite3_column_text(stmt, 22);
|
|
|
+ tmp.over_manager.sensor_val2_upper_threshold_ctrl_para[0] = 0;
|
|
|
+ if(p) strcpy(tmp.over_manager.sensor_val2_upper_threshold_ctrl_para, p);
|
|
|
+
|
|
|
+ tmp.over_manager.sensor_val2_lower_enabled = sqlite3_column_int(stmt, 23);
|
|
|
+ tmp.over_manager.sensor_val2_lower_threshold = sqlite3_column_double(stmt, 24);
|
|
|
+ tmp.over_manager.sensor_val2_lower_threshold_ctrl = sqlite3_column_int(stmt, 25);
|
|
|
+
|
|
|
+ p = (char*)sqlite3_column_text(stmt, 26);
|
|
|
+ tmp.over_manager.sensor_val2_lower_threshold_ctrl_para[0] = 0;
|
|
|
+ if(p) strcpy(tmp.over_manager.sensor_val2_lower_threshold_ctrl_para, p);
|
|
|
+
|
|
|
+ xlist_append(l, tab_id, &tmp, sizeof(tmp));
|
|
|
+ cnt++;
|
|
|
+ }
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ case TAB_ID_SENSOR_INFO:
|
|
|
+ {
|
|
|
+ GlobalSensorInfo tmp;
|
|
|
+
|
|
|
+ snprintf(temp, sizeof(temp), "SELECT * FROM %s WHERE (sensor_samp_time BETWEEN '%s' AND '%s') ORDER BY sensor_samp_time DESC;", tabNamePool[tab_id], time_s, time_e);
|
|
|
+ r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
|
|
|
+ if (r != SQLITE_OK) {
|
|
|
+ log_e("___db_requery 2, sqlite3_prepare_v2 failed\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
|
+ p = (char*)sqlite3_column_text(stmt, 2);
|
|
|
+ tmp.samp_time[0] = 0;
|
|
|
+ if(p) strcpy(tmp.samp_time, p);
|
|
|
+
|
|
|
+ tmp.product_id = sqlite3_column_int(stmt, 3);
|
|
|
+ tmp.sensor_id = sqlite3_column_int(stmt, 4);
|
|
|
+ tmp.val1 = sqlite3_column_double(stmt, 5);
|
|
|
+ tmp.val2 = sqlite3_column_double(stmt, 6);
|
|
|
+
|
|
|
+ xlist_append(l, tab_id, &tmp, sizeof(tmp));
|
|
|
+ cnt++;
|
|
|
+ }
|
|
|
+ sqlite3_finalize(stmt);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+#endif
|
|
|
+ default:
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/// @brief 查询设备信息表
|
|
|
/// @param db 数据库接口
|
|
|
/// @param _globalDeviceInfo 返回数据接口
|
|
|
@@ -4314,6 +4574,113 @@ static int sw_update_all(sqlite3 *db, netswitch_info_t *sw)
|
|
|
}
|
|
|
|
|
|
|
|
|
+////////////////////////////////////////////////////////////////////
|
|
|
+typedef struct {
|
|
|
+ FILE *fp;
|
|
|
+ int id;
|
|
|
+ int type;
|
|
|
+}arg_t;
|
|
|
+
|
|
|
+
|
|
|
+static void replace_chr(char *buf, char a, char b)
|
|
|
+{
|
|
|
+ while(*buf) {
|
|
|
+ if((*buf)==a) {
|
|
|
+ *buf = b;
|
|
|
+ }
|
|
|
+ buf++;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static int pwr_iterator_fn(handle_t l, node_t *node, node_t *xd, void *arg, int *act)
|
|
|
+{
|
|
|
+ arg_t *parg=(arg_t*)arg;
|
|
|
+ power_data_t *tmp=(power_data_t*)xd->buf;
|
|
|
+
|
|
|
+ if(xd->tp==TAB_ID_POWER_INFO) {
|
|
|
+ fprintf(parg->fp, "%d,", parg->id);
|
|
|
+ fprintf(parg->fp, "%d,", tmp->prod_id);
|
|
|
+ fprintf(parg->fp, "%d,", tmp->channel_id);
|
|
|
+ fprintf(parg->fp, "%d,", tmp->product_status);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.voltage);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.current);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.power);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.freq);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.consumption);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.factor);
|
|
|
+
|
|
|
+ replace_chr(tmp->samp_time, '-', '.');
|
|
|
+ fprintf(parg->fp, "%s,", tmp->samp_time);
|
|
|
+ fprintf(parg->fp, "\n");
|
|
|
+ parg->id++;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ fprintf(parg->fp, "%d,", parg->id);
|
|
|
+ fprintf(parg->fp, "%d,", tmp->prod_id);
|
|
|
+ fprintf(parg->fp, "%d,", tmp->channel_id);
|
|
|
+ fprintf(parg->fp, "%d,", tmp->phase_type);
|
|
|
+ fprintf(parg->fp, "%d,", tmp->product_status);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.voltage);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.current);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.power);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.freq);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.consumption);
|
|
|
+ fprintf(parg->fp, "%0.2f,", tmp->power.factor);
|
|
|
+
|
|
|
+ replace_chr(tmp->samp_time, '-', '.');
|
|
|
+ fprintf(parg->fp, "%s,", tmp->samp_time);
|
|
|
+ fprintf(parg->fp, "\n");
|
|
|
+ parg->id++;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+int dev_power_xport(time_t from, time_t to, char *path)
|
|
|
+{
|
|
|
+ int i,r=-1;
|
|
|
+ handle_t l;
|
|
|
+ FILE *fp;
|
|
|
+ arg_t arg;
|
|
|
+ list_cfg_t lc={0,0,-1};
|
|
|
+ GlobalDeviceManager *dm=&__globalDeviceManage;
|
|
|
+ int tab_id=TAB_ID_POWER_INFO;
|
|
|
+
|
|
|
+ if(file_exist(path)) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(dm->_globalDevInfo.product_pwr_type != SmartPDU_AC) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ l = xlist_init(&lc);
|
|
|
+
|
|
|
+ db_exec(dm->db, tab_id, IO_EXPORT, from, to, l);
|
|
|
+
|
|
|
+ fp = fopen(path, "wt");
|
|
|
+ if(!fp) {
|
|
|
+ log_e("__%s open failed\n", path);
|
|
|
+ xlist_free(l);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(tab_id==TAB_ID_POWER_INFO) {
|
|
|
+ fprintf(fp, "id,product_id,product_ch_id,status,voltage,current,power,freq,consumption,factor,samp_time\n");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ fprintf(fp, "id,product_id,product_ch_id,product_ph_type,status,voltage,current,power,freq,consumption,factor,samp_time\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ arg.fp = fp; arg.id = 1;
|
|
|
+ xlist_iterator(l, NULL, pwr_iterator_fn, &arg);
|
|
|
+
|
|
|
+ fclose(fp);
|
|
|
+ xlist_free(l);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
int dev_switch_init(sqlite3 *db, netswitch_info_t *sw)
|
|
|
{
|
|
|
int r;
|