import assert from "node:assert/strict";
import process from "node:process";
const { wrapFetch: FetchWithCookie } = await import("https://deno.land/x/another_cookiejar@v5.0.3/mod.ts");
const fetch = FetchWithCookie();
type ServiceResponse = { id: string; success: boolean };
export async function login() {
const body = new URLSearchParams({
"username": process.env.SUBARU_USERNAME,
"password": process.env.SUBARU_PASSWORD,
"deviceId": process.env.SUBARU_DEVICEID,
"lastSelectedVehicleKey": process.env.SUBARU_VEHICLEKEY,
});
const dest = "https://www.mysubaru.com/login";
const resp = await fetch(dest, { method: "POST", body });
assert(resp.status == 200, `unexpected login response status: ${resp.status}`);
}
export async function status(id: string): Promise<ServiceResponse> {
const body = new URLSearchParams({
serviceRequestId: id,
});
const url = "https://www.mysubaru.com/service/g2/remoteService/status.json";
const res = await fetch(url, { method: "POST", body });
return await parseServiceResponse(res);
}
export async function start(overrides?: Record<string, string>): Promise<ServiceResponse> {
const opts = Object.assign({
"now": `${Date.now()}`,
"pin": process.env.SUBARU_PIN,
"delay": "0",
"horn": "true",
"unlockDoorType": "ALL_DOORS_CMD",
"name": "Auto",
"runTimeMinutes": "10",
"climateZoneFrontTemp": "74",
"climateZoneFrontAirMode": "AUTO",
"climateZoneFrontAirVolume": "AUTO",
"outerAirCirculation": "auto",
"heatedRearWindowActive": "false",
"airConditionOn": "false",
"heatedSeatFrontLeft": "off",
"heatedSeatFrontRight": "off",
"startConfiguration": "START_ENGINE_ALLOW_KEY_IN_IGNITION",
"canEdit": "true",
"disabled": "false",
"vehicleType": "gas",
"presetType": "subaruPreset",
}, overrides);
const url = "https://www.mysubaru.com/service/g2/engineStart/execute.json";
const res = await fetch(url, { method: "POST", body: new URLSearchParams(opts) });
return await parseServiceResponse(res);
}
export async function stop(overrides?: Record<string, string>): Promise<ServiceResponse> {
const opts = Object.assign({
"now": `${Date.now()}`,
"pin": process.env.SUBARU_PIN,
"delay": "0",
"horn": "true",
"unlockDoorType": "ALL_DOORS_CMD",
}, overrides);
const url = "https://www.mysubaru.com/service/g2/engineStop/execute.json";
const res = await fetch(url, { method: "POST", body: new URLSearchParams(opts) });
return await parseServiceResponse(res);
}
async function parseServiceResponse(res: Response): Promise<ServiceResponse> {
assert(res.status == 200, `unexpected service response status: ${res.status}`);
const ctype = res.headers.get("Content-Type");
assert(ctype == "application/json", `unexpected service response content type: ${ctype}`);
const json = await res.json();
assert(json.success, `service request was not successful`);
return { id: json.data.serviceRequestId, success: json.data.success };
}