koishi-plugin-disaster-warning 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -20,6 +20,9 @@ export interface Config {
20
20
  global: boolean;
21
21
  };
22
22
  source_priority: 'auto' | 'wolfx' | 'fanstudio' | 'p2p';
23
+ min_magnitude: number;
24
+ min_intensity: number;
25
+ min_scale: number;
23
26
  }
24
27
  export declare const Config: Schema<Config>;
25
28
  export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -31,6 +31,9 @@ exports.Config = koishi_1.Schema.object({
31
31
  koishi_1.Schema.const('fanstudio').description('优先使用 FAN Studio'),
32
32
  koishi_1.Schema.const('p2p').description('优先使用 P2P地震情報'),
33
33
  ]).default('auto').description('数据源优先级'),
34
+ min_magnitude: koishi_1.Schema.number().default(4.0).description('最小推送震级(M)'),
35
+ min_intensity: koishi_1.Schema.number().default(4.0).description('最小推送烈度(中国标准)'),
36
+ min_scale: koishi_1.Schema.number().default(3.0).description('最小推送震度(日本标准,3.0=震度3)'),
34
37
  });
35
38
  function apply(ctx, config) {
36
39
  const service = new service_1.DisasterWarningService(ctx, config);
package/lib/pusher.js CHANGED
@@ -7,9 +7,6 @@ const logger = new koishi_1.Logger('disaster-pusher');
7
7
  // Hardcoded filter thresholds - no user configuration needed
8
8
  const FILTER_THRESHOLDS = {
9
9
  MIN_MAGNITUDE_ABSOLUTE: 3.0, // Always ignore earthquakes below M3.0
10
- MIN_MAGNITUDE_FOR_PUSH: 4.0, // Push if magnitude >= 4.0
11
- MIN_INTENSITY_FOR_PUSH: 4.0, // Or push if intensity >= 4.0 (China)
12
- MIN_SCALE_FOR_PUSH: 4.0, // Or push if shindo scale >= 4 (Japan)
13
10
  };
14
11
  class MessagePushManager {
15
12
  constructor(ctx, config) {
@@ -33,25 +30,26 @@ class MessagePushManager {
33
30
  return false; // Don't filter tsunami/weather
34
31
  }
35
32
  const data = event.data;
36
- const { MIN_MAGNITUDE_ABSOLUTE, MIN_MAGNITUDE_FOR_PUSH, MIN_INTENSITY_FOR_PUSH, MIN_SCALE_FOR_PUSH } = FILTER_THRESHOLDS;
33
+ const { MIN_MAGNITUDE_ABSOLUTE } = FILTER_THRESHOLDS;
34
+ const { min_magnitude, min_intensity, min_scale } = this.config;
37
35
  // Always filter out very small earthquakes
38
36
  if (data.magnitude !== undefined && data.magnitude < MIN_MAGNITUDE_ABSOLUTE) {
39
37
  return true;
40
38
  }
41
39
  // Pass if magnitude is significant
42
- if (data.magnitude !== undefined && data.magnitude >= MIN_MAGNITUDE_FOR_PUSH) {
40
+ if (data.magnitude !== undefined && data.magnitude >= min_magnitude) {
43
41
  return false;
44
42
  }
45
43
  // Pass if intensity is significant (Chinese sources)
46
- if (data.intensity !== undefined && data.intensity >= MIN_INTENSITY_FOR_PUSH) {
44
+ if (data.intensity !== undefined && data.intensity >= min_intensity) {
47
45
  return false;
48
46
  }
49
47
  // Pass if scale is significant (Japanese sources)
50
- if (data.scale !== undefined && data.scale >= MIN_SCALE_FOR_PUSH) {
48
+ if (data.scale !== undefined && data.scale >= min_scale) {
51
49
  return false;
52
50
  }
53
- // If magnitude is between 3.0-4.0 and no significant intensity/scale, filter out
54
- if (data.magnitude !== undefined && data.magnitude >= MIN_MAGNITUDE_ABSOLUTE && data.magnitude < MIN_MAGNITUDE_FOR_PUSH) {
51
+ // If magnitude is between 3.0 and min_magnitude, and no significant intensity/scale, filter out
52
+ if (data.magnitude !== undefined && data.magnitude >= MIN_MAGNITUDE_ABSOLUTE && data.magnitude < min_magnitude) {
55
53
  // Only filter if we don't have intensity/scale data that would make it significant
56
54
  if (data.intensity === undefined && data.scale === undefined) {
57
55
  return true;
package/lib/service.d.ts CHANGED
@@ -6,6 +6,7 @@ export declare class DisasterWarningService {
6
6
  private reconnectTimers;
7
7
  private pusher;
8
8
  private ctx;
9
+ private stopped;
9
10
  private handlers;
10
11
  private wolfxHandlers;
11
12
  constructor(ctx: Context, config: Config);
package/lib/service.js CHANGED
@@ -11,6 +11,7 @@ class DisasterWarningService {
11
11
  constructor(ctx, config) {
12
12
  this.connections = {};
13
13
  this.reconnectTimers = {};
14
+ this.stopped = false; // Flag to prevent reconnection after stop
14
15
  this.wolfxHandlers = {};
15
16
  this.ctx = ctx;
16
17
  this.config = config;
@@ -29,17 +30,28 @@ class DisasterWarningService {
29
30
  async start() {
30
31
  if (!this.config.enabled)
31
32
  return;
33
+ this.stopped = false; // Reset stopped flag on start
32
34
  logger.info('Disaster Warning Service starting...');
33
35
  this.connectBasedOnConfig();
34
36
  }
35
37
  async stop() {
36
38
  logger.info('Disaster Warning Service stopping...');
37
- for (const key in this.connections) {
38
- this.connections[key].close();
39
- }
39
+ this.stopped = true; // Set stopped flag to prevent reconnection
40
+ // Clear all reconnect timers first
40
41
  for (const key in this.reconnectTimers) {
41
42
  clearTimeout(this.reconnectTimers[key]);
43
+ delete this.reconnectTimers[key];
44
+ }
45
+ // Close all connections
46
+ for (const key in this.connections) {
47
+ const ws = this.connections[key];
48
+ // Remove all listeners to prevent any further events
49
+ ws.removeAllListeners();
50
+ // Force close the connection
51
+ ws.terminate();
52
+ delete this.connections[key];
42
53
  }
54
+ logger.info('Disaster Warning Service stopped.');
43
55
  }
44
56
  connectBasedOnConfig() {
45
57
  const { regions, data_types, source_priority } = this.config;
@@ -90,7 +102,11 @@ class DisasterWarningService {
90
102
  }
91
103
  }
92
104
  connectWebSocket(name, url, onMessage) {
105
+ // Don't connect if service is stopped
106
+ if (this.stopped)
107
+ return;
93
108
  if (this.connections[name]) {
109
+ this.connections[name].removeAllListeners('close');
94
110
  this.connections[name].close();
95
111
  }
96
112
  logger.info(`Connecting to ${name} at ${url}...`);
@@ -99,6 +115,8 @@ class DisasterWarningService {
99
115
  logger.info(`Connected to ${name}`);
100
116
  });
101
117
  ws.on('message', (data) => {
118
+ if (this.stopped)
119
+ return;
102
120
  try {
103
121
  const parsed = JSON.parse(data.toString());
104
122
  onMessage(parsed);
@@ -108,11 +126,14 @@ class DisasterWarningService {
108
126
  }
109
127
  });
110
128
  ws.on('close', () => {
111
- logger.warn(`Disconnected from ${name}, reconnecting in 10s...`);
112
- delete this.connections[name];
113
- this.reconnectTimers[name] = setTimeout(() => {
114
- this.connectWebSocket(name, url, onMessage);
115
- }, 10000);
129
+ // Only reconnect if service is not stopped
130
+ if (!this.stopped) {
131
+ logger.warn(`Disconnected from ${name}, reconnecting in 10s...`);
132
+ delete this.connections[name];
133
+ this.reconnectTimers[name] = setTimeout(() => {
134
+ this.connectWebSocket(name, url, onMessage);
135
+ }, 10000);
136
+ }
116
137
  });
117
138
  ws.on('error', (err) => {
118
139
  logger.error(`Error in ${name} connection:`, err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-disaster-warning",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "Koishi 灾害预警插件,支持多数据源(地震、海啸、气象预警)",
5
5
  "contributors": [
6
6
  "lumia.wang <fenglian19980510@gmail.com>"
@@ -34,12 +34,14 @@
34
34
  "peerDependencies": {
35
35
  "koishi": "^4.18.0"
36
36
  },
37
+ "dependencies": {
38
+ "ws": "^8.18.0"
39
+ },
37
40
  "devDependencies": {
38
41
  "@types/node": "^20.0.0",
39
42
  "@types/ws": "^8.5.10",
40
43
  "koishi": "^4.18.0",
41
- "typescript": "^5.0.0",
42
- "ws": "^8.18.0"
44
+ "typescript": "^5.0.0"
43
45
  },
44
46
  "koishi": {
45
47
  "description": {