The main server, and probably only repository in this org.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
2.9 KiB

Squashed commit of the following: commit 042130f11c6f807d128e2ffbdb76f4be2170a43f Author: Stian Aune <post@stian-aune.com> Date: Wed Apr 10 22:16:14 2019 +0200 Done? commit 3089c6f8be3135ab1aa2b95b9314a753596714a7 Author: Stian Aune <post@stian-aune.com> Date: Wed Mar 27 22:20:07 2019 +0100 Bugfix + some unit tests bc why not? commit 38f874b5538a7d9563b44d60b2fa881d941defc8 Author: Stian Aune <post@stian-aune.com> Date: Tue Mar 26 22:14:22 2019 +0100 Front page. commit 5bdf2a0c48e0e388bffb53714b0410ebd3f8b8e7 Author: Stian Aune <post@stian-aune.com> Date: Thu Mar 7 21:10:57 2019 +0100 Rename groups and stuff. commit 3130710b198c434fe312ea3123cbe5ba3fb526b7 Merge: 118a21a fa98a0b Author: Stian Aune <post@stian-aune.com> Date: Thu Mar 7 21:06:16 2019 +0100 Merge remote-tracking branch 'origin/master' into webui commit 118a21ad42adaa4968318b1e798a58f0565db849 Author: Stian Aune <post@stian-aune.com> Date: Thu Mar 7 21:06:02 2019 +0100 I can a lot of things. commit 089ee6816986e06e77eff404eaa9f04cda0e4275 Merge: 0a6cc16 f27c0a3 Author: Stian Aune <post@stian-aune.com> Date: Tue Mar 5 22:13:41 2019 +0100 Merge remote-tracking branch 'origin/master' into webui commit 0a6cc16e0fda482c04dd88c658273c2774b50d7a Author: Stian Aune <post@stian-aune.com> Date: Tue Mar 5 22:13:31 2019 +0100 More stuff. commit 1ef2623637f2f7c852edffa7914c2095a990539b Author: Stian Aune <post@stian-aune.com> Date: Sun Mar 3 21:59:23 2019 +0100 Add/remove group. commit 59240ce22d305b2b335dd46bb5f528f720b5eedc Merge: 537e86c e11ba33 Author: Stian Aune <post@stian-aune.com> Date: Sun Mar 3 21:15:06 2019 +0100 Merge remote-tracking branch 'origin/master' into webui commit 537e86cced5ab9b8919aaa0f89b277ed9ecdde27 Author: Stian Aune <post@stian-aune.com> Date: Sun Mar 3 21:14:55 2019 +0100 For reals. commit de5fb0d16ddde159a54ce7bb2af09eb4a87960c2 Author: Stian Aune <post@stian-aune.com> Date: Sun Mar 3 21:14:30 2019 +0100 Modal cleanup + light page. commit 01daa2800085df9799de4890a3574ef405b1ec89 Author: Stian Aune <post@stian-aune.com> Date: Sun Feb 24 00:27:36 2019 +0100 Hmm... commit be2fe56e62a212e2691288f5db0a2d826719689d Merge: 99867a5 22dddc7 Author: Stian Aune <post@stian-aune.com> Date: Sat Feb 23 15:16:07 2019 +0100 Merge remote-tracking branch 'origin/master' into webui commit 99867a5fd680e7bd2489df4a01a5609d1e7867d5 Author: Stian Aune <post@stian-aune.com> Date: Sat Feb 23 15:16:02 2019 +0100 WIP. commit d82bbf53f84ab3c47814e5d586400c34787be4d6 Author: Stian Aune <post@stian-aune.com> Date: Tue Feb 19 21:37:59 2019 +0100 WIP. commit 7ed6d2148dcd991050bbc2ecfaef6fc42a0dc4ce Author: Stian Aune <mail@stian-aune.com> Date: Sun Feb 10 13:30:53 2019 +0100 Stuffs. commit ee9a3d485efcf3cf7308a30e4bea27b97002324e Author: Stian Aune <mail@stian-aune.com> Date: Sun Feb 10 12:29:47 2019 +0100 Bleurgh. commit 59ef68ad8f8ce1e05e81c4322bad52cb319cd716 Author: Stian Aune <post@stian-aune.com> Date: Thu Feb 7 21:15:37 2019 +0100 Thursday night commit. commit ee3ff503427fd86306e88bdd4b857690c38f07f4 Merge: daf071a e644f48 Author: Stian Aune <post@stian-aune.com> Date: Wed Feb 6 22:02:37 2019 +0100 Merge remote-tracking branch 'origin/master' into webui commit daf071a4429f5e0158c88e6b142659b6ad6c851a Author: Stian Aune <post@stian-aune.com> Date: Mon Feb 4 20:56:45 2019 +0100 Rewrote light tests. commit edffa49d18ad07b652e5f9d72bd350bd499144a9 Merge: 83052a2 8c302ec Author: Stian Aune <post@stian-aune.com> Date: Mon Feb 4 20:31:26 2019 +0100 Merge remote-tracking branch 'origin/master' into webui commit 83052a2ab8d2b51c3003417142ba15ce1ed06921 Author: Stian Aune <post@stian-aune.com> Date: Mon Feb 4 20:31:22 2019 +0100 Blah. commit aec6cd0b7d5aa812b907b16e1c65687e94e16866 Merge: e6fefb5 0503f79 Author: Stian Aune <post@stian-aune.com> Date: Tue Jan 29 21:06:24 2019 +0100 Merge remote-tracking branch 'origin/master' into webui commit e6fefb5bd1fc487b32515ee5041cd42b37058d52 Merge: 97f7a78 eca8a30 Author: Stian Aune <post@stian-aune.com> Date: Mon Jan 28 22:44:19 2019 +0100 Merge remote-tracking branch 'origin/master' into webui commit 97f7a78f3f72331996eee63c7623d04272bc1d71 Merge: 195738d 174b818 Author: Stian Aune <post@stian-aune.com> Date: Sun Jan 13 13:20:10 2019 +0100 Merge remote-tracking branch 'origin/master' into webui commit 195738d6eddde94f7ca60a2d9b5ea086a204aa7f Merge: a2f2c2b 2e3e120 Author: Stian Aune <post@stian-aune.com> Date: Sat Jan 12 17:24:00 2019 +0100 Merge remote-tracking branch 'origin/master' into webui commit a2f2c2b0bce7d8f54f851dd14d527763390b0123 Merge: edd629e 9004f2f Author: Stian Aune <post@stian-aune.com> Date: Mon Jan 7 20:58:37 2019 +0100 Merge remote-tracking branch 'origin/master' into webui commit edd629eb395af92b250cef9bb452952eded115aa Author: Stian Aune <post@stian-aune.com> Date: Mon Jan 7 20:58:32 2019 +0100 More stuff. commit 206d5005db09f3a149b9dccf9fff886b44252394 Author: Stian Aune <post@stian-aune.com> Date: Mon Jan 7 19:11:03 2019 +0100 Cleanup.
5 years ago
  1. import {randId} from "./random";
  2. import {nullish} from "./null";
  3. import {fetchGet, fetchPatch} from "./fetcher";
  4. const localData = {};
  5. const callbacks = [];
  6. export function subscribeToLight(lightId, callback) {
  7. const callbackId = randId();
  8. callbacks.push({callbackId, lightId, callback});
  9. if (lightId >= 0) {
  10. dispatch();
  11. fetchOne(lightId);
  12. } else {
  13. dispatch();
  14. fetchAll();
  15. }
  16. }
  17. export function unsubscribeFromLight(callbackId) {
  18. const callback = callbacks.find(c => c !== null && c.callbackId === callbackId);
  19. const index = callbacks.indexOf(callback);
  20. callbacks[index] = null;
  21. }
  22. export function changeColor(lightId, newColor, newBrightness, newPower) {
  23. const light = localData[lightId];
  24. if (nullish(light)) {
  25. return;
  26. }
  27. const oldBrightness = light.brightness;
  28. const oldPower = light.on;
  29. const oldColor = light.color;
  30. light.color = newColor;
  31. light.brightness = newBrightness;
  32. light.on = newPower;
  33. dispatch();
  34. fetchPatch(`/light/${lightId}`, {
  35. color: newColor,
  36. brightness: newBrightness,
  37. on: newPower,
  38. }).then(({data, error}) => {
  39. if (error !== null) {
  40. light.color = oldColor;
  41. light.brightness = oldBrightness;
  42. light.on = oldPower;
  43. dispatch();
  44. return;
  45. }
  46. localData[lightId] = data;
  47. dispatch();
  48. });
  49. }
  50. export function changeLight(lightId, name, groupId) {
  51. const light = localData[lightId];
  52. if (nullish(light)) {
  53. return;
  54. }
  55. const oldName = light.name;
  56. const oldGroupId = light.groupId;
  57. light.name = name;
  58. light.groupId = groupId;
  59. dispatch();
  60. console.log({name,groupId});
  61. fetchPatch(`/light/${lightId}`, {
  62. name: name,
  63. groupId: groupId,
  64. }).then(({data, error}) => {
  65. if (error !== null) {
  66. light.name = oldName;
  67. light.groupId = oldGroupId;
  68. dispatch();
  69. return;
  70. }
  71. localData[lightId] = data;
  72. dispatch();
  73. });
  74. }
  75. function fetchAll() {
  76. fetchGet(`/light/`).then(({data, error}) => {
  77. if (error === null) {
  78. handleLights(data);
  79. }
  80. });
  81. }
  82. function fetchOne(id) {
  83. fetchGet(`/light/${id}`).then(({data, error}) => {
  84. if (error === null) {
  85. handleLight(data);
  86. }
  87. });
  88. }
  89. function handleLights(lights) {
  90. lights.forEach(l => handleLight(l));
  91. for (let key in localData) {
  92. if (localData.hasOwnProperty(key) && nullish(lights.find(l => l.id === parseInt(key, 10)))) {
  93. delete localData[key];
  94. }
  95. }
  96. dispatch(lights);
  97. }
  98. function handleLight(light) {
  99. localData[light.id] = light;
  100. dispatch(light);
  101. }
  102. function dispatch(data = null) {
  103. if (data === null) {
  104. data = Object.values(localData);
  105. }
  106. if (Array.isArray(data)) {
  107. callbacks
  108. .filter(c => c !== null)
  109. .filter(c => c.lightId === -1)
  110. .forEach(c => c.callback(data));
  111. } else {
  112. callbacks
  113. .filter(c => c !== null)
  114. .filter(c => c.lightId === data.id)
  115. .forEach(c => c.callback(data));
  116. }
  117. }