Guest User

Untitled

a guest
Oct 13th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7.  
  8. <link rel="stylesheet" href="wolke.css">
  9.  
  10. <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
  11. <script type="text/javascript" src="wolke.js"></script>
  12. </head>
  13.  
  14. <body ng-app="App" ng-controller="world">
  15. <table>
  16. <tr ng-repeat="item in VALUES">
  17. <td>
  18. {{item.name}}
  19. </td>
  20. <td>
  21. <input
  22. ng-model="item.write"
  23. type="text"
  24. ng-keyup="$event.keyCode == 13 &&
  25. (send(item.name + '_value', item.write) ||
  26. $event.target.blur())" />
  27. </td>
  28. <td>
  29. {{item.read}}
  30. </td>
  31. <td>
  32. <input
  33. ng-value="item.active ? 'On' : 'Off'"
  34. ng-style="{'background-color': item.active ? 'var(--green)' : 'var(--red)'}"
  35. type="button"
  36. ng-click="send(item.name + '_toggle', '')" />
  37. </td>
  38. </tr>
  39. </table>
  40.  
  41. <table ng-hide="LOGIN_SUCCESS">
  42. <tr>
  43. <td>
  44. Username:
  45. </td>
  46. <td>
  47. <input
  48. ng-model="username"
  49. type="text" />
  50. </td>
  51. </tr>
  52. <tr>
  53. <td>
  54. Password:
  55. </td>
  56. <td>
  57. <input
  58. ng-model="password"
  59. type="password"
  60. ng-keyup="$event.keyCode == 13 && login()" />
  61. </td>
  62. </tr>
  63. </table>
  64. </body>
  65.  
  66. 'use strict';
  67. if(typeof console === undefined) {console = {log: function() {}}}
  68.  
  69. var PARTICLE = {
  70. deviceId: 'Stern',
  71. status: 'status',
  72. token: null,
  73. api_url: [
  74. 'particle.js',
  75. 'https://cdn.jsdelivr.net/npm/particle-api-js@7/dist/particle.min.js',
  76. ]
  77. };
  78.  
  79. angular.module('App', []).controller('world', function ($scope) {
  80. $scope.LOGIN_SUCCESS = true;
  81.  
  82. $scope.send = function(name, value) {
  83. PARTICLE.api.callFunction({
  84. deviceId: PARTICLE.deviceId,
  85. name: name,
  86. auth: PARTICLE.token,
  87. argument: value,
  88. }).then(
  89. msg => console.log('Function call success ', msg),
  90. err => console.log('Function call failure: ', err)
  91. );
  92. }
  93.  
  94. $scope.login = function() {
  95. PARTICLE.api.login({
  96. username: $scope.username,
  97. password: $scope.password
  98. }).then(data => {
  99. PARTICLE.token = data.body.access_token;
  100. localStorage.setItem('token', PARTICLE.token);
  101. $scope.LOGIN_SUCCESS = true;
  102. $scope.read();
  103. }, err => console.log('Login failure ', err));
  104. }
  105.  
  106. $scope.read = function() {
  107. PARTICLE.api.getVariable({
  108. deviceId: PARTICLE.deviceId,
  109. name: PARTICLE.status,
  110. auth: PARTICLE.token,
  111. }).then(response => {
  112. let raw = JSON.parse(response.body.result);
  113.  
  114. if ($scope.VALUES === undefined) {
  115. raw.forEach(item => item.write = item.read);
  116. $scope.VALUES = raw;
  117. } else {
  118. raw.forEach((item, i) => {
  119. $scope.VALUES[i].read = item.read;
  120. $scope.VALUES[i].active = item.active;
  121. });
  122. }
  123.  
  124. $scope.$apply();
  125. setTimeout($scope.read, 10);
  126. }, err => {
  127. console.log('Read error from: ', err);
  128. $scope.LOGIN_SUCCESS = false;
  129. });
  130. }
  131.  
  132. $scope.setup = function() {
  133. PARTICLE.token = localStorage.getItem('token');
  134. console.log('Restored token = ' + PARTICLE.token);
  135. $scope.read();
  136. }
  137.  
  138. load_particle_api(location.href).then($scope.setup);
  139. });
  140.  
  141.  
  142. function load_particle_api(url) {
  143. // Allows to load fake particle api for debug purposes
  144. return new Promise(resolve => {
  145. let load = () => {
  146. PARTICLE.api = new Particle;
  147. resolve();
  148. };
  149.  
  150. let script = document.createElement('script');
  151. script.src = PARTICLE.api_url[url.indexOf('?') == -1 | 0];
  152. script.onload = () => load();
  153. script.onreadystatechange = () => load();
  154. document.head.appendChild(script);
  155. });
  156. }
  157.  
  158. :root {
  159. /* Taken from http://ethanschoonover.com/solarized */
  160. --background: #002b36;
  161. --edit: #586e75;
  162. --edit_focus: #839496;
  163. --color: #fdf6e3;
  164. --red: #dc322f;
  165. --green: #859900;
  166. }
  167.  
  168. * {
  169. background-color: var(--background);
  170. color: var(--color);
  171. font-size: 16pt;
  172. font-family: sans-serif;
  173. text-align: center;
  174. padding: 0 0 0 0;
  175. }
  176.  
  177. td {
  178. width: 25%;
  179. }
  180.  
  181. input:focus {
  182. border: none;
  183. outline: none;
  184. background-color: var(--edit_focus);
  185. }
  186.  
  187. input {
  188. background-color: var(--edit);
  189. border: none;
  190. width: 100%;
  191. height: 23px;
  192. }
  193.  
  194. 'use strict'
  195.  
  196. class Particle {
  197. constructor() {
  198. this.value = [
  199. {name: 'int', active: 1, read: 100},
  200. {name: 'str', active: 0, read: 'abc'},
  201. ];
  202. }
  203.  
  204. getVariable(arg) {
  205. return new Promise(resolve => {
  206. resolve({body:
  207. {result: JSON.stringify(this.value)}
  208. });
  209. });
  210. }
  211.  
  212. callFunction(arg) {
  213. return new Promise(resolve => {
  214. console.log('Received = ', arg)
  215.  
  216. let name_parts = arg.name.split('_');
  217. let index = name_parts[0] === 'str' | 0;
  218.  
  219. if (name_parts[1] == 'toggle') {
  220. this.value[index].active ^= true;
  221. } else {
  222. this.value[index].read = arg.argument;
  223. }
  224.  
  225. resolve('ok');
  226. });
  227. }
  228. }
Add Comment
Please, Sign In to add comment