1

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?

Markus Safar
  • 6,324
  • 5
  • 28
  • 44
Arthur
  • 159
  • 7

1 Answers1

-1

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");
      ...
    }
    
Markus Safar
  • 6,324
  • 5
  • 28
  • 44
  • 1
    Smart solution but if I connect and value and time the callback will be called twice each time the value change or it will be called only one time cuz it's the same data point ? – Arthur May 05 '23 at 22:22
  • @Arthur The short answer is that with this solution you will only get one callback, as you have connected the dps and their attributes of interest with one `dpConnect` call. So everytime one of these change, the event manager will send one hotlink to the user interface (or control manager) which will result as one callback to be executed. The change of the timestamp is considered a result of the value change. Even if both datapoint elements change at the samte time, there is still only one callback. The detailed explanation for this is a little bit more complicated and won't fit in here – Markus Safar May 06 '23 at 10:25