ParkSysView.cpp
资源名称:停车场管理系统.zip [点击查看]
上传用户:trilite
上传日期:2007-04-24
资源大小:261k
文件大小:12k
源码类别:
酒店行业
开发平台:
Visual C++
- // ParkSysView.cpp : implementation of the CParkSysView class
- //
- #include "stdafx.h"
- #include "ParkSys.h"
- #include "ParkSysDoc.h"
- #include "ParkSysView.h"
- #include "readprice.h"
- #include <atlbase.h>
- #include "led.h"
- #include "InputBox.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CParkSysView
- IMPLEMENT_DYNCREATE(CParkSysView, CFormView)
- BEGIN_MESSAGE_MAP(CParkSysView, CFormView)
- //{{AFX_MSG_MAP(CParkSysView)
- ON_COMMAND(ID_GENERATE_ACCOUNT, OnGenerateAccount)
- ON_WM_CTLCOLOR()
- ON_WM_CANCELMODE()
- ON_COMMAND(ID_CHAREGE, OnCharge)
- //}}AFX_MSG_MAP
- // Standard printing commands
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CParkSysView construction/destruction
- CParkSysView::CParkSysView()
- : CFormView(CParkSysView::IDD)
- {
- //{{AFX_DATA_INIT(CParkSysView)
- m_enCardID = _T("");
- m_enInTime = _T("");
- m_exCardID = _T("");
- m_exChargeNum = _T("");
- m_exInTime = _T("");
- m_exOutTime = _T("");
- m_exUseTime = _T("");
- m_NowCarNum = 0;
- m_TotalCarNum = 0;
- //}}AFX_DATA_INIT
- // TODO: add construction code here
- }
- CParkSysView::~CParkSysView()
- {
- CParkSysApp *app = (CParkSysApp *)AfxGetApp();
- app->database.Close();
- }
- void CParkSysView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CParkSysView)
- DDX_Control(pDX, IDC_EX_ANI, m_aviEx);
- DDX_Control(pDX, IDC_EN_ANI, m_aviEn);
- DDX_Control(pDX, IDC_INIMAGE, m_bmpIn);
- DDX_Text(pDX, IDC_EN_CARDID, m_enCardID);
- DDX_Text(pDX, IDC_EN_INTIME, m_enInTime);
- DDX_Text(pDX, IDC_EX_CARDID, m_exCardID);
- DDX_Text(pDX, IDC_EX_CHARGENUM, m_exChargeNum);
- DDX_Text(pDX, IDC_EX_INTIME, m_exInTime);
- DDX_Text(pDX, IDC_EX_OUTTIME, m_exOutTime);
- DDX_Text(pDX, IDC_EX_USETIME, m_exUseTime);
- DDX_Text(pDX, IDC_NOWCARNUM, m_NowCarNum);
- DDX_Text(pDX, IDC_TOTALCARNUM, m_TotalCarNum);
- //}}AFX_DATA_MAP
- }
- BOOL CParkSysView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // 初始化串口
- //入口
- CRegKey regKey;
- regKey.Open(HKEY_CURRENT_USER,_T("Software\ParkSys\CommSet"));
- //条码打印机
- DWORD LabelPrinter;
- regKey.QueryValue(LabelPrinter, _T("LPComm"));
- LabelPrinter+=1;
- if (m_CommLP.InitPort(this, LabelPrinter, 9600, 'N',8,1))
- m_CommLP.StartMonitoring();
- else
- MessageBox("条码打印机连接错误",NULL,MB_OK | MB_ICONERROR);
- //电子显示屏
- DWORD LED;
- regKey.QueryValue(LED, _T("LEDComm"));
- LED+=1;
- if (m_CommLED.InitPort(this, LED, 9600, 'N',8,1))
- {
- m_CommLED.StartMonitoring();
- }
- else
- MessageBox("电子显示屏连接错误",NULL,MB_OK | MB_ICONERROR);
- //初始化参数
- //显示车位
- CParkSysApp *app = (CParkSysApp *)AfxGetApp();
- CCountTime rs_CountTime( &app->database );
- rs_CountTime.Open();
- while(!rs_CountTime.IsEOF())
- {
- m_NowCarNum += 1;
- rs_CountTime.MoveNext();
- }
- rs_CountTime.Close();
- //显示总的车位数
- regKey.Open(HKEY_CURRENT_USER,_T("Software\ParkSys\ParaSet"));
- regKey.QueryValue(m_TotalCarNum, _T("Park_capacity"));
- regKey.Close();
- return CFormView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CParkSysView printing
- /////////////////////////////////////////////////////////////////////////////
- // CParkSysView diagnostics
- /////////////////////////////////////////////////////////////////////////////
- // CParkSysView message handlers
- void CParkSysView::DisplayImage(CLongBinary* image)
- {
- DWORD lsize = image->m_dwDataLength;
- HANDLE ghandle = image->m_hData;
- DWORD s = GlobalSize(ghandle);
- void* p = GlobalLock(ghandle);
- DWORD nBytesToWrite = lsize;
- /*#if defined(_DEBUG)
- AfxMessageBox("check the existance of carout.bmp");
- #endif*/
- CString str = ((CParkSysApp *)AfxGetApp())->ExtractFilePath();
- str += "carout.bmp";
- HANDLE hFile = CreateFile(str,
- GENERIC_WRITE,
- /*FILE_SHARE_WRITE | FILE_SHARE_READ*/0,NULL,
- CREATE_ALWAYS,0,0);
- ASSERT(hFile != INVALID_HANDLE_VALUE);
- BOOL reval = WriteFile(hFile,p,nBytesToWrite,
- &nBytesToWrite, NULL);
- CloseHandle(hFile);
- /*#if defined(_DEBUG)
- AfxMessageBox("check the content of carout.bmp");
- #endif*/
- BOOL val = GlobalUnlock(ghandle);
- HGLOBAL h = GlobalFree(ghandle);
- CFile fBmp;
- fBmp.Open(str, CFile::modeRead);
- DWORD dwLength=fBmp.GetLength();
- fBmp.Close();
- if (dwLength > 0)
- {
- TRY
- {
- DeleteObject(m_bmpIn.SetBitmap((HBITMAP)LoadFromFile(str)));
- }
- CATCH_ALL(e)
- {
- AfxMessageBox("Delphi DLL LoadFromFile发生错误!");
- THROW_LAST();
- }
- END_CATCH_ALL
- }
- else
- {
- AfxMessageBox("Carout.bmp长度为零!");
- }
- UpdateData(FALSE);
- }
- void CParkSysView::OnGrabimage(CString strCardID)
- {
- CParkSysApp* app = (CParkSysApp *)AfxGetApp();
- CString str = app->ExtractFilePath();
- str += "carin.dib";
- BOOL revalue = capFileSaveDIB(app->avi.m_hWndCap, (LPCTSTR)str);
- UpdateWindow();
- //判断如果保存图像不成功,那么就直接返回。
- if (!revalue)
- {
- AfxMessageBox("保存图像失败,程序还将继续,但是,在此车出门的时候您不能进行比对。");
- return;
- }
- HANDLE hFile = CreateFile(str,GENERIC_READ,
- 0/*FILE_SHARE_READ |FILE_SHARE_WRITE*/,NULL, //读写共享
- OPEN_EXISTING,0,0);
- ASSERT(hFile != INVALID_HANDLE_VALUE);
- DWORD hsize;
- DWORD lsize = GetFileSize(hFile, &hsize);
- HGLOBAL ghandle = GlobalAlloc(GMEM_MOVEABLE, lsize);
- DWORD s = GlobalSize(ghandle);
- void* p = GlobalLock(ghandle);
- DWORD nBytesToRead = lsize;
- BOOL reval = TRUE;
- reval = ReadFile(hFile,p,nBytesToRead, &nBytesToRead, NULL);
- CloseHandle(hFile);
- CLongBinary image;
- CCountTime rs_CountTime( &app->database );
- //转换2进制文件为16进制文件
- CString csSQL = "UPDATE TAB_COUNTTIME SET CAR_IMAGE=0x";
- LPTSTR szTemp = new TCHAR[ lsize*2 + 1 ];
- extern const TCHAR HEX[256][2];
- for (DWORD i = 0; i < lsize; i++)
- {
- * (szTemp + (i << 1)) = HEX[(unsigned char)*((BYTE*)p + i)][0];
- * (szTemp + (i << 1) + 1) = HEX[(unsigned char)*((BYTE*)p + i)][1];
- }
- szTemp[ lsize*2 ] = 0;
- csSQL += szTemp;
- csSQL += " WHERE CARD_ID='" + strCardID+"'";
- app->database.ExecuteSQL( csSQL );
- delete []szTemp;
- BOOL val = GlobalUnlock(ghandle);
- HGLOBAL h = GlobalFree(ghandle);
- }
- void CParkSysView::CreateNewLabel()
- {
- DWORD CardID;
- CRegKey regKey;
- regKey.Open(HKEY_CURRENT_USER,_T("Software\ParkSys\ParaSet"));
- regKey.QueryValue(CardID, _T("LabelCardID"));
- if (CardID>=99999)
- CardID=0;
- CardID += 1;
- regKey.SetValue(CardID, _T("LabelCardID"));
- regKey.Close();
- CParkSysApp* app = (CParkSysApp*)AfxGetApp();
- CCountTime rs_CountTime( &app->database );
- rs_CountTime.Open();
- CardID += 30000000;
- char buff[20];
- _itoa(CardID, buff, 10);
- CString strCardID = buff;
- rs_CountTime.AddNew();
- rs_CountTime.m_CARD_ID = strCardID;
- CTime inTime = CTime::GetCurrentTime();
- rs_CountTime.m_IN_TIME = inTime;
- rs_CountTime.Update();
- rs_CountTime.Close();
- //使用Zebra Corp. ZPL II 编码向支持ZPL的条形码打印机发送打印指令。
- // ZPL 的意思是 Zebra Programming Language
- CString strFormat = "^XAnr"
- "^FO200,200^BY3nr"
- "^BCN,100,Y,N,N^FD%d^FSnr"
- "^FO100,400^FD%s^FSnr"
- "^XZnr";
- CString strZPLLabel;
- //把数据填入到上面的ZPL串中。卡号CardID,进入时间 YYYY-mm-dd hh:MM:SS
- strZPLLabel.Format(strFormat, CardID, inTime.Format("%Y-%m-%d %H:%M:%S"));
- m_CommLP.WriteToPort(strZPLLabel.GetBuffer(strZPLLabel.GetLength() + 1));
- strZPLLabel.ReleaseBuffer();
- OnGrabimage(strCardID); //抓取一幅汽车进入图像
- m_NowCarNum += 1;
- m_enCardID = strCardID;
- m_enInTime = inTime.Format("%Y-%m-%d %H:%M:%S");
- UpdateData(FALSE);
- }
- BOOL CParkSysView::Charge(CString cardid, double &cyFee)
- {
- CParkSysApp *app = (CParkSysApp *)AfxGetApp();
- cyFee = 0.0;
- CTime inTime;
- CTime outTime = CTime::GetCurrentTime();
- CTimeSpan spanTime;
- CCountTime rs_counttime(&app->database);
- rs_counttime.m_strFilter = "Card_ID = '" + cardid + "'";
- rs_counttime.Open();
- rs_counttime.MoveFirst();
- inTime = rs_counttime.m_IN_TIME;
- rs_counttime.Close();
- //显示卡号
- m_exCardID = cardid;
- //显示进出时间
- spanTime = outTime - inTime;
- m_exInTime = inTime.Format( "%Y-%m-%d %H:%M:%S" );
- m_exOutTime += outTime.Format( "%Y-%m-%d %H:%M:%S" );
- m_exUseTime = spanTime.Format( "%D天%H小时%M分%S秒" );
- do
- {
- if (inTime>=outTime)
- {
- AfxMessageBox("系统时间错误");
- break;
- }
- if(cardid >= "30000000")
- {
- double fHours = spanTime.GetTotalMinutes() / 60.0;
- //retCode = m_StoredProcFee.PRC_FEE_PC_HOUR(cardid, fHours, &mFee);
- //每小时停车费2元RMB
- cyFee = fHours * 2.0;
- CString strMoney;
- strMoney.Format("%0.4lf元", cyFee);
- m_exChargeNum = strMoney;
- UpdateData(FALSE);//显示金额
- CLED led(&m_CommLED);
- led.TakeEffect(CString("!\纸卡收费") + strMoney + "\!@@");
- CWaitCursor wait;
- CReadPrice readprice;
- readprice.ReadPrice(atof(strMoney));
- wait.Restore();
- if (AfxMessageBox("纸卡用户按时付费,交易结束后按确定,否则按取消。"
- , MB_OKCANCEL | MB_ICONINFORMATION) != IDOK)
- {
- break;
- }
- }
- UpdateData(FALSE);//显示信息
- return TRUE;
- }while(false);
- UpdateData(FALSE);//显示信息
- return FALSE;
- }
- void CParkSysView::OnGenerateAccount()
- {
- CreateNewLabel();
- m_aviEn.Play();
- }
- HBRUSH CParkSysView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);
- char szClassName[50] = {0};
- GetClassName(pWnd->m_hWnd, szClassName, sizeof(szClassName));
- if (strnicmp(szClassName, "EDIT", sizeof(szClassName)) == 0)
- {
- CBrush brush(RGB(255, 255, 200));
- hbr = (HBRUSH)brush.Detach();
- pDC->SetBkMode(TRANSPARENT);
- }
- // TODO: Return a different brush if the default is not desired
- return hbr;
- }
- void CParkSysView::OnCharge()
- {
- CString strCardID;
- CInputBox inputBox("收费", "请扫描或者输入卡号:", &strCardID);
- if (IDOK == inputBox.DoModal())
- {
- //出车
- BOOL ret = CheckCarOut(strCardID);
- if (ret)
- {
- m_aviEx.Play();
- }
- //计算车辆数量
- RefreshCarCount(strCardID);
- }
- }
- LONG CParkSysView::RefreshCarCount(CString strCardID)
- {
- //得到目前停车场中的车辆数。
- CParkSysApp *app = (CParkSysApp *)AfxGetApp();
- CCountTime rs_CountTime( &app->database );
- rs_CountTime.Open();
- if( rs_CountTime.GetRecordCount() != 0 )
- {
- rs_CountTime.MoveFirst();
- }
- int RecordCount = 0;
- while( !rs_CountTime.IsEOF() )
- {
- rs_CountTime.MoveNext();
- RecordCount += 1;
- }
- rs_CountTime.Close();
- m_NowCarNum = RecordCount;
- UpdateData( FALSE );
- return 0;
- }
- BOOL CParkSysView::CheckCarOut(CString strCardID)
- {
- CParkSysApp *app = (CParkSysApp *)AfxGetApp();
- //显示进门的图象
- CCountTime rs_CountTime( &app->database );
- rs_CountTime.m_strFilter = "TAB_COUNTTIME.CARD_ID='"
- + strCardID+"'";
- rs_CountTime.Open();
- DisplayImage(&rs_CountTime.m_CAR_IMAGE);
- rs_CountTime.Close();
- //开始收费
- double cyFee = 0.0; //金额
- BOOL ret = Charge(strCardID, cyFee);
- if(ret)
- {
- //保存记录和把该车占用的车位还原。
- ret = app->database.BeginTrans();
- if(ret)
- {
- TRY
- {
- const char * sqlTemplate = "insert into tab_history_card select card_id,"
- " in_time, GetDate(), %lf, car_image from"
- " tab_counttime where card_id = '%s'";
- CString strSQL;
- strSQL.Format(sqlTemplate, cyFee, strCardID);
- app->database.ExecuteSQL(strSQL);
- strSQL = "delete from tab_counttime where"
- " Card_id like '" + strCardID + "'";
- app->database.ExecuteSQL(strSQL);
- ret = app->database.CommitTrans();
- if(ret)
- --m_NowCarNum;
- }
- CATCH(CDBException, e)
- {
- app->database.Rollback();
- ret = FALSE;
- }
- END_CATCH
- }
- }
- UpdateData(FALSE);
- return ret;
- }