Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | 1x 1x 1x 1x 1x 4x 5x 5x 2x 3x 1x 2x 2x 1x 17x 17x 17x 17x 17x 4x 17x 8x 17x 10x 10x 17x 17x 17x 7x 7x 17x 17x 10x 10x 10x 6x 2x 4x 4x 4x 2x 4x 1x 1x 1x 1x | 'use strict';
import semver from 'semver';
import {
NodePyATVDeviceOptions,
NodePyATVExecutableType,
NodePyATVFindAndInstanceOptions,
NodePyATVInstanceOptions,
NodePyATVVersionResponse
} from './types';
import {addRequestId, debug, getParamters, removeRequestId, request} from './tools';
import NodePyATVDevice from './device';
/**
* Default class exported by `@sebbo2002/node-pyatv`. Use [[find]] to scan for devices in your local network. Use
* [[device]] to connect to a known device by passing (at least) it's name and IP.
*
* ```typescript
* import pyatv from '@sebbo2002/node-pyatv';
* ```
*/
export default class NodePyATVInstance {
private readonly options: NodePyATVInstanceOptions = {};
/**
* Checks if pyatv is installed and ready to be used.
* Will throw an error if not.
*
* @param options
*/
public static async check(options: NodePyATVInstanceOptions = {}): Promise<void> {
const versions = await this.version(options);
if (!versions.pyatv) {
throw new Error('Unable to find pyatv. Is it installed?');
}
if (semver.lt(versions.pyatv, '0.6.0')) {
throw new Error('Found pyatv, but unforunately it\'s too old. Please update pyatv.');
}
try {
await this.find(options);
}
catch (error) {
throw new Error(`Unable to scan for devices: ${String(error).replace('Error: ', '')}`);
}
}
/**
* Resolves with the version of pyatv and of the module itself.
* If a value can't be found, null is returned instead.
*
* @param options
*/
public static async version(options: NodePyATVInstanceOptions = {}): Promise<NodePyATVVersionResponse> {
const id = addRequestId();
let pyatv = null;
let module = null;
try {
pyatv = await request(id, NodePyATVExecutableType.atvremote, ['--version'], options) as string;
}
catch (error) {
debug(id, `Unable to get pyatv version due to ${error}`, options);
}
if (pyatv && pyatv.substr(0, 10) === 'atvremote ') {
pyatv = pyatv.substr(10);
}
if (!semver.valid(pyatv)) {
debug(id, `String "${pyatv}" is not a valid pyatv version, set it to null`, options);
pyatv = null;
}
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
module = require(__dirname + '/../../package.json').version || null;
}
catch (error) {
debug(id, `Unable to get module version due to ${error}`, options);
}
if (module && !semver.valid(module)) {
debug(id, `String "${module}" is not a valid module version, set it to null`, options);
module = null;
}
removeRequestId(id);
return {
pyatv,
module
};
}
/**
* Scan the network for Apple TVs by using pyatv's atvscript. See [[NodePyATVFindAndInstanceOptions]]
* for the options allowed. Use the `host` / `hosts` attribute to filter by IP addresses. Resolves with
* an array of [[NodePyATVDevice]].
*
* ```typescript
* import pyatv from '@sebbo2002/node-pyatv';
* const devices = await pyatv.find();
* console.log(devices);
* ```
*
* @param options
*/
public static async find(options: NodePyATVFindAndInstanceOptions = {}): Promise<NodePyATVDevice[]> {
const id = addRequestId();
const parameters = getParamters(options);
const result = await request(id, NodePyATVExecutableType.atvscript, [...parameters, 'scan'], options);
if (typeof result !== 'object' || result.result !== 'success' || !Array.isArray(result.devices)) {
throw new Error(`Unable to parse pyatv response: ${JSON.stringify(result, null, ' ')}`);
}
const objects = result.devices.map((device: { address: string, identifier: string, name: string }) =>
this.device(Object.assign({}, options, {
host: device.address,
id: device.identifier,
name: device.name
}))
);
removeRequestId(id);
return objects;
}
/**
* Create a [[NodePyATVDevice]] to query the state and control it.
* At least `host` and `name` are required.
*
* @param options
*/
public static device(options: NodePyATVDeviceOptions): NodePyATVDevice {
return new NodePyATVDevice(options);
}
/**
* Use this to apply [[NodePyATVInstanceOptions]]
* (e.g. debug log method) to all further requests
*
* ```typescript
* import pyatv from '@sebbo2002/node-pyatv';
* const myPyatv = new pyatv({debug: true});
* const devices = myPyatv.find();
* console.log(devices);
* ```
* @param options
*/
public constructor(options: NodePyATVInstanceOptions = {}) {
this.options = Object.assign({}, options);
}
/**
* Checks if pyatv is installed and ready to be used.
* Will throw an error if not.
*
* @param options
*/
public async check(options: NodePyATVInstanceOptions = {}): Promise<void> {
return NodePyATVInstance.check(Object.assign({}, this.options, options));
}
/**
* Resolves with the version of pyatv and of the module itself.
* If a value can't be found, null is returned instead.
*
* @param options
*/
public async version(options: NodePyATVInstanceOptions = {}): Promise<NodePyATVVersionResponse> {
return NodePyATVInstance.version(Object.assign({}, this.options, options));
}
/**
* Scan the network for Apple TVs by using pyatv's atvscript. See [[NodePyATVFindAndInstanceOptions]]
* for the options allowed. Use the `host` / `hosts` attribute to filter by IP addresses. Resolves with
* an array of [[NodePyATVDevice]].
*
* ```typescript
* import pyatv from '@sebbo2002/node-pyatv';
* const myPyATV = new pyatv({debug: true});
* const devices = await myPyATV.find();
* console.log(devices);
* ```
*
* @param options
*/
public async find(options: NodePyATVFindAndInstanceOptions = {}): Promise<NodePyATVDevice[]> {
return NodePyATVInstance.find(Object.assign({}, this.options, options));
}
/**
* Create a [[NodePyATVDevice]] to query the state and control it.
* At least `host` and `name` are required.
*
* @param options
*/
public device(options: NodePyATVDeviceOptions): NodePyATVDevice {
return NodePyATVInstance.device(Object.assign({}, this.options, options));
}
}
|