Mhttpd.js: Difference between revisions

From MidasWiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 565: Line 565:
</pre>
</pre>


[[Category:Web server mhttpd]] [[Category:Custom]] [[Category:Javascript library]]
[[Category:Custom]] [[Category:Javascript library]]

Revision as of 16:27, 13 July 2015


Links

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) --- removed Feb 2014
  • function XMLHttpRequestGeneric()
  • function ODBExtractRecord(record, key)
  • function ODBEdit(path)
  • function ODBFinishInlineEdit(...)
  • function ODBInlineEditKeydown(...)
  • function ODBInlineEdit(...)
  • function ODBCall(...)

ODB access functions:

  • function ODBSetURL(url_of_mhttpd) - set the URL of target mhttpd (if web page is loaded from different source)
  • function ODBSet(path, value, pwdname) - write into ODB
  • function ODBMCopy(paths, callback, format) - read multiple ODB subtrees in ODB, XML or JSON format, asynchronously
  • function ODBMLs(paths, callback) - return ODB directory listings (same as odbedit ls -l)

Obsolete ODB access functions replaced by ODBMCopy():

  • 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 --- replaced by ODBMKey()
  • function ODBCopy(path, format) - read an ODB subtree in ODB, XML or JSON format

ODB entries creation and modification:

  • function ODBMCreate(paths, types, arraylengths, stringlengths, callback) - create multiple ODB entries using db_create_key(path, type)
  • function ODBMResize(paths, arraylengths, stringlengths, callback) - resize multiple ODB arrays, including element size of strings and string arrays
  • function ODBMKey(paths, callback) - read multiple ODB KEYs
  • 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) --- replaced by ODBRpc()
  • function ODBRpc_rev1(name, rpc, max_reply_length, args) --- replaced by ODBRpc()
  • function ODBRpc(name, command, args, callback, max_reply_length)
  • 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

Note: "*" marks obsoleted functions. Please avoid using them for new designs.

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
ODBMLs(paths, callback) no yes yes yes cmd=jcopy internal
ODBMCreate(paths, types, arraylengths, stringlengths, callback) no yes yes yes cmd=jcreate internal
ODBMResize(paths, arraylengths, stringlengths, callback) no yes yes yes cmd=jresize internal
ODBMRename(paths, names, callback) no yes yes yes cmd=jrename internal
ODBMKey(paths, callback) no yes yes yes cmd=jkey&encoding=json 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 -

ODBSetURL

Prototype:

  • function ODBSetURL(url)

Arguments:

  • url - URL of mhttpd we want to talk to.]

Returns: nothing

Example:

ODBSetURL("http://localhost:8080/");

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:

  • synchronous: 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 ] } ]

ODBMLs

Description: return ODB directory listing with full ODB KEY information (same as odbedit ls -l). It uses the jcopy AJAX method with the JSON "norecurse" option. Same as with odbedit "ls -l", symlinks are followed and the linked-to values are reported, with the symlink path reported as "link" entry in the ODB KEY ("xxx/key") section.

See: AJAX#jcopy

Prototype:

  • function ODBMLs(paths, callback)

Arguments:

  • paths - array of strings - odb paths to report
  • callback - name of javascript function for asynchronous callback, or "undefined" for synchronous return

Returns:

  • array of JSON encoded ODB entries
  • synchronous: 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 = [ "/", "/Test/noexistant", "/Experiment" ]; 
var data = ODBMLs(paths);
var obj = JSON.Parse(data);

ODBMCreate

See: AJAX#jcreate

Prototype:

  • function ODBMCreate(paths, types, arraylengths, stringlengths, callback)

Arguments:

  • paths - array of strings - odb paths to create
  • types - array of integers - midas data types of created keys (TID_INT, etc)
  • arraylengths - array of integers - "undefined" - or array size to create
  • stringlengths - array of integers - "undefined" - or array of lengths of strings to create
  • callback - name of javascript function for asynchronous callback, or "undefined" for synchronous return

