-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.ts
141 lines (131 loc) · 3.41 KB
/
store.ts
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
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Status10Energy } from "./mqtt/setupMQTT";
export type InstanceDataInput = {
[K in keyof Pick<InstanceData, "timeout">]?: string;
};
type ServerDataEnergy = {
[K in keyof Status10Energy]: number;
};
type InstanceDataEnergy = {
[K in keyof Status10Energy]: number[];
};
export type InstanceData = {
id: string;
/**
* Energy report per interval
*/
energy: InstanceDataEnergy;
/**
* The time in milliseconds after which the device will be stopped if it is not stopped manually
*/
timeout: number;
/**
* Timestamp when the instance was started
*/
startTimestamp: Date;
/**
* Timestamp when the instance was stopped
*
* Might be `null` if the instance is still running
*/
stopTimestamp: Date | null;
/**
* Timestamp when the socket was powered on
*
* Might be `null` if the socket was already on when the instance was started
*/
powerOnTimestamp: Date | null;
/**
* Timestamp when the socket was powered off.
*
* Might be `null` if the socket is still handling other instances yet this instance has stopped
*/
powerOffTimestamp: Date | null;
/**
* Flag indicating if the instance was stopped due to an emergency stop
*/
isEmergencyStopped: boolean;
/**
* Flag indicating if the instance was stopped manually
*/
isManuallyStopped: boolean;
};
type InstancesData = {
[id: string]: InstanceData;
};
export let instancesData: InstancesData = {};
type PowerStatus = {
/**
* Timestamp when the socket was powered on
*
* Might be `null` if no instances were started yet
*/
powerOn: {
instanceId: string;
timestamp: Date;
} | null;
/**
* Timestamp when the socket was powered off.
*
* Might be `null` if the socket is still handling other instances yet this instance has stopped
*/
powerOff: {
instanceId: string;
timestamp: Date;
} | null;
};
export type ServerData = {
/**
* List of running instances
*/
runningInstances: string[];
/**
* Energy consumption per second
*/
energyToday: number;
initialEnergy: ServerDataEnergy | null;
runningInstancesWithTimeout: string[];
powerStatus: PowerStatus[];
instancesStarting: string[];
instancesStopping: string[];
connectedSocketName: string | null;
/**
* Flag indicating if the socket was stopped due to an emergency stop
*/
isEmergencyStopped: boolean;
};
export let serverData: ServerData = {
runningInstances: [],
energyToday: 0,
runningInstancesWithTimeout: [],
instancesStopping: [],
instancesStarting: [],
powerStatus: [
{
powerOn: null,
powerOff: null,
},
],
connectedSocketName: null,
initialEnergy: null,
isEmergencyStopped: false,
};
export let instancesStartingTimeout: Record<string, NodeJS.Timeout> = {};
export const resetAllData = () => {
instancesData = {};
serverData = {
connectedSocketName: null,
runningInstances: [],
energyToday: 0,
runningInstancesWithTimeout: [],
instancesStarting: [],
instancesStopping: [],
isEmergencyStopped: false,
powerStatus: [
{
powerOn: null,
powerOff: null,
},
],
initialEnergy: null,
};
};