-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
168 lines (156 loc) · 5.25 KB
/
commands.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
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
const axios = require('axios');
const config = require('./config.json');
const logger = require('./logger'); // Import the logger
// Function to fetch data from the API
async function fetchData() {
try {
const response = await axios.get(`${config.api.baseUrl}/realtimedata?boardCuid=${config.api.boardCuid}`, {
headers: {
'accept': '*/*',
'Authorization': config.api.authToken
}
});
return response.data;
} catch (error) {
logger.error('Error fetching data:', error);
throw new Error('Failed to fetch data from API.');
}
}
// Functions to get specific information
async function getCpuInfo(client, target) {
try {
const data = await fetchData();
if (data) {
const cpuInfo = data.systemStats.cpuInfos;
const message = `CPU Usage: ${cpuInfo.avgCpuUsagePercentage}%`;
client.say(target, message);
logger.info(`Output for !cpu: ${message}`);
} else {
const errorMsg = 'Error fetching CPU information.';
client.say(target, errorMsg);
logger.error(errorMsg);
}
} catch (error) {
logger.error('Error in getCpuInfo:', error);
client.say(target, 'Error fetching CPU information.');
}
}
async function getMemoryInfo(client, target) {
try {
const data = await fetchData();
if (data) {
const memInfo = data.systemStats.memInfos;
const message = `Memory Usage: ${memInfo.memUsedPercentage}%, Total Memory: ${memInfo.totalMemInGB}GB`;
client.say(target, message);
logger.info(`Output for !memory: ${message}`);
} else {
const errorMsg = 'Error fetching memory information.';
client.say(target, errorMsg);
logger.error(errorMsg);
}
} catch (error) {
logger.error('Error in getMemoryInfo:', error);
client.say(target, 'Error fetching memory information.');
}
}
async function getDiskInfo(client, target) {
try {
const data = await fetchData();
if (data) {
const diskInfo = data.systemStats.diskInfos;
const message = `Disk Usage: ${diskInfo.diskUsedPercentage}%, Total Disk: ${diskInfo.totalDiskInGB}GB`;
client.say(target, message);
logger.info(`Output for !disk: ${message}`);
} else {
const errorMsg = 'Error fetching disk information.';
client.say(target, errorMsg);
logger.error(errorMsg);
}
} catch (error) {
logger.error('Error in getDiskInfo:', error);
client.say(target, 'Error fetching disk information.');
}
}
async function getTempInfo(client, target) {
try {
const data = await fetchData();
if (data) {
const tempInfo = data.systemStats.tempInfos;
const message = `Average Temperature: ${tempInfo.avgTemp}°C`;
client.say(target, message);
logger.info(`Output for !temp: ${message}`);
} else {
const errorMsg = 'Error fetching temperature information.';
client.say(target, errorMsg);
logger.error(errorMsg);
}
} catch (error) {
logger.error('Error in getTempInfo:', error);
client.say(target, 'Error fetching temperature information.');
}
}
async function getDevicesInfo(client, target) {
try {
const data = await fetchData();
if (data) {
const devices = data.devices;
const message = devices.map(device => {
return `Device: ${device.displayName}, IP: ${device.ipValue}, Connected: ${device.connected}`;
}).join(' | ');
client.say(target, message);
logger.info(`Output for !devices: ${message}`);
} else {
const errorMsg = 'Error fetching devices information.';
client.say(target, errorMsg);
logger.error(errorMsg);
}
} catch (error) {
logger.error('Error in getDevicesInfo:', error);
client.say(target, 'Error fetching devices information.');
}
}
// Function to toggle the IRLBox stream
async function toggleIrlboxStream(client, target, isStreaming) {
try {
const response = await axios.post(`${config.api.baseUrl}/togglestream`, null, {
params: { isStreaming },
headers: {
'accept': 'application/json',
'Authorization': config.api.authToken
}
});
const message = isStreaming ? 'IRLBox stream stopped.' : 'IRLBox stream started.';
client.say(target, message);
logger.info(`Output for !irlbox ${isStreaming ? 'stop' : 'start'}: ${message}`);
} catch (error) {
logger.error('Error in toggleIrlboxStream:', error);
client.say(target, 'Error toggling IRLBox stream.');
}
}
// Function to reboot the IRLBox server
async function rebootIrlboxServer(client, target) {
try {
await axios.post(`${config.api.baseUrl}/reboot`, null, {
params: { boardCuid: config.api.boardCuid },
headers: {
'accept': '*/*',
'Authorization': config.api.authToken
}
});
const message = 'IRLBox is rebooting.';
client.say(target, message);
logger.info(`Output for !reboot: ${message}`);
} catch (error) {
logger.error('Error in rebootIrlboxServer:', error);
client.say(target, 'Error rebooting IRLBox server.');
}
}
module.exports = {
getCpuInfo,
getMemoryInfo,
getDiskInfo,
getTempInfo,
getDevicesInfo,
toggleIrlboxStream,
rebootIrlboxServer
};