<?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">

  <!--

  logic handles following instance variables:

    input.value                   - logic input value.

    counter.flutter.value         - this variable keeps copy of input value processed by flutter.
                                    this value is further passed to kvSettings block to generate output logic value.

    counter.flutter.time          - time remaining to switching value by flutter expressed in tenthes of seconds. meaning of this time depends on
                                    flutter mode - read setting.flutter.mode

    output.value                  - read only. logic output value. if setting.mode is 0 (auto) then counter.flutter.value
                                    processed by kvSettings block generates value of this variable.
                                    if setting.mode is 1 (manual) then
                                    - if setting.manual.mode == 0 then value of setting.manual.value is copied to this variable .
                                    - if setting.manual.mode == 1 then value of setting.manual.value is processed by kvSettings and next
                                      is copied to this variable.

    setting.flutter.time          - time expressed in tenthes of seconds. meaning of this parameter depends on current flutter mode

    setting.flutter.mode          - flutter work mode
                                    0 - normal mode - input is passed to output only when input has stable value at least since given period
                                    1 - min period mode - output value is keep at least by given period of time, regardless of input value.
                                    in both cases, remaining time is copied to the counter.flutter.time variable.

    setting.mode                  - mapper mode.
                                    0 - auto - output.value = kvSettings(counter.flutter.value)
                                    1 - manual - output.value = setting.manual.value or output.value = kvSettings(counter.flutter.value)
                                        depends on setting.manual.mode.

    setting.manual.value          - manual value, that will be passed to output.value, when setting.mode = 1 and setting.manual.mode = 0,
                                    or value, that will be passed to the kvsettings and next to the output value, if setting.mode = 1
                                    and setting.manual.value = 1.

    setting.manual.mode           - determining output value mode when mapperIO is in manual mode. 0..1
                                    0 - directly - setting.manual.value is copied directly to the output.value
                                    1 - kvTrans - setting.manual.value is transformed by kvSettings and output from kvSettings is copied
                                        to the output.value

    counter.intermediate.value    - this variable keeps copy of counter.flutter.value (in auto mode) or setting.manual.value (in manual mode)

   +=================+      +=========+   +=======================+    +==============+                                 +==================+
   | input.value     |=====D| flutter |==D| counter.flutter.value |===D|  kvSettings  |=(if setting.mode == auto or)===D| output.value     |
   +=================+      +=========+   +=======================+    +==============+ ((setting.mode == manual and)   +==================+
                                                     |                         ^        (setting.manual.mode == kvTrans))          ^
                                                     |                         |                                                   |
                                                     |           (if setting.mode == manual)                                       |
            +=======(if setting.mode == auto)========+       (and setting.manual.mode == kvTrans)                                  |
            |                                                                  ^                                                   |
            v                                                                  |                                                   |
   +==========================+                                    +====================+      (if setting.mode == manual)         |
   |counter.intermediate.value|c==(if setting.mode == manual)c=====|setting.manual.value|===D(and setting.manual.mode == manual)===+
   +==========================+                                    +====================+


   logic expects optional KVSettings:

   KVSettings are optional and if no entries in KVSettings exists, then this functional block is transparent (counter.flutter.value is
   copied to output.value). KVSetting entry have "Key" - "Value" pair.
   If KVSettings have any entry, then before copying input of kvSettings to the output.value, logic search all "Keys", to match given
   input value. If matches, then to the output.value is copied "Value" related with found "Key" in single KVSetting. If not found then
   given counter.flutter.value is copied to output.value.

   Key can be a number or span of numbers. for example

   "1"                  - included value:   1
   "-2"                 - included value:  -2
   "[-2..1]"            - included values: -2, -1, 0, 1
   "[-2..]"             - included values  -2, -1, 0, 1, 2, ..., infinity
   "[..-2]"             - included values  -infinity, ..., -3, -2
   "[..]"               - all values are included
   "default"            - if is defined and not found given value in key, then this value is used

   intervals can not be overlapped!!!. "default" can be applied always

   example 1:

   [KVSetting Key="30" Value = "0"]
   [KVSetting Key="10" Value = "1"]

   counter.flutter.value      | output.value (if setting.mode is set to auto)
   +++++++++++++++++++++++++++++++++++++++++++++
                            0 |  0
                            1 |  1
                            2 |  2
                            5 |  5
                           10 |  1
                           20 | 20
                           30 |  0
                           41 | 41

   example 2:

   [KVSetting Key="30" Value = "0"]
   [KVSetting Key="10" Value = "1"]
   [KVSetting Key="20" Value = "2"]

   counter.flutter.value      | output.value (if setting.mode is set to auto)
   +++++++++++++++++++++++++++++++++++++++++++++
                            0 |  0
                            1 |  1
                            2 |  2
                            5 |  5
                           10 |  1
                           20 |  2
                           30 |  0
                           41 | 41


   example 3:

   [KVSetting Key="30" Value = "0"]
   [KVSetting Key="10" Value = "1"]
   [KVSetting Key="20" Value = "2"]
   [KVSetting Key="default" Value = "4"]

   counter.flutter.value      | output.value (if setting.mode is set to auto)
   +++++++++++++++++++++++++++++++++++++++++++++
                            0 |  4
                            1 |  4
                            2 |  4
                            5 |  4
                           10 |  1
                           20 |  2
                           30 |  0
                           41 |  4


   example 4:

   [KVSetting Key="[-2..3]" Value = "44"]
   [KVSetting Key="[6..10]" Value = "55"]
   [KVSetting Key="[13..]" Value = "66"]


   counter.flutter.value      | output.value (if setting.mode is set to auto)
   +++++++++++++++++++++++++++++++++++++++++++++
                           -3 |  -3
                           -2 |  44
                            0 |  44
                            3 |  44
                            4 |  4
                            5 |  5
                            6 |  55
                            8 |  55
                            9 |  55
                           10 |  55
                           11 |  11
                           13 |  66
                           14 |  66
                         1234 |  66
                      3563456 |  66
                     12323523 |  66

   -->



  <!-- Type and Version must be the same as Logic::TYPE and Logic::VERSION in Logic.cpp -->
  <Logic Type="MapperIO" Version="0.6.15.1413">

    <Declaration>

      <GlobalDeclSection>

      </GlobalDeclSection>

      <InstanceDeclSection>


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

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

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

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

        <Var Name="counter.flutter.time" Type="T_UINT"
          CanLocalRead="true" CanLocalWrite="true"
          CanExternRead="true" CanExternWrite="false"
          Direction="Output" Default="0" IsStorable="false"/>

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

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

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

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

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



      </InstanceDeclSection>

    </Declaration>

    <Initialization>

      <GlobalInitSection>

      </GlobalInitSection>

      <Instances>

        <!-- elektrozawór polewania złoża wodą -->
        <Instance Name="do.water.valve">
          <InstanceInitSection>
            <VarAssign Name="input.value"          Value="0"  SrcVar="Lua.GHE03.0.output.water.valve"/>
            <VarAssign Name="output.value"         Value="0"  DstVar="rs.0.id.1.output.do.0"/>
            <VarAssign Name="setting.flutter.mode" Value="1"/>
            <VarAssign Name="setting.flutter.time" Value="50"/>
            <VarAssign Name="setting.manual.mode"  Value="1"/>
            <Settings>
              <KVSettings>
                <KVSetting Key="0" Value="0"/>
                <KVSetting Key="default" Value="1"/>
              </KVSettings>
            </Settings>
          </InstanceInitSection>
        </Instance>

        <!-- sygnał wyłączenia nawiewu (ochrona przed zamarzaniem złoża zimą) -->
        <Instance Name="do.signal.vent.disable">
          <InstanceInitSection>
            <VarAssign Name="input.value" Value="0" SrcVar="Lua.GHE03.0.output.signal.vent.disable"/>
            <VarAssign Name="output.value" Value="0" DstVar="rs.0.id.1.output.do.1"/>
            <VarAssign Name="setting.flutter.mode" Value="1"/>
            <VarAssign Name="setting.flutter.time" Value="50"/>
            <VarAssign Name="setting.manual.mode" Value="1"/>
            <Settings>
              <KVSettings>
                <KVSetting Key="0" Value="0"/>
                <KVSetting Key="default" Value="1"/>
              </KVSettings>
            </Settings>
          </InstanceInitSection>
        </Instance>

        <!-- czujnik poziomu wody (0 - rozwarte/OK; 1 - zwarte/wysoki poziom wody) -->
        <Instance Name="di.water.level">
          <InstanceInitSection>
            <VarAssign Name="input.value" Value="0" SrcVar="rs.0.id.1.input.t.0.err"/>
            <VarAssign Name="setting.flutter.mode" Value="0"/>
            <VarAssign Name="setting.flutter.time" Value="50"/>
            <VarAssign Name="setting.manual.mode" Value="0"/>
            <Settings>
              <KVSettings>
                <KVSetting Key="1" Value="1"/>
                <KVSetting Key="default" Value="0"/>
              </KVSettings>
            </Settings>
          </InstanceInitSection>
        </Instance>

        <!-- impulsowy przepływomierz wody zraszającej -->
        <Instance Name="ai.water.pulse">
          <InstanceInitSection>
            <VarAssign Name="input.value" Value="0" SrcVar="rs.0.id.1.counter.pulse.value"/>
            <VarAssign Name="setting.flutter.mode" Value="0"/>
            <VarAssign Name="setting.flutter.time" Value="1"/>
            <VarAssign Name="setting.manual.mode" Value="0"/>
          </InstanceInitSection>
        </Instance>

        <!-- wilgotność względna powietrza zewnętrznego -->
        <Instance Name="ai.outdoor.humidity">
          <InstanceInitSection>
            <VarAssign Name="input.value" Value="0" SrcVar="rs.0.id.10.input.humidity"/>
            <VarAssign Name="setting.flutter.mode" Value="0"/>
            <VarAssign Name="setting.flutter.time" Value="1"/>
            <VarAssign Name="setting.manual.mode" Value="0"/>
          </InstanceInitSection>
        </Instance>

        <!-- wilgotność względna złoża GWC 1 -->
        <Instance Name="ai.ghe.1.innner.humidity">
          <InstanceInitSection>
            <VarAssign Name="input.value" Value="0" SrcVar="rs.0.id.11.input.humidity"/>
            <VarAssign Name="setting.flutter.mode" Value="0"/>
            <VarAssign Name="setting.flutter.time" Value="1"/>
            <VarAssign Name="setting.manual.mode" Value="0"/>
          </InstanceInitSection>
        </Instance>

        <!-- wilgotność względna powietrza nawiewanego GWC 1 -->
        <Instance Name="ai.ghe.1.vent.supply.humidity">
          <InstanceInitSection>
            <VarAssign Name="input.value" Value="0" SrcVar="rs.0.id.12.input.humidity"/>
            <VarAssign Name="setting.flutter.mode" Value="0"/>
            <VarAssign Name="setting.flutter.time" Value="1"/>
            <VarAssign Name="setting.manual.mode" Value="0"/>
          </InstanceInitSection>
        </Instance>

      </Instances>

    </Initialization>

  </Logic>

</Config>
