-
Notifications
You must be signed in to change notification settings - Fork 1
/
idgenerator.js
102 lines (87 loc) · 2.76 KB
/
idgenerator.js
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
const emergencyIDLength = 4;
const maxAttempts = 10000;
// Extremely small likelihood of timing due to infinite loop out unless we are genuinely full.
// We have 36^4 = 1.6 million possible IDs.
const testID = "TEST";
const testDevice = "computer-id";
// No one should ever generate the testID except for testing purposes.
var usedEmergencyID = {
testID: true
};
var deviceToEmergency = {};
// The archive is like deviceToEmergency, but is not deleted at the end, so that checking the status
// can be done after the emergency has ended.
var dToEArchive = {};
var emergencyToDevice = {};
var emergencyToStage = {};
function makeByDevice(deviceID) {
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
if (deviceID == testDevice) {
console.log("Generating the test ID");
emergencyID = testID;
deviceToEmergency[deviceID] = emergencyID;
dToEArchive[deviceID] = emergencyID;
emergencyToDevice[emergencyID] = deviceID;
return testID;
}
if (deviceToEmergency[deviceID]) {
throw "Device already has an emergency";
}
var emergencyID;
var called = 0;
do {
called += 1;
emergencyID = "";
for (var i = 0; i < emergencyIDLength; i++) {
emergencyID += possible.charAt(Math.floor(Math.random() * possible.length));
}
} while (usedEmergencyID[emergencyID] && called < maxAttempts);
if (called == maxAttempts) {
throw "No unused IDs available.";
}
usedEmergencyID[emergencyID] = true;
deviceToEmergency[deviceID] = emergencyID;
dToEArchive[deviceID] = emergencyID;
emergencyToDevice[emergencyID] = deviceID;
return emergencyID;
}
function getEmergencyByDevice(deviceID) {
emergencyID = deviceToEmergency[deviceID];
if (!emergencyID) {
throw "Getting undefined ID.";
}
return emergencyID;
}
function getEmergencyFromArchive(deviceID) {
emergencyID = dToEArchive[deviceID];
if (!emergencyID) {
throw "Getting undefined ID from archive."
}
return emergencyID;
}
function setStageByEmergency(emergencyID, stage) {
if (!emergencyToStage[emergencyID]) {
emergencyToStage[emergencyID] = stage;
return
}
currentStage = emergencyToStage[emergencyID];
if (currentStage >= stage) {
throw "Emergency " + emergencyID + " stage is being set out of order. Current stage: " +
currentStage + ". New Stage: " + stage + ".";
}
emergencyToStage[emergencyID] = stage;
}
function endByEmergency(emergencyID) {
// We don't free up usedEmergencyID because it has already been used and will never be used again.
deviceID = emergencyToDevice[emergencyID];
delete emergencyToDevice[emergencyID];
delete deviceToEmergency[deviceID]
delete emergencyToStage[emergencyID];
}
module.exports = {
makeByDevice,
getEmergencyByDevice,
setStageByEmergency,
endByEmergency,
getEmergencyFromArchive
};