$data · 駕駛資料
$data 按本儀表的輸入標識符提供資料:current 是當前輸入,history 是當前行程最近 30 秒的輸入採樣。
可讀取的資料
點擊編輯器頂部的儀表名稱,打開 儀表設定 → 輸入綁定。齒輪進入 參數設定,用於定義輸入的名稱、標識符和類型;回傳輸入綁定後填寫本機數值,或為 OBD 輸入選擇自己的車輛參數。基礎資料由 App 自動提供。
腳本通過 $data.current.<identifier> 訪問輸入。例如,基礎速度輸入 speed 使用 $data.current.speed,OBD 輸入 coolant 使用 $data.current.coolant。欄位名與車輛參數 UUID、設備名稱相互獨立。更換車輛參數只需重新綁定;修改輸入標識符才需要修改程式碼。
| 類型 | 配置與腳本讀到的內容 |
|---|---|
| 數值 | 設定預設值及可選上下限;腳本直接讀到數字,例如 $data.current.limit。 |
| 文字 | 設定預設文字;腳本直接讀到字串,例如 $data.current.label。 |
| 開關 | 設定預設開關狀態;腳本直接讀到布爾值,例如 $data.current.showUnit。 |
| 選擇項 | 定義可選文字和預設項;腳本讀到選中的字串,例如 $data.current.style。 |
| 基礎資料 | 選擇速度、G 值、位置或時間等來源;讀數結構隨來源而定。 |
| OBD 資料 | 定義預期值類型,再在輸入綁定中選擇設備及車輛參數;讀數為 { value, unit } 或 null。 |
下面的欄位表使用示例標識符,不表示每塊儀表自動擁有這些欄位。實際接口和類型提示按當前儀表的輸入定義生成。
讀數結構與空值
基礎量值和 OBD 讀數通常為 { value, unit };位置、G 值、姿態等基礎資料具有各自分量,記錄狀態為布爾值。數值、文本、開關與選擇項直接回傳其配置值。
OBD 未綁定或配置不存在時整個輸入為 null;綁定存在但暫時缺測時保留 { value: null, unit }。使用 reading?.value == null 檢查缺測,保留有效的 0。OBD 的 value 可能為映射文字;類型聲明不做運行時轉換,呼叫數字方法前應檢查類型。
歷史視窗與時間單位
$data.history 是 當前行程最近 30 秒 的已選資料,按從舊到新排列。宿主統一儲存已採集的全部資料,再按儀表輸入參數按需組裝;打開儀表即可讀取已有窗口。暫停繪制、跳過渲染或重新建立運行實例不會丟失窗口。結束或切換行程、切換運行模式時重置;沒有活動行程時歷史窗口為空。
歷史最多按 5 Hz 採樣,相鄰採樣至少相隔 200 毫秒,最多保留 151 項。重復採樣時間和僅由顯示環境變化引起的重繪不會增加採樣;資料停止後,宿主仍會按單調時間清除過期採樣,無需腳本執行。此上限不限制 current 讀取本次最新輸入,也不會發起額外的 OBD 查詢。
每個採樣採用與 current 一致的欄位結構,可能包含缺測。修改輸入定義或綁定會重新組裝窗口;數值、開關、文本與選項等固定參數使用當前配置。資料更新只比較 $data.current 的實際內容來決定是否執行 JS,數值、單位和分組子欄位都參與比較;同值的新響應、歷史增長或裁剪、內部採集時間及未選來源更新不會單獨觸發執行。宿主仍持續維護完整窗口,下一次 current 改變時腳本取得最新曆史。顯式選擇的時間戳或已記錄時長屬於 current,數值變化仍會觸發執行。OBD 讀數保留採樣時的數值、單位與缺測狀態,不會用後來的讀數回填;基礎速度會按當前顯示單位轉換。elapsed.value 是當前行程內經過的秒數,timestamp.value 是毫秒時間戳,movingTime.value 是以秒計的行駛時長。繪制時間曲線時使用實際時間,並保留缺測造成的斷點。
示例:同時顯示速度與 OBD 參數
定義兩個輸入:標識符為 speed 的基礎速度輸入,以及標識符為 coolant 的 OBD 輸入。為後者綁定本機車輛參數。下例通過各自的輸入標識符顯示讀數與單位;重新綁定設備時無需修改示例程式碼。
function main() {
const speed = $data.current.speed;
const parameter = $data.current.coolant;
return (
<VStack spacing={8}>
<Text fontSize={40}>
{speed?.value == null ? '—' : speed.value.toFixed(0)}
</Text>
<Text>{speed?.unit ?? $env.speedUnit}</Text>
<Text>
{parameter?.value == null ? '—' : parameter.value.toString()}
</Text>
<Text>{parameter?.unit ?? ''}</Text>
</VStack>
);
}
更新與資料來源
每次腳本執行使用本次最新輸入;實際頻率由資料來源、輸入變化和處理耗時決定。current 和 history 回傳資料副本,修改它們不會改變感測器讀數或車輛配置。
基礎速度等欄位會應用有效的行程資料替換設定。直接讀取 OBD 參數則使用該參數自己的讀數及單位,查詢間隔、車輛回應和其他請求會影響實際更新。資料可用性還受定位、識別狀態和設備連線影響;編輯器預覽使用獨立的模擬輸入。
欄位
current{ [identifier: string]: number | string | boolean | object | null }所選資料的最新值
history{ [identifier: string]: number | string | boolean | object | null }[]最近 30 秒的已選資料,按時間從早到晚
current.speed{ value: number | null; unit: "km/h" | "mph" }速度
current.gForce{ longitudinal: number | null; lateral: number | null; combined: number | null; unit: string }G 值
current.heading{ direction: number | null; accuracy: number | null; unit: string }車頭朝向
current.location{ latitude: number | null; longitude: number | null; unit: string }定位
current.vehicleAttitude{ roll: number | null; pitch: number | null; unit: string }車輛姿態
current.locationAccuracy{ value: number | null; unit: "m" }定位精度
current.altitude{ value: number | null; unit: "m" }海拔
current.distance{ value: number | null; unit: "m" }里程
current.movingTime{ value: number | null; unit: "s" }行駛時間
current.timestamp{ value: number | null; unit: "ms" }擷取時間
current.elapsed{ value: number | null; unit: "s" }記錄時長
current.recordingboolean正在記錄
current.coolant{ value: number | string | null; unit: string | null } | nullOBD 資料
current.limitnumber數值
current.labelstring文字
current.showUnitboolean開關
current.stylestring選擇項