hiển thị thuyền thông tin tàu

This commit is contained in:
Tran Anh Tuan
2025-12-03 16:22:25 +07:00
parent 47e9bac0f9
commit 22a3b591c6
22 changed files with 2135 additions and 260 deletions

View File

@@ -4,12 +4,14 @@ import {
EVENT_BANZONE_DATA,
EVENT_ENTITY_DATA,
EVENT_GPS_DATA,
EVENT_SEARCH_THINGS,
EVENT_TRACK_POINTS_DATA,
} from "@/constants";
import {
queryAlarm,
queryEntities,
queryGpsData,
querySearchThings,
queryTrackPoints,
} from "@/controller/DeviceController";
import { queryBanzones } from "@/controller/MapController";
@@ -21,12 +23,14 @@ const intervals: {
entities: ReturnType<typeof setInterval> | null;
trackPoints: ReturnType<typeof setInterval> | null;
banzones: ReturnType<typeof setInterval> | null;
searchThings: ReturnType<typeof setInterval> | null;
} = {
gps: null,
alarm: null,
entities: null,
trackPoints: null,
banzones: null,
searchThings: null,
};
export function getGpsEventBus() {
@@ -153,6 +157,37 @@ export function getBanzonesEventBus() {
}, AUTO_REFRESH_INTERVAL * 60);
}
export function searchThingEventBus(body: Model.SearchThingBody) {
// Clear interval cũ nếu có
if (intervals.searchThings) {
clearInterval(intervals.searchThings);
intervals.searchThings = null;
}
const searchThingsData = async () => {
// console.log("call api with body:", body);
try {
const resp = await querySearchThings(body);
if (resp && resp.data) {
eventBus.emit(EVENT_SEARCH_THINGS, resp.data);
} else {
console.log("SearchThings: no data returned");
}
} catch (err) {
console.error("SearchThings: fetch error", err);
}
};
// Gọi ngay lần đầu
searchThingsData();
// Sau đó setup interval để gọi lại mỗi 30s
intervals.searchThings = setInterval(() => {
searchThingsData();
}, AUTO_REFRESH_INTERVAL * 6); // 30 seconds
}
export function stopEvents() {
Object.keys(intervals).forEach((k) => {
const key = k as keyof typeof intervals;

43
services/time_service.tsx Normal file
View File

@@ -0,0 +1,43 @@
import dayjs from "dayjs";
import "dayjs/locale/vi";
import relativeTime from "dayjs/plugin/relativeTime";
dayjs.extend(relativeTime);
dayjs.locale("vi");
export { dayjs };
/**
* Chuyển đổi unix timestamp thành text thời gian tương đối
* @param unixTime - Unix timestamp (seconds hoặc milliseconds)
* @returns String mô tả thời gian tương đối (vd: "5 phút trước", "2 giờ trước")
*/
export function formatRelativeTime(unixTime: number): string {
if (!unixTime || unixTime <= 0) return "Không rõ";
// Xác định đơn vị timestamp (seconds hoặc milliseconds)
const timestamp = unixTime < 10000000000 ? unixTime * 1000 : unixTime;
const updateDate = new Date(timestamp);
const now = new Date();
const diffMs = now.getTime() - updateDate.getTime();
// Nếu thời gian trong tương lai
if (diffMs < 0) return "Vừa xong";
const diffSeconds = Math.floor(diffMs / 1000);
const diffMins = Math.floor(diffSeconds / 60);
const diffHours = Math.floor(diffMins / 60);
const diffDays = Math.floor(diffHours / 24);
const diffWeeks = Math.floor(diffDays / 7);
const diffMonths = Math.floor(diffDays / 30);
const diffYears = Math.floor(diffDays / 365);
if (diffSeconds < 60) return "Vừa xong";
if (diffMins < 60) return `${diffMins} phút trước`;
if (diffHours < 24) return `${diffHours} giờ trước`;
if (diffDays < 7) return `${diffDays} ngày trước`;
if (diffWeeks < 4) return `${diffWeeks} tuần trước`;
if (diffMonths < 12) return `${diffMonths} tháng trước`;
return `${diffYears} năm trước`;
}