2025-06-29 11:25:40 +08:00
# include "communication_cmd.hpp"
# include <fstream>
# include <boost/algorithm/string.hpp>
# include <zlog.h>
# include "common/global.hpp"
# include "dbaccess/sql_db.hpp"
# include "platform/platform_init.hpp"
extern zlog_category_t * zct ;
extern const char * JSON_FIELD_CMD ;
2025-06-29 14:08:45 +08:00
extern std : : map < std : : string , int8_t > device_map ;
2025-06-29 11:25:40 +08:00
std : : string JsonData : : JsonCmd_Cgi_100 ( Param_100 & param ) {
Json : : Value jsonVal ;
jsonVal . clear ( ) ;
jsonVal [ JSON_FIELD_CMD ] = " 100 " ;
jsonVal [ " success " ] = true ;
jsonVal [ " message " ] = " 获取传感器信息成功 " ;
Json : : Value jsBody ;
array_t arrRes = sqlite_db_ctrl : : instance ( ) . GetDataMultiLine ( T_SENSOR_BT_INFO ( TNAME ) , " * " , NULL ) ;
2025-06-29 14:08:45 +08:00
int iResult = arrRes . size ( ) ;
2025-06-29 11:25:40 +08:00
if ( arrRes . size ( ) > 0 ) {
int packgeNo = param . mPackageFlag ;
int packgeMax = 0 ;
int packgeNum = 0 ;
jsonVal [ " package " ] = packgeNo ;
int lastSize = iResult % 10 ;
int index = iResult / 10 ;
if ( lastSize > 0 & & index > 0 ) {
packgeMax = index + 1 ;
if ( packgeNo + 1 = = packgeMax ) {
packgeNum = iResult ;
jsonVal [ " packageMax " ] = index + 1 ;
} else {
packgeNum = ( packgeNo + 1 ) * 10 ;
jsonVal [ " packageMax " ] = index + 1 ;
}
} else if ( lastSize = = 0 & & index > 0 ) {
packgeNum = ( packgeNo + 1 ) * 10 ;
packgeMax = index ;
jsonVal [ " packageMax " ] = index ;
} else if ( lastSize > 0 & & index = = 0 ) {
packgeNum = lastSize ;
packgeMax = index + 1 ;
jsonVal [ " packageMax " ] = index ;
}
2025-06-29 14:08:45 +08:00
for ( size_t i = packgeNo * 10 ; i < packgeNum ; i + + ) {
2025-06-29 11:25:40 +08:00
Json : : Value jsSensorData ;
jsSensorData [ " mac " ] = arrRes [ i ] [ 0 ] ;
jsSensorData [ " name " ] = arrRes [ i ] [ 1 ] ;
jsSensorData [ " type " ] = atoi ( arrRes [ i ] [ 2 ] . c_str ( ) ) ;
jsSensorData [ " status " ] = atoi ( arrRes [ i ] [ 3 ] . c_str ( ) ) ;
jsSensorData [ " config_date " ] = atoi ( arrRes [ i ] [ 4 ] . c_str ( ) ) ;
std : : string strMac = arrRes [ i ] [ 0 ] ;
2025-06-29 14:08:45 +08:00
char whereCon [ 256 ] = { 0x00 } ;
sprintf ( whereCon , " mac = '%s' order by timestamp desc limit 1 " , strMac . c_str ( ) ) ;
vec_t vec_ret = sqlite_db_ctrl : : instance ( ) . GetDataSingleLine ( " blueteeth_info " , " * " , whereCon ) ;
2025-06-29 11:25:40 +08:00
jsSensorData [ " env_temp " ] = atof ( vec_ret [ 7 ] . c_str ( ) ) ;
jsSensorData [ " dev_temp " ] = atof ( vec_ret [ 6 ] . c_str ( ) ) ;
jsSensorData [ " battery_v " ] = atof ( vec_ret [ 4 ] . c_str ( ) ) ;
jsSensorData [ " rssi " ] = atoi ( vec_ret [ 2 ] . c_str ( ) ) ;
2025-06-29 14:08:45 +08:00
jsSensorData [ " feature_date " ] = vec_ret [ 8 ] ;
2025-06-29 11:25:40 +08:00
jsBody . append ( jsSensorData ) ;
}
jsonVal [ " content " ] = jsBody ;
} else {
jsonVal [ " success " ] = false ;
jsonVal [ " message " ] = " 没有传感器信息 " ;
jsonVal [ " content " ] . resize ( 0 ) ;
}
return show_value_ . write ( jsonVal ) ;
}
std : : string JsonData : : JsonCmd_Cgi_101 ( Param_101 & param ) {
Json : : Value jsonVal ;
jsonVal . clear ( ) ;
jsonVal [ JSON_FIELD_CMD ] = " 101 " ;
jsonVal [ " success " ] = true ;
jsonVal [ " message " ] = " 获取传感器信息成功 " ;
Json : : Value jsBody ;
char selectCon [ 256 ] = { 0 } , szTableName [ 100 ] = { 0x00 } , whereCon [ 256 ] = { 0x00 } ;
sprintf ( whereCon , " mac = '%s' and timeStamp < '%s' and timeStamp > '%s' " , param . mac . c_str ( ) , param . timeEnd . c_str ( ) , param . timeStart . c_str ( ) ) ;
int rows = sqlite_db_ctrl : : instance ( ) . GetTableRows ( " blueteeth_info " , whereCon ) ;
int packgeNo = param . mPackageFlag ;
int packgeMax = 0 ;
int packgeNum = 0 ;
jsonVal [ " package " ] = packgeNo ;
int lastSize = rows % 550 ;
int index = rows / 550 ;
if ( lastSize > 0 & & index > 0 ) {
packgeMax = index + 1 ;
if ( packgeNo + 1 = = packgeMax ) {
packgeNum = rows - lastSize ;
jsonVal [ " packageMax " ] = index + 1 ;
} else {
packgeNum = ( packgeNo ) * 550 ;
jsonVal [ " packageMax " ] = index + 1 ;
}
} else if ( lastSize = = 0 & & index > 0 ) {
packgeNum = ( packgeNo + 1 ) * 550 ;
packgeMax = index ;
jsonVal [ " packageMax " ] = index ;
} else if ( lastSize > 0 & & index = = 0 ) {
packgeNum = 0 ;
packgeMax = index + 1 ;
jsonVal [ " packageMax " ] = index ;
}
memset ( whereCon , 0x00 , sizeof ( whereCon ) ) ;
sprintf ( whereCon , " mac = '%s' and timeStamp < '%s' and timeStamp > '%s' order by timeStamp asc LIMIT %d OFFSET %d " , param . mac . c_str ( ) , param . timeEnd . c_str ( ) , param . timeStart . c_str ( ) , 550 , packgeNum ) ;
array_t arrRes = sqlite_db_ctrl : : instance ( ) . GetDataMultiLine ( " blueteeth_info " , " env_temp,chip_temp,volt,timestamp " , whereCon ) ;
2025-06-29 14:08:45 +08:00
if ( arrRes . size ( ) > 0 ) {
2025-06-29 11:25:40 +08:00
Json : : Value jsSensorData ;
2025-06-29 14:08:45 +08:00
Json : : Value iTem1 , iTem2 , iTem3 ;
for ( size_t i = 0 ; i < arrRes . size ( ) ; i + + ) {
iTem1 . append ( arrRes [ i ] [ 0 ] ) ;
iTem1 . append ( arrRes [ i ] [ 3 ] ) ;
iTem2 . append ( arrRes [ i ] [ 1 ] ) ;
iTem2 . append ( arrRes [ i ] [ 3 ] ) ;
iTem3 . append ( arrRes [ i ] [ 2 ] ) ;
iTem3 . append ( arrRes [ i ] [ 3 ] ) ;
2025-06-29 11:25:40 +08:00
}
jsSensorData [ " env_temp " ] = iTem1 ;
jsSensorData [ " chip_temp " ] = iTem2 ;
jsSensorData [ " volt " ] = iTem3 ;
jsBody . append ( jsSensorData ) ;
jsonVal [ " content " ] = jsBody ;
} else {
jsonVal [ " success " ] = false ;
jsonVal [ " message " ] = " 没有传感器信息 " ;
jsonVal [ " content " ] . resize ( 0 ) ;
}
2025-06-29 14:08:45 +08:00
return show_value_ . write ( jsonVal ) ;
2025-06-29 11:25:40 +08:00
}
std : : string JsonData : : JsonCmd_Cgi_102 ( ) {
2025-06-29 14:08:45 +08:00
for ( auto it = device_map . begin ( ) ; it ! = device_map . end ( ) ; + + it )
2025-06-29 11:25:40 +08:00
{
Json : : Value jsonVal ;
jsonVal . clear ( ) ;
jsonVal [ JSON_FIELD_CMD ] = " 102 " ;
jsonVal [ " success " ] = true ;
jsonVal [ " message " ] = " 获取传感器信息成功 " ;
Json : : Value jsBody ;
2025-06-29 14:08:45 +08:00
jsBody [ " mac " ] = it - > first ;
char whereCon [ 256 ] = { 0x00 } ;
sprintf ( whereCon , " mac = '%s' " , it - > first . c_str ( ) ) ;
int rows = sqlite_db_ctrl : : instance ( ) . GetTableRows ( T_SENSOR_BT_INFO ( TNAME ) , whereCon ) ;
2025-06-29 11:25:40 +08:00
if ( rows > 1 ) {
jsBody [ " added " ] = true ;
2025-06-29 14:08:45 +08:00
} else {
jsBody [ " added " ] = false ;
2025-06-29 11:25:40 +08:00
}
2025-06-29 14:08:45 +08:00
jsBody [ " rssi " ] = it - > second ;
2025-06-29 11:25:40 +08:00
jsonVal [ " content " ] = jsBody ;
return show_value_ . write ( jsonVal ) ;
}
}
std : : string JsonData : : JsonCmd_Cgi_103 ( Param_103 & param ) {
Json : : Value jsonVal ;
jsonVal . clear ( ) ;
jsonVal [ JSON_FIELD_CMD ] = " 103 " ;
jsonVal [ " success " ] = true ;
jsonVal [ " message " ] = " 获取传感器信息成功 " ;
2025-06-29 14:08:45 +08:00
char insertSql [ 256 ] = { 0x00 } , updateSql [ 256 ] = { 0x00 } ,
whereCon [ 256 ] = { 0x00 } ;
char localtimestamp [ 32 ] = { 0 } ;
sprintf ( whereCon , " mac = '%s' " , param . mac . c_str ( ) ) ;
GetTimeNet ( localtimestamp , 1 ) ;
2025-06-29 11:25:40 +08:00
if ( param . operate = = " add " ) {
2025-06-29 14:08:45 +08:00
sprintf ( insertSql , " '%s','','DN-801','0','%s' " , param . mac . c_str ( ) , localtimestamp ) ;
sqlite_db_ctrl : : instance ( ) . InsertData ( T_SENSOR_BT_INFO ( TNAME ) , insertSql ) ;
2025-06-29 11:25:40 +08:00
} else if ( param . operate = = " update " ) {
2025-06-29 14:08:45 +08:00
sprintf ( updateSql , " name = '%s' " , param . name . c_str ( ) ) ;
sqlite_db_ctrl : : instance ( ) . UpdateTableData ( T_SENSOR_BT_INFO ( TNAME ) , updateSql , whereCon ) ;
2025-06-29 11:25:40 +08:00
} else if ( param . operate = = " delete " ) {
2025-06-29 14:08:45 +08:00
sqlite_db_ctrl : : instance ( ) . DeleteTableData ( T_SENSOR_BT_INFO ( TNAME ) , whereCon ) ;
2025-06-29 11:25:40 +08:00
}
return show_value_ . write ( jsonVal ) ;
}