wirelessgateway/platform/SH_PlatformInit.cpp

377 lines
14 KiB
C++

#include <fstream>
#include "SH_PlatformInit.hpp"
#include "../common/SH_global.h"
#include "../dbaccess/SH_SqlDB.hpp"
#include "../API_log/SH_log.h"
/*********************全局变量声明***********************/
int GlobalConfig::RUN_MODE = 0;
int GlobalConfig::QuitFlag_G = 1;
int GlobalConfig::LinkStatus_G = 0;
int GlobalConfig::LinkCount = 0;
int GlobalConfig::net0Status = 1;
std::string GlobalConfig::Version = "3.2.5";
std::string GlobalConfig::MacAddr_G = "";
std::string GlobalConfig::MacAddr_G2 = "";
std::string GlobalConfig::IpAddr_G = "";
std::string GlobalConfig::ServerIP = "";
std::string GlobalConfig::NetStatus = "";
std::string GlobalConfig::NetType = "";
std::string GlobalConfig::NR5GTemp = "";
int GlobalConfig::serverStatus = 0;
int GlobalConfig::NetSignal = 0;
int GlobalConfig::ServerPort = 0;
int GlobalConfig::threadStatus = 1;
int GlobalConfig::day = 0;
extern map<string,compressWaveChannel> g_mapCompress;
TopicList GlobalConfig::Topic_G;
ZigbeeInfo GlobalConfig::ZigbeeInfo_G;
ZIGBEE GlobalConfig::Zigbee_G;
GPIOInfo GlobalConfig::GPIO_G;
std::string GlobalConfig::DbName_G = "/opt/configenv/cidn.db"; //数据库名称
std::string GlobalConfig::Config_G = "/opt/configenv/config.ini";
std::string GlobalConfig::UartName_G = "/dev/ttymxc4";
// 处理ZigBee网络接收过程的PanID切换
enumZigBeeTransmitStatus GlobalConfig::EnterZigBeeWaveTransmittingFlag_G = NO_ENTER_TRANSMITTING_STATUS;
int GlobalConfig::EnterZigBeeWaveTransmittingCnt_G = 0;
PlatformInit::PlatformInit()
{
}
PlatformInit::~PlatformInit()
{
}
void PlatformInit::PlatFormInit()
{
try{
GlobalConfig::MacAddr_G = GetLocalMac("eth0");
GlobalConfig::MacAddr_G2 = GetLocalMac("eth1");
transform(GlobalConfig::MacAddr_G.begin(), GlobalConfig::MacAddr_G.end(), GlobalConfig::MacAddr_G.begin(), ::toupper);
transform(GlobalConfig::MacAddr_G2.begin(), GlobalConfig::MacAddr_G2.end(), GlobalConfig::MacAddr_G2.begin(), ::toupper);
GlobalConfig::IpAddr_G = IpAddrInit();
print_info("GlobalConfig::IpAddr_G : %s\n", GlobalConfig::IpAddr_G.c_str());
this->ConfigFileCheck();
print_info("ConfigFileCheck\n");
this->TopicInit();
print_info("TopicInit\n");
this->ServerIpInit();
print_info("TopicInit\n");
this->Sqlite3Init();
this->GPIOInit();
print_info("ServerIP : %s \n", GlobalConfig::ServerIP.c_str());
std::string strMac = sql_ctl->GetData("t_gateway_info","gatewayMAC",NULL);
if(strMac == "60294D208517"){
char szUpdateSql[100]={0x00};
sprintf(szUpdateSql,"gatewayMAC = '%s'",GlobalConfig::MacAddr_G.c_str());
sql_ctl->UpdateTableData("t_gateway_info",szUpdateSql,NULL);
}
else if(strMac != GlobalConfig::MacAddr_G ){
LOG_ERROR("MAC error strMac1 = %s,MacAddr_G1 = %s\n",strMac.c_str(),GlobalConfig::MacAddr_G.c_str());
strMac.insert(2,":");
strMac.insert(5,":");
strMac.insert(8,":");
strMac.insert(11,":");
strMac.insert(14,":");
ModifyMac((char*)strMac.c_str());
//system("reboot");
}
#ifdef G2UL_GATEWAY
std::string strMac2 = sql_ctl->GetData("t_gateway_info","MAC2",NULL);
if(strMac2 == "60294D208518"){
char szUpdateSql[100]={0x00};
sprintf(szUpdateSql,"MAC2 = '%s'",GlobalConfig::MacAddr_G2.c_str());
sql_ctl->UpdateTableData("t_gateway_info",szUpdateSql,NULL);
}
else if(strMac2 != GlobalConfig::MacAddr_G2 ){
LOG_ERROR("MAC error strMac2 = %s,MacAddr_G2 = %s\n",strMac2.c_str(),GlobalConfig::MacAddr_G2.c_str());
strMac2.insert(2,":");
strMac2.insert(5,":");
strMac2.insert(8,":");
strMac2.insert(11,":");
strMac2.insert(14,":");
ModifyMac((char*)strMac2.c_str());
//system("reboot");
}
#endif
// sleep(10);
} catch(...){
print_error("PlatFormInit exception happend.\n");
std::string errorinfo = "系统初始化异常";
}
vec_t vecResult = sql_ctl->GetDataMultiLineOfOneColumn(T_SENSOR_INFO(TNAME)," zigbeeShortAddr ",NULL);
for (size_t i = 0; i < vecResult.size(); i++)
{
compressWaveChannel tempchannel;
g_mapCompress.insert(std::make_pair(vecResult[0],tempchannel));
}
}
void PlatformInit::TopicInit()
{
GlobalConfig::Topic_G.mPubData = "wireless/statisticData/" + GlobalConfig::MacAddr_G;
GlobalConfig::Topic_G.mPubStatus = "wireless/status/" + GlobalConfig::MacAddr_G;
GlobalConfig::Topic_G.mPubHeart = "wireless/heart/" + GlobalConfig::MacAddr_G;//
GlobalConfig::Topic_G.mPubConfig = "wireless/configureInfo/" + GlobalConfig::MacAddr_G;
GlobalConfig::Topic_G.mPubWaveData = "wireless/realTimeData/" + GlobalConfig::MacAddr_G;
GlobalConfig::Topic_G.mPubWaveSecondData = "wireless/secondTimeData/" + GlobalConfig::MacAddr_G;
GlobalConfig::Topic_G.mPubCmd = "wireless/cmd/" + GlobalConfig::MacAddr_G;//
GlobalConfig::Topic_G.mSubData = "cmd/" + GlobalConfig::MacAddr_G;//
GlobalConfig::Topic_G.mPubRep = "wireless/resp/" + GlobalConfig::MacAddr_G;
GlobalConfig::Topic_G.mPubTiming = "equipment/cmd/" + GlobalConfig::MacAddr_G;
GlobalConfig::Topic_G.mPubLocalWifi = "up/" + GlobalConfig::MacAddr_G + "/recive/wifi";
GlobalConfig::Topic_G.mPubLocalWaveServer = "up/" + GlobalConfig::MacAddr_G + "/recive/waveserver";
GlobalConfig::Topic_G.mPubLocalWaveQt = "up/" + GlobalConfig::MacAddr_G + "/recive/waveqt";
GlobalConfig::Topic_G.mPubLocalConfig = "up/" + GlobalConfig::MacAddr_G + "/recive/config";
GlobalConfig::Topic_G.mPubLocalTrigger = "up/" + GlobalConfig::MacAddr_G + "/recive/trigger";
GlobalConfig::Topic_G.mPubLocalCmd= "up/" + GlobalConfig::MacAddr_G + "/recive/cmd";
}
void PlatformInit::ConfigFileCheck()
{
if (access(SYSTEMINFOFILE, 0) < 0) {
WriteStr2Config(SYSTEMINFOFILE, "SystemInfo", "dataNodeGatewayName", "");
WriteStr2Config(SYSTEMINFOFILE, "SystemInfo", "dataNodeGatewayAssetId", "");
WriteStr2Config(SYSTEMINFOFILE, "SystemInfo", "dataNodeGatewayAddedBy", "");
WriteStr2Config(SYSTEMINFOFILE, "SystemInfo", "dataNodeGatewayAddedDate", "");
WriteStr2Config(SYSTEMINFOFILE, "SystemInfo", "timezone", "UTC+8");
WriteStr2Config(SYSTEMINFOFILE, "SystemInfo", "dataNodeGatewayNo", GlobalConfig::MacAddr_G);
WriteStr2Config(SYSTEMINFOFILE, "Area", "countryId", "");
WriteStr2Config(SYSTEMINFOFILE, "Area", "provincesId", "");
WriteStr2Config(SYSTEMINFOFILE, "Area", "cityListId", "");
WriteStr2Config(SYSTEMINFOFILE, "Uint", "uint", "ST");
}
if (access(NETWORKCONFIG, 0) < 0) {
WriteStr2Config(NETWORKCONFIG, "Net", "dnsName", " ");
WriteStr2Config(NETWORKCONFIG, "Net", "networkPortStatus", "DHCP");
WriteStr2Config(NETWORKCONFIG, "Net", "gateway", "0.0.0.0");
WriteStr2Config(NETWORKCONFIG, "Net", "subnetMask", "0.0.0.0");
WriteStr2Config(NETWORKCONFIG, "Net", "ipAddress", "0.0.0.0");
WriteStr2Config(NETWORKCONFIG, "Net", "hostName", "0.0.0.0");
}
if (access(SERVERCONFIG, 0) < 0) {
WriteStr2Config(SERVERCONFIG, "Server", "localServerIpAddress", "0.0.0.0");
WriteStr2Config(SERVERCONFIG, "Server", "localServerPort", "51613");
}
}
int PlatformInit::ServerIpInit()
{
if (access(SERVERCONFIG, 0) >= 0) {
GlobalConfig::ServerIP = ReadStrByOpt(SERVERCONFIG, "Server", "localServerIpAddress");
GlobalConfig::ServerPort = atoi(ReadStrByOpt(SERVERCONFIG, "Server", "localServerPort").c_str());
}
}
void PlatformInit::Sqlite3Init()
{
sql_ctl->OpenDB(GlobalConfig::DbName_G.c_str());
char szValue[10] = {0x00};
int nType = readIntValue( "Update", "type",(char*)GlobalConfig::Config_G.c_str());
if(nType== 1){
sql_ctl->SqliteInitDel(GlobalConfig::DbName_G.c_str());
writeIntValue("Update", "type",0,(char*)GlobalConfig::Config_G.c_str());
}
print_info("szValue = %d\n",nType);
sql_ctl->SqliteInit(GlobalConfig::DbName_G.c_str());
print_info("Sqlite3Init \n");
}
void PlatformInit::GPIOInit()
{
#ifdef G2UL_GATEWAY
GlobalConfig::GPIO_G.zigAckrep = 507;
GlobalConfig::GPIO_G.zigAckreset = 499;
GlobalConfig::GPIO_G.zigReset = 496;
GlobalConfig::GPIO_G.commPower = 363;
GlobalConfig::GPIO_G.vol3_8 = 498;
GlobalConfig::GPIO_G.commRest = 466;
GlobalConfig::GPIO_G.wifiPower = 488;
GlobalConfig::GPIO_G.wifiReset = 474;
GlobalConfig::GPIO_G.hardWatchDog = 497;
GlobalConfig::GPIO_G.zigDef = 408;
GlobalConfig::GPIO_G.runLed = 409;
GlobalConfig::GPIO_G.errorLed = 410;
GlobalConfig::GPIO_G.netResetNet0 = 489;
#else if IMX6UL_GATEWAY
GlobalConfig::GPIO_G.zigAckrep = 119;
GlobalConfig::GPIO_G.zigAckreset = 120;
GlobalConfig::GPIO_G.zigReset = 116;
GlobalConfig::GPIO_G.wifiReset = 8;
GlobalConfig::GPIO_G.commRest = 8;
GlobalConfig::GPIO_G.power = 9;
GlobalConfig::GPIO_G.errorLed = 130;
#endif
}
void PlatformInit::SystemInfoInit()
{
std::ifstream ifileOut(SYSTEMINFOFILE);
Json::Reader recvReader;
Json::Value Systeminfo;
//从配置文件中读取json数据
if (ifileOut.is_open()) {
print_blue("open %s\n", SYSTEMINFOFILE);
}
if (!recvReader.parse(ifileOut, Systeminfo, false)) {
if (ifileOut.is_open()) {
ifileOut.close();
} else {
return;
}
}
ifileOut.close();
Systeminfo["dataWatchIpAddress"] = GlobalConfig::IpAddr_G;
Systeminfo["softVersion"] = GlobalConfig::Version;
std::string strSerialNumber = GetFileContent(SN, 1);
Systeminfo["serialNumber"] = strSerialNumber;
Json::StyledWriter sw;
std::ofstream os;
os.open(SYSTEMINFOFILE);
os << sw.write(Systeminfo);
os.close();
}
void PlatformInit::UserInit()
{
std::string userName = ReadStrByOpt(SYSTEMINFOFILE, "UserInfo", "UserName");
if (userName.length() == 0) {
WriteStr2Config(SYSTEMINFOFILE, "UserInfo", "UserName", "Admin");
WriteStr2Config(SYSTEMINFOFILE, "UserInfo", "adminPassword", "njchaos");
WriteStr2Config(SYSTEMINFOFILE, "UserInfo", "userPassword", "");
}
}
void PlatformInit::EquipIpInit(std::string eth)
{
//eth0 初始化
int flag = 0;
std::string networkPortStatus = ReadStrByOpt(NETWORKCONFIG, eth, "networkPortStatus");
std::string subnetMask = ReadStrByOpt(NETWORKCONFIG, eth, "subnetMask");
std::string gateway = ReadStrByOpt(NETWORKCONFIG, eth, "gateway");
std::string dataWatchIpAddress = ReadStrByOpt(NETWORKCONFIG, eth, "ipAddress");
std::string strStatus = GetFileContent(NETWORK, 18);
print_red("strStatus:%s\n networkPortStatus:%s dataWatchIpAddress:%s subnetMask:%s gateway:%s\n",strStatus.c_str(),networkPortStatus.c_str(),dataWatchIpAddress.c_str(),subnetMask.c_str(),gateway.c_str());
if(0 == networkPortStatus.compare("DHCP"))
{
print_white("dhcp config\n");
std::string cmd = "";
#ifdef IMX6UL_GATEWAY
system("sed -i '17c auto eth0' /etc/network/interfaces");
system("sed -i '18c iface eth0 inet dhcp' /etc/network/interfaces");
system("sed -i '20c ' /etc/network/interfaces");
system("sed -i '21c ' /etc/network/interfaces");
system("sed -i '22c ' /etc/network/interfaces");
#endif
#ifdef G2UL_GATEWAY
if(eth == "eth0"){
system("sed -i '5c DHCP=ipv4' /etc/systemd/network/static/10-eth0-static.network");
system("sed -i '6c ' /etc/systemd/network/static/10-eth0-static.network");
system("sed -i '7c ' /etc/systemd/network/static/10-eth0-static.network");
}else if(eth == "eth1"){
system("sed -i '5c DHCP=ipv4' /etc/systemd/network/dynamic/20-eth1-dynamic.network");
system("sed -i '6c ' /etc/systemd/network/dynamic/20-eth1-dynamic.network");
system("sed -i '7c ' /etc/systemd/network/dynamic/20-eth1-dynamic.network");
}
#endif
flag = 0;
}
if(0 == networkPortStatus.compare("STATIC"))
{
std::string cmd = "";
print_red("static config\n");
#ifdef IMX6UL_GATEWAY
system("sed -i '18c iface eth0 inet static' /etc/network/interfaces");
if(dataWatchIpAddress.length() > 0)
{
cmd = "sed -i '20c address " + dataWatchIpAddress + "' /etc/network/interfaces";
}else{
cmd = "sed -i '20c address 0.0.0.0' /etc/network/interfaces";
}
system(cmd.c_str());
cmd.clear();
if(subnetMask.length() > 0)
{
cmd = "sed -i '21c netmask " + subnetMask + "' /etc/network/interfaces";
}else{
cmd = "sed -i '21c netmask 255.255.255.0' /etc/network/interfaces";
}
system(cmd.c_str());
cmd.clear();
if(gateway.length() > 0)
{
cmd = "sed -i '22c gateway " + gateway + "' /etc/network/interfaces";
}else{
cmd = "sed -i '22c gateway 0.0.0.0' /etc/network/interfaces";
}
system(cmd.c_str());
cmd.clear();
#endif
#ifdef G2UL_GATEWAY
if(eth == "eth0"){
cmd = "sed -i '5c Address=" + dataWatchIpAddress + "' /etc/systemd/network/static/10-eth0-static.network";
system(cmd.c_str());
cmd = "sed -i '6c Netmask=" + subnetMask + "' /etc/systemd/network/static/10-eth0-static.network";
system(cmd.c_str());
cmd = "sed -i '7c Gateway=" + gateway + "' /etc/systemd/network/static/10-eth0-static.network";
system(cmd.c_str());
}else if(eth == "eth1"){
cmd = "sed -i '5c Address=" + dataWatchIpAddress + "' /etc/systemd/network/dynamic/20-eth1-dynamic.network";
system(cmd.c_str());
cmd = "sed -i '6c Netmask=" + subnetMask + "' /etc/systemd/network/dynamic/20-eth1-dynamic.network";
system(cmd.c_str());
cmd = "sed -i '7c Gateway=" + gateway + "' /etc/systemd/network/dynamic/20-eth1-dynamic.network";
system(cmd.c_str());
}
#endif
flag = 1;
}
#ifdef IMX6UL_GATEWAY
system("reboot");
#endif
#ifdef G2UL_GATEWAY
system("systemctl restart systemd-networkd.service");
#endif
}