Facility Control

 View Only

Capture UDP messages in DashBoard

  • 1.  Capture UDP messages in DashBoard

    Posted 07-30-2018 17:24

    Hi all.
    I was recently asked about capturing UDP traffic in DashBoard so it could be replayed through buttons in a custom panel.

    I thought I would share the example panel I created in case anyone has similar questions going forward.

    Enjoy!

    James

    <abs contexttype="opengear">
       <meta>
          <params>
             <param access="1" constraint="0.0;65535.0;0.0;65535.0;1" constrainttype="INT_STEP_RANGE" name="UDP IN" oid="UDP_IN" precision="0" type="INT32" value="7891" widget="spinner"/>
             <param access="1" constraint="0.0;65535.0;0.0;65535.0;1" constrainttype="INT_STEP_RANGE" name="UDP OUT PORT" oid="UDP_OUT_PORT" precision="0" type="INT32" value="7890" widget="spinner"/>
             <param access="1" maxlength="0" name="Message RX" oid="Message_RX" type="STRING" value="" widget="multiline-text"/>
             <param access="1" maxlength="0" name="UDP OUT HOST" oid="UDP_OUT_HOST" type="STRING" value="localhost" widget="text"/>
             <param access="1" maxlength="0" name="Test Message" oid="Test_Message" type="STRING" value="00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 01 02 03 04" widget="text"/>
          </params>
          <listener autostart="true" listenport="%value[UDP_IN][0]%" maxlength="1024" udp="true">
             <task tasktype="ogscript">if (event.isMessageEvent())
    {
       var bytes = event.getBytes();
       ogscript.sendUDPBytes(params.getValue('UDP_OUT_HOST', 0), params.getValue('UDP_OUT_PORT', 0), bytes);
       
       var bytesAsString = '';
       for (var i = 0; i &lt; bytes.length; i++)
       {
          var byteAsString = (0xFF &amp; (bytes[i] + 0)).toString(16).toUpperCase();
          if (byteAsString.length == 1)
          {
             byteAsString = '0' + byteAsString;
          }
          bytesAsString = bytesAsString + byteAsString + ' ';
       }
    
       params.setValue('Message_RX', 0, (params.getValue('Message_RX', 0) + '\n' + bytesAsString).trim());
    }</task>
          </listener>
       </meta>
       <label height="47" left="18" name="UDP IN PORT:" style="txt-align:east" top="22" width="152"/>
       <param expand="true" height="44" left="174" oid="UDP_IN" top="26" width="182">
          <task tasktype="ogscript">ogscript.reload(null);</task>
       </param>
       <label height="47" left="19" name="UDP OUT HOST/PORT:" style="txt-align:east;" top="77" width="152"/>
       <param expand="true" height="51" left="422" oid="UDP_OUT_PORT" top="79" width="183"/>
       <param height="197" left="15" oid="Message_RX" top="147" width="438"/>
       <param expand="true" height="46" left="179" oid="UDP_OUT_HOST" top="80" width="237"/>
       <button buttontype="push" height="42" left="461" name="Test" top="402" width="125">
          <task tasktype="ogscript">ogscript.sendUDPAsBytes('localhost', params.getValue('UDP_IN', 0), params.getValue('Test_Message', 0));</task>
       </button>
       <button buttontype="push" height="38" left="14" name="Clear" top="353" width="443">
          <task tasktype="ogscript">params.setValue('Message_RX', 0, '');
    </task>
       </button>
       <param expand="true" height="45" left="20" oid="Test_Message" top="400" width="436"/>
    </abs>