kioskStorage.ts 10.4 KB
import type { GuideLocale } from "./guideContent";

export const KIOSK_EVENT = "kiosk-settings-updated";

export function emitKioskUpdate() {
  window.dispatchEvent(new CustomEvent(KIOSK_EVENT));
}

const LS = {
  WELCOME: "kiosk_welcome_v1",
  BG_HOME: "kiosk_bg_home_urls_v1",
  BG_CAROUSEL: "kiosk_bg_carousel_urls_v1",
  GUIDE: "kiosk_guide_overrides_v1",
  KNOWLEDGE: "kiosk_knowledge_db_v1",
  VIDEO_SOURCE: "kiosk_video_source_v1",
} as const;

/** 与 Layout 默认一致 */
export const DEFAULT_HOME_BG_URLS: readonly string[] = [
  "/backgrounds/daocheng-radio-array.png",
  "/backgrounds/daocheng-campus-aerial.png",
];

export const DEFAULT_CAROUSEL_BG_URLS: readonly string[] = [
  "https://images.unsplash.com/photo-1419242902214-272b3f66ee7a?auto=format&fit=crop&w=1920&q=80",
  "https://images.unsplash.com/photo-1462331940025-496dfbfc7564?auto=format&fit=crop&w=1920&q=80",
  "https://images.unsplash.com/photo-1543722530-d2c3201371e7?auto=format&fit=crop&w=1920&q=80",
  "https://images.unsplash.com/photo-1444703686971-f7864ae02629?auto=format&fit=crop&w=1920&q=80",
  "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?auto=format&fit=crop&w=1920&q=80",
  "https://images.unsplash.com/photo-1464802686167-b939a6910659?auto=format&fit=crop&w=1920&q=80",
];

export type WelcomeMessages = Record<"zh-CN" | "en" | "bo", string>;

export function loadHomeBackgrounds(): string[] {
  try {
    const raw = localStorage.getItem(LS.BG_HOME);
    if (!raw) return [...DEFAULT_HOME_BG_URLS];
    const arr = JSON.parse(raw) as unknown;
    if (!Array.isArray(arr) || arr.length === 0) return [...DEFAULT_HOME_BG_URLS];
    return arr.filter((u): u is string => typeof u === "string" && u.length > 0);
  } catch {
    return [...DEFAULT_HOME_BG_URLS];
  }
}

export function saveHomeBackgrounds(urls: string[]) {
  localStorage.setItem(LS.BG_HOME, JSON.stringify(urls));
  emitKioskUpdate();
}

export function loadCarouselBackgrounds(): string[] {
  try {
    const raw = localStorage.getItem(LS.BG_CAROUSEL);
    if (!raw) return [...DEFAULT_CAROUSEL_BG_URLS];
    const arr = JSON.parse(raw) as unknown;
    if (!Array.isArray(arr) || arr.length === 0) return [...DEFAULT_CAROUSEL_BG_URLS];
    return arr.filter((u): u is string => typeof u === "string" && u.length > 0);
  } catch {
    return [...DEFAULT_CAROUSEL_BG_URLS];
  }
}

export function saveCarouselBackgrounds(urls: string[]) {
  localStorage.setItem(LS.BG_CAROUSEL, JSON.stringify(urls));
  emitKioskUpdate();
}

export function loadWelcome(defaults: WelcomeMessages): WelcomeMessages {
  try {
    const raw = localStorage.getItem(LS.WELCOME);
    if (!raw) return { ...defaults };
    const o = JSON.parse(raw) as Partial<WelcomeMessages>;
    return {
      "zh-CN": typeof o["zh-CN"] === "string" ? o["zh-CN"] : defaults["zh-CN"],
      en: typeof o.en === "string" ? o.en : defaults.en,
      bo: typeof o.bo === "string" ? o.bo : defaults.bo,
    };
  } catch {
    return { ...defaults };
  }
}

export function saveWelcome(w: WelcomeMessages) {
  localStorage.setItem(LS.WELCOME, JSON.stringify(w));
  emitKioskUpdate();
}

export type GuideLangKey = "zh-CN" | "en" | "bo";

export type GuideLocalePatch = {
  intro?: string;
  touchItems?: string[];
  softwareItems?: string[];
};

