Search code examples
scadawincc-oa

How to know which data point was trigged inside dpConnect()


As the documentation of dpConnect([class object,] string|function_ptr work, [bool answer,] string dp1 [, string dp2 ...] | dyn_string dp_list) states, the work function is called when dp1 | dp2... are changed.

How do we know which one of the connected datapoint elements triggered the work function?


Solution

  • As you already noticed, the callback you register with dpConnect gets called when either of the connected datapoint values changes.

    There are multiple possible solutions to your scenario where the most common are the two following ones:

    1. If you are just interested in getting the callback when either of the specified datapoint element's value changes and you do not need the values from all connected datapoint elements within your callback, you can simply connect the callback separately for each datapoint element of interest.

      Instead of writing...

      ...
      dpConnect("Callback", "SomeExampleDatapoint1", "SomeExampleDatapoint2");
      ...
      
      /**
        @summary: Gets called when either of the connected datapoint values changes.
        @param firstDpName: The name of the datapoint element that has changed.
        @param firstValue: The new value the datapoint element has changed to.
        @param secondDpName: The name of the datapoint element that has changed.
        @param secondValue: The new value the datapoint element has changed to.
        */
      private void Callback(string firstDpName, int firstValue,
                            string secondDpName, int secondValue)
      {
        ...
      }
      

      ...you can can write the following:

      ...
      dpConnect("Callback", "SomeExampleDatapoint1");
      dpConnect("Callback", "SomeExampleDatapoint2");
      ...
      
      /**
        @summary: Gets called when either of the connected datapoint values changes.
        @param name: The name of the datapoint element that has changed.
        @param vlue: The new value the datapoint element has changed to.
        */
      private void Callback(string name, int value)
      {
        ...
      }
      
    2. Connect to the source time (_online.._stime) of the datapoint elements in addition to their values. In this way you not only retrieve the values but also the timestamp of their last change. The following example should guide you in the right direction:
      (I have specified _online.._value explicitly in the below example to clarify that both attributes (_value and _stime) are getting connected. Of course specifying _online.._value is not required but it increased the readability.)

      ...
      dpConnect("Callback",
                "SomeExampleDatapoint1:_online.._value",                   
                "SomeExampleDatapoint1:_online.._stime",                   
                "SomeExampleDatapoint2:_online.._value",                   
                "SomeExampleDatapoint2:_online.._stime");
      ...
      
      /**
        @summary: Gets called when either of the connected datapoint values changes.
        @param firstValueDpName: The name of the datapoint element that has changed.
        @param firstValue: The new value the datapoint element has changed to.
        @param firstSourceTimeDpName: The name of the datapoint element that has changed.
        @param firstSourceTime: The timestamp when the datapoint element has changed.
        @param secondValueDpName: The name of the datapoint element that has changed.
        @param secondValue: The new value the datapoint element has changed to.
        @param secondSourceTimeDpName: The name of the datapoint element that has changed.
        @param secondSourceTime: The timestamp when the datapoint element has changed.
        */
      private void Callback(string firstValueDpName, int firstValue,
                            string firstSourceTimeDpName, time firstSourceTime,
                            string secondValueDpName, int secondValue,
                            string secondSourceTimeDpName, time secondSourceTime)
      {
        // Evaluate whether the first datapoint element changed last or not
        bool firstDpElementChangedLast = firstSourceTime > secondSourceTime;
      
        if (firstDpElementChangedLast)
          DebugN(firstValueDpName + " changed last");
        else
          DebugN(secondValueDpName + " changed last");
        ...
      }