index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // (c) 2014 Don Coleman
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /* global mainPage, deviceList, refreshButton */
  15. /* global detailPage, lightSwitch, dimmer, disconnectButton */
  16. /* global cordova, ble */
  17. /* jshint browser: true , devel: true*/
  18. 'use strict';
  19. var arrayBufferToInt = function (ab) {
  20. var a = new Uint8Array(ab);
  21. return a[0];
  22. };
  23. var robosmart = {
  24. service: 'FF10',
  25. lightSwitch: 'FF11',
  26. brightness: 'FF12',
  27. powerConsumed: 'FF16',
  28. name: 'FF17',
  29. description: 'FF18',
  30. room: 'FF19',
  31. locationName: 'FF1b',
  32. locationGps: 'FF1d',
  33. disconnect: 'FF1a',
  34. };
  35. var app = {
  36. initialize: function() {
  37. this.bindEvents();
  38. detailPage.hidden = true;
  39. },
  40. bindEvents: function() {
  41. document.addEventListener('deviceready', this.onDeviceReady, false);
  42. refreshButton.addEventListener('touchstart', this.refreshDeviceList, false);
  43. lightSwitch.addEventListener('change', this.setOnOff, false);
  44. dimmer.addEventListener('change', this.setBrightness, false);
  45. disconnectButton.addEventListener('touchstart', this.disconnect, false);
  46. deviceList.addEventListener('touchstart', this.connect, false); // assume not scrolling
  47. },
  48. onDeviceReady: function() {
  49. app.refreshDeviceList();
  50. },
  51. refreshDeviceList: function() {
  52. deviceList.innerHTML = ''; // empties the list
  53. if (cordova.platformId === 'android') {
  54. // Bug: Android can't find RoboSmart when scanning for service
  55. ble.scan([], 5, app.onDiscoverDevice, app.onError);
  56. } else {
  57. ble.scan([robosmart.service], 5, app.onDiscoverDevice, app.onError);
  58. }
  59. },
  60. onDiscoverDevice: function(device) {
  61. var listItem = document.createElement('li'),
  62. html = '<b>' + device.name + '</b><br/>' +
  63. 'RSSI: ' + device.rssi + '&nbsp;|&nbsp;' +
  64. device.id;
  65. listItem.dataset.deviceId = device.id;
  66. listItem.innerHTML = html;
  67. deviceList.appendChild(listItem);
  68. },
  69. connect: function(e) {
  70. var deviceId = e.target.dataset.deviceId,
  71. onConnect = function() {
  72. // subscribe for power consumption notifications
  73. disconnectButton.dataset.deviceId = deviceId;
  74. lightSwitch.dataset.deviceId = deviceId;
  75. dimmer.dataset.deviceId = deviceId;
  76. app.showDetailPage();
  77. app.syncUI(deviceId);
  78. };
  79. ble.connect(deviceId, onConnect, app.onError);
  80. },
  81. setOnOff: function(event) { // send data to robosmart
  82. var success = function() {
  83. console.log("success");
  84. };
  85. var failure = function() {
  86. alert("Failed writing data to the robosmart");
  87. };
  88. var data = new Uint8Array(1);
  89. data[0] = event.target.checked ? 0x1 : 0x0;
  90. var deviceId = event.target.dataset.deviceId;
  91. ble.write(deviceId, robosmart.service, robosmart.lightSwitch, data.buffer, success, failure);
  92. },
  93. setBrightness: function(event) { // send data to robosmart
  94. var success = function() {
  95. // if the light was off and brightness is adjusted, the light will turn on
  96. lightSwitch.checked = true;
  97. console.log("success");
  98. };
  99. var failure = function() {
  100. alert("Failed writing data to the robosmart");
  101. };
  102. var deviceId = event.target.dataset.deviceId;
  103. // pass the brightness from the slider to the light
  104. var brightness = new Uint8Array(1);
  105. brightness[0] = event.target.value;
  106. ble.write(deviceId, robosmart.service, robosmart.brightness, brightness.buffer, success, failure);
  107. },
  108. syncUI: function(deviceId) {
  109. var switchCallback = function(data) {
  110. lightSwitch.checked = arrayBufferToInt(data) === 0x1;
  111. };
  112. var dimmerCallback = function(data) {
  113. dimmer.value = arrayBufferToInt(data);
  114. };
  115. var failure = function(reason) {
  116. console.log("Error syncing UI with the current state " + reason);
  117. };
  118. ble.read(deviceId, robosmart.service, robosmart.lightSwitch, switchCallback, failure);
  119. ble.read(deviceId, robosmart.service, robosmart.brightness, dimmerCallback, failure);
  120. },
  121. disconnect: function(event) {
  122. var deviceId = event.target.dataset.deviceId;
  123. ble.disconnect(deviceId, app.showMainPage, app.onError);
  124. },
  125. showMainPage: function() {
  126. mainPage.hidden = false;
  127. detailPage.hidden = true;
  128. },
  129. showDetailPage: function() {
  130. mainPage.hidden = true;
  131. detailPage.hidden = false;
  132. },
  133. onError: function(reason) {
  134. alert("ERROR: " + reason); // real apps should use notification.alert
  135. }
  136. };