Mhttpd.js

From MidasWiki
Revision as of 09:28, 9 September 2013 by Olchansk (talk | contribs) (→‎About)
Jump to navigation Jump to search

About

mhttpd.js contains javascript function wrappers for most MIDAS AJAX functions typically used for implementing interactive custom pages for MIDAS experiments.

AJAX is a set of web development techniques used to construct interactive web applications:

mhttpd.js is typically served by mhttpd from $MIDASSYS/resources/mhttpd.js

<html>
<head>
...
<script src='mhttpd.js'></script>
...
</head>
...
</html>

A mostly up to date copy of mhttpd.js is linked here:

An example for using all mhttpd.js functions is included in the MIDAS distribution under examples/javascript1:

MIDAS AJAX functions can be accessed directly without using mhttpd.js wrappers (i.e. using the JSON-P script-tag method)

  • AJAX - MIDAS AJAX functions

By default, MIDAS AJAX functions assume that the web page was loaded from mhttpd and use relative URLs to access AJAX functions of the same mhttpd. If the web page is loaded from some other source, or needs to talk to a different mhttpd, use the ODBSetURL() function to set the base URL of the target mhttpd. Such cross-site access is normally blocked by web browsers, but recent versions of mhttpd implement the "cross-origin resource sharing" http header to permit such access.

Functions

Helper functions:

  • function getMouseXY(e)
  • function XMLHttpRequestGeneric()
  • function ODBExtractRecord(record, key)
  • function ODBEdit(path)
  • function ODBFinishInlineEdit(...)
  • function ODBInlineEditKeydown(...)
  • function ODBInlineEdit(...)

ODB access functions:

  • function ODBSet(path, value, pwdname) - write into ODB
  • function ODBGet(path, format, defval, len, type) - read from ODB
  • function ODBMGet(paths, callback, formats) - read multiple ODB entries
  • function ODBGetRecord(path) - read from ODB
  • function ODBKey(path) - get the description of an ODB entry
  • function ODBCopy(path, format) - read an ODB subtree in ODB, XML or JSON format
  • function ODBMCopy(paths, callback, format) - read multiple ODB subtrees in ODB, XML or JSON format, asynchronously
  • function ODBMCreate(paths, types, callback) - create multiple ODB entries using db_create_key(path, type)
  • function ODBMDelete(paths, callback) - delete multiple ODB entries using db_delete_key(path)
  • function ODBMRename(paths, names, callback) - rename multiple ODB entries using db_rename_key(path, name)
  • function ODBMLink(paths, links, callback) - create symbolic links using db_create_link(link, path)
  • function ODBMReorder(paths, indices, callback) - reorder ODB entries using db_reorder_key(path, index)

MIDAS access functions:

  • function ODBRpc_rev0(name, rpc, args)
  • function ODBRpc_rev1(name, rpc, max_reply_length, args)
  • function ODBGetMsg(n) - get midas messages from midas.log
  • function ODBGenerateMsg(m) - write a midas message
  • function ODBGetAlarms() - get list of active alarms

Summary table

Summary of mhttpd.js functions
Function XML JSON JSON-P async AJAX URI encoding
ODBSet(path, value, pwdname) no no no no cmd=jset by caller, except for "value"
ODBGet(path, format, defval, len, type) no no no no cmd=jget by caller, except for "format"
ODBMGet(paths, callback, formats) no no no yes cmd=jget by caller, including "formats"
ODBGetRecord(path) no no no no cmd=jget with "name=1" by caller
ODBKey(path) no yes no no cmd=jkey by caller
ODBCopy(path, format) yes yes yes no cmd=jcopy by caller, except for "format"
ODBMCopy(paths, callback, format) yes yes yes yes cmd=jcopy internal
ODBMCreate(paths, types, callback) no yes yes yes cmd=jcreate internal
ODBMRename(paths, names, callback) no yes yes yes cmd=jrename internal
ODBMDelete(paths, callback) no yes yes yes cmd=jdelete internal
ODBMLink(paths, links, callback) no yes yes yes cmd=jlink internal
ODBMReorder(paths, indices, callback) no yes yes yes cmd=jreorder internal
ODBRpc_rev0(name, rpc, args) no no no no cmd=jrpc_rev0
ODBRpc_rev1(name, rpc, max_reply_length, args) no no no no cmd=jrpc_rev1
ODBRpc(...) yes yes yes yes cmd=jrpc
ODBGetMsg(n) no no no no cmd=jmsg not needed
ODBGenerateMsg(m) no no no no cmd=jgenmsg by caller
ODBGetAlarms() no no no no cmd=jalm -

