// Greg Stitt, Ann Gordon-Ross // vahidDlg.cpp : implementation file // #include "stdafx.h" #include "vahid.h" #include "vahidDlg.h" #include "ComPort.h" #include "port.h" #include "special.h" #include "SendText.h" #include "SendOptions.h" #include #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CVahidDlg dialog // constants used for registry const char CVahidDlg::s_profileHeading[] = "Send Conference Info"; const char CVahidDlg::s_ComPort[] = "COM"; const char CVahidDlg::s_SendTime[] = "Send Time"; const char CVahidDlg::s_Path[] = "Path"; const char CVahidDlg::s_Month[] = "Month"; const char CVahidDlg::s_Day[] = "Day"; CVahidDlg::CVahidDlg(CWnd* pParent /*=NULL*/) : CDialog(CVahidDlg::IDD, pParent) { //{{AFX_DATA_INIT(CVahidDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); // initialize timer event and sending to false m_bTimerEvent = FALSE; m_bSending = FALSE; } void CVahidDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CVahidDlg) DDX_Control(pDX, IDC_ERRORMSG, m_lblError); DDX_Control(pDX, ID_SEND, m_SendButton); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CVahidDlg, CDialog) // message maps for menu options ON_COMMAND(ID_OPTIONS_SETTINGS, OnMainSettings) ON_COMMAND(ID_OPTIONS_EXIT, OnMenuExit) ON_COMMAND(ID_OPTIONS_PORT, OnComPort) ON_COMMAND(ID_HELP_ABOUT, OnAbout) //{{AFX_MSG_MAP(CVahidDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(ID_SEND, OnSend) ON_WM_DESTROY() ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CVahidDlg message handlers BOOL CVahidDlg::OnInitDialog() { // gets registry settings m_nComPort = AfxGetApp()->GetProfileInt(s_profileHeading, s_ComPort, 1); m_nSendTime = AfxGetApp()->GetProfileInt(s_profileHeading, s_SendTime, 60); m_nOldMonth = AfxGetApp()->GetProfileInt(s_profileHeading, s_Month, CTime::GetCurrentTime().GetMonth()); m_nOldDay = AfxGetApp()->GetProfileInt(s_profileHeading, s_Day, CTime::GetCurrentTime().GetDay()); m_strPath = AfxGetApp()->GetProfileString(s_profileHeading, s_Path); TRACE("Month = %d, Day = %d\n", m_nOldMonth, m_nOldDay); // load accelerator for menu m_hAccel = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1)); CDialog::OnInitDialog(); // start the program sending when the program first starts m_SendButton.SetWindowText("Stop Sending"); m_bSending = TRUE; SetTimer(1, TIME * m_nSendTime, NULL); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } void CVahidDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CVahidDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CVahidDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } // this is commented out to avoid having the program exit // when enter is pressed void CVahidDlg::OnOK() { // CDialog::OnOK(); } void CVahidDlg::OnCancel() { CDialog::OnCancel(); } void CVahidDlg::OnMenuExit() { // exits the program by closing the dialog box CDialog::OnOK(); } void CVahidDlg::OnAbout() { // shows About dialog box CAboutDlg dialog; dialog.DoModal(); } void CVahidDlg::OnComPort() { CComPort dialog; // -1 and + 1 make sure that m_nComPort holds correct number because // dialog.m_nComPort contains the Com number - 1 dialog.m_nComPort = m_nComPort - 1; if (dialog.DoModal() == IDOK); { m_nComPort = dialog.m_nComPort + 1; AfxGetApp()->WriteProfileInt(s_profileHeading, s_ComPort, m_nComPort); } } // occurs when user clicks the send button void CVahidDlg::OnSend() { BOOL newday = FALSE; KillTimer(1); // hide error label m_lblError.ShowWindow(SW_HIDE); // get current time CTime time = CTime::GetCurrentTime(); m_nSends = NUM_SENDS; if ((time.GetDay() != m_nOldDay) && m_bSending == FALSE) { // if it is a new day, send the data more times m_nSends = NUM_SENDS_NEWDAY; newday = TRUE; m_nOldDay = time.GetDay(); AfxGetApp()->WriteProfileInt(s_profileHeading, s_Day, m_nOldDay); } if (time.GetMonth() != m_nOldMonth) { // if the month has changed // erase the text from the previous month CTime oldtime(time.GetYear(), m_nOldMonth, 1,1,1,1); // call ResetFile with the name of last month ResetFile(oldtime.Format("%B.txt")); // update old month m_nOldMonth = time.GetMonth(); AfxGetApp()->WriteProfileInt(s_profileHeading, s_Month, m_nOldMonth); } TRACE("Numsends from vahiddlg = %d\n", m_nSends); // if it wasn't sending when the user clicked send if (m_bSending == FALSE) { CSendText dlg[NUM_ROOMS]; // send for each room for (int i = 0; i < NUM_ROOMS; i ++) { // initialize dlg data dlg[i].m_nCom = m_nComPort; dlg[i].m_nBaud = BAUD_RATE; dlg[i].m_nRoom = i; dlg[i].m_nSends = m_nSends; dlg[i].m_bNewDay = newday; dlg[i].m_strPath = m_strPath; // initialize oldtext for (int j = 0; j < NUM_TIMES; j ++) { dlg[i].m_oldtext[j] = m_oldtext[i][j]; } if (dlg[i].DoModal() == ID_ERRORFILE) { // if there was an error reading the // file, then the error label becomes // visible m_lblError.ShowWindow(SW_SHOW); } else { m_lblError.ShowWindow(SW_HIDE); // save the old text for (int j = 0; j < NUM_TIMES; j ++) { m_oldtext[i][j] = dlg[i].m_text[j]; } } } } // update buttons and timer if (m_bSending == FALSE) { // if it wasn't sending, change the button to // to Stop Sending m_SendButton.SetWindowText("Stop Sending"); m_bSending = TRUE; SetTimer(1, TIME * m_nSendTime, NULL); } else if (m_bSending == TRUE) { // if it was sending, change the button to SendData m_SendButton.SetWindowText("Send Data"); m_bSending = FALSE; KillTimer(1); } } // occurs when user selects MainSettings from menu void CVahidDlg::OnMainSettings() { CSendOptions dlg; dlg.m_nSendTime = m_nSendTime; dlg.m_strPath = m_strPath; if (dlg.DoModal() == IDOK) { // update SendTime and path m_nSendTime = dlg.m_nSendTime; m_strPath = dlg.m_strPath; AfxGetApp()->WriteProfileString(s_profileHeading, s_Path, m_strPath); AfxGetApp()->WriteProfileInt(s_profileHeading, s_SendTime, m_nSendTime); // update timer if (m_bSending == TRUE) SetTimer(1, TIME * m_nSendTime, NULL); else KillTimer(1); } } void CVahidDlg::OnDestroy() { // update registry settings AfxGetApp()->WriteProfileInt(s_profileHeading, s_ComPort, m_nComPort); AfxGetApp()->WriteProfileInt(s_profileHeading, s_SendTime, m_nSendTime); AfxGetApp()->WriteProfileInt(s_profileHeading, s_Month, m_nOldMonth); AfxGetApp()->WriteProfileInt(s_profileHeading, s_Day, m_nOldDay); AfxGetApp()->WriteProfileString(s_profileHeading, s_Path, m_strPath); CDialog::OnDestroy(); } // used so the accelerator works on the menu BOOL CVahidDlg::PreTranslateMessage(MSG* pMsg) { BOOL ret = ::TranslateAccelerator(GetSafeHwnd(), m_hAccel, pMsg); if(!ret) ret = CDialog::PreTranslateMessage(pMsg); return ret; } // occurs from a timer event when sending void CVahidDlg::OnTimer(UINT nIDEvent) { BOOL newday = FALSE; KillTimer(1); m_bSending = TRUE; CSendText dlg[NUM_ROOMS]; // get current time CTime time = CTime::GetCurrentTime(); m_nSends = NUM_SENDS; if (time.GetDay() != m_nOldDay) { // if it is a newday, send the data more times m_nSends = NUM_SENDS_NEWDAY; newday = TRUE; } if (time.GetMonth() != m_nOldMonth) { // if the month has changed // erase the text from the previous month CTime oldtime(1, m_nOldMonth, 1,1,1,1); // call ResetFile with the name of last month ResetFile(oldtime.Format("%B.txt")); } TRACE("Numsends from vahiddlg = %d\n", m_nSends); // update old month and day m_nOldDay = time.GetDay(); AfxGetApp()->WriteProfileInt(s_profileHeading, s_Day, m_nOldDay); m_nOldMonth = time.GetMonth(); AfxGetApp()->WriteProfileInt(s_profileHeading, s_Month, m_nOldMonth); // send for each room for (int i = 0; i < NUM_ROOMS; i ++) { dlg[i].m_nCom = m_nComPort; dlg[i].m_nBaud = BAUD_RATE; dlg[i].m_nRoom = i; dlg[i].m_nSends = m_nSends; dlg[i].m_bNewDay = newday; dlg[i].m_strPath = m_strPath; // initialize old text for (int j = 0; j < NUM_TIMES; j ++) { dlg[i].m_oldtext[j] = m_oldtext[i][j]; } if (dlg[i].DoModal() == ID_ERRORFILE) { // if error reading file, make error label // visible m_lblError.ShowWindow(SW_SHOW); } else { m_lblError.ShowWindow(SW_HIDE); // save old text for (int j = 0; j < NUM_TIMES; j ++) { m_oldtext[i][j] = dlg[i].m_text[j]; } } } SetTimer(1, TIME * m_nSendTime, NULL); } // this file resets the a month's file to all '\0's // each creates a line of MAX_LINE_CHAR '\0's for each time // of each day for the entire month void CVahidDlg::ResetFile(CString file) { // open files for updating ofstream outfile1, outfile2, outfile3; outfile1.open(m_strPath + "\\A265\\" + file, ios::nocreate); outfile2.open(m_strPath + "\\A275\\" + file, ios::nocreate); outfile3.open(m_strPath + "\\A277\\" + file, ios::nocreate); TRACE("%s was reset\n", file); // not a string, this is just an array of bytes used to // output to the file char reset[MAX_LINE_CHAR + 1]; // fill array with all '\0's and a \n at the end for (int j = 0; j < MAX_LINE_CHAR; j ++) { reset[j] = '\0'; } reset[MAX_LINE_CHAR] = '\n'; // exits the function if error opening file if (!outfile1 || !outfile2 || !outfile3) { return; } outfile1.seekp(0); outfile2.seekp(0); outfile3.seekp(0); // resets file so that each line is full of nulls for (int i = 0; i < NUM_TIMES * 31; i ++) { outfile1.write((char *) reset, MAX_LINE_CHAR + 1); outfile2.write((char *) reset, MAX_LINE_CHAR + 1); outfile3.write((char *) reset, MAX_LINE_CHAR + 1); } } // old code used for sequential file access /* void CVahidDlg::ResetFile(CString file) { // open files for updating ofstream outfile1, outfile2, outfile3; outfile1.open(m_strPath + "\\A265\\" + file, ios::nocreate); outfile2.open(m_strPath + "\\A275\\" + file, ios::nocreate); outfile3.open(m_strPath + "\\A277\\" + file, ios::nocreate); TRACE("%s was reset\n", file); // exits the function if error opening file if (!outfile1 || !outfile2 || !outfile3) { return; } // replace files with \n for (int i = 0; i < NUM_TIMES * 31; i ++) { outfile1 << "\n"; outfile2 << "\n"; outfile3 << "\n"; } } */