Example #1
0
int main()
{
	Log log("./data/temp_log.txt", 1024 * 1024);
	rrdWeather *rrdW = new rrdWeather();
	rrdBattery *rrdB = new rrdBattery();
	string port = "/dev/ttyAMA0";
	int iSpeed = 9600;
	char szReadBuffer[128];
	int iBytesRead = 0;
	bool bLoop = true;
	memset(&szReadBuffer[0], 0, 128*sizeof(char));

	LLAP llap;
	Serial serial;
	serial.Configure(port, iSpeed);
	serial.Open();

	int tem = fcntl(0, F_GETFL, 0);
  	fcntl (0, F_SETFL, (tem | O_NDELAY));

	cout << "creating database if not exist" << endl;
	rrdW->create();
	rrdB->create();
	cout << "Listening serial port " << port << endl;

	string tmp;
	int illapRet = 0;

	while(true == bLoop)
	{
		memset(&szReadBuffer[0], 0, 128*sizeof(char));
		iBytesRead = serial.Read(&szReadBuffer[0], 128, 2500, 12);
		if(0 < iBytesRead)
		{
			illapRet = llap.ParseMsg(szReadBuffer, iBytesRead);
			if(MSG_READY == illapRet || MSG_READY_PARTIAL == illapRet)
			{
				LLAP_MSG *pMsg = llap.GetLastMsg();
				string strTime = GetCurrentTime(false);
				//Write to rrd
				if(0 == strcasecmp("TMPA", pMsg->m_szCmd))
				{
					rrdW->update(atof(pMsg->m_szValue));
					//Copy file to /var/www/rpi_web
					CopyFile(rrdW->GetFile(1), "/var/www/rpi_web/01_daily.png");
					CopyFile(rrdW->GetFile(2), "/var/www/rpi_web/02_weekly.png");
					CopyFile(rrdW->GetFile(3), "/var/www/rpi_web/03_monthly.png");
					CopyFile(rrdW->GetFile(4), "/var/www/rpi_web/04_yearly.png");
				}
				if(0 == strcasecmp("BATT", pMsg->m_szCmd))
				{
					rrdB->update(atof(pMsg->m_szValue));
					//Copy file to /var/www/rpi_web
					CopyFile(rrdB->GetFile(1), "/var/www/rpi_web/battery.png");
				}
				if(0 == strcasecmp("BATTLOW", pMsg->m_szCmd))
                                {
                                        rrdB->LowBattWarning();
                                        //Copy file to /var/www/rpi_web
                                        CopyFile(rrdB->GetFile(1), "/var/www/rpi_web/battery.png");
                                }

				//Write to screen and log
				tmp = strTime + "\t" + pMsg->m_szId + " " + pMsg->m_szCmd + " " + pMsg->m_szValue + "\t\t" + pMsg->m_szOriginal;
				cout << tmp << endl;
				log.Write(tmp);
			}
		}
		if('q' == getchar())
		{
			bLoop = false;
		}
	}
	serial.Close();
	cout << "Exiting program" << endl;
	fcntl(0, F_SETFL, tem);

	delete rrdW;
	delete rrdB;
	return 0;
}