ODBSet

See: AJAX#jset

See: http://ladd00.triumf.ca/~daqweb/doc/midas-old/html/RC_mhttpd_custom_ODB_access.html#RC_mhttpd_custom_odbset

Prototype:

  • function ODBSet(path, value, pwdname)

Arguments:

  • path - ODB path, must be URI-encoded
  • value - value to write, must NOT be URI-encoded (encoding is done inside ODBSet())
  • pwdname - see AJAX#jset

Returns: nothing

Error handling: issues an alert() if there are errors.

BUG: may call alert() and require user interaction.

ODBGet

See: AJAX#jset

See: http://ladd00.triumf.ca/~daqweb/doc/midas-old/html/RC_mhttpd_custom_ODB_access.html#RC_mhttpd_custom_odbget

Prototype:

  • function ODBGet(path, format, defval, len, type)

Arguments:

  • path - ODB path to read, must be URI-encoded
  • format - sprintf format for the data, must NOT be URI-encoded
  • defval - if ODB path does not exist, it is created using AJAX#jset with this value, must be URI-encoded. Disable this by using "undefined"
  • len - data length for jset
  • type - data type for jset

ODBMGet

See: AJAX#jget

See: http://ladd00.triumf.ca/~daqweb/doc/midas-old/html/RC_mhttpd_custom_ODB_access.html#RC_mhttpd_custom_odbmget

Prototype:

  • function ODBMGet(paths, callback, formats)

Arguments:

  • paths - array of strings - ODB paths to read, must be URI-encoded
  • callback - name of javascript function for asynchronous callback
  • formats - array of strings - sprintf() format strings for each ODB path, see BUGS in AJAX#jget, must by URI-encoded

Valid ODB path targets are: simple variables and arrays (specified as "/eq/foo/var/array[*]").

ODBMGet for subdirectories does not work (AJAX jget returns correct data, but incorrectly decoded by ODBMGet() javascript code).

ODBMGet for arrays specified without "[*]" is unknown.

ODBGetRecord

See: AJAX#jget with parameter "name=1"

See: http://ladd00.triumf.ca/~daqweb/doc/midas-old/html/RC_mhttpd_custom_ODB_access.html#RC_mhttpd_custom_odbgetrecord

Prototype:

  • function ODBGetRecord(path)

Arguments:

  • path - ODB path to read, must be URI-encoded

Returns:

  • request.responseText
  • use "function ODBExtractRecord(record, key)" to extract ODB values from data retuned by ODBGetRecord().

Valid ODB path targets are: simple variables, array elements ("/eq/foo/var/array[0]"), arrays (specified as "/eq/foo/var/array[*]") and subdirectories ("/experiment")

Usage:

      var rec1 = ODBGetRecord(encodeURIComponent("/Equipment/RpcExample/Variables"));
      document.getElementById('foo').innerHTML = rec1;
      document.getElementById('bar').innerHTML = ODBExtractRecord(rec1, 'SLOW[2]');

ODBKey

See: AJAX#jkey

Prototype:

  • function ODBKey(path)

Arguments:

  • path - ODB path, must be URI-encoded

Returns: javascript object with data fields corresponding to the data fields of MIDAS KEY (midas.h):

{"name":"SLOW","type":"TID_DOUBLE","num_values":"3","item_size":"8","last_written":"1376633931"}

Usage:

