-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbleQueueing.js
More file actions
128 lines (110 loc) · 2.93 KB
/
bleQueueing.js
File metadata and controls
128 lines (110 loc) · 2.93 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import {
setCurrentWorkflow,
setCurrentWorkflowStepId,
setCurrentStepEllapsedTimeInSeconds,
} from "../features/workflowEditor/workflowSlice";
import store from "../store";
import { getCharacteristic } from "./BleCharacteristicCache";
import { fanOffUuid } from "../constants/uuids";
import { convertToUInt8BLE } from "./utils";
const currentIntervals = [];
const currentSetTimeouts = [];
const queue = [];
const priorityQueue = [];
let isQueueProcessing = false;
let currentWorkflowIndex = 0;
let workflowFunctions;
export { currentIntervals, currentSetTimeouts };
export function clearIntervals() {
while (currentIntervals.length > 0) {
clearInterval(currentIntervals.pop());
}
}
export function clearTimeouts() {
while (currentSetTimeouts.length > 0) {
clearTimeout(currentSetTimeouts.pop());
}
}
export function AddToQueue(func) {
queue.push(func);
if (!isQueueProcessing) {
isQueueProcessing = true;
ProcessQueue();
}
}
export function cancelCurrentWorkflow(turnFanOff = true) {
clearIntervals();
clearTimeouts();
workflowFunctions = {};
currentWorkflowIndex = -1;
store.dispatch(setCurrentWorkflowStepId());
store.dispatch(setCurrentWorkflow());
store.dispatch(setCurrentStepEllapsedTimeInSeconds(0));
if (turnFanOff) {
const blePayload = async () => {
const fanOffCharacteristic = getCharacteristic(fanOffUuid);
const buffer = convertToUInt8BLE(0);
await fanOffCharacteristic.writeValue(buffer);
};
AddToQueue(blePayload);
}
}
export function AddToPriorityQueue(func) {
priorityQueue.push(func);
if (!isQueueProcessing) {
isQueueProcessing = true;
ProcessQueue();
}
}
async function ProcessQueue() {
isQueueProcessing = true;
if (queue.length === 0 && priorityQueue.length === 0) {
isQueueProcessing = false;
return;
}
try {
let func;
if (priorityQueue.length > 0) {
func = priorityQueue.shift();
} else {
func = queue.shift();
}
await func();
setTimeout(() => {
ProcessQueue();
}, 0);
} catch (error) {
console.log(`QUEUE ERROR: ${error.toString()}`);
ProcessQueue();
}
}
export function AddToWorkflowQueue(func) {
workflowFunctions = func;
currentWorkflowIndex = -1;
ProcessWorkflowQueue();
}
function ProcessWorkflowQueue() {
let currentFunc;
const next = () => {
if (currentWorkflowIndex + 1 >= workflowFunctions.length) {
store.dispatch(setCurrentWorkflow());
store.dispatch(setCurrentWorkflowStepId());
store.dispatch(setCurrentStepEllapsedTimeInSeconds(0));
}
currentFunc = workflowFunctions[currentWorkflowIndex + 1];
if (!currentFunc) return;
setTimeout(() => {
AddToQueue(async () => {
await currentFunc(next);
currentWorkflowIndex++;
});
}, 0);
};
next();
}
export function clearQueuesAndTimers() {
cancelCurrentWorkflow(false);
queue.length = 0;
priorityQueue.length = 0;
isQueueProcessing = false;
}