File size: 2,782 Bytes
5893f8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
+(function (factory) {
  if (typeof exports === 'undefined') {
    factory(webduino || {});
  } else {
    module.exports = factory;
  }
}(function (scope) {
  'use strict';

  var util = scope.util,
    TransportEvent = scope.TransportEvent,
    Board = scope.Board,
    proto;

  function WebArduino(options) {
    if (typeof options === 'string') {
      options = {
        device: options
      };
    }
    options = util.extend(util.extend({}, getDefaultOptions()), options);
    options.url = dertermineServerUrl(options.server) + '/';

    Board.call(this, options);
  }

  function getDefaultOptions() {
    return {
      transport: 'mqtt',
      server: WebArduino.DEFAULT_SERVER,
      login: 'admin',
      password: 'password'
    };
  }

  function dertermineServerUrl(url) {
    var parsed = util.parseURL(url);
    if (['ws:', 'wss:', 'tcp:'].indexOf(parsed.protocol) !== -1) {
      return url;
    } else {
      return (typeof location !== 'undefined' && location.protocol === 'https:' ? 'wss:' : 'ws:') +
        '//' + url;
    }
  }

  WebArduino.prototype = proto = Object.create(Board.prototype, {
    constructor: {
      value: WebArduino
    }
  });

  proto.reportFirmware = function () {
    var msg = [
      240, 121, 2, 4, 119, 0, 101, 0, 98, 0, 100, 0, 117, 0, 105, 0,
      110, 0, 111, 0, 46, 0, 105, 0, 110, 0, 111, 0, 247
    ];
    mockMessageEvent(this, msg);
  };

  proto.queryCapabilities = function () {
    var msg = [
      240, 108, 127, 127,
      0, 1, 1, 1, 4, 14, 127,
      0, 1, 1, 1, 3, 8, 4, 14, 127,
      0, 1, 1, 1, 4, 14, 127,
      0, 1, 1, 1, 3, 8, 4, 14, 127,
      0, 1, 1, 1, 3, 8, 4, 14, 127,
      0, 1, 1, 1, 4, 14, 127,
      0, 1, 1, 1, 4, 14, 127,
      0, 1, 1, 1, 3, 8, 4, 14, 127,
      0, 1, 1, 1, 3, 8, 4, 14, 127,
      0, 1, 1, 1, 3, 8, 4, 14, 127,
      0, 1, 1, 1, 4, 14, 127,
      0, 1, 1, 1, 4, 14, 127,
      0, 1, 1, 1, 2, 10, 4, 14, 127,
      0, 1, 1, 1, 2, 10, 4, 14, 127,
      0, 1, 1, 1, 2, 10, 4, 14, 127,
      0, 1, 1, 1, 2, 10, 4, 14, 127,
      0, 1, 1, 1, 2, 10, 4, 14, 127,
      0, 1, 1, 1, 2, 10, 4, 14, 127,
      2, 10, 127, 2, 10, 127, 247
    ];
    mockMessageEvent(this, msg);
  };

  proto.queryAnalogMapping = function () {
    var msg = [
      240, 106, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
      0, 1, 2, 3, 4, 5, 6, 7, 247
    ];
    mockMessageEvent(this, msg);
  };

  proto.startup = function () {
    var msg = [144, 0, 0, 145, 0, 0, 146, 0, 0];
    Board.prototype.startup.call(this);
    mockMessageEvent(this, msg);
  };

  WebArduino.DEFAULT_SERVER = 'wss://ws.webduino.io:443';

  function mockMessageEvent(board, message) {
    board._transport.emit(TransportEvent.MESSAGE, message);
  }

  scope.WebArduino = WebArduino;
}));