/*
$Log: Sio32Micro.cc,v $
Revision 1.5  1998/12/18 21:19:15  oreilly
*** empty log message ***

Revision 1.4  1997/10/03 09:33:04  oreilly
*** empty log message ***

Revision 1.3  97/08/12  14:57:34  14:57:34  oreilly (Thomas C. O'Reilly)
*** empty log message ***

Revision 1.2  97/06/03  13:04:45  13:04:45  oreilly (Thomas C. O'Reilly)
*** empty log message ***

Revision 1.1  97/05/06  16:30:55  16:30:55  oreilly (Thomas C. O'Reilly)
Initial revision

*/
#include <stdlib.h>
#include <Xm/Form.h>
#include <Xm/RowColumn.h>
#include <Xm/Label.h>
#include <Xm/PushB.h>
#include <Xm/Separator.h>
#include <Xm/Frame.h>
#include <Vk/VkQuestionDialog.h>
#include <Vk/VkErrorDialog.h>
#include "Sio32Micro.h"
#include "TiburonApp.h"
#include "DmErrno.h"
#include "parseUtils.h"
#include "DataRates.h"
#include "sio32Diag.h"

Sio32Micro::Sio32Micro(const char *name, const char *dmPrefix)
  : Micro(name, dmPrefix)
{
  sio32Dm = new Sio32Dm(name, dmPrefix);
  if (sio32Dm->error())
  {
    char errorBuf[errorMsgSize];
    sio32Dm->errorMsg(errorBuf);
    setError(errorBuf);
    return;
  }
}


Sio32Micro::~Sio32Micro()
{
}


MicroGui *Sio32Micro::gui(Widget parent, int period)
{
  Sio32MicroGui *gui = new Sio32MicroGui(_name, parent, this, period);
  gui->show();
  return gui;
}


MicroH2OTemp *Sio32Micro::h2OTemp(Widget parent, int period)
{
  Sio32H2OTemp *gui = new Sio32H2OTemp(_name, parent, this, period);
  gui->show();
  return gui;
}


MicroGf *Sio32Micro::gf(Widget parent, int period)
{
  Sio32Gf *gui = new Sio32Gf(_name, parent, this, period);
  gui->show();
  return gui;
}


Sio32H2OTemp::Sio32H2OTemp(const char *name, Widget parent, 
			       Sio32Micro *micro,
			       int period)
  : MicroH2OTemp(name, parent)
{
  if (error())
    return;
  
  /* Find alarm items */
  SystemAlarms *alarms = ((TiburonApp *)theApplication)->systemAlarms;
  
  char alarmName[MaxDmNameLen];
  char errorBuf[errorMsgSize];
  
  strcpy(alarmName, (const char *)micro->sio32Dm->dmPrefix());
  
  char *alarmNamePtr = alarmName + strlen(alarmName);

  strcpy(alarmNamePtr, TEMPERATURE_ALARM_DM);
  Alarm *tempAlarm;
  if ((tempAlarm = alarms->find(alarmName)) == NULL)
  {
    sprintf(errorBuf, 
	    "Sio32H2OTemp::Sio32H2OTemp() - couldn't find alarm \"%s\"",
	    alarmName);
    
    theErrorDialog->postAndWait(errorBuf);
  }

  Widget nameWidget =
    XtVaCreateManagedWidget(name, xmLabelWidgetClass, _row->baseWidget(),
			    NULL);			    

  _row->append(nameWidget);
  
  _temp = 
    new AlarmedInt16Text("temperature", _row->baseWidget(), "%-3.0f C", 
			 micro->sio32Dm->temperature, 
			 period, tempAlarm, Dynamic); 

  _row->append(_temp);
  
  Widget notApplicable;

  notApplicable =
    XtVaCreateManagedWidget("notApplicable", xmLabelWidgetClass, 
			    _row->baseWidget(), 
			    NULL);

  _row->append(notApplicable);

  notApplicable =
    XtVaCreateManagedWidget("notApplicable", xmLabelWidgetClass, 
			    _row->baseWidget(), 
			    NULL);

  _row->append(notApplicable);

  notApplicable =
    XtVaCreateManagedWidget("notApplicable", xmLabelWidgetClass, 
			    _row->baseWidget(), 
			    NULL);

  _row->append(notApplicable);

  _row->show();
}


Sio32H2OTemp::~Sio32H2OTemp()
{
}