var key = new ODBKey(encodeURIComponent("/Equipment/RpcExample/Variables/SLOW"));
document.getElementById('foo').innerHTML = "name: " + key.name + " type: " + key.type;

ODBCopy

See: AJAX#jcopy

Prototype:

  • function ODBCopy(path, format)

Arguments:

  • path - ODB path, must be URI-encoded
  • format - data format (same as "encoding"), must NOT be URI-encoded

Returns: request.responseText, use JSON.parse() to decode JSON data.

Usage:

var data_json = ODBCopy("/Equipment/RpcExample/Variables", "json");
var obj = JSON.parse(data_json);
document.getElementById('json_data0').innerHTML = obj.SLOW[0];

Return value:

{
  "SLOW/key" : { "type" : 10, "num_values" : 3, "last_written" : 1376634320 },
  "SLOW" : [ 2357, 1376634320, -8.6602540236010100e+01 ]
}

ODBMCopy

See: AJAX#jcopy

Prototype:

  • function ODBMCopy(paths, callback, encoding)

Arguments:

  • paths - array of strings - odb paths to read
  • callback - name of javascript function for asynchronous callback, or "undefined" for synchronous return
  • encoding - requested data encoding - ODB, XML or JSON, see AJAX#jcopy

Returns:

  • synchrounous: returns request.responseText, use JSON.parse() to decode JSON data
  • asynchronous: calls the callback function with a single argument set to request.responseText, use JSON.parse() to decode JSON data

Usage:

      var paths = [
      "/Equipment/deapvme01/Settings/EnableControl",
      "/Equipment/deapvme01/Settings/mainSwitch",
      "/Equipment/deapvme01/Readback/sysMainSwitch.0",
      "/Equipment/deapvme01/Variables",
      "/Equipment/deapvme02/Settings/EnableControl",
      "/Equipment/deapvme02/Settings/mainSwitch",
      "/Equipment/deapvme02/Readback/sysMainSwitch.0",
      "/Equipment/deapvme02/Variables",
      "/Equipment/deapvme03/Settings/EnableControl",
      "/Equipment/deapvme03/Settings/mainSwitch",
      "/Equipment/deapvme03/Readback/sysMainSwitch.0",
      "/Equipment/deapvme03/Variables",
      ];

      ODBMCopy(paths, load_callback, "json");