export type GuideOverrides = Partial<Record<GuideLangKey, GuideLocalePatch>>;

export function loadGuideOverrides(): GuideOverrides {
  try {
    const raw = localStorage.getItem(LS.GUIDE);
    if (!raw) return {};
    return JSON.parse(raw) as GuideOverrides;
  } catch {
    return {};
  }
}

export function saveGuideOverrides(o: GuideOverrides) {
  localStorage.setItem(LS.GUIDE, JSON.stringify(o));
  emitKioskUpdate();
}

export function mergeGuideLocales(
  base: readonly GuideLocale[],
  overrides: GuideOverrides
): GuideLocale[] {
  return base.map((loc) => {
    const key = loc.htmlLang as GuideLangKey;
    const p = overrides[key];
    if (!p) return { ...loc };
    return {
      ...loc,
      intro: p.intro ?? loc.intro,
      touchItems: p.touchItems ?? loc.touchItems,
      softwareItems: p.softwareItems ?? loc.softwareItems,
    };
  });
}

export type KnowledgeEntry = {
  id: string;
  type: "文字" | "图片" | "视频";
  title: string;
  content: string;
  date: string;
  tags: string[];
  image?: string;
  videoUrl?: string;
};

export type VideoProtocol = "HLS" | "WebRTC" | "RTSP";

export type VideoSourceConfig = {
  protocol: VideoProtocol;
  url: string;
  note: string;
};

export const DEFAULT_VIDEO_SOURCE: VideoSourceConfig = {
  protocol: "HLS",
  url: "",
  note: "",
};

export const DEFAULT_KNOWLEDGE_ENTRIES: KnowledgeEntry[] = [
  {
    id: "k1",
    type: "文字",
    title: "黑洞的形成与演化",
    content:
      "黑洞是宇宙中最神秘的天体之一,当大质量恒星耗尽核燃料后,会发生引力坍缩并形成事件视界。事件视界内的光与物质无法逃逸,外部可通过吸积盘辐射与引力波信号间接探测。",
    date: "2026-03-15",
    tags: ["黑洞", "天体物理"],
    image: "/knowledge/black-hole.png",
  },
  {
    id: "k2",
    type: "图片",
    title: "猎户座大星云观测图",
    content:
      "使用稻城天文台2.5米望远镜拍摄的猎户座大星云高清图像,展示了恒星诞生的壮观景象。图像涵盖电离氢区与年轻星团,可用于公众科普与教学对比不同时段观测条件。",
    date: "2026-03-20",
    tags: ["星云", "观测数据"],
    image:
      "https://images.unsplash.com/photo-1628632642012-290ae1f4b1ac?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&w=400",
  },
  {
    id: "k3",
    type: "视频",
    title: "日全食观测全程记录",
    content:
      "2026年3月稻城地区日全食的完整观测记录,包含贝利珠、日冕等精彩现象。片中含安全观测提示与关键时间节点标注,适合展厅循环播放与导览讲解引用。",
    date: "2026-03-21",
    tags: ["日食", "观测记录"],
    image: "/knowledge/total-solar-eclipse.png",
  },
  {
    id: "k4",
    type: "文字",
    title: "暗物质探测最新进展",
    content:
      "稻城天文台参与的暗物质探测项目取得重大突破,可能为理解宇宙组成提供新线索。团队结合多波段数据与统计方法,正在验证新的信号模型并推进国际合作数据共享。",
    date: "2026-03-18",
    tags: ["暗物质", "科研进展"],
    image: "/knowledge/dark-matter.png",
  },
  {
    id: "k5",
    type: "图片",
    title: "银河中心区域深空摄影",
    content:
      "长曝光拍摄的银河中心区域,清晰展现了恒星密集区和暗物质带。后期校准了平场与偏置帧,可在高对比度显示器上呈现银心附近尘埃带的层次细节。",
    date: "2026-03-10",
    tags: ["银河系", "深空摄影"],
    image:
      "https://images.unsplash.com/photo-1631594274394-279ab758c1ed?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&w=400",
  },
  {
    id: "k6",
    type: "视频",
    title: "天文望远镜操作教程",
    content:
      "详细介绍如何使用稻城天文台的大型望远镜进行天体观测和数据采集。内容涵盖指向校准、导星设置、曝光序列与安全收镜流程,并附常见故障排查要点。",
    date: "2026-03-05",
    tags: ["教程", "望远镜"],
    image: "/knowledge/telescope.png",
  },
  {
    id: "k7",
    type: "文字",
    title: "火星冲日与行星观测要点",
    content:
      "冲日期间火星视直径最大、亮度最高,是行星观测与摄影的窗口期,需注意大气视宁度与设备校准。建议选择透明度高、气流稳定的夜晚,并预留色散与滤镜的对比实验时间。",
    date: "2026-03-12",
    tags: ["行星", "观测记录"],
    image: "/knowledge/planet-observation.png",
  },
  {
    id: "k8",
    type: "图片",
    title: "月球环形山特写",
    content:
      "高分辨率月面成像,展示哥白尼环形山及周边辐射纹细节,可用于科普对比不同时相月貌。拍摄采用短曝光叠加,突出环形山壁与中央峰的明暗过渡。",
    date: "2026-03-08",
    tags: ["月球", "观测数据"],
    image: "/knowledge/moon-crater.png",
  },
  {
    id: "k9",
    type: "视频",
    title: "流星雨观测指南短片",
    content:
      "英仙座与双子座流星雨观测技巧:光害规避、曝光参数与记录表格示例,适合公众开放夜播放。片中归纳了辐射点高度与流量峰值时段,便于安排户外讲解动线。",
    date: "2026-03-01",
    tags: ["流星雨", "教程"],
    image: "/knowledge/meteor-shower.png",
  },
  {
    id: "k10",
    type: "文字",
    title: "系外行星凌星测光简介",
    content:
      "当行星从恒星盘面前方经过会造成微小亮度下降,稻城台站可参与后续跟进与科普数据展示。简介测光曲线形态与噪声来源,帮助观众理解“凌星”信号的物理含义。",
    date: "2026-02-28",
    tags: ["系外行星", "科研进展"],
    image: "/knowledge/exoplanet.png",
  },
];

