/* State Control Script
123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456*/
/*===================================-======================================*/
/*                          SYSTEM STATE CONTROL                            */
/*-----------------------------------=--------------------------------------*/
/* AUTHORs:        S.G.Basiladze

   DESIGN ISSUEs:  This Script provides the Local and Global Control of the
                   System States functons and Transition functions

   MODIF_HISTORY:  01.03.2001 - first version
                   09.11.2002 -  2-nd version, DAQ Control is added
                   07.05.2003 -  3-rd version, DDC Control is added
                   04.07.2003 -  4-th version, for MoPS (and Barr) subsystem
                   28.09.2003 -  5-th version, States Control by Script
                   12.10.2003 -  6-th version, Off,Stb,On+Conf+Init States
/*--------------------------------------------------------------------------*/
/* SYSTEM CONTROL DATAPOINTS:
                   Host.Stat  - Desirable   (new) system state;
                   Syst.Stat  - Current (and old) system state;
                   Next.Stat  - Next step in the Syst->->->Host sequence;
                   Busy.Stat  - synchronisation, is using in 2 cases
                                a) for disabling of Transition function
                                   (for instance, if Syst_Off->->Host_Mon,
                                   then Next_ConfFunction will be skipped);                                   
                                b) for waiting when Transition will be made.*/
/* The *.Stat DataPoints bits assignment:

        DCS Mode : 0x1* - Global Control ("on write" - from DAQ),
                   0x2* - Local  Control ("on event" - control button click).

        DCS State: 0x20 – Init/Out,     MPS State: 0x00 – Init/Out,   Test:
                   0x24 – Configured,              0x10 - Configured, (0x20)
                   0x28 – Monitoring,              0x14 – Det_Off,    (0x24)
                   0x1C – DAQ control.             0x18 – Det_Stb,    (0x28)
                                                   0x1C - Det_On,     (0x2C)

      Status bits:   00 - Normal        Colours  : Green[Local] or Cyan[Glob]
                     10 - Warning                  Yellow
                     01 - Alarm                    Red
                     11 - reserved (Disabled)      Gray
  ===================================-======================================*/

