Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
evandcoleman committed Feb 2, 2020
1 parent 90fb5dc commit 05e8419
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 510 deletions.
157 changes: 64 additions & 93 deletions dist/bin/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const caporal = require("caporal");
let cli = caporal;
Expand All @@ -7,100 +16,86 @@ const credentials_1 = require("../lib/credentials");
const scan_1 = require("./scan");
const pair_1 = require("./pair");
const project = require('../../package.json');
function openDevice(credentials, logger) {
return __awaiter(this, void 0, void 0, function* () {
let device = yield scan_1.scan(logger, null, credentials.uniqueIdentifier);
device.on('debug', (message) => {
logger.debug(message);
});
device.on('error', (error) => {
logger.error(error.message);
logger.debug(error.stack);
});
return yield device.openConnection(credentials);
});
}
cli
.version(project.version)
.command('pair', 'Pair with an Apple TV')
.option('--timeout <timeout>', 'The amount of time (in seconds) to scan for Apple TVs', cli.INTEGER)
.action((args, options, logger) => {
scan_1.scan(logger, options.timeout)
.then(device => {
.action((args, options, logger) => __awaiter(void 0, void 0, void 0, function* () {
try {
let device = yield scan_1.scan(logger, options.timeout);
device.on('debug', (message) => {
logger.debug(message);
});
device.on('error', (error) => {
logger.error(error.message);
logger.debug(error.stack);
});
return pair_1.pair(device, logger)
.then(keys => {
logger.info("Credentials: " + device.credentials.toString());
process.exit();
});
})
.catch(error => {
let keys = yield pair_1.pair(device, logger);
logger.info("Credentials: " + device.credentials.toString());
process.exit();
}
catch (error) {
logger.error(error.message);
logger.debug(error.stack);
process.exit();
});
});
}
}));
cli
.command('command', 'Send a command to an Apple TV')
.argument('<command>', 'The command to send', /^up|down|left|right|menu|play|pause|next|previous|suspend$/)
.option('--credentials <credentials>', 'The device credentials from pairing', cli.STRING)
.action((args, options, logger) => {
.action((args, options, logger) => __awaiter(void 0, void 0, void 0, function* () {
if (!options.credentials) {
logger.error("Credentials are required. Pair first.");
process.exit();
}
let credentials = credentials_1.Credentials.parse(options.credentials);
scan_1.scan(logger, null, credentials.uniqueIdentifier)
.then(device => {
device.on('debug', (message) => {
logger.debug(message);
});
device.on('error', (error) => {
logger.error(error.message);
logger.debug(error.stack);
});
return device
.openConnection(credentials)
.then(() => {
return device
.sendKeyCommand(appletv_1.AppleTV.key(args["command"]))
.then(result => {
logger.info("Success!");
process.exit();
});
});
})
.catch(error => {
try {
let device = yield openDevice(credentials, logger);
yield device.sendKeyCommand(appletv_1.AppleTV.key(args["command"]));
logger.info("Success!");
process.exit();
}
catch (error) {
logger.error(error.message);
logger.debug(error.stack);
process.exit();
});
});
}
}));
cli
.command('state', 'Logs the playback state from the Apple TV')
.option('--credentials <credentials>', 'The device credentials from pairing', cli.STRING)
.action((args, options, logger) => {
.action((args, options, logger) => __awaiter(void 0, void 0, void 0, function* () {
if (!options.credentials) {
logger.error("Credentials are required. Pair first.");
process.exit();
}
let credentials = credentials_1.Credentials.parse(options.credentials);
scan_1.scan(logger, null, credentials.uniqueIdentifier)
.then(device => {
device.on('debug', (message) => {
logger.debug(message);
});
device.on('error', (error) => {
logger.error(error.message);
logger.debug(error.stack);
});
return device
.openConnection(credentials);
})
.then(device => {
try {
let device = yield openDevice(credentials, logger);
device.on('nowPlaying', (info) => {
logger.info(info.toString());
});
})
.catch(error => {
}
catch (error) {
logger.error(error.message);
logger.debug(error.stack);
process.exit();
});
});
}
}));
cli
.command('queue', 'Request the playback state from the Apple TV')
.option('--credentials <credentials>', 'The device credentials from pairing', cli.STRING)
Expand All @@ -109,73 +104,49 @@ cli
.option('--metadata', 'Include metadata', cli.BOOLEAN)
.option('--lyrics', 'Include lyrics', cli.BOOLEAN)
.option('--languages', 'Include language options', cli.BOOLEAN)
.action((args, options, logger) => {
.action((args, options, logger) => __awaiter(void 0, void 0, void 0, function* () {
if (!options.credentials) {
logger.error("Credentials are required. Pair first.");
process.exit();
}
let credentials = credentials_1.Credentials.parse(options.credentials);
scan_1.scan(logger, null, credentials.uniqueIdentifier)
.then(device => {
device.on('debug', (message) => {
logger.debug(message);
});
device.on('error', (error) => {
logger.error(error.message);
logger.debug(error.stack);
});
return device
.openConnection(credentials);
})
.then(device => {
return device
try {
let device = yield openDevice(credentials, logger);
let message = yield device
.requestPlaybackQueue({
location: options.location || 0,
length: options.length || 1,
includeMetadata: options.metadata,
includeLyrics: options.lyrics,
includeLanguageOptions: options.languages
});
})
.then(message => {
logger.info(message);
})
.catch(error => {
}
catch (error) {
logger.error(error.message);
logger.debug(error.stack);
process.exit();
});
});
}
}));
cli
.command('messages', 'Log all messages sent from the Apple TV')
.option('--credentials <credentials>', 'The device credentials from pairing', cli.STRING)
.action((args, options, logger) => {
.action((args, options, logger) => __awaiter(void 0, void 0, void 0, function* () {
if (!options.credentials) {
logger.error("Credentials are required. Pair first.");
process.exit();
}
let credentials = credentials_1.Credentials.parse(options.credentials);
scan_1.scan(logger, null, credentials.uniqueIdentifier)
.then(device => {
device.on('debug', (message) => {
logger.debug(message);
});
device.on('error', (error) => {
logger.error(error.message);
logger.debug(error.stack);
});
return device
.openConnection(credentials);
})
.then(device => {
try {
let device = yield openDevice(credentials, logger);
device.on('message', (message) => {
logger.info(JSON.stringify(message.toObject(), null, 2));
});
})
.catch(error => {
}
catch (error) {
logger.error(error.message);
logger.debug(error.stack);
process.exit();
});
});
}
}));
cli.parse(process.argv);
5 changes: 5 additions & 0 deletions dist/lib/appletv.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export declare class AppleTV extends EventEmitter {
pairingId: string;
credentials: Credentials;
connection: Connection;
private queuePollTimer?;
constructor(service: Service);
/**
* Pair with an already discovered AppleTV.
Expand Down Expand Up @@ -85,6 +86,10 @@ export declare class AppleTV extends EventEmitter {
private sendConnectionState;
private sendClientUpdatesConfig;
private sendWakeDevice;
private onReceiveMessage;
private onNewListener;
private onRemoveListener;
private setupListeners;
}
export declare module AppleTV {
interface Events {
Expand Down
Loading

0 comments on commit 05e8419

Please sign in to comment.