Add: Check for machineID for plexPlayer

pull/16/head
Juraj Nyíri 4 years ago
parent 884e87609c
commit ac4c746799

@ -18671,10 +18671,19 @@ class Plex {
constructor(ip, port = 32400, token, protocol = 'http') { constructor(ip, port = 32400, token, protocol = 'http') {
this.serverInfo = {}; this.serverInfo = {};
this.clients = []; this.clients = [];
this.init = async () => {
await this.getClients();
/*
setInterval(() => {
this.getClients();
}, 30000);
*/
};
this.getClients = async () => { this.getClients = async () => {
const url = `${this.protocol}://${this.ip}:${this.port}/clients?X-Plex-Token=${this.token}`; const url = `${this.protocol}://${this.ip}:${this.port}/clients?X-Plex-Token=${this.token}`;
const result = await axios.get(url); const result = await axios.get(url);
return result; this.clients = result.data.MediaContainer.Server;
return this.clients;
}; };
this.getServerID = async () => { this.getServerID = async () => {
if (lodash.isEmpty(this.serverInfo)) { if (lodash.isEmpty(this.serverInfo)) {
@ -18907,9 +18916,15 @@ class PlayController {
}); });
return service; return service;
}; };
// todo: finish check
this.isPlexPlayerSupported = () => { this.isPlexPlayerSupported = () => {
return true; let found = false;
lodash.forEach(this.plex.clients, plexClient => {
if (lodash.isEqual(plexClient.machineIdentifier, this.entity.plexPlayer)) {
found = true;
return false;
}
});
return found;
}; };
this.isPlaySupported = (data) => { this.isPlaySupported = (data) => {
return !lodash.isEmpty(this.getPlayService(data)); return !lodash.isEmpty(this.getPlayService(data));
@ -19539,6 +19554,9 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.loadInitialData = async () => { this.loadInitialData = async () => {
this.loading = true; this.loading = true;
this.renderPage(); this.renderPage();
if (this.plex) {
await this.plex.init();
}
try { try {
if (this.plex) { if (this.plex) {
const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]); const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]);

@ -230,9 +230,15 @@ class PlayController {
return service; return service;
}; };
// todo: finish check
isPlexPlayerSupported = (): boolean => { isPlexPlayerSupported = (): boolean => {
return true; let found = false;
_.forEach(this.plex.clients, plexClient => {
if (_.isEqual(plexClient.machineIdentifier, this.entity.plexPlayer)) {
found = true;
return false;
}
});
return found;
}; };
isPlaySupported = (data: Record<string, any>): boolean => { isPlaySupported = (data: Record<string, any>): boolean => {

@ -13,6 +13,8 @@ class Plex {
serverInfo: Record<string, any> = {}; serverInfo: Record<string, any> = {};
clients: Array<Record<string, any>> = [];
constructor(ip: string, port = 32400, token: string, protocol: 'http' | 'https' = 'http') { constructor(ip: string, port = 32400, token: string, protocol: 'http' | 'https' = 'http') {
this.ip = ip; this.ip = ip;
this.port = port; this.port = port;
@ -20,6 +22,22 @@ class Plex {
this.protocol = protocol; this.protocol = protocol;
} }
init = async (): Promise<void> => {
await this.getClients();
/*
setInterval(() => {
this.getClients();
}, 30000);
*/
};
getClients = async (): Promise<Record<string, any>> => {
const url = `${this.protocol}://${this.ip}:${this.port}/clients?X-Plex-Token=${this.token}`;
const result = await axios.get(url);
this.clients = result.data.MediaContainer.Server;
return this.clients;
};
getServerID = async (): Promise<any> => { getServerID = async (): Promise<any> => {
if (_.isEmpty(this.serverInfo)) { if (_.isEmpty(this.serverInfo)) {
await this.getServerInfo(); await this.getServerInfo();

@ -100,6 +100,10 @@ class PlexMeetsHomeAssistant extends HTMLElement {
loadInitialData = async (): Promise<void> => { loadInitialData = async (): Promise<void> => {
this.loading = true; this.loading = true;
this.renderPage(); this.renderPage();
if (this.plex) {
await this.plex.init();
}
try { try {
if (this.plex) { if (this.plex) {
const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]); const [serverID, plexSections] = await Promise.all([this.plex.getServerID(), this.plex.getSectionsData()]);

Loading…
Cancel
Save