322 lines
13 KiB
TypeScript
322 lines
13 KiB
TypeScript
import client from "@/lib/client";
|
|
import config from "@/lib/config";
|
|
import initI18n from "@/lib/i18n";
|
|
import Memory from "@/lib/memory";
|
|
import { format } from "date-fns";
|
|
import i18next from "i18next";
|
|
import { readFileSync } from "node:fs";
|
|
import { EOL } from "node:os";
|
|
import { WebSocket } from "ws";
|
|
|
|
await initI18n();
|
|
|
|
if (config.earthquake?.useHistoryData) {
|
|
console.log("過去の地震情報を配信します");
|
|
const history = JSON.parse(readFileSync(`${import.meta.dirname}/../../260420.json`, "utf-8"));
|
|
history.reverse();
|
|
|
|
let i = 0;
|
|
setInterval(() => {
|
|
processMessage(history[i]);
|
|
i++;
|
|
}, 10 * 1000);
|
|
} else {
|
|
const WEBSOCKET_URL = config.debug
|
|
? "wss://api-realtime-sandbox.p2pquake.net/v2/ws"
|
|
: "wss://api.p2pquake.net/v2/ws";
|
|
|
|
console.log("P2P地震情報のWebSocketに接続します");
|
|
const socket = new WebSocket(WEBSOCKET_URL);
|
|
|
|
socket.addEventListener("open", () => {
|
|
console.log("P2P地震情報のWebSocketに接続しました");
|
|
});
|
|
|
|
socket.addEventListener("message", async (event) => {
|
|
let message;
|
|
|
|
try {
|
|
message = typeof event.data === "string"
|
|
? JSON.parse(event.data)
|
|
: event.data;
|
|
} catch (err) {
|
|
console.error(`地震情報メッセージのパースでエラーが発生: ${err}`);
|
|
return;
|
|
}
|
|
|
|
const id = message.id ?? message._id;
|
|
const mem = Memory.memory;
|
|
if (mem.processedInfo.includes(id) && !config.debug) {
|
|
console.log("重複した地震情報: ", message.id);
|
|
return;
|
|
}
|
|
|
|
processMessage(message);
|
|
|
|
if (!config.debug) {
|
|
mem.processedInfo.concat([id]);
|
|
Memory.memory = mem;
|
|
}
|
|
});
|
|
}
|
|
|
|
const processMessage = async (message: any) => {
|
|
try {
|
|
const scaleMessages: Record<string, string> = {
|
|
"-1": "不明",
|
|
"10": "震度1",
|
|
"20": "震度2",
|
|
"30": "震度3",
|
|
"40": "震度4",
|
|
"45": "震度5弱",
|
|
"46": "**推定**震度5弱以上***(正確には不明)***",
|
|
"50": "震度5強",
|
|
"55": "震度6弱",
|
|
"60": "震度6強",
|
|
"70": "震度7",
|
|
"99": "程度以上",
|
|
}
|
|
|
|
switch (message.code) {
|
|
case 555:
|
|
console.log("ピアの地域分布情報を受信しました");
|
|
break;
|
|
case 551:
|
|
{
|
|
console.log("地震発生情報を受信しました");
|
|
|
|
const domesticTsunamiMessages: Record<string, string> = {
|
|
"None": "😌この地震による**国内**の津波の心配はありません。",
|
|
"Unknown": "😕この地震による**国内**の***津波情報は***不明です。",
|
|
"Checking": "🧐この地震による**国内**の津波情報を**調査中です。**",
|
|
"NonEffective": "😌この地震による**国内**の**海面変動が予想されますが**、被害の心配はありません。",
|
|
"Watch": "⚠️この地震により**国内**で津波注意報が発令しています。",
|
|
"Warning": "🚨この地震による**国内**の津波予報があります。",
|
|
}
|
|
|
|
const foreignTsunamiMessages: Record<string, string> = {
|
|
"None": "😌この地震による**国外**の津波の心配はありません。",
|
|
"Unknown": "😕この地震による**国外**の***津波情報は***不明です。",
|
|
"Checking": "🧐この地震による**国外**の津波情報を**調査中です。**",
|
|
"NonEffectiveNearby": "😌この地震によって**国外**にて震源の近傍で**小さな津波の可能性はありますが**、被害の心配はありません。",
|
|
"WarningNearby": "⚠️この地震によって**国外**にて震源の近傍で**津波の可能性**があります。",
|
|
"WarningPacific": "⚠️この地震によって**太平洋**にて**津波の可能性**があります。",
|
|
"WarningPacificWide": "🚨この地震によって**太平洋の広域**にて**津波の可能性**があります。",
|
|
"WarningIndian": "⚠️この地震によって**インド洋**にて**津波の可能性**があります。",
|
|
"WarningIndianWide": "🚨この地震によって**インド洋の広域**にて**津波の可能性**があります。",
|
|
"Potential": "🚨この地震によって**一般的に**この規模では津波の可能性があると考えられています。",
|
|
}
|
|
|
|
let points: Record<string, any[]> = {};
|
|
for (const point of message.points) {
|
|
const scaleMsg = scaleMessages[String(point.scale)];
|
|
if (!scaleMsg)
|
|
continue;
|
|
|
|
points[scaleMsg]?.push(point);
|
|
}
|
|
|
|
const grouped: Record<string, { scale: number; addrs: string[] }> = {};
|
|
for (const point of message.points) {
|
|
const { addr, scale } = point;
|
|
const label = scaleMessages[String(scale)] ?? "不明";
|
|
|
|
if (!grouped[label]) {
|
|
grouped[label] = { scale, addrs: [] };
|
|
}
|
|
|
|
grouped[label].addrs.push(addr);
|
|
}
|
|
|
|
const pointsMsg = Object.entries(grouped)
|
|
.sort((a, b) => b[1].scale - a[1].scale)
|
|
.map(([label, { addrs }]) =>
|
|
`【${label}】${EOL}${addrs.join("・")}`)
|
|
.join(EOL.repeat(2)).trim();
|
|
|
|
const response = await client.request("ueuse/create", {
|
|
text: i18next.t("earthquakeNotice", {
|
|
occuredTime: format(new Date(message.earthquake.time), "yyyy年M月d日 H:mm"),
|
|
maxScale: scaleMessages[String(message.earthquake.maxScale)],
|
|
epicenter: message.earthquake.hypocenter.name === ""
|
|
? "不明"
|
|
: message.earthquake.hypocenter.name,
|
|
magnitude: message.earthquake.hypocenter.magnitude === -1
|
|
? "不明"
|
|
: `M${message.earthquake.hypocenter.magnitude.toFixed(1)}`,
|
|
depth: message.earthquake.hypocenter.depth === 0
|
|
? "ごく浅い"
|
|
: (message.earthquake.hypocenter.depth === -1
|
|
? "不明"
|
|
: `${message.earthquake.hypocenter.depth}km`),
|
|
domesticTsunami: domesticTsunamiMessages[(message.earthquake.domesticTsunami ?? "Unknown")],
|
|
foreignTsunami: foreignTsunamiMessages[(message.earthquake.foreignTsunami ?? "Unknown")],
|
|
points: pointsMsg === ""
|
|
? ""
|
|
: EOL.repeat(2) + pointsMsg,
|
|
source: message.issue.source ?? "不明",
|
|
comment: message.comments.freeFormComment === ""
|
|
? ""
|
|
: EOL + message.comments.freeFormComment + EOL,
|
|
}),
|
|
});
|
|
|
|
if (!response.success) {
|
|
console.warn("ユーズの作成に失敗しました:", response.error_code);
|
|
break;
|
|
}
|
|
|
|
console.log("地震発生情報を投稿:", response.uniqid);
|
|
}
|
|
break;
|
|
case 552:
|
|
{
|
|
console.log("津波予報情報を受信しました");
|
|
|
|
if (message.cancelled) {
|
|
const response = await client.request("ueuse/create", {
|
|
text: i18next.t("tsunamiCancelNotice", {
|
|
announceTime: format(new Date(message.issue.time), "yyyy年M月d日 H:mm:ss"),
|
|
source: message.issue.source ?? "不明",
|
|
}),
|
|
});
|
|
|
|
if (!response.success) {
|
|
console.warn("ユーズの作成に失敗しました:", response.error_code);
|
|
break;
|
|
}
|
|
|
|
console.log("津波予報解除情報を投稿:", response.uniqid);
|
|
break;
|
|
}
|
|
|
|
const gradeMessages: Record<string, string> = {
|
|
"Unknown": "不明",
|
|
"Watch": "津波注意報",
|
|
"Warning": "津波警報",
|
|
"MajorWarning": "大津波警報",
|
|
}
|
|
|
|
let areasMsg = "";
|
|
for (const area of message.areas) {
|
|
areasMsg += i18next.t("tsunamiAreaMsg", {
|
|
name: area.name,
|
|
immediate: area.immediate
|
|
? EOL + "🚨***直ちに津波が来襲すると予想されています。***"
|
|
: "",
|
|
grade: gradeMessages[area.grade],
|
|
arrivalTime: format(new Date(area.firstHeight.arrivalTime), "yyyy年M月d日 H:mm"),
|
|
condition: area.firstHeight.condition
|
|
? `${area.firstHeight.condition}されています`
|
|
: "不明",
|
|
maxHeight: area.maxHeight.value === 0.2
|
|
? "0.2m未満"
|
|
: (area.maxHeight.value
|
|
? `${area.maxHeight.value}m`
|
|
: area.maxHeight.description),
|
|
}) + EOL.repeat(2);
|
|
}
|
|
|
|
const response = await client.request("ueuse/create", {
|
|
text: i18next.t("tsunamiForecastNotice", {
|
|
announceTime: format(new Date(message.issue.time), "yyyy年M月d日 H:mm:ss"),
|
|
areasMsg: areasMsg.trim(),
|
|
source: message.issue.source ?? "不明",
|
|
}),
|
|
});
|
|
|
|
if (!response.success) {
|
|
console.warn("ユーズの作成に失敗しました:", response.error_code);
|
|
break;
|
|
}
|
|
|
|
console.log("津波予報情報を投稿:", response.uniqid);
|
|
}
|
|
break;
|
|
case 556:
|
|
{
|
|
console.log("緊急地震速報(警報)を受信しました");
|
|
|
|
if (message.cancelled) {
|
|
const response = await client.request("ueuse/create", {
|
|
text: i18next.t("eewCancelNotice", {
|
|
isTest: message.test
|
|
? "⚒️これは**テストです。**"
|
|
: "🚨これは**テストではありません。**",
|
|
announceTime: format(new Date(message.issue.time), "yyyy年M月d日 H:mm:ss"),
|
|
}),
|
|
});
|
|
|
|
if (!response.success) {
|
|
console.warn("ユーズの作成に失敗しました:", response.error_code);
|
|
break;
|
|
}
|
|
|
|
console.log("緊急地震速報(警報)解除情報を投稿:", response.uniqid);
|
|
}
|
|
|
|
const kindMessages: Record<string, string> = {
|
|
"10": "⏳主要動は、**未到達と予測**されています。",
|
|
"11": "🫨主要動が、**既に到達していると予測**されています。",
|
|
"19": "🧐PLUM法によると、主要動の***到達予想は***ありません。",
|
|
}
|
|
|
|
let areasMsg = "";
|
|
for (const area of message.areas) {
|
|
areasMsg += i18next.t("eewAreaMsg", {
|
|
name: area.name,
|
|
maxScale: scaleMessages[String(Math.floor(area.scaleFrom))] +
|
|
(area.scaleTo === 99
|
|
? "程度以上"
|
|
: area.scaleFrom !== area.scaleTo
|
|
? `から${scaleMessages[String(Math.floor(area.scaleTo))]}`
|
|
: ""),
|
|
kind: kindMessages[area.kindCode] ?? "😕主要動の***到達予想は***ありません。",
|
|
arrivalTime: area.arrivalTime !== undefined
|
|
? format(new Date(area.arrivalTime), "yyyy年M月d日 H:mm:ss")
|
|
: "不明",
|
|
}) + EOL.repeat(2);
|
|
}
|
|
|
|
const response = await client.request("ueuse/create", {
|
|
text: i18next.t("eewNotice", {
|
|
isTest: message.test
|
|
? "⚒️これは**テストです。**"
|
|
: "🚨これは**テストではありません。**",
|
|
announceTime: format(new Date(message.issue.time), "yyyy年M月d日 H:mm:ss"),
|
|
occuredTime: format(new Date(message.earthquake.originTime), "yyyy年M月d日 H:mm:ss"),
|
|
arrivalTime: format(new Date(message.earthquake.arrivalTime), "yyyy年M月d日 H:mm:ss"),
|
|
isAssume: message.earthquake.condition === "仮定震源要素"
|
|
? `${EOL}❓これは、仮定震源要素です。そのため、震源に関する情報が保証できません。`
|
|
: "",
|
|
epicenter: message.earthquake.hypocenter.name ?? "不明",
|
|
depth: message.earthquake.hypocenter.depth === undefined ||
|
|
message.earthquake.hypocenter.depth === -1
|
|
? "不明"
|
|
: `${Math.floor(message.earthquake.hypocenter.depth)}km`,
|
|
magnitude: message.earthquake.hypocenter.magnitude === undefined ||
|
|
message.earthquake.hypocenter.magnitude === -1
|
|
? "不明"
|
|
: `M${message.earthquake.hypocenter.magnitude}`,
|
|
areas: areasMsg !== ""
|
|
? EOL.repeat(2) + areasMsg.trim()
|
|
: "",
|
|
}),
|
|
});
|
|
|
|
if (!response.success) {
|
|
console.warn("ユーズの作成に失敗しました:", response.error_code);
|
|
break;
|
|
}
|
|
|
|
console.log("緊急地震速報(警報)情報を投稿:", response.uniqid);
|
|
}
|
|
break;
|
|
default:
|
|
console.log("未対応の情報:", message);
|
|
break;
|
|
}
|
|
} catch (err) {
|
|
console.warn("メッセージの処理に失敗しました:", err);
|
|
}
|
|
} |