wirelessgateway/platform/SH_PlatformInit.cpp

251 lines
8.4 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;
std::string GlobalConfig::Version = "2.0.11";
std::string GlobalConfig::MacAddr_G = "";
std::string GlobalConfig::IpAddr_G = "";
std::string GlobalConfig::ServerIP = "";
int GlobalConfig::ServerPort = 0;
TopicList GlobalConfig::Topic_G;
ZigbeeInfo GlobalConfig::ZigbeeInfo_G;
ZIGBEE GlobalConfig::Zigbee_G;
std::string GlobalConfig::DbName_G = "/opt/configenv/cidn.db"; //数据库名称
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();
transform(GlobalConfig::MacAddr_G.begin(), GlobalConfig::MacAddr_G.end(), GlobalConfig::MacAddr_G.begin(), ::toupper);
GlobalConfig::IpAddr_G = IpAddrInit();
print_info("GlobalConfig::IpAddr_G : %s\n", GlobalConfig::IpAddr_G.c_str());
this->ConfigFileCheck();
this->TopicInit();
this->ServerIpInit();
this->Sqlite3Init();
print_info("ServerIP : %s \n", GlobalConfig::ServerIP.c_str());
// sleep(10);
} catch(...){
print_error("PlatFormInit exception happend.\n");
std::string errorinfo = "系统初始化异常";
}
}
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/configureInfor/" + 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.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 = boost::lexical_cast<int>(ReadStrByOpt(SERVERCONFIG, "Server", "localServerPort"));
}
}
void PlatformInit::Sqlite3Init()
{
sql_ctl->OpenDB(GlobalConfig::DbName_G.c_str());
sql_ctl->SqliteInit(GlobalConfig::DbName_G.c_str());
print_info("Sqlite3Init \n");
}
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()
{
//eth0 初始化
int flag = 0;
std::string networkPortStatus = ReadStrByOpt(NETWORKCONFIG, "Net", "networkPortStatus");
std::string subnetMask = ReadStrByOpt(NETWORKCONFIG, "Net", "subnetMask");
std::string gateway = ReadStrByOpt(NETWORKCONFIG, "Net", "gateway");
std::string dataWatchIpAddress = ReadStrByOpt(NETWORKCONFIG, "Net", "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 = "";
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");
flag = 0;
}
if(0 == networkPortStatus.compare("STATIC"))
{
std::string cmd = "";
print_red("static config\n");
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();
flag = 1;
}
if(1 == flag){ //静态直接更新
system("source /etc/config.sh");
}
if(0 == flag) //动态重新启动获取
{
system("reboot");
}
}