Source: org/terraswarm/accessor/accessors/web/services/WiFiScan.js

  1. // Copyright (c) 2017 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. /** Obtain a scan of nearby WiFi networks.
  24. *
  25. * The output is in the form below:
  26. * ```
  27. * networks = [
  28. * { "level": signal_level, // raw RSSI value
  29. * "SSID": ssid, // SSID as string, with escaped double quotes: "\"ssid name\""
  30. * "BSSID": bssid // MAC address of WiFi router as string
  31. * "frequency": frequency of the access point channel in MHz
  32. * "capabilities": capabilities // Describes the authentication, key management, and encryption schemes supported by the access point.
  33. * }
  34. * ]
  35. * ```
  36. *
  37. * @accessor services/WiFiScan
  38. * @input trigger Initiates a WiFiScan.
  39. * @output wifiData The list of networks obtained from the
  40. * wifi-scanner module. The output is a JSON array of objects shown
  41. * above.
  42. *
  43. * @author Matt Weber
  44. * @version $$Id: swarmlet.js 1502 2017-04-17 21:34:03Z cxh $$
  45. */
  46. // Stop extra messages from jslint and jshint. Note that there should
  47. // be no space between the / and the * and global. See
  48. // https://chess.eecs.berkeley.edu/ptexternal/wiki/Main/JSHint */
  49. /*globals addInputHandler, console, get, getParameter, getResource, error, exports, extend, get, input, output, parameter, require, send */
  50. /*jshint globalstrict: true*/
  51. 'use strict';
  52. var scanner = require('@accessors-modules/wifi-scanner');
  53. exports.setup = function(){
  54. this.input('trigger');
  55. this.output('wifiData',{
  56. 'type':'JSON'
  57. });
  58. };
  59. //Callback function passed to scanner.scan
  60. function scanSuccess(arg){
  61. this.send('wifiData', arg);
  62. };
  63. //Callback function passed to scanner.scan
  64. function scanFailure(arg){
  65. throw "WiFiScan scanning failed: " + JSON.stringify(arg);
  66. };
  67. exports.initialize = function(){
  68. var thiz = this;
  69. this.addInputHandler('trigger', function(){
  70. scanner.scan(scanSuccess.bind(thiz), scanFailure.bind(thiz));
  71. });
  72. };