Skip to content
Permalink
Browse files

Add demo Node application

  • Loading branch information...
jodal committed Nov 18, 2018
1 parent 4f34b16 commit 9db5a903024cae2d6aed7ec52a25919ad9af6b7a
Showing with 126 additions and 1 deletion.
  1. +24 −1 README.md
  2. +102 −0 examples/mpc.js
@@ -73,7 +73,7 @@ To build updated JavaScript files for browser use in `dist/`, run:
yarn build
```

## Demo application
## Demo web application

![Web demo](examples/web.png)

@@ -103,6 +103,29 @@ application files, `examples/web.{html,js}`, will instantly be visible in
your browser. Thus, this can serve as a nice playing ground to get to know
the capabilities of Mopidy and Mopidy.js.

## Demo console application

In the `examples/` directory of the Gir repo, you can find `mpc.js`, a partial
clone of the `mpc` utility built using Mopidy.js on Node.

Output from the original command:

```
$ mpc
Jon Hopkins - C O S M
[playing] #6/12 2:33/7:08 (35%)
volume:100% repeat: off random: on single: off consume: off
```

Output from the `mpc.js` example:

```
$ ./examples/mpc.js
Jon Hopkins - C O S M
[playing] #6/- 2:33/7:08 (36%)
volume:100% repeat: off random: on single: off consume: off
```

## Changelog

See [CHANGELOG.md](CHANGELOG.md).
@@ -0,0 +1,102 @@
#!/usr/bin/env node

/* eslint-env node */
/* eslint-disable no-console */

/*
Output when offline:
WebSocket error: ...
Output when not playing:
volume:100% repeat: off random: on single: off consume: off
Output when playing:
Kiasmos - Shed
[playing] #9/14 0:26/5:35 (7%)
volume:100% repeat: off random: on single: off consume: off
*/

const Mopidy = require("../mopidy");

const mopidy = new Mopidy({
autoConnect: false,
webSocketUrl: "ws://localhost:6680/mopidy/ws",
});

function renderTrackNumber(track) {
return `#${track.track_no}/${track.album.num_tracks || "-"}`;
}

function renderTime(timeInSeconds) {
const minutes = Math.floor(timeInSeconds / 1000 / 60);
const seconds = Math.floor((timeInSeconds / 1000) % 60)
.toString()
.padStart(2, "0");
return `${minutes}:${seconds}`;
}

function renderPosition(track, timePosition) {
const pos = renderTime(timePosition);
const length = renderTime(track.length);
const percentage = Math.floor((timePosition * 100) / track.length);
return `${pos}/${length} (${percentage}%)`;
}

async function showPlaybackInfo() {
const trackPromise = mopidy.playback.getCurrentTrack();
const statePromise = mopidy.playback.getState();
const timePositionPromise = mopidy.playback.getTimePosition();

const track = await trackPromise;
const state = await statePromise;
const timePosition = await timePositionPromise;

if (state === "stopped") {
return;
}

const artists = track.artists.map(a => a.name).join(", ");
console.log(`${artists} - ${track.name}`);
console.log(
`[${state}] ${renderTrackNumber(track)} ` +
`${renderPosition(track, timePosition)}`
);
}

async function showTracklistInfo() {
const volumePromise = mopidy.mixer.getVolume();
const repeatPromise = mopidy.tracklist.getRepeat();
const randomPromise = mopidy.tracklist.getRandom();
const singlePromise = mopidy.tracklist.getSingle();
const consumePromise = mopidy.tracklist.getConsume();

const volume = (await volumePromise).toString().padStart(3, " ");
const repeat = ((await repeatPromise) && "on ") || "off";
const random = ((await randomPromise) && "on ") || "off";
const single = ((await singlePromise) && "on ") || "off";
const consume = ((await consumePromise) && "on ") || "off";

console.log(
`volume:${volume}% ` +
`repeat: ${repeat} ` +
`random: ${random} ` +
`single: ${single} ` +
`consume: ${consume}`
);
}

mopidy.on("state:online", async () => {
await showPlaybackInfo();
await showTracklistInfo();
process.exit();
});

mopidy.on("websocket:error", error => {
console.log(`WebSocket error: ${error.message}`);
process.exit(1);
});

mopidy.connect();

0 comments on commit 9db5a90

Please sign in to comment.
You can’t perform that action at this time.