Skip to content

Getting player status

Use the MQTT connection flow from Connecting to players before requesting status. The status command asks the player to publish its current state to the status data topic.

Subscribe to device/{id}/data/status after the MQTT client connects:

const statusTopic = `device/${deviceId}/data/status`;
mqttClient.subscribe(statusTopic);

Publish an empty message to device/{id}/command/status/request:

const statusRequestTopic = `device/${deviceId}/command/status/request`;
mqttClient.publish(statusRequestTopic, "");

The player publishes its current public status to device/{id}/data/status. The payload contains a status object. Parse that message and read batteryLevel from the status object:

const statusTopic = `device/${deviceId}/data/status`;
mqttClient.on("message", (topic, message) => {
if (topic !== statusTopic) {
return;
}
const { status } = JSON.parse(message.toString());
console.log("Player status version:", status.statusVersion);
console.log("Player firmware:", status.fwVersion);
console.log("Player battery level:", status.batteryLevel);
});

If no message arrives, check that the player is online and that the client is subscribed before publishing the status request.

Show complete example
import { connect } from "mqtt";
export const MQTT_URL =
"wss://aqrphjqbp3u2z-ats.iot.eu-west-2.amazonaws.com/mqtt";
export function getStatusTopic({ deviceId }) {
return `device/${deviceId}/data/status`;
}
export function getStatusRequestTopic({ deviceId }) {
return `device/${deviceId}/command/status/request`;
}
export function readPlayerStatus({ topic, message, deviceId }) {
const expectedTopic = getStatusTopic({ deviceId });
if (topic !== expectedTopic) {
return null;
}
const status = JSON.parse(message.toString());
const playerStatus = status.status;
if (!playerStatus) {
throw new Error(
`Player status message did not include status. Received keys: ${Object.keys(
status
).join(", ")}. Payload: ${JSON.stringify(status)}`
);
}
if (playerStatus.statusVersion === undefined) {
throw new Error(
`Player status did not include statusVersion. Received keys: ${Object.keys(
playerStatus
).join(", ")}. Payload: ${JSON.stringify(status)}`
);
}
return playerStatus;
}
export async function getDevices({ accessToken }) {
const deviceResponse = await fetch(
"https://api.yotoplay.com/device-v2/devices/mine",
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
if (!deviceResponse.ok) {
throw new Error(`Failed to fetch devices: ${deviceResponse.statusText}`);
}
const { devices } = await deviceResponse.json();
return devices;
}
export async function getPlayerStatus({
accessToken,
deviceId,
timeoutMs = 30_000,
}) {
const devices = await getDevices({ accessToken });
const device = devices.find(function (currentDevice) {
return currentDevice.deviceId === deviceId;
});
if (!device) {
throw new Error(`Device ${deviceId} was not found in this account`);
}
if (!device.online) {
throw new Error(`Device ${deviceId} is not online`);
}
const client = connect(MQTT_URL, {
keepalive: 300,
port: 443,
protocol: "wss",
username: `${deviceId}?x-amz-customauthorizer-name=PublicJWTAuthorizer`,
password: accessToken,
reconnectPeriod: 0,
clientId: `DASH${deviceId}`,
ALPNProtocols: ["x-amzn-mqtt-ca"],
});
return await new Promise(function (resolve, reject) {
const statusTopic = getStatusTopic({ deviceId });
const statusRequestTopic = getStatusRequestTopic({ deviceId });
const timeout = setTimeout(function () {
client.end(true);
reject(new Error(`Timed out waiting for ${statusTopic}`));
}, timeoutMs);
client.on("error", function (error) {
clearTimeout(timeout);
client.end(true);
reject(error);
});
client.on("connect", function () {
client.subscribe(statusTopic, function (subscribeError) {
if (subscribeError) {
clearTimeout(timeout);
client.end(true);
reject(subscribeError);
return;
}
client.publish(statusRequestTopic, "");
});
});
client.on("message", function (topic, message) {
let status;
try {
status = readPlayerStatus({ topic, message, deviceId });
} catch (error) {
clearTimeout(timeout);
client.end(true);
reject(error);
return;
}
if (status === null) {
return;
}
clearTimeout(timeout);
client.end(true);
resolve(status);
});
});
}