Source: org/terraswarm/accessor/accessors/web/contextAware/ContextAwareTest.js

  1. // Copyright (c) 2015-2016 The Regents of the University of California.
  2. // All rights reserved.
  3. //
  4. // Permission is hereby granted, without written agreement and without
  5. // license or royalty fees, to use, copy, modify, and distribute this
  6. // software and its documentation for any purpose, provided that the above
  7. // copyright notice and the following two paragraphs appear in all copies
  8. // of this software.
  9. //
  10. // IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  11. // FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  12. // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  13. // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  14. // SUCH DAMAGE.
  15. //
  16. // THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  17. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
  19. // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
  20. // CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
  21. // ENHANCEMENTS, OR MODIFICATIONS.
  22. //
  23. /** This accessor discovers IOT services on web.
  24. * It requires the contextAware module. Please see:
  25. * https://www.icyphy.org/accessors/wiki/Version0/ContextAware
  26. *
  27. * @accessor contextAware/contextAwareTest
  28. * @author Anne H. Ngu (angu@btxstate.edu)
  29. * @input {number} input to the accessor
  30. * @parameter {{string} the name of the REST service that context aware tries
  31. * to adapt. A list of available services are presented as option.
  32. * @version $$Id$$
  33. */
  34. // Stop extra messages from jslint and jshint. Note that there should be no
  35. // space between the / and the * and global. See https://chess.eecs.berkeley.edu/ptexternal/wiki/Main/JSHint */
  36. /*global addInputHandler, console, exports, extend, get, getParameter, implement, input, output, parameter, removeInputHandler, require, send */
  37. /*jshint globalstrict: true*/
  38. "use strict";
  39. // Use the SandBox version for test
  40. var contextAware = require("contextAwareTest");
  41. // Initialize the context aware service discovery class. Not used currently
  42. var contextAwareService = new contextAware.DiscoveryOfRESTService();
  43. var selectedService;
  44. exports.setup = function () {
  45. this.input('input');
  46. // a simple UI interface to start the dialog with users
  47. this.parameter('RESTSource', {
  48. 'type': 'string',
  49. 'value': 'Make a selection',
  50. 'options': contextAware.services()
  51. });
  52. selectedService = this.getParameter('RESTSource');
  53. if (selectedService == 'GSN') {
  54. this.implement("contextAware/GSNInterface.js");
  55. this.input('dataType', {
  56. 'type': 'string',
  57. 'value': 'all',
  58. 'options': contextAware.gsnServices()
  59. });
  60. } else if (selectedService == 'Paraimpu') {
  61. this.implement("contextAware/ParaimpuInterface.js");
  62. this.input('dataType', {
  63. type: 'string',
  64. value: 'all',
  65. 'options': contextAware.paraimpuServices()
  66. });
  67. } else if (selectedService == 'Firebase') {
  68. this.implement("contextAware/FirebaseInterface.js");
  69. this.input('dataType', {
  70. type: 'string',
  71. value: 'all',
  72. 'options': contextAware.firebaseServices()
  73. });
  74. } else {
  75. console.log("Cannot load service interface !!");
  76. }
  77. this.extend("net/REST.js");
  78. this.input('command', {
  79. 'visibility': 'expert'
  80. });
  81. this.input('arguments', {
  82. 'visibility': 'expert'
  83. });
  84. this.input('options', {
  85. 'visibility': 'expert'
  86. });
  87. this.output('header', {
  88. 'visibility': 'expert'
  89. });
  90. this.input('trigger', {
  91. 'visibility': 'expert'
  92. });
  93. };
  94. /** Upon receiving details of a REST service, construct a concrete accessor to access it.
  95. */
  96. var handle;
  97. exports.initialize = function () {
  98. // The superclass registers a handler for the 'trigger' input
  99. // to issue an HTTP request based on the current inputs.
  100. this.ssuper.initialize();
  101. var serviceParam; //the input that is needed for the options port in REST
  102. // Add a handler for the 'input' input.
  103. handle = this.addInputHandler('input', function () {
  104. console.log("ContextAwareTest.js input handler start");
  105. serviceParam = contextAwareService.discoverServices();
  106. console.log("org/terraswarm/accessor/accessors/web/contextAware/ContextAwareTest.js: serviceParam: " + serviceParam);
  107. //var serviceURL = this.getParameter('ipAddress');
  108. var serviceURL = {
  109. "url": {
  110. "host": getParameter('host'),
  111. "port": this.getParameter('port'),
  112. "protocol": this.getParameter('protocol')
  113. }
  114. };
  115. this.send('options', serviceURL);
  116. this.send('command', this.getParameter('path'));
  117. if (selectedService == 'Paraimpu') {
  118. //sample access token to use "46e0ee55195c4dd9dca295a7ac8282d28f4a2259"
  119. var arg = {
  120. "access_token": this.getParameter('accessToken')
  121. };
  122. console.log("org/terraswarm/accessor/accessors/web/contextAware/ContextAwareTest.js: access_token:" + arg);
  123. send('arguments', arg);
  124. }
  125. //ex. of valid json format for reference
  126. //send('options', {"url":"http://pluto.cs.txstate.edu:22001"});
  127. //send('options', {"url":{"host":"pluto.cs.txstate.edu","port":22001}});
  128. // Cause the base class handler to issue the HTTP request.
  129. this.send('trigger', true);
  130. //send('response', this.issueCommand(handleResponse))
  131. // console.log(this.get('response'));
  132. console.log("ContextAwareTest.js input handler end");
  133. });
  134. };
  135. /** Filter the response from Firebase.
  136. */
  137. var getFirebaseData = function (response) {
  138. var type = this.get('dataType');
  139. var result = JSON.parse(response);
  140. switch (type) {
  141. case "microwave":
  142. this.send('microwave', result.Microwave);
  143. console.log("ContextAwareTest filterResponse() " + JSON.stringify(result.Microwave));
  144. break;
  145. case "microwaveStatus":
  146. this.send('microwaveStatus', result.Microwave.status);
  147. break;
  148. case "pastValues":
  149. this.send('pastValues', result.Microwave.pastValues);
  150. break;
  151. case "all":
  152. this.send('microwave', result.Microwave);
  153. this.send('microwaveStatus', result.Microwave.status);
  154. this.send('pastValues', result.Microwave.pastValues);
  155. break;
  156. default:
  157. this.send('microwave', result.Microwave);
  158. }
  159. };
  160. /** Filter the response from Paraimpu.
  161. */
  162. var getParaimpuData = function (response) {
  163. var type = this.get('dataType');
  164. var result = JSON.parse(response);
  165. switch (type) {
  166. case "payload":
  167. this.send('payload', result.payload);
  168. console.log("ContextAwareTest filterResponse() " + JSON.stringify(result.payload));
  169. break;
  170. case "thingId":
  171. this.send('sensorId', result.thingId);
  172. break;
  173. case "producer":
  174. this.send('producer', result.producer);
  175. break;
  176. case "all":
  177. this.send('payload', result.payload);
  178. this.send('sensorId', result.thingId);
  179. this.send('producer', result.producer);
  180. break;
  181. default:
  182. this.send('response', result);
  183. }
  184. };
  185. /** Filter the response from GSN. Need to convert the data to json format first.
  186. */
  187. var getGSNData = function (response) {
  188. var type = this.get('dataType');
  189. var xmlJson = {};
  190. xmlJson = contextAware.xmlToJson(response);
  191. var result = JSON.parse(xmlJson);
  192. switch (type) {
  193. case "sound":
  194. // jsdoc was failing with "line 271: missing name after . operator"
  195. // This code has no tests because the GSN source on the web does not stay up.
  196. // http://stackoverflow.com/questions/19217365/missing-name-after-operator-yui-compressor-for-socket-io-js-files
  197. // suggests using ['..']
  198. //send('sound', result."virtual-sensor"[2].field[2]);
  199. this.send('sound', result['virtual-sensor'][2].field[2]);
  200. break;
  201. case "sensorName":
  202. //send('sensorName', result."virtual-sensor"[2].name);
  203. this.send('sensorName', result['virtual-sensor'][2].name);
  204. break;
  205. case "all":
  206. //send('sound', result."virtual-sensor"[2].field[2]);
  207. this.send('sound', result['virtual-sensor'][2].field[2]);
  208. //send('sensorName', result."virtual-sensor"[2].name);
  209. this.send('sensorName', result['virtual-sensor'][2].name);
  210. break;
  211. default:
  212. //send('response', result."virtual-sensor");
  213. this.send('response', result['virtual-sensor']);
  214. }
  215. };
  216. /**
  217. * Filter the response. It overrides the filterResponse() in the base class to
  218. * extract a portion of the response that is defined in the corresponding
  219. * service interface
  220. */
  221. exports.filterResponse = function (response) {
  222. switch (selectedService) {
  223. case "GSN":
  224. getGSNData.call(this, response);
  225. break;
  226. case "Paraimpu":
  227. getParaimpuData.call(this, response);
  228. break;
  229. case "Firebase":
  230. getFirebaseData.call(this, response);
  231. break;
  232. }
  233. console.log("Response" + response);
  234. return response;
  235. };
  236. /*var itemList = [];
  237. var type = "Microwave";
  238. var itemKeys = Object.keys(result);
  239. for (var x in itemKeys) {
  240. itemList.push(itemKeys[x]);
  241. }
  242. for (var y in itemList) {
  243. if (itemList[y] == "Microwave")
  244. console.log("ContextAwareTest filterResponse() " + JSON.stringify("result."+ type));
  245. }
  246. console.log("ContextAwareTest filterResponse() " + JSON.stringify(result.Microwave.pastValues));
  247. */
  248. // return result;
  249. //};
  250. exports.wrapup = function () {
  251. this.removeInputHandler(handle);
  252. };