Sio32Gf::Sio32Gf(const char *name, Widget parent, Sio32Micro *micro, 
		 int period)
  : MicroGf(name, parent)
{
  _localGf = 0;
  
  if (error())
    return;
  
  /* Find alarm items */
  SystemAlarms *alarms = ((TiburonApp *)theApplication)->systemAlarms;
  
  char alarmName[MaxDmNameLen];
  char errorBuf[errorMsgSize];
  
  strcpy(alarmName, (const char *)micro->sio32Dm->dmPrefix());
  
  char *alarmNamePtr = alarmName + strlen(alarmName);

  strcpy(alarmNamePtr, LOCAL_GND_FLT_ALARM_DM);
  Alarm *localGfAlarm;
  if ((localGfAlarm = alarms->find(alarmName)) == NULL)
  {
    sprintf(errorBuf, 
	    "Sio32Gf::Sio32Gf() - couldn't find alarm \"%s\"",
	    alarmName);
    
    theErrorDialog->postAndWait(errorBuf);
  }

  Widget nameWidget =
    XtVaCreateManagedWidget(name, xmLabelWidgetClass, _row->baseWidget(),
			    NULL);			    

  _row->append(nameWidget);

  _localGf = 
    new AlarmedInt16Text("localGf", _row->baseWidget(), "%-3.0f", 
			 micro->sio32Dm->localGfCurrent,
			 period, localGfAlarm, Dynamic); 

  _row->append(_localGf);

  Widget notApplicable = 
    XtVaCreateManagedWidget("notApplicable", xmLabelWidgetClass, 
			    _row->baseWidget(),
			    NULL);			    

  _row->append(notApplicable);

  _row->show();
}


Sio32Gf::~Sio32Gf()
{
  delete _localGf;
}


Sio32MicroGui::Sio32MicroGui(const char *name, Widget parent, 
			       Sio32Micro *micro, int period)
  : MbariMicroGui(name, parent, micro)
{
  _temp = 0;
  _localGf = 0;
  _gfSelfTest = 0;

  Sio32Dm *sio32Dm = micro->sio32Dm;
  
  /* Find alarm items */
  SystemAlarms *alarms = ((TiburonApp *)theApplication)->systemAlarms;
  
  char alarmName[MaxDmNameLen];
  char errorBuf[errorMsgSize];
  
  strcpy(alarmName, (const char *)micro->sio32Dm->dmPrefix());
  
  char *alarmNamePtr = alarmName + strlen(alarmName);

  strcpy(alarmNamePtr, TEMPERATURE_ALARM_DM);
  Alarm *tempAlarm;
  if ((tempAlarm = alarms->find(alarmName)) == NULL)
  {
    sprintf(errorBuf, 
	    "Sio32H2OTemp::Sio32H2OTemp() - couldn't find alarm \"%s\"",
	    alarmName);
    
    theErrorDialog->postAndWait(errorBuf);
  }

  strcpy(alarmName, (const char *)micro->sio32Dm->dmPrefix());
  
  alarmNamePtr = alarmName + strlen(alarmName);

  strcpy(alarmNamePtr, LOCAL_GND_FLT_ALARM_DM);
  Alarm *localGfAlarm;
  if ((localGfAlarm = alarms->find(alarmName)) == NULL)
  {
    sprintf(errorBuf, 
	    "Sio32Gf::Sio32Gf() - couldn't find alarm \"%s\"",
	    alarmName);
    
    theErrorDialog->postAndWait(errorBuf);
  }

  Widget label;
  TableRowForm *mgr;
  
  mgr = new TableRowForm("mgr", _baseWidget, 2);
  
  label = 
    XtVaCreateManagedWidget("tempLabel", xmLabelWidgetClass, 
			    mgr->baseWidget(),
			    NULL);
  
  mgr->append(label);
  
  _temp = 
    new AlarmedInt16Text("temperature", mgr->baseWidget(), "%-3.0f C",
			 sio32Dm->microTemp, period, tempAlarm, Dynamic);
  
  mgr->append(_temp);
  
  mgr->show();

  mgr = new TableRowForm("mgr", _baseWidget, 2);

  label = 
    XtVaCreateManagedWidget("localGfLabel", xmLabelWidgetClass, 
			    mgr->baseWidget(),
			    NULL);

  mgr->append(label);
  
  _localGf = 
    new AlarmedInt16Text("localGf", mgr->baseWidget(), "%-3.0f", 
			 micro->sio32Dm->localGfCurrent,
			 period, localGfAlarm, Dynamic); 

  mgr->append(_localGf);
  
  mgr->show();
  
  strcpy(alarmNamePtr, GND_FLT_TEST_STATUS_DM);  
  char cmdItemName[MaxDmNameLen];
  strcpy(cmdItemName, micro->sio32Dm->dmPrefix());
  strcat(cmdItemName, GND_FLT_TEST_DM);
  _gfSelfTest = new GfSelfTest("gfSelfTest", _baseWidget, 
			       cmdItemName, alarmName);
  
  _gfSelfTest->show();
}


Sio32MicroGui::~Sio32MicroGui()
{
  delete _temp;
  delete _localGf;
  delete _gfSelfTest;
}


void Sio32MicroGui::updateGui(DM_Item handle)
{
  MicroGui::updateGui();
}


