/***************************************************************************
 *   Copyright (C) 2007 by    *
 *      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "acsuhboard024.h"
#include <qtango.h>

	char tmBuffer[80];
/*
  timestamp() 
  send a timestamp to standard output
*/
void timestamp() 
{
	struct tm *mytime;
	time_t myTime_t;
	myTime_t = time(NULL);
	mytime = localtime(&myTime_t);
	strftime(tmBuffer, 79, "%Y-%m-%d %H:%M:%S ", mytime);
	return;
}

acsuhboard024::acsuhboard024(QWidget *parent) : QWidget(parent)
{
	// setup User Interface
	try {
		ui.setupUi(this);
	}
	catch (const Tango::DevFailed &e) {
		timestamp();
		cout << tmBuffer << "cannot setup UI" << endl;
	}
	catch (...) {
		timestamp();
		cout << tmBuffer << "default exception, cannot setup UI" << endl;
	}
	// connect to control access server
	try {
		// plc_server = new Tango::DeviceProxy("ken/plcaccess/0");
		plc_server = new Tango::DeviceProxy("f/access_control/safety");
	}
	catch (const Tango::DevFailed &e) {
		timestamp();
		cout << tmBuffer << "Fatal Error: Access Control Device Server not responding, the application will be terminated immediately" << endl;
		exit(1);
	}
	catch (...) {
		timestamp();
		cout << tmBuffer << "Fatal Error: Access Control Device Server not responding, the application will be terminated immediately" << endl;
		exit(1);
	}
	refresh();
	// init timer
	try {
		timer = new QTimer(this);
		timer->setInterval(1000);
		timer->setSingleShot(false);
		connect(timer, SIGNAL(timeout()), this, SLOT(refresh()));
		timer->start();
	}
	catch (const Tango::DevFailed &e) {
		timestamp();
		cout << tmBuffer << "cannot connect UI" << endl;
	}
	catch (...) {
		timestamp();
		cout << tmBuffer << "default exception, cannot setup UI" << endl;
	}
}

acsuhboard024::~acsuhboard024()
{
}    

int acsuhboard024::myReadAttribute(const char *attrName)
{
	try {
		myAttribute = plc_server->read_attribute(attrName);
		return 1;
	}
	catch (const Tango::DevFailed &e) {
		timestamp();
		cout << "error reading attribute: " << attrName << endl;
	}
	catch (...) {
		timestamp();
		cout << tmBuffer << "default exception reading attribute: " << attrName << endl;
	}
	return 0;
}

void acsuhboard024::refresh()
{
	vector<bool> stat;
	if (myReadAttribute("Undulator_board_disabled_024")) {
		try {
			myAttribute >> stat;
		}
		catch (const Tango::DevFailed &e) {
		}
		ui.checkBox_0->setCheckState(stat[0]? Qt::Checked: Qt::Unchecked);
		ui.checkBox_1->setCheckState(stat[1]? Qt::Checked: Qt::Unchecked);
		ui.checkBox_2->setCheckState(stat[2]? Qt::Checked: Qt::Unchecked);
		ui.checkBox_3->setCheckState(stat[3]? Qt::Checked: Qt::Unchecked);
	}
}