dataSyncService.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DataSyncService = void 0;
  4. const device_1 = require("../models/device");
  5. const clientConnection_1 = require("../models/clientConnection");
  6. const mqttMessage_1 = require("../models/mqttMessage");
  7. class DataSyncService {
  8. constructor() {
  9. this.isRunning = false;
  10. this.syncInterval = null;
  11. this.syncIntervalMs = 60000;
  12. }
  13. start(intervalMs) {
  14. if (this.isRunning) {
  15. console.log('数据同步服务已在运行中');
  16. return;
  17. }
  18. if (intervalMs) {
  19. this.syncIntervalMs = intervalMs;
  20. }
  21. this.isRunning = true;
  22. console.log(`数据同步服务已启动,同步间隔: ${this.syncIntervalMs}ms`);
  23. this.performSync().catch(error => {
  24. console.error('初始数据同步失败:', error);
  25. });
  26. this.syncInterval = setInterval(() => {
  27. this.performSync().catch(error => {
  28. console.error('定时数据同步失败:', error);
  29. });
  30. }, this.syncIntervalMs);
  31. }
  32. stop() {
  33. if (!this.isRunning) {
  34. console.log('数据同步服务未在运行');
  35. return;
  36. }
  37. this.isRunning = false;
  38. if (this.syncInterval) {
  39. clearInterval(this.syncInterval);
  40. this.syncInterval = null;
  41. }
  42. console.log('数据同步服务已停止');
  43. }
  44. getStatus() {
  45. return {
  46. isRunning: this.isRunning,
  47. intervalMs: this.syncIntervalMs
  48. };
  49. }
  50. async performSync() {
  51. if (!this.isRunning) {
  52. return;
  53. }
  54. try {
  55. console.log('开始执行数据同步...');
  56. await this.readDevicesFromDatabase();
  57. await this.readConnectionsFromDatabase();
  58. await this.readMessagesFromDatabase();
  59. console.log('数据同步完成');
  60. }
  61. catch (error) {
  62. console.error('数据同步过程中发生错误:', error);
  63. throw error;
  64. }
  65. }
  66. async readDevicesFromDatabase() {
  67. try {
  68. console.log('开始从数据库读取设备数据...');
  69. const devices = await device_1.DeviceModel.getAll();
  70. console.log(`从数据库读取到 ${devices.length} 个设备数据`);
  71. console.log('设备数据读取完成');
  72. }
  73. catch (error) {
  74. console.error('从数据库读取设备数据时出错:', error);
  75. throw error;
  76. }
  77. }
  78. async readConnectionsFromDatabase() {
  79. try {
  80. console.log('开始从数据库读取连接数据...');
  81. const connections = await clientConnection_1.ClientConnectionModel.getAll();
  82. console.log(`从数据库读取到 ${connections.length} 个连接数据`);
  83. console.log('连接数据读取完成');
  84. }
  85. catch (error) {
  86. console.error('从数据库读取连接数据时出错:', error);
  87. throw error;
  88. }
  89. }
  90. async readMessagesFromDatabase() {
  91. try {
  92. console.log('开始从数据库读取消息数据...');
  93. const messages = await mqttMessage_1.MqttMessageModel.getAll();
  94. console.log(`从数据库读取到 ${messages.length} 个消息数据`);
  95. console.log('消息数据读取完成');
  96. }
  97. catch (error) {
  98. console.error('从数据库读取消息数据时出错:', error);
  99. throw error;
  100. }
  101. }
  102. }
  103. exports.DataSyncService = DataSyncService;
  104. exports.default = new DataSyncService();
  105. //# sourceMappingURL=dataSyncService.js.map