From 36aecfea44bd15c0a88124ffb29aca73a410f306 Mon Sep 17 00:00:00 2001 From: sam Date: Fri, 28 Feb 2025 16:40:08 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20sungrow=20site=20routing=20and=20ap?= =?UTF-8?q?i=20call?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/(defaults)/sungrow/plant/page.tsx | 105 ++++++++++++++++++++++++++ app/api/sungrow/site/route.ts | 22 ++++++ app/layout.tsx | 2 +- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 app/(defaults)/sungrow/plant/page.tsx create mode 100644 app/api/sungrow/site/route.ts diff --git a/app/(defaults)/sungrow/plant/page.tsx b/app/(defaults)/sungrow/plant/page.tsx new file mode 100644 index 0000000..16ba0c6 --- /dev/null +++ b/app/(defaults)/sungrow/plant/page.tsx @@ -0,0 +1,105 @@ +"use client"; + +import IconTrashLines from '@/components/icon/icon-trash-lines'; +import PanelCodeHighlight from '@/components/panel-code-highlight'; +import ComponentsTablesSimple from '@/components/tables/components-tables-simple' +import { formatUnixTimestamp } from '@/utils/helpers'; +import Tippy from '@tippyjs/react'; +import axios from 'axios'; +import React, { useEffect, useState } from "react" + +type Props = {} + +const SungrowPlant = (props: Props) => { + const [sites, setSites] = useState([]) + const [loading, setLoading] = useState(true) + + + useEffect(() => { + const fetchSites = async () => { + try { + const res = await fetch("/api/sungrow/site") + const data = await res.json() + console.log("data", data) + setSites(data) + } catch (error) { + console.error("Error fetching inverters:", error) + } finally { + setLoading(false) + } + } + + fetchSites() + }, []) + + const statusLabels: Record = { + 0: "Offline", + 1: "Normal", + } + const plantTypeLabel: Record = { + 3: "Commercial PV", + 4: "Residential PV", + } + + + return ( +
+ {loading ?

Loading...

: ( + +
+ + + + + + + {/* + + + + + + + */} + + + + + {sites.map((data) => ( + + + + + {/* + + + + + + + */} + + + ))} + +
Site NameStatusPlant TypeInstalled PowerReal-time PowerYield TodayMonthly YieldAnnual YieldTotal YieldEquivalent HoursRemarksAction
+
{data.ps_name}
+
+
+ {statusLabels[data.online_status] || "-"} +
+
{plantTypeLabel[data.ps_type] || "-"} + + + +
+
+
+ )} +
+ ) +} + +export default SungrowPlant diff --git a/app/api/sungrow/site/route.ts b/app/api/sungrow/site/route.ts new file mode 100644 index 0000000..aa99e66 --- /dev/null +++ b/app/api/sungrow/site/route.ts @@ -0,0 +1,22 @@ +import { NextResponse } from "next/server"; +import axios from "axios"; + +export async function GET() { + try { + const res = await axios.post("https://gateway.isolarcloud.com.hk/openapi/platform/queryPowerStationList", { + "page": 1, + "size": 10, + "appkey": `${process.env.SUNGROW_APP_KEY}` + } ,{ + headers: { + "Authorization": `Bearer ${process.env.SUNGROW_ACCESS_TOKEN}`, + "x-access-key": `${process.env.SUNGROW_SECRET_KEY}` + } + }) + // console.log("res", res.data) + return NextResponse.json(res.data.result_data.pageList) + } catch (error) { + console.error("API fetch error:", error); + return NextResponse.json({ error: "Failed to fetch inverters" }, { status: 500 }); + } +} diff --git a/app/layout.tsx b/app/layout.tsx index 819a18e..8ba396c 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -24,5 +24,5 @@ export default function RootLayout({ children }: { children: React.ReactNode }) {children} - ); + ) }