<?xml version="1.0" encoding="UTF-8"?>
<Config Version="1.3"
        xmlns="http://www.insbud.net/ibmanager/logicCfg"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.insbud.net/ibmanager/logicCfg logicCfg.xsd">

  <!--
    ======================
    Description
    ======================

    Logic operates on values from input.values list and returns one result depends of setting.option

    ======================
    Parameters
    ======================

    SubLogic supports following variables:

    input.values                             - input list of all values

    output.value                             - calculated output

    setting.option                           - determinate how output is calculated
                                               0 - min value from input list
                                               1 - max value from input list
                                               2 - if any value from list is different from 0 then output = 1. If all values == 0 then output = 0
                                               3 - if all values from list is different from 0 then output = 1. If any from values == 0 then output = 0
                                               4 - if any value from list is different from 0 then output = 0. If all values == 0 then output = 1
                                               5 - if any value from list is changed since setting.timer.time then output = 1.
                                                   If all values are not changed i this time then output = 0
                                               6 - if any value from list is not changed since setting.timer.time then output = 1.
                                                   If all values are changed i this time then output = 0
                                               7 - if there is more than (setting.value) quantity more or equal values from list different from 0 then output = 1. 
                                                   For example if more or equal than 4 values form list are different from 0 then output = 1 else output = 0.
                                               8 - result is addition of all values from list

    setting.option                           - determinate how output is calculated

    setting.value                            - value for calculating output for options
                                               7 - it is quantity of values higher than 0 for output=1

    setting.timer.time                       - timer value

    counter.timer.downcounter                - current value of timer

    counter.value                            - auxilary counter depends of options
                                               7 - current quantity of values from list higher than 0

    Logic uses lua language to implement own behaviour

    ======================
    mandatory variables
    ======================

    Logic expects following mandatory variables:

    reload.trigger                          - causes reloading lua script

    memcnt                                  - current amount of memory used by lua in bytes

    Logic expects following kv settings:

    LuaScriptPath                           - path to the lua script - must be absolute

    ======================
    ChangeLog
    ======================

    2022-10-18 ver 0.3.8.0

    + fix dla setting.option == 3

    2018-09-26 ver 0.3.7.0

    + fix dla setting.option == 4

    2018-05-03 ver 0.3.6.0

    + setting.option == 8

    2017-12-12 ver 0.2.5.0

    + setting.option == 7

    2017-12-02 ver 0.1.4.0

    + setting.option == 5
    + setting.option == 6

    2017-07-25 ver 0.0.3.0

    + setting.option == 4

    2017-06-07 ver 0.0.2.825

    # automatic build support

    2017-04-13 ver 0.0.1

    + setting.option == 3

    2017-03-24 ver 0.0.0

    + First release

   -->

  <!-- Type and Version must be the same as Logic::TYPE and Logic::VERSION in Logic.cpp -->
  <Logic Type="Lua" SubType="ListUtils" Version="1.0.10.1421" SubVersion="0.3.8.0">

    <Declaration>

      <GlobalDeclSection>

      </GlobalDeclSection>

      <InstanceDeclSection>

        <!-- mandatory variables -->

        <Var Name="reload.trigger" Type="T_INT"
          CanLocalRead="true" CanLocalWrite="true"
          CanExternRead="true" CanExternWrite="true"
          Direction="Input" Min="0" Max="1"
          Default="0" IsStorable="false"/>

        <Var Name="memcnt" Type="T_LONGLONG"
          CanLocalRead="true" CanLocalWrite="true"
          CanExternRead="true" CanExternWrite="false"
          Direction="Output" Min="0"
          Default="0" IsStorable="false"/>

        <!-- sublogic specific variables -->

        <VarList Name="input.values" Type="T_INT"
          CanLocalRead="true" CanLocalWrite="true"
          CanExternRead="true" CanExternWrite="true"
          Direction="Input"  GroupName="g"
          Default="0" IsStorable="true"/>

        <Var Name="output.value" Type="T_INT"
          CanLocalRead="true" CanLocalWrite="true"
          CanExternRead="true" CanExternWrite="true"
          Direction="Input" Default="0" IsStorable="false"/>

        <Var Name="setting.option" Type="T_INT"
          CanLocalRead="true" CanLocalWrite="true"
          CanExternRead="true" CanExternWrite="true"
          Direction="Input" Default="0" IsStorable="true"/>

        <Var Name="setting.value" Type="T_INT"
          CanLocalRead="true" CanLocalWrite="true" 
          CanExternRead="true" CanExternWrite="true" Direction="Input" 
          Min="0" Default="0" IsStorable="true"/>

        <Var Name="setting.timer.time" Type="T_LONGLONG"
          CanLocalRead="true" CanLocalWrite="true" 
          CanExternRead="true" CanExternWrite="true" Direction="Input" 
          Min="0" Default="600" IsStorable="true"/>

        <Var Name="counter.timer.downcounter" Type="T_LONGLONG"
          CanLocalRead="true" CanLocalWrite="true"
          CanExternRead="true" CanExternWrite="false"
          Direction="Output" Min="0"
          Default="0" IsStorable="false"/>

        <Var Name="counter.value" Type="T_INT"
          CanLocalRead="true" CanLocalWrite="true"
          CanExternRead="true" CanExternWrite="false"
          Direction="Output" Min="0"
          Default="0" IsStorable="false"/>

        <Settings>
          <KVSettings>
            <!-- pointing source list -->
            <KVSetting Key="LuaScriptPath" Value="logic/scripts/ListUtils.lua"/>
          </KVSettings>
        </Settings>

      </InstanceDeclSection>

    </Declaration>

    <Initialization>

      <GlobalInitSection>
      </GlobalInitSection>

      <Instances>

        <!-- czy jest aktywny jakikolwiek błąd temperatury -->
        <Instance Name="err.any.temp">
          <InstanceInitSection>

            <VarAssign Name="setting.option" Value="2"/>
          </InstanceInitSection>
        </Instance>

        <!-- czy jest aktywny jakikolwiek błąd z wejść cyfrowych -->
        <Instance Name="err.any.di">
          <InstanceInitSection>

            <VarAssign Name="setting.option" Value="2"/>
          </InstanceInitSection>
        </Instance>

        <!-- czy jest aktywny jakikolwiek błąd -->
        <Instance Name="err.any">
          <InstanceInitSection>
            <VarListItem ListName="input.values"            Postfix="err.any.dev"                  Value="0"  SrcVar="Lua.DeviceMonitor.dev.counter.err.comm.state"/>

            <VarListItem ListName="input.values"            Postfix="err.any.temp"                 Value="0"  SrcVar="Lua.ListUtils.err.any.temp.output.value"/>
            <VarListItem ListName="input.values"            Postfix="err.any.di"                   Value="0"  SrcVar="Lua.ListUtils.err.any.di.output.value"/>

            <VarAssign Name="setting.option" Value="2"/>
          </InstanceInitSection>
        </Instance>

        <!-- czy jest jakiekolwiek wejście lub wyjście jest w trybie manualnym -->
        <Instance Name="warn.manu">
          <InstanceInitSection>
            <VarListItem ListName="input.values"            Postfix="temp.outdoor.value.mode"               Value="0"  SrcVar="MapperTemp.outdoor.setting.t.value.mode"/>
            <VarListItem ListName="input.values"            Postfix="temp.outdoor.err.mode"                 Value="0"  SrcVar="MapperTemp.outdoor.setting.t.err.mode"/>
            <VarListItem ListName="input.values"            Postfix="temp.ghe.1.inner.value.mode"           Value="0"  SrcVar="MapperTemp.ghe.1.inner.setting.t.value.mode"/>
            <VarListItem ListName="input.values"            Postfix="temp.ghe.1.inner.err.mode"             Value="0"  SrcVar="MapperTemp.ghe.1.inner.setting.t.err.mode"/>
            <VarListItem ListName="input.values"            Postfix="temp.ghe.1.vent.supply.value.mode"     Value="0"  SrcVar="MapperTemp.ghe.1.vent.supply.setting.t.value.mode"/>
            <VarListItem ListName="input.values"            Postfix="temp.ghe.1.vent.supply.err.mode"       Value="0"  SrcVar="MapperTemp.ghe.1.vent.supply.setting.t.err.mode"/>
            <VarListItem ListName="input.values"            Postfix="temp.water.value.mode"                 Value="0"  SrcVar="MapperTemp.water.setting.t.value.mode"/>
            <VarListItem ListName="input.values"            Postfix="temp.water.err.mode"                   Value="0"  SrcVar="MapperTemp.water.setting.t.err.mode"/>

            <VarListItem ListName="input.values"            Postfix="do.water.valve"                        Value="0"  SrcVar="MapperIO.do.water.valve.setting.mode"/>
            <VarListItem ListName="input.values"            Postfix="do.signal.vent.disable"                Value="0"  SrcVar="MapperIO.do.signal.vent.disable.setting.mode"/>

            <VarListItem ListName="input.values"            Postfix="di.water.level"                        Value="0"  SrcVar="MapperIO.di.water.level.setting.mode"/>
            
            <VarListItem ListName="input.values"            Postfix="ai.water.pulse"                        Value="0"  SrcVar="MapperIO.ai.water.pulse.setting.mode"/>
            <VarListItem ListName="input.values"            Postfix="ai.outdoor.humidity"                   Value="0"  SrcVar="MapperIO.ai.outdoor.humidity.setting.mode"/>
            <VarListItem ListName="input.values"            Postfix="ai.ghe.1.innner.humidity"              Value="0"  SrcVar="MapperIO.ai.ghe.1.innner.humidity.setting.mode"/>
            <VarListItem ListName="input.values"            Postfix="ai.ghe.1.vent.supply.humidity"         Value="0"  SrcVar="MapperIO.ai.ghe.1.vent.supply.humidity.setting.mode"/>

            <VarAssign Name="setting.option" Value="2"/>
          </InstanceInitSection>
        </Instance>

        <!-- czy jest aktywne jakiekolwiek ostrzeżenie z wejść cyfrowych -->
        <Instance Name="warn.any.di">
          <InstanceInitSection>

            <VarAssign Name="setting.option" Value="2"/>
          </InstanceInitSection>
        </Instance>
        
        <!-- czy jest aktywne jakiekolwiek ostrzeżenie -->
        <Instance Name="warn.any">
          <InstanceInitSection>
            <VarListItem ListName="input.values"            Postfix="warn.manu"                    Value="0"  SrcVar="Lua.ListUtils.warn.manu.output.value"/>
            <VarListItem ListName="input.values"            Postfix="warn.any.di"                  Value="0"  SrcVar="Lua.ListUtils.warn.any.di.output.value"/>

            <VarAssign Name="setting.option" Value="2"/>
          </InstanceInitSection>
        </Instance>

      </Instances>
    </Initialization>

  </Logic>

</Config>