Returns:

  • array of integers: status of db_create_key() for each element of "paths"
  • synchronous: 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 = [ "/Test/foo", "/Test/noexistant", "/Test/bar" ]; 
var types = [ TID_INT, 0, TID_STRING ]; 
ODBMCreate(paths, types);

ODBMResize

See: AJAX#jresize

Prototype:

  • function ODBMResize(paths, arraylengths, stringlengths, callback)

Arguments:

  • paths - array of strings - odb paths to resize
  • arraylengths - array of integers - new array size, use "0" for "no change"
  • stringlengths - array of integers - new string length or new element length for string arrays, use "0" for "no change"
  • callback - name of javascript function for asynchronous callback, or "undefined" for synchronous return

Returns:

  • array of integers: status of db_set_num_values() for each element of "paths"
  • synchronous: 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 = [ "/Test/foo", "/Test/noexistant", "/Test/bar" ]; 
var arraylengths = [ 10, 0, 15 ]; 
var stringarrays = [ 0, 0, 16 ];
ODBMResize(paths, arraylengths, stringlengths);

ODBMKey

See: AJAX#jkey

Prototype:

  • function ODBMKey(paths, callback)

Arguments:

  • paths - array of strings - odb paths to read ODB KEYs
  • callback - name of javascript function for asynchronous callback, or "undefined" for synchronous return

Returns:

  • array of ODB KEYs
  • synchronous: 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 = [ "/Test/foo", "/Test/noexistant", "/Test/bar" ]; 
var data = JSON.parse(ODBMKey(paths));

Example:

var paths = [ "/Test/foo", "/Test/noexistant", "/Test/bar", "/Test/foofoo", "/Test/barlink", "/Test/foo10", "/Test/bar15" ];
var data = ODBMKey(paths);
xprint data;
[ { "name":"foo","type":7,"type_name":"TID_INT","num_values":1,"item_size":4,"last_written":0 }, { "/error":312 }, { "name":"bar","type":12,"type_name":"TID_STRING","num_values":1,"item_size":0,"last_written":0 }, { "/error":312 }, { "name":"bar","type":12,"type_name":"TID_STRING","num_values":1,"item_size":0,"last_written":0 }, { "name":"foo10","type":7,"type_name":"TID_INT","num_values":5,"item_size":4,"last_written":1380134916 }, { "name":"bar15","type":12,"type_name":"TID_STRING","num_values":7,"item_size":12,"last_written":1380134916 } ]

ODBMDelete

See: AJAX#jdelete

Prototype:

  • function ODBMDelete(paths, callback)

Arguments:

  • paths - array of strings - odb paths to delete
  • callback - name of javascript function for asynchronous callback, or "undefined" for synchronous return

Returns:

  • array of integer: status of db_delete_key() for each element of "paths"
  • synchronous: 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 = [ "/Test/foo", "/Test/noexistant", "/Test/bar" ]; 
ODBMDelete(paths);

ODBMRename

See: AJAX#jrename

Prototype:

  • function ODBMRename(paths, names, callback)

Arguments:

  • paths - array of strings - odb paths to rename
  • names - array of strings - new names (subject to ODB limitation on key names)
  • callback - name of javascript function for asynchronous callback, or "undefined" for synchronous return

Returns:

  • array of integer: status of db_rename_key() for each element of "paths"
  • synchronous: 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 = [ "/Test/foo", "/Test/noexistant", "/Test/bar" ]; 
var names = [ "fooxxx", "zzz", "barxxx" ];
ODBMRename(paths, names);

ODBMLink

See: AJAX#jlink

Prototype:

  • function ODBMLink(paths, links, callback)

Arguments:

  • paths - array of strings - odb paths create as symlinks
  • links - array of strings - odb paths of existing odb entries that we link to
  • callback - name of javascript function for asynchronous callback, or "undefined" for synchronous return

Returns:

  • array of integer: status of db_create_link() for each element of "paths"
  • synchronous: 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 = [ "/Test/foolink", "/Test/barlink" ]; 
var links = [ "/Test/foo", "/Test/bar" ];
ODBMLink(paths, links);

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");