...
      function load_callback(data)
      {
      var obj = JSON.parse(data);
      document.getElementById('enableControl1').innerHTML = obj[0].EnableControl;
      document.getElementById('mainSwitch1').innerHTML = obj[1]['mainSwitch'] + " / " + obj[2]['sysMainSwitch.0'];
      document.getElementById('p5v1').innerHTML = obj[3].current[0];
...

JSON data returned from ODBMCopy():

[ { "EnableControl/key" : { "type" : 7, "last_written" : 1375145722 }, "EnableControl" : 1 } , { "mainSwitch/key" : { "type" : 7, "last_written" : 1376610870 }, "mainSwitch" : 1 } , { "sysMainSwitch.0/key" : { "type" : 7, "last_written" : 1376681747 }, "sysMainSwitch.0" : 1 } , { "switch/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681747 }, "switch" : [ 1, 1, 1 ], "status/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681747 }, "status" : [ 1, 1, 1 ], "demandVoltage/key" : { "type" : 9, "num_values" : 3, "last_written" : 1376681747 }, "demandVoltage" : [ 5, 12, 12 ], "senseVoltage/key" : { "type" : 9, "num_values" : 3, "last_written" : 1376681747 }, "senseVoltage" : [ 5.0500002e+00, 1.1980000e+01, 1.1990000e+01 ], "current/key" : { "type" : 9, "num_values" : 3, "last_written" : 1376681747 }, "current" : [ 9.0229996e+01, 4.2700000e+00, 5.9899998e+00 ], "sparkCount/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681747 }, "sparkCount" : [ 0, 0, 0 ], "sensorTemperature/key" : { "type" : 7, "num_values" : 8, "last_written" : 1376681747 }, "sensorTemperature" : [ -128, -128, 25, -128, -128, 25, -128, -128 ], "fanSpeed/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681747 }, "fanSpeed" : [ 3180, 3112, 3236 ] } , { "EnableControl/key" : { "type" : 7, "last_written" : 1375145722 }, "EnableControl" : 1 } , { "mainSwitch/key" : { "type" : 7, "last_written" : 1376603253 }, "mainSwitch" : 1 } , { "sysMainSwitch.0/key" : { "type" : 7, "last_written" : 1376681750 }, "sysMainSwitch.0" : 1 } , { "switch/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681750 }, "switch" : [ 1, 1, 1 ], "status/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681750 }, "status" : [ 1, 1, 1 ], "demandVoltage/key" : { "type" : 9, "num_values" : 3, "last_written" : 1376681750 }, "demandVoltage" : [ 5, 12, 12 ], "senseVoltage/key" : { "type" : 9, "num_values" : 3, "last_written" : 1376681750 }, "senseVoltage" : [ 5, 1.1950000e+01, 1.1980000e+01 ], "current/key" : { "type" : 9, "num_values" : 3, "last_written" : 1376681750 }, "current" : [ 1.4169999e+01, 1.1000000e+00, 8.0000001e-01 ], "sparkCount/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681750 }, "sparkCount" : [ 0, 0, 0 ], "sensorTemperature/key" : { "type" : 7, "num_values" : 8, "last_written" : 1376681750 }, "sensorTemperature" : [ -128, -128, -128, -128, -128, -128, -128, 22 ], "fanSpeed/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681750 }, "fanSpeed" : [ 3217, 3225, 3213 ] } , { "EnableControl/key" : { "type" : 7, "last_written" : 1375145722 }, "EnableControl" : 1 } , { "mainSwitch/key" : { "type" : 7, "last_written" : 1376596430 }, "mainSwitch" : 1 } , { "sysMainSwitch.0/key" : { "type" : 7, "last_written" : 1376681752 }, "sysMainSwitch.0" : 1 } , { "switch/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681752 }, "switch" : [ 1, 1, 1 ], "status/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681752 }, "status" : [ 1, 1, 1 ], "demandVoltage/key" : { "type" : 9, "num_values" : 3, "last_written" : 1376681752 }, "demandVoltage" : [ 5, 12, 12 ], "senseVoltage/key" : { "type" : 9, "num_values" : 3, "last_written" : 1376681752 }, "senseVoltage" : [ 4.9800000e+00, 1.2009999e+01, 1.1969999e+01 ], "current/key" : { "type" : 9, "num_values" : 3, "last_written" : 1376681752 }, "current" : [ 9.1139999e+01, 4.1999998e+00, 6.0300002e+00 ], "sparkCount/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681752 }, "sparkCount" : [ 0, 0, 0 ], "sensorTemperature/key" : { "type" : 7, "num_values" : 8, "last_written" : 1376681752 }, "sensorTemperature" : [ -128, -128, 24, -128, -128, 27, -128, -128 ], "fanSpeed/key" : { "type" : 7, "num_values" : 3, "last_written" : 1376681752 }, "fanSpeed" : [ 3153, 3067, 3180 ] } ]

ODBGetMsg

See: AJAX#jmsg

Prototype:

  • function ODBGetMsg(n)

Arguments:

  • n - number of midas messages to return

Returns: array of strings, if n==1, as single string.

Usage:

var data = ODBGetMsg(10);
document.getElementById("foo").innerHTML = data.join("\n");

ODBGenerateMsg

See: AJAX#jgenmsg

Prototype:

  • function ODBGenerateMsg(m)

Arguments:

  • m - text written into midas messages, must be URI-encoded.

Usage:

ODBGenerateMsg(encodeURIComponent("message text"));

ODBGetAlarms

See: AJAX#jalm

Prototype:

  • function ODBGetAlarms()

Returns: array of strings

Usage:

var data = ODBGetAlarms();
document.getElementById("foo").innerHTML = data.join("\n");