85 lines
3.6 KiB
TypeScript
85 lines
3.6 KiB
TypeScript
import db from './db/database';
|
||
import type { SensorCount, SensorRow } from './interfaces/SensorRow';
|
||
|
||
|
||
const updateInterval = 30000; // 30 секунд
|
||
|
||
// Периодическое обновление аварийного состояния:
|
||
setInterval(() => {
|
||
const currentCount = (db.prepare("SELECT COUNT(*) as count FROM sensors WHERE value = 2").get() as SensorCount).count;
|
||
if (currentCount >= 3) {
|
||
console.log("Максимальное количество датчиков с аварийным состоянием уже достигнуто.");
|
||
return;
|
||
}
|
||
|
||
const availableSensors = db.prepare("SELECT * FROM sensors WHERE value != 2").all() as SensorRow[];
|
||
if (availableSensors.length === 0) return;
|
||
|
||
const maxToUpdate = Math.min(3 - currentCount, availableSensors.length);
|
||
const countToUpdate = Math.floor(Math.random() * maxToUpdate) + 1;
|
||
|
||
const selectedSensors: SensorRow[] = [];
|
||
while (selectedSensors.length < countToUpdate) {
|
||
const randomIndex = Math.floor(Math.random() * availableSensors.length);
|
||
const sensor = availableSensors[randomIndex];
|
||
if (!selectedSensors.find(s => s.id === sensor.id)) {
|
||
selectedSensors.push(sensor);
|
||
}
|
||
}
|
||
|
||
const errorDescriptions = [
|
||
"Перегрев системы.",
|
||
"Ошибка датчика.",
|
||
"Низкий уровень масла.",
|
||
"Сбой питания.",
|
||
"Перегрузка электросети."
|
||
];
|
||
const possibleAlarmCodes = [1, 2, 3];
|
||
|
||
selectedSensors.forEach(sensor => {
|
||
db.prepare("UPDATE sensors SET value = 2 WHERE id = ?").run(sensor.id);
|
||
const numErrors = Math.floor(Math.random() * 2) + 1;
|
||
for (let i = 0; i < numErrors; i++) {
|
||
const alarmCode = possibleAlarmCodes[Math.floor(Math.random() * possibleAlarmCodes.length)];
|
||
const description = errorDescriptions[Math.floor(Math.random() * errorDescriptions.length)];
|
||
const timestamp = new Date().toLocaleString("ru-RU");
|
||
db.prepare("INSERT INTO errors (sensor_id, alarmCode, description, timestamp) VALUES (?, ?, ?, ?)")
|
||
.run(sensor.id, alarmCode, description, timestamp);
|
||
}
|
||
});
|
||
|
||
console.log(`Периодическое обновление: для ${countToUpdate} датчика(ов) установлено аварийное состояние и добавлены ошибки.`);
|
||
}, updateInterval);
|
||
|
||
// Периодическое включение выключенных датчиков:
|
||
setInterval(() => {
|
||
const offSensors = db.prepare("SELECT * FROM sensors WHERE value = 0").all() as SensorRow[];
|
||
const offCount = offSensors.length;
|
||
if (offCount === 0) {
|
||
console.log("Нет выключенных датчиков для обновления.");
|
||
return;
|
||
}
|
||
|
||
const maxToUpdate = Math.floor(offCount / 2);
|
||
if (maxToUpdate < 1) {
|
||
console.log("Слишком мало выключенных датчиков для обновления.");
|
||
return;
|
||
}
|
||
|
||
const countToUpdate = Math.floor(Math.random() * maxToUpdate) + 1;
|
||
const selectedSensors: SensorRow[] = [];
|
||
while (selectedSensors.length < countToUpdate) {
|
||
const randomIndex = Math.floor(Math.random() * offSensors.length);
|
||
const sensor = offSensors[randomIndex];
|
||
if (!selectedSensors.find(s => s.id === sensor.id)) {
|
||
selectedSensors.push(sensor);
|
||
}
|
||
}
|
||
|
||
selectedSensors.forEach(sensor => {
|
||
db.prepare("UPDATE sensors SET value = 1 WHERE id = ?").run(sensor.id);
|
||
});
|
||
|
||
console.log(`Периодическое обновление: для ${countToUpdate} выключенных датчика(ов) установлено состояние 'on' (value = 1).`);
|
||
}, updateInterval);
|