/// Ehouse 1 Device Status / rs485 cache status instance

///roommanager , heatmanager, externalmanager status EhDev array
/**
 * @author robert jarzabek 
 * http://www.isys.pl/
 * http://inteligentny-dom.ehouse.pro/
 * http://www.ehouse.pro/
 * http://sterowanie.biz/
 * 
*/
package org.openremote.controller.protocol.ehouse;
import java.util.Calendar;
  public  class StatusEhouse
  {         //Indexes locations in status buffer
final static int STATUS_OFFSET                 = 1;                     //byte index location of binary status query results
final static int RM_STATUS_ADC                 = 1  + STATUS_OFFSET;    //start of adc measurement
final static int RM_STATUS_OUT  	       = 17 + STATUS_OFFSET;    //RM start of outputs
final static int HM_STATUS_OUT                 = 33 + STATUS_OFFSET;    //HM start of outputs
final static int RM_STATUS_IN                  = 20 + STATUS_OFFSET;    //RM start of inputs
final static int RM_STATUS_INT                 = 21 + STATUS_OFFSET;    //rm start of inputs (fast)
final static int RM_STATUS_OUT25               = 22 + STATUS_OFFSET;    //rm starts of outputs => 25-32
final static int RM_STATUS_LIGHT               = 23 + STATUS_OFFSET;    //rm light level start
final static int RM_STATUS_ZONE_PGM            = 26 + STATUS_OFFSET;    //rm current zone
final static int RM_STATUS_PROGRAM             = 27 + STATUS_OFFSET;    //rm current program
final static int RM_STATUS_INPUTEXT_A_ACTIVE   = 28 + STATUS_OFFSET;    //em input extenders A status active inputs
final static int RM_STATUS_INPUTEXT_B_ACTIVE   = 32 + STATUS_OFFSET;    //em input extenders B status active inputs
final static int RM_STATUS_INPUTEXT_C_ACTIVE   = 36 + STATUS_OFFSET;    //em input extenders C status active inputs
final static int RM_STATUS_INPUTEXT_A          = 40 + STATUS_OFFSET;    //em --||-
final static int RM_STATUS_INPUTEXT_B          = 50 + STATUS_OFFSET;    //em 
final static int RM_STATUS_INPUTEXT_C          = 60 + STATUS_OFFSET;    //em 
final static int HM_STATUS_PROGRAM             = 36 + STATUS_OFFSET;    //hm current program
final static int HM_STATUS_KOMINEK 	       = 46 + STATUS_OFFSET;    //hm status bonfire
final static int HM_STATUS_RECU 	       = 48 + STATUS_OFFSET;    //hm status recu
final static int HM_WENT_MODE		       = 49 + STATUS_OFFSET;    //hm went mode

      
            public String recent="";
            public boolean InitializedMatrix=false;
            int STATUS_INDEX = -1;
            final static int OUTPUTS_COUNT_RM       = 35; //max nr of digital outputs for device
            final static int INPUTS_COUNT_RM        = 16;   //max nr of digital inputs for device
            final static int SENSORS_COUNT_RM       = 16;   //max nr of adc inputs for device
            final static int SENSORS_COUNT_ALARM    = 128;  //max nr of alarm sensors inputs for external manager
            final static int STATUS_SIZE            = 256;  //max length of query status received
            final static int GRAPHIC_OBJECTS_OTHER  = 1000; //max nr of unconditional graphic objects for device (unconditional cache)
            final static int GRAPHIC_OBJECTS_CURRENT= 3000; //max nr ob graphic objects for device (dynamic cache)
            final static int DIMMERS_COUNT_RM       = 3;    //max nr of pwm dimmers per device 
            final static int DIMMERS_RGB_COUNT_RM   = 1;    //max nr of rgb - pwm dimer for device
            final static int PROGRAMS_COUNT_RM      = 24;   //programs count
        /// All necessary data for visualisation (names and status)
            public String   DeviceName;                                                 //device name
            public String[] ProgramNames        = new String[PROGRAMS_COUNT_RM];        //program names list
            public String[] OutputNames         = new String[OUTPUTS_COUNT_RM];         //name of output list
            public String[] DimmerNames         = new String[DIMMERS_COUNT_RM];         //name of output list
            public String[] DimmerRGBNames      = new String[DIMMERS_RGB_COUNT_RM];     //name of output list
            public int[]    Dimmer              = new int[DIMMERS_COUNT_RM];            //dimmers values
            public int[]    DimmerRGB           = new int[DIMMERS_RGB_COUNT_RM];        //RGB dimmer values
            public int      OtherViewsIndex     = 0;    //index of last item in unconditional cache buffer
            public int      CurrentViewStatIndex = 0;//index of last item in device cache buffer
            public String[] InputNames      = new String[INPUTS_COUNT_RM];  //names of inputs list
            public String[] SensorNames     = new String[SENSORS_COUNT_RM]; //ADC Sensors (inputs) names
            public int[] SensorType=new int[SENSORS_COUNT_RM];  //type of adc sensor (%, invert %, value, absolute value, voltage, temp, light etc)
            public int[] SensorABSValues            = new int[SENSORS_COUNT_RM];  //sensors absolute values 0..1023
            public int[] Calibration                = new int[SENSORS_COUNT_RM]; //calibration * 100 value for acurate measurement calculation
            public Double[] SensorTemps             = new Double[SENSORS_COUNT_RM];      //sensor Temp value calculated
            public Double[] SensorPercents          = new Double[SENSORS_COUNT_RM];   //sensor percent value calculate comparing to power supply
            public Double[] SensorLights            = new Double[SENSORS_COUNT_RM]; //inverted percent value calculation for negative scale sensors
            public Double[] SensorVolts             = new Double[SENSORS_COUNT_RM]; //voltage value calculated
            public Boolean[] OutputStates           = new Boolean[OUTPUTS_COUNT_RM];   //digital output states / values (0/1) ON/OFF 
            public Boolean[] InputStates            = new Boolean[INPUTS_COUNT_RM];     //digital input states / values (0/1) ON/OFF
            public Boolean[] AlarmSensorsActive     = new Boolean[SENSORS_COUNT_ALARM]; //alarm sensors inputs active state (0/1) for information / no alarm, warning, monitoring, sms
            public Boolean[] AlarmSensorsWarning = new Boolean[SENSORS_COUNT_ALARM];    //alarm sensors inputs warning state (0/1) 
            public Boolean[] MonitoringSensorsWarning = new Boolean[SENSORS_COUNT_ALARM];   //alarm sensors inputs states for monitoring output
            public Boolean[] AlarmSensorsAlarm = new Boolean[SENSORS_COUNT_ALARM];  //alarm sensor inputs alarm states (0/1) for alarm output
            public String[] AlarmSensorsNames = new String[SENSORS_COUNT_ALARM];    //alarm sensor inputs names list

            public int DevAdrH;                                             //device addr h
            public int DevAdrL;                                             //device addr l
            public boolean changed;                                            //flag to set if changed value
            public boolean AdcChanged;                                      //adc measurement values was changed from previous status - flag
            //public static DateTime LastRead;
            public int CurrentProgram;                                      //current program of device
            public String CurrentProgramName;                               //current program name of device
            public int CurrentZone;                                         //curent zone nr (for EM)
            public String CurrentZoneName;                                  //curent zone name (for em)
            public Byte[] CurrentStatus = new Byte[STATUS_SIZE];            //current status binary (received from host not decoded for comparison)
            //public Byte[] PreviousStatus = new Byte[STATUS_SIZE];
            public boolean IsBatch;                                         //not device but batch file
            public boolean IsRM;                                            // device is RoomManager
            public boolean IsHM;                                            // device is HeatManager
            public boolean IsEM;                                            // device is ExternalMaanger
            public boolean IsAlarm;                                         // device is SecurityManager
            public boolean IsEthernet;
            public int VCC; //power suply for calibration * 100

////////////////////////////////////////////////////////////////////////////////////////////////////////////
        final static int ADC_N_OFFSET = 4;                                  //binary status byte index offset from 0 ADC Results 2 bytes per input
        final static int O_N_OFFSET=ADC_N_OFFSET+16;                        //offset for digital outputs
        final static int I_N_OFFSET=O_N_OFFSET+35;                          //offset for digital inputs
//////////////////////////////////////////////////////////////////////////////////////////////////////////        
      public void SetStatusIndex(int nr)
      {
        STATUS_INDEX=nr;

      }
//////////////////////////////////////////////////////////////////////////////////////////////////////////      
/**
 * Check ADC changed flag
 * @return 
 */      
      
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// check adc changed flag and reset the flag if it was changed
public boolean ChkIsAdcChanged()
      {
          if (AdcChanged)
          {
              AdcChanged = false;
              return true;
          }
          return false;
      }
///////////////////////////////////////////////////////////////////////////////////////////////////////
// search for device name return true if dname is the saame as current device name
//
public boolean IsDeviceName(String dname)
      {
          if (DeviceName == null) return false;
          if (DeviceName.length() == 0) return false;
      if (dname.length() > 0)
          {
          if (dname.compareTo(DeviceName) == 0) return true;
          }
      return false;
      }
/***
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// get temperature calculation from lm335 digital temperature sensor
// dta (absolute value 0..1023) 
// calibration - for calculation value including calibration
// VCC real power supply for reference
// round to 1 digit of fraction
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/
private double gettemplm(int dta, int calibration, double k, int VCC)
    {
    double temp;
    temp=(dta*VCC/(1023*k))+calibration/100;
//t=Math.Round(temp*10);
//temp=(t/10);
    temp = Math.round(temp*10)/10;
    return temp;
    }
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//check integer and convert str to integer
/***
 * Check Integer in string  and parse to int
 * @param stt
 * @param defaut
 * @return 
 */
private int check_in(String stt, int defaut)
    {        
    double dd = Double.valueOf(stt);
    int ino = (int)dd;
    //ino=st.IConvertible.ToInt32();
    return (ino);
    }
/**
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// read alarm sensors names from file and initialization of values;
//
 */
public void ReadAlarmSensorsNames(String pat)
{
    int i=0;
    int z=0;
    if (ehousecommunication.FileExists(pat))
                        {
                         String[] AlarmS=ehousecommunication.getfile(pat);
                        while (AlarmS[i]!=null)
                        {
                            
                            {
                                AlarmSensorsActive[i] = false;
                                AlarmSensorsAlarm[i] = false;
                                AlarmSensorsWarning[i] = false;
                            }
                            AlarmSensorsNames[i] = AlarmS[i];
                            i++;
                            if (i == SENSORS_COUNT_ALARM) break;
                            //z++;
                            
                        }
                            while (i<SENSORS_COUNT_ALARM)
                            {
                            AlarmSensorsActive[i] = false;
                            AlarmSensorsAlarm[i] = false;
                            AlarmSensorsWarning[i] = false;
                            AlarmSensorsNames[i] = "";
                            i++;
                            }
                            
                        }
}                  
/////////////////////////////////////////////////////////////////////////////////
/**
 * read heatmanager temperature sensors names, output names and program names for initialization
 */
////////////////////////////////////////////////////////////////////////////////////////////////////////////
public void ReadHMSensorsNames()
{
    int i=0;
    int z=0;
    String pat="hmtemps.txt";
    if (ehousecommunication.FileExists(ehousecommunication.locpath+pat))
                        {
                         String[] AlarmS=ehousecommunication.getfile(ehousecommunication.locpath+pat);
                        while (AlarmS[i]!=null)
                        {
                            

                            SensorNames[i] = AlarmS[i];
                            i++;
                            if (i == SENSORS_COUNT_RM) break;
                           
                            
                        }
                            while (i<SENSORS_COUNT_RM)
                            {
                            
                            SensorNames[i] = "";
                            i++;
                            }
                            
                        }
    i=0;
    pat="hmouts.txt";
    if (ehousecommunication.FileExists(ehousecommunication.locpath+pat))
                        {
                         String[] AlarmS=ehousecommunication.getfile(ehousecommunication.locpath+pat);
                        while (AlarmS[i]!=null)
                        {
                            

                            OutputNames[i] = AlarmS[i];
                            i++;
                            if (i == OUTPUTS_COUNT_RM) break;
                           
                            
                        }
                            while (i<OUTPUTS_COUNT_RM)
                            {
                            
                            OutputNames[i] = "";
                            i++;
                            }
                            
                        }
    i=0;
    pat="heater.evs";
    if (ehousecommunication.FileExists(ehousecommunication.locpath+pat))
                        {
                         String[] AlarmS=ehousecommunication.getfile(ehousecommunication.locpath+pat);
                        while (AlarmS[i]!=null)
                        {
                            

                            ProgramNames[i] = AlarmS[i];
                            i++;
                            if (i == PROGRAMS_COUNT_RM) break;
                           
                            
                        }
                            while (i<PROGRAMS_COUNT_RM)
                            {
                            
                            ProgramNames[i] = "";
                            i++;
                            }

                        }
    
}                  

/******************************************************************************************************/
/**
 * load configuration names for current controller 
 */
public void LoadDta(String name/*, Encoding enc*/)
        {
            
            String temp;
            int i;
            {

            if (ehousecommunication.FileExists(name))
                {
                    String[] SteH=ehousecommunication.getfile(name);
                    DeviceName = SteH[0];                                                      //device name
                    DevAdrH = check_in(SteH[1],0);                                             //device addr h
                    DevAdrL = check_in(SteH[2],0);                                             //device addr l                    
                    temp = SteH[3];
                    if (temp.compareTo("HM")==0) IsHM = true;               //heatmanager flag
                    if (temp.compareTo("EM")==0) IsEM = true;               //externalmanager flag
                    if (temp.compareTo("RM")==0) IsRM = true;               //roommanager flag
                    for (i = 0; i < SENSORS_COUNT_RM; i++)                  //read sensor names and reset values to 0
                        {
                        SensorNames[i] = SteH[4+i];
                        SensorABSValues[i] = 0;
                        SensorTemps[i] = 0.0;      //sensor Temp value calibrated
                        SensorPercents[i] = 0.0;   //sensor percent value
                        SensorLights[i] = 0.0;
                        SensorVolts[i]=0.0;
                        Calibration[i] = -27315;   //!!!! future use - read calibration offset from file
                        }
                    VCC = 5000;// 4810; ;           //!!! future use - read power supply voltage from file for calibration
                    for (i = 0; i < OUTPUTS_COUNT_RM; i++)  //read all output names and reset values to 0
                        {
                        OutputNames[i] = SteH[4+i+SENSORS_COUNT_RM];
                        OutputStates[i] = false;
                        }
                    for (i = 0; i < INPUTS_COUNT_RM; i++)   //read all inputs names and reset values to 0
                        {
                        InputNames[i] = SteH[4+i+OUTPUTS_COUNT_RM+SENSORS_COUNT_RM];
                        InputStates[i] = false;
                        }
                        
                    changed = false;                                            //flag to set if changed value
//                    AdcChanged = false;
                    //LastRead = 0;
                    CurrentProgram = 0;
                    CurrentProgramName = "";
                    for (i = 0; i<STATUS_SIZE; i++)
                    {
                        CurrentStatus[i] = 0;
//                        PreviousStatus[i] = 0;
                    }
                    IsBatch = false;
                    IsAlarm = false;
                    IsEthernet = false;

                }
            }
            
        }
        
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
/** calculate percent value in refference to power supply vcc rounds to 2 fractional digit */
//
public double calculate_percent(int dta)
            {
    
        
             double tmp = Math.round((double)((dta * 100 ) / 1023)*100)/100;  //do 2 miejsc po przecinku
                //tmp = (tmp) / 100;
            return tmp;
            }
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// calculate light level value 
//
public double getlight(int dta)
  {
 
  //double dtta=(1023-dta)*100;
      double dtta = (dta) * 100;
  dtta = ((((double)dtta) / 1023));
    return Math.round(dtta*10)/10;
  }
      
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
/**
 * Return adc value for current input
 * 
 * @param index index of input
 * @return value in string with suffix
 */
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// get adc value of sensor at index
//
//
public String ADCVal(int index)
{
if (!IsHM) 
    if (index==0) 
        return SensorLights[index].toString()+" %"; //if index 0 and not HM default sensor is Light Sensor
    return SensorTemps[index].toString()+" C"; // otherwise temperature value
        
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * get digital input and output state (stored in bits of status)
 *
 * 
 * @param dta - byte
 * @param offset - offset for single bit
 * @return 
 */
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

public boolean GetInOutValue(int dta, int offset)
    {
    int temp=dta>>offset;
    if ((temp & 0x01)>0) 
            return true;
    else return false;
    }
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*public int[] GetOptsIndex(String EventName,int AlarmState)
    {
        int i;
    int[] m=new int[2];
    for (i=0;i<OUTPUTS_COUNT_RM;i++)
    {
        if (OutputNames[i].length()>0)
            if (EventName.compareTo(OutputNames[i])==0)
                {
                m[1]=i;
                m[0]=1;
                return m;// (STATUS_INDEX<<16)+(1<<8)+i;
                }
    }
    for (i=0;i<INPUTS_COUNT_RM;i++)
    {
        if (InputNames[i].length()>0)
            if (EventName.compareTo(InputNames[i])==0)
                {
                m[1]=i;
                m[0]=2;
                return m;// (STATUS_INDEX<<16)+(1<<8)+i;
                }
    }
    
    for (i = 0; i < SENSORS_COUNT_RM; i++)
    {
        if (SensorNames[i].length() > 0)
            if (EventName.compareTo(SensorNames[i]) == 0)
            {
                m[1] = i;
                m[0] = 3;
                return m;// (STATUS_INDEX<<16)+(1<<8)+i;
            }
    }
    if (IsEM)
    for (i = 0; i < SENSORS_COUNT_ALARM;i++ )
        if (AlarmSensorsNames[i].length() > 0)
        {
            if (EventName.compareTo(AlarmSensorsNames[i]) == 0)
            {
                m[1] = i;
                m[0] = 4 + AlarmState;
                return m;
            }
        }
        
    m[1] = 255;
    m[0] = 255;
    return m;
    }*/
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
/**
 * perform binary query status decoding
 * 
 * 
 * 
 * @param dta - status data bytes
 * @param InitFromPrevious - unconditional update
 * @return 
 */
///
////////////////////////////////////////////////////////////////////////////////////////////////////////////

public boolean WriteCheckIfChangedAndCpy(byte[] dta, boolean InitFromPrevious)

        { int i,k;
        int sss=0;
        boolean tempboolean = false;
        boolean unconditional = InitFromPrevious;
        int Max_ADC = 8;
        Calendar c = Calendar.getInstance(); 
        recent=c.getTime().toLocaleString();
//            boolean changedd=false;
            if (InitFromPrevious) changed = true;   //unconditional update
            else changed=false;
            if (!InitFromPrevious)  //normal query
              //if (dta != null)
                for (i = 0; i < STATUS_SIZE; i++) //compare binary status data from query byte by byte
                    {
                     if (CurrentStatus[i] != dta[i])    //if different from previous
                        {
                            CurrentStatus[i] = dta[i];  //update
                            changed = true;             // set changed flag
                        }
                    }
        
        if (changed) //data changed or unconditional
        {   //update data
        if ((IsRM) || (IsEM)) 
            {
            Max_ADC=8;
            if ((IsRM) && (!InitFromPrevious))
                CurrentStatus[STATUS_SIZE-1] = 0;
            for (k = 0; k < 8; k++) //update output and input states and visualization objects
                {
                    OutputStates[k] = GetInOutValue(CurrentStatus[RM_STATUS_OUT], k);
                    OutputStates[k + 8]  = GetInOutValue(CurrentStatus[RM_STATUS_OUT + 1], k); 
                    OutputStates[k + 16] = GetInOutValue(CurrentStatus[RM_STATUS_OUT + 2], k); 
                    OutputStates[k + 24] = GetInOutValue(CurrentStatus[RM_STATUS_OUT25], k); 
                    InputStates[k]       = GetInOutValue(CurrentStatus[RM_STATUS_IN], k);
                    InputStates[k+8]     = GetInOutValue(CurrentStatus[RM_STATUS_INT], k);
                if (IsEM)   //for em additionally update input extenders alarm sensor inputs states and visualization objects
                    {
                    AlarmSensorsActive[k] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_A_ACTIVE], k);
                    AlarmSensorsActive[k + 8] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_A_ACTIVE + 1], k); 
                    AlarmSensorsActive[k + 16] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_A_ACTIVE + 2], k); 
                    AlarmSensorsActive[k + 24] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_A_ACTIVE + 3], k); 
                    AlarmSensorsActive[k + 32] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_A + 4], k);
                    AlarmSensorsActive[k + 40] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B_ACTIVE + 0], k); 
                    AlarmSensorsActive[k + 48] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B_ACTIVE + 1], k); 
                    AlarmSensorsActive[k + 56] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B_ACTIVE + 2], k); 
                    AlarmSensorsActive[k + 64] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B_ACTIVE + 3], k);
                    AlarmSensorsActive[k + 72] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B + 4], k);
                    AlarmSensorsActive[k + 80] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_C_ACTIVE + 0], k);
                    AlarmSensorsActive[k + 88] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_C_ACTIVE + 1], k);
                    AlarmSensorsActive[k + 96] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_C_ACTIVE + 2], k);
                    AlarmSensorsActive[k + 104] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_C_ACTIVE + 3], k);
                    AlarmSensorsActive[k + 112] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_C + 4], k);
                    AlarmSensorsAlarm[k] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_A], k);
                    AlarmSensorsAlarm[k + 8] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_A + 1], k); 
                    AlarmSensorsAlarm[k + 16] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_A + 2], k); 
                    AlarmSensorsAlarm[k + 24] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_A + 3], k); 
                    AlarmSensorsAlarm[k + 32] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_A + 4], k);
                    AlarmSensorsAlarm[k + 40] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B + 0], k); 
                    AlarmSensorsAlarm[k + 48] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B + 1], k);
                    AlarmSensorsAlarm[k + 56] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B + 2], k);
                    AlarmSensorsAlarm[k + 64] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B + 3], k);
                    AlarmSensorsAlarm[k + 72] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B + 4], k);
                    AlarmSensorsAlarm[k + 80] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B + 0], k);
                    AlarmSensorsAlarm[k + 88] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B + 1], k);
                    AlarmSensorsAlarm[k + 96] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B + 2], k);
                    AlarmSensorsAlarm[k + 104] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B + 3], k);
                    AlarmSensorsAlarm[k + 112] = GetInOutValue(CurrentStatus[RM_STATUS_INPUTEXT_B + 4], k);
                    }
                }

            CurrentProgram=CurrentStatus[RM_STATUS_PROGRAM];    //update current program nr
            CurrentZone = CurrentStatus[RM_STATUS_ZONE_PGM];    //update current zone
            CurrentProgramName="";//na razie                    //load current program name from list
            CurrentZoneName="";                                 //load cuurrent zones from list
            
            }
        if (IsEM)
        {
            CurrentProgramName = "";
        }
        if (IsHM)   //heatmanager
            {
            Max_ADC=16;
            CurrentProgram = CurrentStatus[HM_STATUS_PROGRAM];  //current program for heatmanager
            CurrentProgramName = "";//na razie                  //current program name for heatmanager
            for (k = 0; k < 8; k++) //update output states and visualization objects
                {
                int mm = 0;
                OutputStates[k] = GetInOutValue(CurrentStatus[HM_STATUS_OUT], k);
                OutputStates[k + 8] = GetInOutValue(CurrentStatus[HM_STATUS_OUT + 1], k); //bitoffset++; k++;
                OutputStates[k + 16] = GetInOutValue(CurrentStatus[HM_STATUS_OUT + 2], k); //bitoffset++; k++;
//                InputStates[k] = GetInOutValue(CurrentStatus[RM_STATUS_IN ], k);
  //              InputStates[k + 8] = GetInOutValue(CurrentStatus[RM_STATUS_INT ], k);
                }
            }
        i=0;
        while (i < Max_ADC)         //perform all adc results (2bytes per result)
                {                                               //offset od 2                       //offset od 2
                //int temppp=(CurrentStatus[i * 2 + 2] << 8) + CurrentStatus[i * 2 + 1 + 2];
                int temppp= CurrentStatus[i * 2 + 1+2 ];
                if (temppp<0) temppp+=256;
                 temppp+=(CurrentStatus[2+i * 2 ] << 8);
                
                if (SensorABSValues[i] != temppp) //compare to previous value
                    {
                    AdcChanged = true;  //changed flag for ADC
                    }
                    SensorABSValues[i] = temppp;    //update value
                    SensorTemps[i] = gettemplm(SensorABSValues[i], Calibration[i], 10, VCC);    //calculate temp value
                    SensorLights[i] = getlight(SensorABSValues[i]); //calculate light level value
                    SensorPercents[i] = calculate_percent(SensorABSValues[i]);  //calculate percent value
                    i++;
                }
        //wrlog();
        int m=0;
        for (int ii=0;ii<DIMMERS_RGB_COUNT_RM;ii++) DimmerRGB[ii]=0;
        
        for (int ii=0;ii<DIMMERS_COUNT_RM;ii++)         //set dimmer values for single dimmer and RGB consists of 3 single
            {
            DimmerRGB[ii/3]=CurrentStatus[RM_STATUS_LIGHT+(ii%3)];
            if ((ii%3)!=2) 
                    DimmerRGB[ii/3]=DimmerRGB[ii/3]<<8;
            Dimmer[ii]= (int) CurrentStatus[RM_STATUS_LIGHT+(ii)];
             }
        
        
        }
        
        return changed;
        }
////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
/**
 check if this is correct device by address

 * 
 * @param devadrh - address high
 * @param devadrl - address low
 * @return true if address matches
 */
//
////////////////////////////////////////////////////////////////////////////////////////////
        public  boolean isAddress(int devadrh, int devadrl)
        {
        if ((devadrh==DevAdrH) && (devadrl==DevAdrL)) return true;
            return false;

        }
         
    
    }

