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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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. }