📄 public.cpp
字号:
#include "stdafx.h"
#include "天气预报采集系统.h"
#include "天气预报采集系统Dlg.h"
#include "public.h"
#include <string>
#include <list>
#include "ParseHTML.h"
#include "AHttpForCE.h"
#include "URLEncode.h"
#include "../oci/jy_oci.h"
#include "CharConvert.h"
std::list<CCity >g_list_city;
std::list<CCityWeatherInfo >g_list_city_weather;
CRITICAL_SECTION g_section_list_city_weather;
UINT ProcGetCityWeather(LPVOID lpPara)
{
ObjectThreadPara *para=(ObjectThreadPara *)lpPara;
para->Create();
while(1)
{
if(WaitForSingleObject(para->m_event_stop,1000)!=WAIT_TIMEOUT)
{
TRACE("\r\n ProcGetCityWeather 线程函数已退出\r\n");
SetEvent(para->m_event_stopped);
delete para;
return 1;
}
std::list<CCity>::iterator pos=g_list_city.begin();
while(pos!=g_list_city.end())
{
CCity city=(*pos++);
CString city_weather;
GetWeatherByCity(city.GetCityName(),city_weather);
CCityWeatherInfo weather(city.GetCityName(),city_weather);
((CTQYBDlg*)AfxGetApp()->m_pMainWnd)->m_Log_Edit.SetWindowText(city.GetCityName()+"\t"+city_weather+"\r\n");
EnterCriticalSection(&g_section_list_city_weather);
g_list_city_weather.push_back(weather);
LeaveCriticalSection(&g_section_list_city_weather);
}
SetEvent(para->m_event_stop);
}
return 1;
}
BOOL GetCityWeatherFromlist(CCityWeatherInfo &result)
{
BOOL boolen=FALSE;
EnterCriticalSection(&g_section_list_city_weather);
if(!g_list_city_weather.empty())
{
result=g_list_city_weather.front();
g_list_city_weather.pop_front();
boolen=TRUE;
}
LeaveCriticalSection(&g_section_list_city_weather);
return boolen;
}
//将天气信息写入数据库中
UINT ProcInsertCityWeather(LPVOID lpPara)
{
ObjectThreadPara *para=(ObjectThreadPara *)lpPara;
para->Create();
COciConnection conn;
conn.Connect("ESCHOOL", "mobile", "mobile_+098op");
int row_num=0;
char szSqlStr[1024]={0};
sprintf(szSqlStr,"%s","begin up_insert_into_weather_report(:in_date,:in_city_name,:in_weather_status,:out_result);end;");
while(1)
{
if(WaitForSingleObject(para->m_event_stop,10)!=WAIT_TIMEOUT)
{
TRACE("\r\n ProcInsertCityWeather 线程函数已退出\r\n");
SetEvent(para->m_event_stopped);
return 1;
}
CTime time=CTime::GetCurrentTime();
CString s;
s.Format(_T("%s"),time.Format(_T("%Y-%m-%d")));
CCityWeatherInfo result;
if(!GetCityWeatherFromlist(result))
{
Sleep(1000);
continue;
}
try
{
COciCursor cursor(&conn);
CString s_city_name=result.GetCityName();
char *city_name=s_city_name.GetBuffer();
CString s_city_weather=result.GetCityWeather();
char *city_weather=s_city_weather.GetBuffer();
char *in_date=s.GetBuffer();
int out_result=-1;
OCITransStart(conn.m_svchp, conn.m_errhp, 60, OCI_TRANS_NEW);
cursor.Prepare(szSqlStr);
cursor.BindByName(":in_date",in_date,strlen(in_date)+1,NULL,SQLT_STR);
cursor.BindByName(":in_city_name",city_name,strlen(city_name)+1,NULL,SQLT_STR);
cursor.BindByName(":in_weather_status",city_weather,strlen(city_weather)+1,NULL,SQLT_STR);
cursor.BindByName(":out_result",&out_result,sizeof(out_result),NULL,SQLT_INT);
cursor.Execute();
OCITransCommit(conn.m_svchp, conn.m_errhp, (ub4) 0);
}
catch(COciException &e)
{
}
}
}
// 通过城市名获取天气数据
BOOL GetWeatherByCity(LPCTSTR CITY,CString& DATA)
{
CString html;
CString str;
unsigned short nPort = 80;
DWORD dwRes = 0;
CURLEncode en;
BOOL RET;
try
{
str.Format("city=%s&Submit5=%s",en.URLEncode(CString(CITY)),en.URLEncode(CString("提交")));
RET=CAHttpForCE::Post(html,"www.cma.gov.cn",nPort,"tqyb/v2/url.php",str,dwRes,TRUE);
if (!RET)
{
return FALSE;
}
html.Replace(" ","");
CParseHTML ph;
CString section;
RET=ph.GetHtmlSignSection(html,"<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"table_blue2\">","</table>",section,1,2);
if (!RET)
return FALSE;
RET=ph.GetSection2Data(section,DATA);
if (!RET)
return FALSE;
return TRUE;
}
catch(...)
{
return FALSE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -