lgq
3 天以前 081f12a52906abe6c2d139fdc144135978681009
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
 * OS Module
 * Features:
 * - Provides system information (uptime, memory, disk, CPU ID).
 * - Executes shell commands.
 * - Manages device modes and rebooting.
 * 
 * Usage:
 * - Accessing OS-level and hardware-specific information and controls.
 * 
 * Doc/Demo : https://github.com/DejaOS/DejaOS
 */
import { osClass } from './libvbar-m-dxos.so'
import dxMap from './dxMap.js'
import * as std from 'std';
 
const dxOs = {};
 
// Create a singleton instance of the native osClass.
// This is executed only once when the module is first imported due to the nature of ES modules.
const osObj = new osClass();
 
/**
 * Get the running time of system startup (in seconds).
 * @returns {number} Running time.
 */
dxOs.getUptime = function () {
    return osObj.getUptime();
}
 
/**
 * Get the total memory of the system (in bytes).
 * @returns {number} Total memory.
 */
dxOs.getTotalmem = function () {
    return osObj.getTotalmem();
}
 
/**
 * Retrieve the remaining memory of the system (in bytes).
 * @returns {number} Remaining memory.
 */
dxOs.getFreemem = function () {
    return osObj.getFreemem();
}
 
/**
 * Get the total number of available disks in the system (in bytes).
 * @param {string} [path="/"] - The disk partition name (e.g., "/").
 * @returns {number} Total disk space.
 */
dxOs.getTotaldisk = function (path) {
    return osObj.getTotaldisk(path || "/");
}
 
/**
 * Retrieve the remaining available amount of system disk (in bytes).
 * @param {string} [path="/"] - The disk partition name (e.g., "/").
 * @returns {number} Free disk space.
 */
dxOs.getFreedisk = function (path) {
    return osObj.getFreedisk(path || "/");
}
 
/**
 * Get CPU ID.
 * @returns {string} CPU ID.
 */
dxOs.getCpuid = function () {
    // The C function expects a buffer size. 33 is a safe length for the CPU ID.
    return osObj.getCpuid(33);
}
 
/**
 * Get device UUID.
 * @returns {string} Device UUID.
 */
dxOs.getUuid = function () {
    // The C function expects a buffer size. 19 is a safe length for the UUID.
    return osObj.getUuid(19);
}
 
/**
 * Get the device's serial number (SN).
 * It first tries to read from '/etc/.sn'. If that fails, it falls back to the device UUID.
 * This operation is now handled in C layer to hide file paths.
 * @returns {string} The device's serial number.
 */
dxOs.getSn = function () {
    return osObj.getSn();
}
 
/**
 * Obtain the MAC address calculated through UUID, which can be used to initialize the network card.
 * @returns {string} MAC address in the format 'b2:a1:63:3f:99:b6'.
 */
dxOs.getUuid2mac = function () {
    // The C function expects a buffer size. 19 is a safe length.
    return osObj.getUuid2mac(19);
}
 
/**
 * Get CPU usage rate.
 * @returns {number} A number not greater than 100.
 */
dxOs.getFreecpu = function () {
    return osObj.getFreecpu();
}
 
/**
 * Execute a shell command without printing the result to the terminal.
 * @param {string} cmd - The command to execute.
 * @returns {number} The exit code of the command.
 */
dxOs.system = function (cmd) {
    return osObj.system(cmd);
}
 
/**
 * Execute a shell command and print the result to the terminal.
 * @param {string} cmd - The command to execute.
 * @returns {number} The exit code of the command.
 */
dxOs.systemBrief = function (cmd) {
    return osObj.systemBrief(cmd);
}
 
/**
 * Execute a shell command and return the result as a string.
 * @param {string} cmd - The command to execute.
 * @param {number} resLen - The maximum length of the result to receive.
 * @returns {string} The command's stdout result.
 */
dxOs.systemWithRes = function (cmd, resLen) {
    return osObj.systemWithRes(cmd, resLen);
}
 
/**
 * Execute a shell command and wait for it to complete.
 * @param {string} cmd - The command to execute.
 * @returns {number} The exit code of the command.
 */
dxOs.systemBlocked = function (cmd) {
    return osObj.systemBlocked(cmd);
}
 
/**
 * Asynchronously restart the device after a delay.
 * @param {number} delay_s - The delay in seconds before restarting.
 * @returns {number} Result of the operation.
 */
dxOs.asyncReboot = function (delay_s) {
    return osObj.asyncReboot(delay_s);
}
 
/**
 * Switch device mode. The device will restart after switching.
 * @param {string} mode - The target mode. Must be one of `"dev"`, `"test"`, `"prod"`, `"safe"`.
 * @returns {boolean} `true` if the mode switch was initiated, `false` otherwise.
 */
dxOs.setMode = function (mode) {
    return osObj.setMode(mode);
}
 
/**
 * Query current device mode.
 * @returns {string|null}
 *   - `string`: The current mode name (Must be one of `"dev"`, `"test"`, `"prod"`, `"safe"`).
 *   - `null`: If the mode file is not found or is empty.
 */
dxOs.getMode = function () {
    return osObj.getMode();
}
 
/**
 * @namespace dxOs.sync
 * @description Provides a simple mechanism for synchronous-like communication between asynchronous parts of an application.
 * Note: This is a simplified implementation and may not be suitable for complex scenarios.
 */
dxOs.sync = {
    /**
     * Waits for a response on a specific topic.
     * @param {string} topic - The topic to wait for.
     * @param {number} timeout - Timeout in milliseconds.
     * @returns {*} The data received on the topic, or undefined if it times out.
     */
    request: function (topic, timeout) {
        let map = dxMap.get("SYNC");
        map.put(topic + "__request__", topic);
        let count = 0;
        let data;
        while (count * 10 < timeout) {
            data = map.get(topic);
            if (data !== undefined && data !== null) {
                break;
            }
            std.sleep(10);
            count += 1;
        }
        let res = map.get(topic);
        map.del(topic);
        map.del(topic + "__request__");
        return res;
    },
    /**
     * Sends a response to a specific topic.
     * @param {string} topic - The topic to respond to.
     * @param {*} data - The data to send.
     */
    response: function (topic, data) {
        let map = dxMap.get("SYNC");
        if (map.get(topic + "__request__") == topic) {
            map.put(topic, data);
        }
    },
};
 
/**
 * @private
 * @description A generic handler for managing component instance pointers by ID.
 * This is used internally by other DejaOS modules.
 */
dxOs.handleId = function (name, id, pointer) {
    // Component name cannot be empty
    if (name === undefined || name === null || name === "" || typeof name !== 'string') {
        return
    }
    let map = dxMap.get('handleIds')
    // Handle ID
    if (id === undefined || id === null || id === "" || typeof id !== 'string') {
        id = "__" + name + "_default"
    }
    if (pointer === undefined || pointer === null || typeof pointer !== 'number') {
        // If the pointer is empty, it is obtained
        return map.get(id)
    } else {
        // If the pointer is not empty, it is set
        let isExist = map.get(id)
        if (isExist) {
            // Handle already exists
            return
        }
        map.put(id, pointer)
    }
}
 
export default dxOs;