main()
{ int    flag, tout, int1;
  string stri, estr;
  
  tout = 30 * 60;                    /* TimeOut is 30 minutes               */

  dpSet("SCTmops1:Host.Stat:_original.._value", 0x0);    /* Local Off State */
  dpSet("SCTmops1:Next.Stat:_original.._value", 0x0);    /* Local Off State */
  dpSet("SCTmops1:Syst.Stat:_original.._value", 0x0);    /* Local Off State */
  dpSet("SCTmops1:Conf.Indx:_original.._value", 0);      /* Init Script Flag*/
  dpSet("SCTmops1:Conf.Stat:_original.._value", 0x0);    /* Init Script Stat*/
  dpSet("SCTmops1:Conf.Data:_original.._value", 0.0);
  delay(1);
  dpSet("SCTmops1:Conf.Indx:_original.._value", 1);      /* Set Script Flag */
/**/
  sprintf(stri,"\n  STATES CONTROL SCRIPT IS WAITNG FOR CONFIG PANEL... \n");
  Debug(stri);                                                     /* Debug */
  int1=0; flag=0; while(flag != 1)   /* Waiting for some Panel will be open */
  { delay(1);
    dpGet("SCTmops1:Conf.Numb:_original.._value", flag);
    int1++; if(int1 >= tout)
    { sprintf(estr,
"\n ERROR: TimeOut '%d sec' - CONFIG PANEL Waiting ! RESTART PROJECT OR CONTROL SCRIPT \n",
                    tout);
      Debug(estr);
      dpSet("SCTmops1:Eror.Stat:_original.._value",0x1); /* Red Colour      */
      sprintf(estr,"\n TimeOut '%d sec' - CONFIG PANEL Waiting ", tout);
      ErrorStop(estr," RESTART PROJECT OR CONTROL SCRIPT ");
    }
  } delay(2);
  dpConnect("StateFunction"       ,"SCTmops1:Host.Stat:_original.._value");
  dpConnect("InitConfTransition"  ,"SCTmops1:Next.Stat:_original.._value");
  dpConnect("ConfOutTransition"   ,"SCTmops1:Next.Stat:_original.._value");
  dpConnect("ConfOffTransition"   ,"SCTmops1:Next.Stat:_original.._value");
  dpConnect("OffConfTransition"   ,"SCTmops1:Next.Stat:_original.._value");
  dpConnect("OffStbTransition"    ,"SCTmops1:Next.Stat:_original.._value");
  dpConnect("StbOnTransition"     ,"SCTmops1:Next.Stat:_original.._value");
  dpConnect("OnStbTransition"     ,"SCTmops1:Next.Stat:_original.._value");
  dpConnect("RestartStateFunction","SCTmops1:Conf.Numb:_original.._value");
  Debug("\n  STATES CONTROL SCRIPT STARTED... \n");
}

 StateFunction(string Dapo, int Stat) 
 { int    stat, init, mode, flag;
   string estr, mstr;                /* Error and Monitor STRings           */
   
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   init = stat & 0x3C;                  mode = stat & 0x30;
/*
   sprintf(mstr," MakeState: Mode=%Xh, Stat=%Xh ", mode, stat & 0xC);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2); /* Debug */
     if((init != 0x0) && (mode != 0x10) && (mode != 0x20))
   { sprintf(estr," INCORRECT CONTROL MODE CODE, HOST_MODE = %Xh ", mode);
     ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
   } else
   {   switch(stat & 0x3C)
     { case   0x0 : InitFunction();    break;
       case   0x10: ConfigFunction();  break;
       case   0x20: ConfigFunction();  break;            /* Manual test     */
       case   0x14: OffFunction();     break;
       case   0x24: OffFunction();     break;            /* Manual test     */
       case   0x18: StandbyFunction(); break;
       case   0x28: StandbyFunction(); break;            /* Manual test     */
       case   0x1C: OnFunction();      break;
       case   0x2C: OnFunction();      break;            /* Manual test     */
       default:
               sprintf(estr," INCORRECT CONTROL STATE CODE = %X", stat & 0xC);
               ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
     } delay(2);
     dpGet("SCTmops1:Conf.Indx:_original.._value", flag);
       if(flag == 1)
     { dpSet("SCTmops1:Conf.Indx:_original.._value", 2); /* Config is "used"*/
     }
   }
/*
   sprintf(mstr," StateMade: Mode=%Xh, Stat=%Xh ", mode, stat & 0xC);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2); /* Debug */
 }
 
 RestartStateFunction(string Dapo, int Stat) 
 { int    panl, scri;                /* Panel and Script handshake flags    */
   string mstr;
 
   dpGet("SCTmops1:Conf.Indx:_original.._value", scri);
   dpGet("SCTmops1:Conf.Numb:_original.._value", panl);
/*
   sprintf(mstr," Restart: Script_Flag=%d, Panel_Flag=%d ", scri, panl);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(1); /* Debug */
     if((scri > 1) && (panl == 1))
   { dpSet("SCTmops1:Host.Stat:_original.._value", 0x0); /* Local Off State */
     dpSet("SCTmops1:Next.Stat:_original.._value", 0x0); /* Local Off State */
     dpSet("SCTmops1:Syst.Stat:_original.._value", 0x0); /* Local Off State */
     dpSet("SCTmops1:Conf.Stat:_original.._value", 0x0); /* Init Script Stat*/
     dpSet("SCTmops1:Conf.Data:_original.._value", 0.0);
     dpSet("SCTmops1:Conf.Indx:_original.._value", 1);   /* Set Script Flag */
     Debug("\n  STATES CONTROL SCRIPT RESTARTED... \n");
     StateFunction("", 0x0);
   }
 }

 InitFunction()
 { int    syss, stat, stus, busy;    /* SYStem STATe, STatUS, BUSY Stat     */
   int    tout, eror;                /* TimeOUT (sec), system ERrOR         */
   int    used, int1, int2;
   string snam, estr, mstr, stri;    /* Syst NAMe, Error and Monitor STRings*/
 
   tout = 180;
   dpGet("SCTmops1:Syst.Stat:_original.._value", stat);
/*
   dpGet("SCTmops1:Host.Stat:_original.._value", int1);
   dpGet("SCTmops1:Next.Stat:_original.._value", stus);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
   sprintf(mstr," MakeOff: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                           int1, stus, stat, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2); /* Debug */
   syss = stat & 0xC;                   stus = stat & 0x5500033;
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
     if(((stus & 0x30) > 0) && ((stat & 0x30) == 0))
   {   switch(syss)
     { case 0xC:                     /* Current      State is 'Det_On'      */
                dpSet("SCTmops1:Next.Stat:_original.._value", 0x8 | stus);
                delay(1);
                busy=1; while((busy > 0) && (tout > 0))
                { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                  delay(1); tout--;
                } if(tout <= 0)
                { sprintf(estr," TimeOut Waiting for Det_Stb State"); 
                  ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
                }
                syss = 0x8 | stus;   /* Intermediate State is 'Det_Stb'     */
                dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
                delay(1);
       case 0x8:                     /* Current      State is 'Det_Stb'     */
                dpSet("SCTmops1:Next.Stat:_original.._value", 0x4 | stus);
                delay(1);
                busy=1; while((busy > 0) && (tout > 0))
                { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                  delay(1); tout--;
                } if(tout <= 0)
                { sprintf(estr," TimeOut Waiting for Det_Off State"); 
                  ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
                }
                syss = 0x4 | stus;   /* Intermediate State is 'Det_Off'     */
                dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
                delay(1);
       case 0x4:                     /* Current      State is 'Det_Off'     */
                dpSet("SCTmops1:Next.Stat:_original.._value", stus & 0x3);
                delay(1);
                busy=1; while((busy > 0) && (tout > 0))
                { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                  delay(1); tout--;
                } if(tout <= 0)
                { sprintf(estr," TimeOut Waiting for Config State"); 
                  ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
                }
                syss = stus & 0x3;   /* Final State is 'Out'                */
                dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
                break;
       case 0x0:                     /* Current      State is 'Config'      */
                dpSet("SCTmops1:Next.Stat:_original.._value",  stus & 0x3);
                delay(1);
                busy=1; while((busy > 0) && (tout > 0))
                { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                  delay(1); tout--;
                } if(tout <= 0)
                { sprintf(estr," TimeOut Waiting for Out State"); 
                  ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
                }
                syss = 0x0 | (stus & 0x3);/* Final State is 'Out'           */
                dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
                break;
       default : ;
     }
   }
   dpGet("SCTmops1:Eror.Stat:_original.._value", eror);
     if((eror & 0x1) > 0)
   { dpGet("SCTmops1:Eror.Name:_original.._value", estr);
     ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
   } else
   { dpGet("SCTmops1:Host.Stat:_original.._value", stat);
     dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
     dpGet("SCTmops1:Conf.Indx:_original.._value", used);
/*
     dpGet("SCTmops1:Next.Stat:_original.._value", stus);
     dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
     sprintf(mstr," OffMade: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                             stat, stus, syss, busy);
     dpSet("SCTmops1:Moni.Name:_original.._value", mstr); delay(2);/* Debug */
       if(((stat & 0xC) == 0) && ((syss & 0xC) == 0) && (used == 2))
     {
///  ...                             /* Make your ShutDown job in between   */
         if(1)
       { ShutDown();
       }
///  ...                             /* Make your ShutDown job in between   */
       dpSet("SCTmops1:Moni.Stat:_original.._value",0x2);/* Yellow colour   */
       sprintf(mstr," BY...!");
       dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
       dpSet("SCTmops1:Busy.Numb:_original.._value", 1); /* Set PanlOff Flag*/
         while(used > 0)             /* Handshake for 'PanelOff'            */
       { delay(1);
         dpGet("SCTmops1:Conf.Numb:_original.._value", used);
       } dpSet("SCTmops1:Conf.Indx:_original.._value",3);/* Restart mode    */
     } else
     { dpSet("SCTmops1:Busy.Stat:_original.._value", 0x4);
     
///  ...                             /* Make your StartUp job in between    */
         if(1)
       { StartUp();
       }
///  ...                             /* Make your StartUp job in between    */
       dpSet("SCTmops1:Moni.Stat:_original.._value",0x0);/* Green Background*/
       dpGet("SCTmops1:Conf.Name:_original.._value", snam);
       sprintf(mstr," %s IS WAITING...", snam);
       dpSet("SCTmops1:Moni.Name:_original.._value", mstr);
       dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
     }
   }
 }
 
 InitConfTransition(string Dapo, int Stat)
 { int    syss, next, stat, busy;
   string mstr;

   dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
   dpGet("SCTmops1:Next.Stat:_original.._value", next);
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
/*
   sprintf(mstr," InitConf: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                           stat, next, syss, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);            /* Debug */
     if(((syss & 0x3C) == 0x0) &&
       (((next & 0x3C) == 0x10) || ((next & 0x3C) == 0x20)) && (!busy))
   { dpSet("SCTmops1:Busy.Stat:_original.._value", 0x2);
///  ...                             /* Make your Init->Conf job in between */

       if((stat & 0x3C) == 0x20)
     { saveELMBloading();
     }

       if(stat & 0x400000)           /* New Constants for 'Det_On' State    */
     { //LoadDataPoints(0xC);          /* Load DaPo_On                        */
//     ...
       sprintf(mstr," 'ON'_LOADED DPs ARE SENT TO CRATE CONTROLLERs... ");
       dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
     }
       if(stat & 0x100000)           /* New Constants for 'Det_Stb' State   */
     { //LoadDataPoints(0x8);          /* Load DaPo_Stb                       */
//     ...
       sprintf(mstr," 'STB'_LOADED DPs ARE SENT TO CRATE CONTROLLERs... ");
       dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
     }
       if(stat & 0x40000)            /* New Constants for 'Det_Off' State   */
     { //MakeConfigs(0x4, 0x8);        /* Load DaPo_Off, create OPC-configs   */
//     ...
       sprintf(mstr," 'OFF'_LOADED DPs ARE SENT TO CRATE CONTROLLERs... ");
       dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
     } else
     {   if(stat & 0x10000)          /* New Constants for 'Config' State    */
       { //MakeConfigs(0x4, 0x8);      /* Load DaPo_Off, create OPC-configs   */
         sprintf(mstr," DPs ARE 'OFF'_LOADED & OPC CONFIGURED... ");
         dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
       }
     }
///  ...                             /* Make your Init->Conf job in between */
/*
     delay(600);                                                   /* Debug */
     dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
   }
 }

 ConfOutTransition(string Dapo, int Stat)
 { int    syss, next, stat, busy;
   string mstr;

   dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
   dpGet("SCTmops1:Next.Stat:_original.._value", next);
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
/*
   sprintf(mstr," ConfOut: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                           stat, next, syss, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr); delay(3);  /* Debug */
     if((((syss & 0x3C) == 0x10) || ((syss & 0x3C) == 0x20)) &&
         ((next & 0x3C) == 0x0) && (!busy))
   { dpSet("SCTmops1:Busy.Stat:_original.._value", 0x2);
///  ...                             /* Make your Conf->Out job in between  */

///  ...                             /* Make your Conf->Out job in between  */
/*
     delay(60);                                                    /* Debug */
     dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
   }
 }

 ConfigFunction()
 { int    syss, stat, stus, busy;    /* SYStem STATe, STatUS, BUSY Stat     */
   int    tout, eror;                /* TimeOUT (sec), system ERrOR         */
   int    int1, int2;
   string snam, estr, mstr, stri;    /* Syst NAMe, Error and Monitor STRings*/

   tout = 300;  
   dpGet("SCTmops1:Syst.Stat:_original.._value", stat);
/*
   dpGet("SCTmops1:Host.Stat:_original.._value", int1);
   dpGet("SCTmops1:Next.Stat:_original.._value", stus);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
   sprintf(mstr," MakeConf: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                            int1, stus, stat, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2); /* Debug */
   stus = stat & 0x3;                   syss = stat;
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   stus = stus | (stat & 0x550030);  /* Adding Manual or External mode      */
   stat = syss & 0x3C;                  syss = stat & 0xC;
     if(stat == 0x0)
   {            dpSet("SCTmops1:Next.Stat:_original.._value", 0x0 | stus);
                delay(1); 
                busy=1; while((busy > 0) && (tout > 0))
                { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                  delay(1); tout--;
                } if(tout <= 0)
                { sprintf(estr," TimeOut Waiting for Config State"); 
                  ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
                }
                syss = 0x0 | stus;   /* Final State is 'Config'             */
                dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
   } else
   {  switch(syss)
     { case 0xC:                     /* Current State is 'Det_On'           */
                dpSet("SCTmops1:Next.Stat:_original.._value", 0x8 | stus);
                delay(1);
                busy=1; while((busy > 0) && (tout > 0))
                { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                  delay(1); tout--;
                } if(tout <= 0)
                { sprintf(estr," TimeOut Waiting for Det_Stb State"); 
                  ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
                }
                syss = 0x8 | stus;   /* Intermediate State is 'Det_Stb'     */
                dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
                delay(1);
        case 0x8:                    /* Current      State is 'Det_Stb'     */
                dpSet("SCTmops1:Next.Stat:_original.._value", 0x4 | stus);
                delay(1);
                busy=1; while((busy > 0) && (tout > 0))
                { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                  delay(1); tout--;
                } if(tout <= 0)
                { sprintf(estr," TimeOut Waiting for Det_Off State"); 
                  ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
                }
                syss = 0x4 | stus;   /* Intermediate State is 'Det_Off'     */
                dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
                delay(1);
        case 0x4:                    /* Current      State is 'Det_Off'     */
                dpSet("SCTmops1:Next.Stat:_original.._value", 0x0 | stus);
                delay(1);
                busy=1; while((busy > 0) && (tout > 0))
                { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                  delay(1); tout--;
                } if(tout <= 0)
                { sprintf(estr," TimeOut Waiting for Config State"); 
                  ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
                }
                syss = 0x0 | stus;   /* Final State is 'Config'             */
                dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
                break;
       default : ;
     }
   }
   dpGet("SCTmops1:Eror.Stat:_original.._value", eror);
     if((eror & 0x1) > 0)
   { dpGet("SCTmops1:Eror.Name:_original.._value", estr);
     ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
   } else
   { dpGet("SCTmops1:Syst.Stat:_original.._value", stat);
     stat = stat & 0x7FFEFFFF;       /* Reset 'Config' bit in SystStat      */
     dpSet("SCTmops1:Syst.Stat:_original.._value", stat);
///  ...                             /* Make your 'Config' job in between   */

///  ...                             /* Make your 'Config' job in between   */
     dpGet("SCTmops1:Conf.Name:_original.._value", snam);
     sprintf(mstr," %s IS WAITING...", snam);
     dpSet("SCTmops1:Moni.Name:_original.._value", mstr);
/*
     dpGet("SCTmops1:Host.Stat:_original.._value", stat);
     dpGet("SCTmops1:Next.Stat:_original.._value", stus);
     dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
     dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
     sprintf(mstr," ConfMade: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                              stat, stus, syss, busy);
     dpSet("SCTmops1:Moni.Name:_original.._value", mstr); delay(2);/* Debug */
   }
 }
 
 ConfOffTransition(string Dapo, int Stat)
 { int    syss, next, stat, busy;
   string mstr;

   dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
   dpGet("SCTmops1:Next.Stat:_original.._value", next);
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
/*
   sprintf(mstr," ConfOff: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                           stat, next, syss, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);            /* Debug */
     if((((syss & 0x3C) == 0x10) || ((syss & 0x3C) == 0x20)) &&
         ((next & 0xC) == 0x4) && (!busy))
   { dpSet("SCTmops1:Busy.Stat:_original.._value", 0x4);
///  ...                             /* Make your Config->Off job in between*/

       if((stat & 0xC) == 0x4) 
     { LoadDataPoints(0x4);          /* Load DaPo->Off from 'Para' confFile */ 
       sprintf(mstr," DPs ARE 'OFF'_LOADED... ");
         if((stat & 0x3C) == 0x24)
       { detOFF();
         sprintf(mstr," DPs & ELMB ARE 'OFF'_LOADED... ");
       }
       dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
     }
       if(syss & 0x40000)            /* New Constants for 'Det_OFF' State   */
     { //LoadDataPoints(0x4);          /* Load DaPo->Off from 'Para' confFile */
       //sprintf(mstr," DPs ARE 'OFF'_LOADED... ");
       //dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
     }
///  ...                             /* Make your Config->Off job in between*/
/*
     delay(60);                                                    /* Debug */
     dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
   }
 }

 OffConfTransition(string Dapo, int Stat)
 { int    syss, next, stat, busy;
   string mstr;

   dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
   dpGet("SCTmops1:Next.Stat:_original.._value", next);
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
/*
   sprintf(mstr," OffConf: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                           stat, next, syss, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);            /* Debug */
     if(((syss & 0xC) == 0x4) &&
       (((next & 0x3C) == 0x10) || ((next & 0x3C) == 0x20)) && (!busy))
   { dpSet("SCTmops1:Busy.Stat:_original.._value", 0x4);
///  ...                             /* Make your Off->Config job in between*/
       if((stat & 0x3C) == 0x20)
     { saveELMBloading();
     }
///  ...                             /* Make your Off->Config job in between*/
/*
     delay(60);                                                    /* Debug */
     dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
   }
 }

 OffFunction()
 { int    syss, stat, stus, busy;    /* SYStem STATe, STatUS, BUSY Stat     */
   int    tout, eror;                /* TimeOUT (sec), system ERrOR         */
   int    int1, int2;
   string snam, estr, mstr, stri;    /* Syst NAMe, Error and Monitor STRings*/

   tout = 180;
   dpGet("SCTmops1:Syst.Stat:_original.._value", stat);
/*
   dpGet("SCTmops1:Host.Stat:_original.._value", int1);
   dpGet("SCTmops1:Next.Stat:_original.._value", stus);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
   sprintf(mstr," MakeStb: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                           int1, stus, stat, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2); /* Debug */
   syss = stat & 0xC;                   stus = stat & 0x5500033;
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   stus = stus | (stat & 0x30);      /* Adding Manual or External mode      */
     switch(syss)
   { case 0x0:                       /* Current State is 'Config' or 'Init' */
              dpSet("SCTmops1:Next.Stat:_original.._value", 0x4 | stus);
              delay(1);
              busy=1; while((busy > 0) && (tout > 0))
              { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                delay(1); tout--;
              } if(tout <= 0)
              { sprintf(estr," TimeOut Waiting for 'Det_Off' State"); 
                ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
              }
              syss = 0x4 | stus;     /* Final State is 'Det_Off'            */
              dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
              break;
     case 0x4:                       /* Current State is 'Det_Off'          */
              break;
     case 0xC:                       /* Current State is 'Det_On'           */
              dpSet("SCTmops1:Next.Stat:_original.._value", 0x8 | stus);
              delay(1);
              busy=1; while((busy > 0) && (tout > 0))
              { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                delay(1); tout--;
              } if(tout <= 0)
              { sprintf(estr," TimeOut Waiting for Det_Stb State"); 
                ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
              }
              syss = 0x8 | stus;     /* Intermediate State is 'Det_Stb'     */
              dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
     case 0x8:                       /* Current      State is 'Det_Stb'     */
              dpSet("SCTmops1:Next.Stat:_original.._value", 0x4 | stus);
              delay(1);
              busy=1; while((busy > 0) && (tout > 0))
              { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                delay(1); tout--;
              } if(tout <= 0)
              { sprintf(estr," TimeOut Waiting for Det_Stb State"); 
                ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
              }
              syss = 0x4 | stus;     /* Final State is 'Det_Off'            */
              dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
              break;
     default : ;
   }
   dpGet("SCTmops1:Eror.Stat:_original.._value", eror);
     if((eror & 0x1) > 0)
   { dpGet("SCTmops1:Eror.Name:_original.._value", estr);
     ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
   } else
   { dpGet("SCTmops1:Syst.Stat:_original.._value", stat);
     stat = stat & 0x7FFBFFFF;       /* Reset 'New_OFF' bit in SystStat     */
     dpSet("SCTmops1:Syst.Stat:_original.._value", stat);
///  ...                             /* Make your 'Standby' job in between  */

///  ...                             /* Make your 'Standby' job in between  */
     dpGet("SCTmops1:Conf.Name:_original.._value", snam);
     sprintf(mstr," %s IS WAITING...", snam);
     dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(1);
     dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
/*
     dpGet("SCTmops1:Host.Stat:_original.._value", stat);
     dpGet("SCTmops1:Next.Stat:_original.._value", stus);
     dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
     dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
     sprintf(mstr," StbMade: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                             stat, stus, syss, busy);
     dpSet("SCTmops1:Moni.Name:_original.._value", mstr); delay(2);/* Debug */
   }
 }
 
 OffStbTransition(string Dapo, int Stat)
 { int    syss, next, stat, busy;
   string mstr;

   dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
   dpGet("SCTmops1:Next.Stat:_original.._value", next);
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
/*
   sprintf(mstr," OffStb: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                         stat, next, syss, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);            /* Debug */
     if(((syss & 0xC) == 0x8) && ((next & 0xC) == 0xC) && (!busy))
   { dpSet("SCTmops1:Busy.Stat:_original.._value", 0x8);
   
///  ...                             /* Make your Off->Config job in between*/
       if((stat & 0xC) == 0x8) 
     { LoadDataPoints(0x8);          /* Load DaPo->Stb from 'Para' confFile */ 
       sprintf(mstr," DPs ARE 'STB'_LOADED... ");
         if((stat & 0x3C) == 0x28)
       { detSTB();
         sprintf(mstr," DPs & ELMB ARE 'STB'_LOADED... ");
       }
       dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
     }
       if(syss & 0x100000)           /* New Constants for 'Det_STB' State   */
     { //LoadDataPoints(0x8);          /* Load DaPo->Stb from 'Para' confFile */
       sprintf(mstr," DPs ARE 'STB'_LOADED... ");
       dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
     }
///  ...                             /* Make your Off->Config job in between*/
/*
     delay(70);                                                    /* Debug */
     dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
   }
 }
 
 StbOffTransition(string Dapo, int Stat)
 { int    syss, next, stat, busy;
   string mstr;

   dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
   dpGet("SCTmops1:Next.Stat:_original.._value", next);
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
/*
   sprintf(mstr," StbOff: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                         stat, next, syss, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);            /* Debug */
     if(((syss & 0xC) == 0xC) && ((next & 0xC) == 0x8) && (!busy))
   { dpSet("SCTmops1:Busy.Stat:_original.._value", 0x8);
   
///  ...                             /* Make your Off->Config job in between*/
///  ...                             /* Make your Off->Config job in between*/
/*
     delay(70);                                                    /* Debug */
     dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
   }
 }

 StandbyFunction()
 { int    syss, stat, stus, busy;    /* SYStem STATe, STatUS, BUSY Stat     */
   int    tout, eror;                /* TimeOUT (sec), system ERrOR         */
   int    int1, int2;
   string snam, estr, mstr, stri;    /* Syst NAMe, Error and Monitor STRings*/

   tout = 180;
   dpGet("SCTmops1:Syst.Stat:_original.._value", stat);
/*
   dpGet("SCTmops1:Host.Stat:_original.._value", int1);
   dpGet("SCTmops1:Next.Stat:_original.._value", stus);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
   sprintf(mstr," MakeStb: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                           int1, stus, stat, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2); /* Debug */
   syss = stat & 0xC;                   stus = stat & 0x5500033;
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   stus = stus | (stat & 0x30);      /* Adding Manual or External mode      */
     switch(syss)
   { case 0x0:                       /* Current State is 'Conf' or 'Init'   */
              dpSet("SCTmops1:Next.Stat:_original.._value", 0x4 | stus);
              delay(1);
              busy=1; while((busy > 0) && (tout > 0))
              { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                delay(1); tout--;
              } if(tout <= 0)
              { sprintf(estr," TimeOut Waiting for Det_Off State"); 
                ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
              }
              syss = 0x4 | stus;     /* Intermediate State is 'Det_Off'     */
              dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
     case 0x4:                       /* Current      State is 'Det_Off'     */
              dpSet("SCTmops1:Next.Stat:_original.._value", 0x8 | stus);
              delay(1);
              busy=1; while((busy > 0) && (tout > 0))
              { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                delay(1); tout--;
              } if(tout <= 0)
              { sprintf(estr," TimeOut Waiting for Det_Stb State"); 
                ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
              }
              syss = 0x8 | stus;     /* Final State is 'Det_Stb'            */
              dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
              break;
     case 0x8:                       /* Current State is 'Det_Stb'          */
              break;
     case 0xC:                       /* Current State is 'Det_On'           */
              dpSet("SCTmops1:Next.Stat:_original.._value", 0x8 | stus);
              delay(1);
              busy=1; while((busy > 0) && (tout > 0))
              { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                delay(1); tout--;
              } if(tout <= 0)
              { sprintf(estr," TimeOut Waiting for det_Stb State"); 
                ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
              }
              syss = 0x8 | stus;     /* Final State is 'Det_Stb'            */
              dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
              break;
     default : ;
   }
   dpGet("SCTmops1:Eror.Stat:_original.._value", eror);
     if((eror & 0x1) > 0)
   { dpGet("SCTmops1:Eror.Name:_original.._value", estr);
     ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
   } else
   { dpGet("SCTmops1:Syst.Stat:_original.._value", stat);
     stat = stat & 0x7FEFFFFF;       /* Reset 'New_STB' bit in SystStat     */
     dpSet("SCTmops1:Syst.Stat:_original.._value", stat);
///  ...                             /* Make your 'Standby' job in between  */

///  ...                             /* Make your 'Standby' job in between  */
     dpGet("SCTmops1:Conf.Name:_original.._value", snam);
     sprintf(mstr," %s IS WAITING...", snam);
     dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(1);
     dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
/*
     dpGet("SCTmops1:Host.Stat:_original.._value", stat);
     dpGet("SCTmops1:Next.Stat:_original.._value", stus);
     dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
     dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
     sprintf(mstr," StbMade: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                             stat, stus, syss, busy);
     dpSet("SCTmops1:Moni.Name:_original.._value", mstr); delay(2);/* Debug */
   }
 }
 
 StbOnTransition(string Dapo, int Stat)
 { int    syss, next, stat, busy;
   string mstr;

   dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
   dpGet("SCTmops1:Next.Stat:_original.._value", next);
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
/*
   sprintf(mstr," StbOn: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                         stat, next, syss, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);            /* Debug */
     if(((syss & 0xC) == 0x8) && ((next & 0xC) == 0xC) && (!busy))
   { dpSet("SCTmops1:Busy.Stat:_original.._value", 0x8);
///  ...                             /* Make your Off->Config job in between*/
       if((stat & 0xC) == 0xC) 
     { LoadDataPoints(0xC);          /* Load DaPo->On from 'Para' confFile */ 
       sprintf(mstr," DPs ARE 'ON'_LOADED... ");
         if((stat & 0x3C) == 0x2C)
       { detON();
         sprintf(mstr," DPs & ELMB ARE 'ON'_LOADED... ");
       }
       dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
     }
       if(syss & 0x400000)           /* New Constants for 'Det_ON' State    */
     { //LoadDataPoints(0xC);          /* Load DaPo->Stb from 'Para' confFile */
       sprintf(mstr," DPs ARE 'ON'_LOADED... ");
       dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
     }
///  ...                             /* Make your Off->Config job in between*/
/*
     delay(60);                                                    /* Debug */
     dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
   }
 }
 
 OnStbTransition(string Dapo, int Stat)
 { int    syss, next, stat, busy;
   string mstr;

   dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
   dpGet("SCTmops1:Next.Stat:_original.._value", next);
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
/*
   sprintf(mstr," OnStb: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                         stat, next, syss, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);            /* Debug */
     if(((syss & 0xC) == 0xC) && ((next & 0xC) == 0x8) && (!busy))
   { dpSet("SCTmops1:Busy.Stat:_original.._value", 0x8);
///  ...                             /* Make your Off->Config job in between*/

///  ...                             /* Make your Off->Config job in between*/
/*
     delay(60);                                                    /* Debug */
     dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
   }
 }

 OnFunction()
 { int    syss, stat, stus, busy;    /* SYStem STATe, STatUS, BUSY Stat     */
   int    tout, eror;                /* TimeOUT (sec), system ERrOR         */
   int    int1, int2;
   int    pmon, scan, simu, wait, host;   /* Period of MONitoring           */
   string snam, estr, mstr, stri;    /* Syst NAMe, Error and Monitor STRings*/

   tout = 180;
   dpGet("SCTmops1:Syst.Stat:_original.._value", stat);
/*
   dpGet("SCTmops1:Host.Stat:_original.._value", int1);
   dpGet("SCTmops1:Next.Stat:_original.._value", stus);
   dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
   sprintf(mstr," MakeOn: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                          int1, stus, stat, busy);
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2); /* Debug */
   syss = stat & 0xC;                   stus = stat & 0x5500033;
   dpGet("SCTmops1:Host.Stat:_original.._value", stat);
   stus = stus | (stat & 0x30);      /* Adding Manual or External mode      */
     switch(syss)
   { case 0x0:                       /* Current State is 'Conf' or 'Init'   */
              dpSet("SCTmops1:Next.Stat:_original.._value", 0x4 | stus);
              delay(1);
              busy=1; while((busy > 0) && (tout > 0))
              { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                delay(1); tout--;
              } if(tout <= 0)
              { sprintf(estr," TimeOut Waiting for Det_Off State"); 
                ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
              }
              syss = 0x4 | stus;     /* Intermediate State is 'Det_Off'     */
              dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
              delay(1);
     case 0x4:                       /* Current      State is 'Det_Off'     */
              dpSet("SCTmops1:Next.Stat:_original.._value", 0x8 | stus);
              delay(1);
              busy=1; while((busy > 0) && (tout > 0))
              { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                delay(1); tout--;
              } if(tout <= 0)
              { sprintf(estr," TimeOut Waiting for Det_Stb State"); 
                ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
              }
              syss = 0x8 | stus;     /* Intermediate State is 'Det_Stb'     */
              dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
              dpGet("SCTmops1:Syst.Stat:_original.._value", stat);
              stat = stat & 0x7FEFFFFF;   /* Reset 'New_STB' bit in SystStat*/
              dpSet("SCTmops1:Syst.Stat:_original.._value", stat);
              stus = stus & 0x7FEFFFFF;   /* Reset 'New_STB' bit in SystStat*/
              delay(1);
     case 0x8:                       /* Current      State is 'Det_Stb'     */
              dpSet("SCTmops1:Next.Stat:_original.._value", 0xC | stus);
              delay(1);
              busy=1; while((busy > 0) && (tout > 0))
              { dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
                delay(1); tout--;
              } if(tout <= 0)
              { sprintf(estr," TimeOut Waiting for On State"); 
                ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
              }
              syss = 0xC | stus;     /* Final State is 'Det_On'             */
              dpSet("SCTmops1:Syst.Stat:_original.._value", syss);
     case 0xC:                       /* Current      State is 'Det_On'      */
              break;
     default : ;
   }
   dpGet("SCTmops1:Eror.Stat:_original.._value", eror);
     if((eror & 0x1) > 0)
   { dpGet("SCTmops1:Eror.Name:_original.._value", estr);
     ErrorStop(estr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
   } else
   { dpGet("SCTmops1:Syst.Stat:_original.._value", stat);
     stat = stat & 0x7FBFFFFF;       /* Reset 'New_ON' bit in SystStat      */
     dpSet("SCTmops1:Syst.Stat:_original.._value", stat);
///  ...                             /* Make your start 'On' job in between */

///  ...                             /* Make your start 'On' job in between */
     dpGet("SCTmops1:Conf.Name:_original.._value", snam);
     sprintf(mstr," %s IS STARTING...", snam);
     dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
     dpSet("SCTmops1:Busy.Stat:_original.._value", 0xC);
/*
     dpGet("SCTmops1:Host.Stat:_original.._value", stat);
     dpGet("SCTmops1:Next.Stat:_original.._value", stus);
     dpGet("SCTmops1:Syst.Stat:_original.._value", syss);
     dpGet("SCTmops1:Busy.Stat:_original.._value", busy);
     sprintf(mstr," OnMade: Host=%Xh, Next=%Xh, Syst=%Xh, Busy=%Xh",
                            stat, stus, syss, busy);
     dpSet("SCTmops1:Moni.Name:_original.._value", mstr); delay(2);/* Debug */
       while(1)
     { dpGet("SCTmops1:Host.Stat:_original.._value", stat);
         if((stat & 0xC) != 0xC)
       { sprintf(mstr," STOP...");
         dpSet("SCTmops1:Moni.Name:_original.._value", mstr);
         break;
       } delay(1);
     } dpSet("SCTmops1:Busy.Stat:_original.._value", 0x0);
   }
 }

/* ======================================================================== */
/* LOAD DATA-POINTS WITH 'OFF', 'STB', 'ON' PARAMETERS FROM CONFIG-FILES    */
/*                                                                          */
/*                      Load -       ( if Serv == 0x0 or 0x4 or 0x8 )       */
/*                             0x0 - Load NOTHING       from 'Para' confFile*/
/*                             0x4 - Load 'Off' to DaPo from 'Para' confFile*/
/*                             0x8 - Load 'Stb' to DaPo from 'Para' confFile*/
/*                             0xC - Load 'On'  to DaPo from 'Para' confFile*/
/*                                                                          */
 LoadDataPoints(int Load)
 { int    load, stat, tout, int1;
   float  trig;
   string dstr;
   
   tout = 10 * 60;                   /* TimeOut is 10 minutes               */
/*
   sprintf(dstr," Load DataPoints: Load=%Xh", Load);
   dpSet("SCTmops1:Moni.Name:_original.._value", dstr);  delay(2); /* Debug */
   load = Load & 0xF;
   dpSet("SCTmops1:Conf.Stat:_original.._value", load);
   dpGet("SCTmops1:Conf.Data:_original.._value", trig);
     if(trig < 2)
   { dpSet("SCTmops1:Conf.Data:_original.._value", 2.0);
   } else
   { dpSet("SCTmops1:Conf.Data:_original.._value", 1.0);
   } int1 = 0;
   stat=0xF; while(stat > 0)         /* Waiting for execution               */
   { delay(1);
     dpGet("SCTmops1:Conf.Stat:_original.._value", stat);
     int1++; if(int1 >= tout)
     { sprintf(dstr," TimeOut '%d sec' in LoadDataPoints() ", tout);
       ErrorStop(dstr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
     }
   }
 }

/* CREATE OPC_CONFIG FILE AND PVSS OPC_ADDRESSes                            */
/*                                                                          */
/*                      Serv - what will be created for OPC-server:         */
/*                             0x0 - Create NOTHING                         */
/*                             0x4 - Create OPC-configFile                  */
/*                             0x8 - Create OPC-addresses and PVSS          */
/*                                          comm/mesg conversion formulas   */
/*                             0xC - Create NOTHING, Load NOTHING (OPC Run) */
/*                                                                          */
 CreateOPCconfigs(int Serv)
 { int    serv, stat, tout, int1;
   float  trig;
   string dstr;
   
   tout = 10 * 60;                   /* TimeOut is 10 minutes               */
/*
   sprintf(dstr," Create OPC: Serv=%Xh", Serv);
   dpSet("SCTmops1:Moni.Name:_original.._value", dstr);  delay(2); /* Debug */
   serv = (Serv << 4) & 0xF0;
   dpSet("SCTmops1:Conf.Stat:_original.._value", serv);
   dpGet("SCTmops1:Conf.Data:_original.._value", trig);
     if(trig < 2)
   { dpSet("SCTmops1:Conf.Data:_original.._value", 2.0);
   } else
   { dpSet("SCTmops1:Conf.Data:_original.._value", 1.0);
   } int1 = 0;
   stat=0xF0; while(stat > 0)        /* Waiting for execution               */
   { delay(1);
     dpGet("SCTmops1:Conf.Stat:_original.._value", stat);
     int1++; if(int1 >= tout)
     { sprintf(dstr," TimeOut '%d sec' in CreateOPCconfigs() ", tout);
       ErrorStop(dstr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
     }
   }
 }

/* CREATE PVSS CONFIGs AND LOAD THEM, MAKE OPC_CONFIG FILE & PVSS OPC ADDRs */
/*                                                                          */
/*                      Load -       ( if Serv == 0x0 or 0x4 or 0x8 )       */
/*                             0x0 - Load NOTHING       from 'Para' confFile*/
/*                             0x4 - Load 'Off' to DaPo from 'Para' confFile*/
/*                             0x8 - Load 'Stb' to DaPo from 'Para' confFile*/
/*                             0xC - Load 'On'  to DaPo from 'Para' confFile*/
/*                      Serv - what will be created for OPC-server:         */
/*                             0x0 - Create NOTHING                         */
/*                             0x4 - Create OPC-configFile                  */
/*                             0x8 - Create OPC-addresses and PVSS          */
/*                                          comm/mesg conversion formulas   */
/*                             0xC - Create NOTHING, Load NOTHING (OPC Run) */
/*                                                                          */
 MakeConfigs(int Load, int Serv)
 { int    load, serv, stat, tout, int1;
   float  trig;
   string dstr;
   
   tout = 10 * 60;                   /* TimeOut is 10 minutes               */
/*
   sprintf(dstr," Load DataPoints: Load=%Xh", Load);
   dpSet("SCTmops1:Moni.Name:_original.._value", dstr);  delay(2); /* Debug */
   load = Load & 0xF;
   serv = (Serv << 4) & 0xF0;
   dpSet("SCTmops1:Conf.Stat:_original.._value", serv | load);
   dpGet("SCTmops1:Conf.Data:_original.._value", trig);
     if(trig < 2)
   { dpSet("SCTmops1:Conf.Data:_original.._value", 2.0);
   } else
   { dpSet("SCTmops1:Conf.Data:_original.._value", 1.0);
   } int1 = 0;
   stat=0xFF; while(stat > 0)        /* Waiting for execution               */
   { delay(1);
     dpGet("SCTmops1:Conf.Stat:_original.._value", stat);
     int1++; if(int1 >= tout)
     { sprintf(dstr," TimeOut '%d sec' in MakeConfigs() ", tout);
       ErrorStop(dstr," CHECK ERROR, PUSH EXIT [X] & RESTART ");
     }
   }
 }

/* STOP ON ERROR, SHOW ERR0R & DEBUG TEXTs IN THE ERROR & MONI INDICATORs   */
/*                                                                          */
/*                      Estr - Error STRing (text) for Error Indicator      */
/*                      Dstr - Debug STRing (text) for Moni  Indicator      */
/*                                                                          */
 ErrorStop(string Estr, string Dstr)
 { string eror;
 
   dpSet("SCTmops1:Eror.Stat:_original.._value", 0x1);   /* Red Background  */
   dpSet("SCTmops1:Moni.Stat:_original.._value", 0x2);   /*Yellow Background*/
   sprintf(eror,"%s\n           PROJECT SCRIPTS ARE STOPPED ! \n", Estr);
   dpSet("SCTmops1:Eror.Name:_original.._value", eror); 
   dpSet("SCTmops1:Moni.Name:_original.._value", Dstr); 
   dpSet("SCTmops1:Conf.Numb:_original.._value", 3);      /* Panel  Flag    */
   dpSet("SCTmops1:Conf.Stat:_original.._value", 0x0);
   dpSet("SCTmops1:Conf.Data:_original.._value", 0.0);
   dpSet("SCTmops1:Conf.Indx:_original.._value", 3);      /* Script Flag    */
   Debug(eror);                                           /* Debugger       */
   while(1) { delay(1); }
 }
 
/* STOP ON PREDEFINED NUMBER, RESET SYNCHRO-SIGNALS FOR PROJECT CLOSING     */
/*                                                                          */
/*                      Dstr - Debug STRing   (text)                        */
/*                      Numb - Selected NUMBer (int)                        */
/*                      Maxi - Selected number MAXImal value                */
/*                                                                          */
 StepStop(string Dstr, float Numb, float Maxi)
 { string dstr;
 
     if(Numb < Maxi)
   { sprintf(dstr,"\n  PASS_DEBUGGER! %s: %2.1f < %2.1f; \n",Dstr,Numb,Maxi);
     Debug(dstr);
   } else
   { sprintf(dstr,
"\n  STOP_DEBUGGER! %s: %2.1f >= %2.1f; CLICK ON EXIT [X]\n",Dstr, Numb,Maxi);
     Debug(dstr);
   dpSet("SCTmops1:Conf.Numb:_original.._value", 3);      /* Panel  Flag    */
   dpSet("SCTmops1:Conf.Stat:_original.._value", 0x0);
   dpSet("SCTmops1:Conf.Data:_original.._value", 0.0);
   dpSet("SCTmops1:Conf.Indx:_original.._value", 3);      /* Script Flag    */
     while(1) { delay(1); }
   }
 }
 
/*===================================-======================================*/
/*                   PUT YOUR STATEs CONTROL FUNCTIONS HERE                 */
/* ----------------------------------=------------------------------------- */
/*                      AN EXAMPLE OF CONTROL FUNCTION                      */
/*                                                                          */
 StateControl()
 { int channel, stat;
   int    unit, umax, chan, cmax;    /* Unit (ELMB) & Chan(nel) MAX_numbers */
   string stri, dstr, estr;          /* Current, Debugger and Error STRings */
/**/
  dpSet("SCTmops1:Moni.Stat:_original.._value", 0x2);   /* Yellow Background*/
  sprintf(dstr," StateControl() started... ");
  dpSet("SCTmops1:Moni.Name:_original.._value", dstr);   delay(1); /* Debug */

///  ...                             /* Please, add something in between    */
//
///  ...                             /* Please, add something in between    */

    if(0)                            /* SYSTEM ERROR EXAMPLE                */
  { sprintf(estr," StateControl()\n      Check everything !!! "); 
    dpSet("SCTmops1:Eror.Name:_original.._value", estr); 
    dpSet("SCTmops1:Eror.Stat:_original.._value", 0x1);
  }
/**/
  sprintf(dstr," StateControl() finished ");
  dpSet("SCTmops1:Moni.Name:_original.._value", dstr);   delay(1); /* Debug */
  dpSet("SCTmops1:Moni.Stat:_original.._value", 0x0);    /* Green Background*/
}
/* ----------------------------------=------------------------------------- */
/*                                                                          */
 ShutDown()
 { string mstr;

   sprintf(mstr," ShutDown... ");
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
 }

 StartUp()
 { string mstr;

   sprintf(mstr," StartUp... ");
   dpSet("SCTmops1:Moni.Name:_original.._value", mstr);  delay(2);
 }

 
 detOFF()
 {
      int iState;
      iState=0; // the state is set to ON
      //DebugN("state is OFF");
      dpSet("Info.Name:_original.._value", "CS: Setting PS channels OFF");
      PSset2(iState);
 }
 
 detSTB()
 {
      int iState;
      iState=2; // the state is set to ON
      //DebugN("state is OFF");
      dpSet("Info.Name:_original.._value", "CS: Setting PS channels OFF");
      PSset2(iState);
 }

 detON()
 {
      int iState;
      iState=1; // the state is set to ON
      //DebugN("state is OFF");
      dpSet("Info.Name:_original.._value", "CS: Setting PS channels OFF");
      PSset2(iState);
 }
  
  PSset2(int iState)
{
int  i;
int NofCrates;
string datapoint;


 dpGet("Crate.Indx:_original.._value", NofCrates);
 NofCrates=1;
 for(i=0;i<NofCrates;i++)
 {
 sprintf(datapoint,"Crate%02d.Controller.CCch_Set.Send:_original.._value",i);
 channel = 255 | (( iState & 0x00ff )<<8);
 dpSetWait(datapoint,channel);
 }
} 

 saveELMBloading()
 {

  int iState;
  
  iState=3; //save ELMB loading
            
  dpSet("Info.Name:_original.._value", "CS: Loading new configuration to CC");
  // Send save configuration to ELMB
  LoadDataPoints(0x4);
  SaveConfig(0); //save OFF loading in ELMB
  LoadDataPoints(0x8);
  SaveConfig(2); //save STB loading in ELMB
  LoadDataPoints(0xC);
  SaveConfig(1); //save ON loading in ELMB
 }
 
 
SaveConfig(int iRST)
{
  //Tell crate controller what datapoints to store 0 = off:
  dpSetWait("Crate00.Controller.CCcf_RST.Send:_original.._value", iRST);
  dpSet("Info.Name:_original.._value", "CS: Setting CC in ", iRST, "mode");
  //When finished tell crate controller to save these values as off/stb/on
  //parameters:
  dpSetWait("Crate00.Controller.CCcf_Set.Send:_original.._value", 1702257011);
  dpSet("Info.Name:_original.._value", "CS: Saving parameters to CC memory");
}

 
 
 
/* ======================================================================== */