export function loadKnowledgeEntries(): KnowledgeEntry[] {
  try {
    const raw = localStorage.getItem(LS.KNOWLEDGE);
    if (!raw) return DEFAULT_KNOWLEDGE_ENTRIES.map((e) => ({ ...e }));
    const arr = JSON.parse(raw) as unknown;
    if (!Array.isArray(arr) || arr.length === 0) return DEFAULT_KNOWLEDGE_ENTRIES.map((e) => ({ ...e }));
    return arr.filter((x): x is KnowledgeEntry => x && typeof x.id === "string" && typeof x.title === "string");
  } catch {
    return DEFAULT_KNOWLEDGE_ENTRIES.map((e) => ({ ...e }));
  }
}

export function saveKnowledgeEntries(entries: KnowledgeEntry[]) {
  localStorage.setItem(LS.KNOWLEDGE, JSON.stringify(entries));
  emitKioskUpdate();
}

export function loadVideoSourceConfig(): VideoSourceConfig {
  try {
    const raw = localStorage.getItem(LS.VIDEO_SOURCE);
    if (!raw) return { ...DEFAULT_VIDEO_SOURCE };
    const parsed = JSON.parse(raw) as Partial<VideoSourceConfig>;
    const protocol: VideoProtocol =
      parsed.protocol === "WebRTC" || parsed.protocol === "RTSP" || parsed.protocol === "HLS"
        ? parsed.protocol
        : "HLS";
    return {
      protocol,
      url: typeof parsed.url === "string" ? parsed.url : "",
      note: typeof parsed.note === "string" ? parsed.note : "",
    };
  } catch {
    return { ...DEFAULT_VIDEO_SOURCE };
  }
}

export function saveVideoSourceConfig(cfg: VideoSourceConfig) {
  localStorage.setItem(LS.VIDEO_SOURCE, JSON.stringify(cfg));
  emitKioskUpdate();
}

export function resetVideoSourceConfig() {
  localStorage.removeItem(LS.VIDEO_SOURCE);
  emitKioskUpdate();
}