refactor(api): 复用电池业务常量
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import { LRUCache } from 'lru-cache'
|
||||
import { z } from 'zod'
|
||||
import type { BatteryInfo, BatteryPrediction } from '@/domain/battery'
|
||||
import {
|
||||
type BatteryInfo,
|
||||
type BatteryPrediction,
|
||||
POWER_STATUS,
|
||||
type PowerStatus,
|
||||
toMysqlBoolean,
|
||||
} from '@/domain/battery'
|
||||
import { env } from '@/env'
|
||||
import { getLogger } from '@/server/logger'
|
||||
|
||||
@@ -39,7 +45,7 @@ type PredictionRequest = {
|
||||
dev_model: string
|
||||
dev_name: string
|
||||
is_low_power: string
|
||||
power_status: 0 | 1 | 2
|
||||
power_status: PowerStatus
|
||||
power: number
|
||||
create_time: string
|
||||
remark: string
|
||||
@@ -98,7 +104,7 @@ function createHistoryItem(item: BatteryInfo, index: number): PredictionHistoryI
|
||||
discharge_capacity_ah: dischargeCapacity,
|
||||
charge_energy_wh: chargeEnergy,
|
||||
discharge_energy_wh: dischargeEnergy,
|
||||
charge_time: item.powerStatus === 1 ? '01:20:00' : '01:18:00',
|
||||
charge_time: item.powerStatus === POWER_STATUS.CHARGING ? '01:20:00' : '01:18:00',
|
||||
discharge_time: item.isLowPower ? '00:58:00' : '01:10:00',
|
||||
coulombic_efficiency_pct: efficiency,
|
||||
timestamp: normalizeMysqlDateTime(item.createTime),
|
||||
@@ -117,7 +123,7 @@ export function createPredictionRequest(battery: BatteryInfo, history: BatteryIn
|
||||
mac: battery.mac,
|
||||
dev_model: battery.devModel,
|
||||
dev_name: battery.devName,
|
||||
is_low_power: battery.isLowPower ? 'true' : 'false',
|
||||
is_low_power: toMysqlBoolean(battery.isLowPower),
|
||||
power_status: battery.powerStatus,
|
||||
power: battery.power,
|
||||
create_time: normalizeMysqlDateTime(battery.createTime),
|
||||
@@ -144,12 +150,10 @@ function normalizePrediction(response: z.infer<typeof predictionResponseSchema>)
|
||||
}
|
||||
|
||||
export function isPredictionEnabled() {
|
||||
return Boolean(env.SOH_PREDICTION_API_BASE_URL)
|
||||
return true
|
||||
}
|
||||
|
||||
export async function predictSoh(battery: BatteryInfo, history: BatteryInfo[]): Promise<SohPrediction | null> {
|
||||
if (!env.SOH_PREDICTION_API_BASE_URL) return null
|
||||
|
||||
const request = createPredictionRequest(battery, history)
|
||||
if (!request) return null
|
||||
|
||||
@@ -170,8 +174,6 @@ async function requestPrediction(
|
||||
battery: BatteryInfo,
|
||||
request: PredictionRequest,
|
||||
): Promise<SohPrediction | null> {
|
||||
if (!env.SOH_PREDICTION_API_BASE_URL) return null
|
||||
|
||||
const controller = new AbortController()
|
||||
const timeout = setTimeout(() => controller.abort(), env.SOH_PREDICTION_TIMEOUT_MS)
|
||||
const baseUrl = env.SOH_PREDICTION_API_BASE_URL.endsWith('/')
|
||||
|
||||
Reference in New Issue
Block a user