| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.DataSyncService = void 0;
- const device_1 = require("../models/device");
- const clientConnection_1 = require("../models/clientConnection");
- const mqttMessage_1 = require("../models/mqttMessage");
- class DataSyncService {
- constructor() {
- this.isRunning = false;
- this.syncInterval = null;
- this.syncIntervalMs = 60000;
- }
- start(intervalMs) {
- if (this.isRunning) {
- console.log('数据同步服务已在运行中');
- return;
- }
- if (intervalMs) {
- this.syncIntervalMs = intervalMs;
- }
- this.isRunning = true;
- console.log(`数据同步服务已启动,同步间隔: ${this.syncIntervalMs}ms`);
- this.performSync().catch(error => {
- console.error('初始数据同步失败:', error);
- });
- this.syncInterval = setInterval(() => {
- this.performSync().catch(error => {
- console.error('定时数据同步失败:', error);
- });
- }, this.syncIntervalMs);
- }
- stop() {
- if (!this.isRunning) {
- console.log('数据同步服务未在运行');
- return;
- }
- this.isRunning = false;
- if (this.syncInterval) {
- clearInterval(this.syncInterval);
- this.syncInterval = null;
- }
- console.log('数据同步服务已停止');
- }
- getStatus() {
- return {
- isRunning: this.isRunning,
- intervalMs: this.syncIntervalMs
- };
- }
- async performSync() {
- if (!this.isRunning) {
- return;
- }
- try {
- console.log('开始执行数据同步...');
- await this.readDevicesFromDatabase();
- await this.readConnectionsFromDatabase();
- await this.readMessagesFromDatabase();
- console.log('数据同步完成');
- }
- catch (error) {
- console.error('数据同步过程中发生错误:', error);
- throw error;
- }
- }
- async readDevicesFromDatabase() {
- try {
- console.log('开始从数据库读取设备数据...');
- const devices = await device_1.DeviceModel.getAll();
- console.log(`从数据库读取到 ${devices.length} 个设备数据`);
- console.log('设备数据读取完成');
- }
- catch (error) {
- console.error('从数据库读取设备数据时出错:', error);
- throw error;
- }
- }
- async readConnectionsFromDatabase() {
- try {
- console.log('开始从数据库读取连接数据...');
- const connections = await clientConnection_1.ClientConnectionModel.getAll();
- console.log(`从数据库读取到 ${connections.length} 个连接数据`);
- console.log('连接数据读取完成');
- }
- catch (error) {
- console.error('从数据库读取连接数据时出错:', error);
- throw error;
- }
- }
- async readMessagesFromDatabase() {
- try {
- console.log('开始从数据库读取消息数据...');
- const messages = await mqttMessage_1.MqttMessageModel.getAll();
- console.log(`从数据库读取到 ${messages.length} 个消息数据`);
- console.log('消息数据读取完成');
- }
- catch (error) {
- console.error('从数据库读取消息数据时出错:', error);
- throw error;
- }
- }
- }
- exports.DataSyncService = DataSyncService;
- exports.default = new DataSyncService();
- //# sourceMappingURL=dataSyncService.js.map
|