Skip to content

Commit

Permalink
feat: update bindings (#62)
Browse files Browse the repository at this point in the history
* feat: start converting to new binding api

* feat: update all bindings

* feat: add removeMultipleTracks binding
  • Loading branch information
believer authored Oct 5, 2019
1 parent 6abed20 commit 8d37304
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 85 deletions.
8 changes: 4 additions & 4 deletions __tests__/NowPlaying_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ open Expect;

describe("#nowPlayingData", () => {
test("nothing is playing", () => {
let sonos: Sonos.Decode.currentTrackResponse = {
let sonos: Sonos.Decode.CurrentTrack.t = {
album: Some("30 Seconds to Mars"),
albumArtURI: "",
albumArtURL: "",
uri: "",
uri: Some(""),
artist: "30 Seconds to Mars",
duration: 3600.0,
title: "Echelon",
Expand All @@ -19,11 +19,11 @@ describe("#nowPlayingData", () => {
});

test("current track", () => {
let sonos: Sonos.Decode.currentTrackResponse = {
let sonos: Sonos.Decode.CurrentTrack.t = {
album: Some("30 Seconds to Mars"),
albumArtURI: "",
albumArtURL: "",
uri: "",
uri: Some(""),
artist: "30 Seconds to Mars",
duration: 360.0,
title: "Echelon",
Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@
"bs-express": "0.12.1",
"bs-platform": "5.0.6",
"chalk": "2.4.2",
"cross-env": "6.0.0",
"cross-env": "6.0.3",
"is-ci-cli": "1.1.1",
"jest-date-mock": "1.0.7",
"jest-watch-typeahead": "0.4.0",
"nodemon": "1.19.2"
"nodemon": "1.19.3"
},
"dependencies": {
"@ryb73/decco": "^0.1.0",
"@wejay/spotify": "0.1.5",
"body-parser": "1.19.0",
"bs-sonos": "0.1.1",
"date-fns": "2.2.1",
"bs-sonos": "2.1.0",
"date-fns": "2.4.1",
"dotenv": "8.1.0",
"express": "4.17.1",
"sonos": "1.12.3"
"sonos": "1.12.4"
},
"jest": {
"setupFiles": [
Expand Down
9 changes: 2 additions & 7 deletions src/Config.re
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
type t;

[@bs.val] external fromStringWithEncoding: string => t = "Buffer.from";
[@bs.send] external toString: (t, string) => string = "toString";

Dotenv.config();

let env = Node.Process.process##env;
Expand All @@ -16,6 +11,6 @@ let mostPlayedUrl = Js.Dict.unsafeGet(env, "MOST_PLAYED_URL");
let toplistUrl = Js.Dict.unsafeGet(env, "TOPLIST_URL");
let blameUrl = Js.Dict.unsafeGet(env, "BLAME_URL");

let device = Sonos.Methods.device(wejayIp);
let device = Sonos.Methods.Device.make(wejayIp);

device->Sonos.Methods.setSpotifyRegion(Sonos.Methods.regionEurope);
device->Sonos.Methods.SpotifyRegion.set(Sonos.Methods.SpotifyRegion.europe);
1 change: 0 additions & 1 deletion src/Server.re
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
open Express;

let app = express();
let device = Sonos.Methods.device(Config.wejayIp);

App.use(app, Middleware.json());
App.use(app, Middleware.urlencoded(~extended=false, ()));
Expand Down
2 changes: 0 additions & 2 deletions src/SonosSpecialCase.re

This file was deleted.

2 changes: 1 addition & 1 deletion src/commands/Blame.re
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module Request = {
let run = () =>
Js.Promise.(
Services.getCurrentTrack()
|> then_(({uri}: Sonos.Decode.currentTrackResponse) => {
|> then_(({uri}: Sonos.Decode.CurrentTrack.t) => {
let uri = Utils.sonosUriToSpotifyUri(uri);

Request.make(uri)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/EasterEgg.re
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module Test = {
let make = continuation => {
Js.Promise.(
Services.getCurrentTrack()
|> then_(({uri}: Sonos.Decode.currentTrackResponse) => {
|> then_(({uri}: Sonos.Decode.CurrentTrack.t) => {
let isEasterEgg =
Track.easterEggTracks
->Belt.List.some(track =>
Expand Down
4 changes: 2 additions & 2 deletions src/commands/NowPlaying.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let message = (~sonos, ~cover) => {
let {artist, title, album, position, duration}: Sonos.Decode.currentTrackResponse = sonos;
let {artist, title, album, position, duration}: Sonos.Decode.CurrentTrack.t = sonos;

let trackDuration = Utils.parseDuration(duration);
let currentPosition = Utils.parseDuration(position);
Expand All @@ -20,7 +20,7 @@ let message = (~sonos, ~cover) => {
let run = () =>
Js.Promise.(
Services.getCurrentTrack()
|> then_((sonos: Sonos.Decode.currentTrackResponse) => {
|> then_((sonos: Sonos.Decode.CurrentTrack.t) => {
let uri = Utils.sonosUriToSpotifyUri(sonos.uri);
let id = SpotifyUtils.trackId(uri);

Expand Down
23 changes: 12 additions & 11 deletions src/commands/PlayerControl.re
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
open Sonos.Methods;
open Js.Promise;

/* The Sonos device we send all commands to. Connect devices in the Sonos
* Controller App to make it play everywhere. */
let device = Config.device;

let pause = () =>
device->pause()
device->Sonos.Methods.PlayerControl.pause()
|> then_(_ =>
`Ok(Slack.Block.make([`Section("Playback paused")])) |> resolve
)
Expand All @@ -17,7 +16,7 @@ let pause = () =>

let next = () =>
EasterEgg.Test.make(
device->next()
device->Sonos.Methods.PlayerControl.next()
|> then_(_ =>
`Ok(Slack.Block.make([`Section("Playing next track")])) |> resolve
)
Expand All @@ -29,7 +28,7 @@ let next = () =>

let previous = () =>
EasterEgg.Test.make(
device->previous()
device->Sonos.Methods.PlayerControl.previous()
|> then_(_ =>
`Ok(Slack.Block.make([`Section("Playing previous track")]))
|> resolve
Expand All @@ -41,7 +40,7 @@ let previous = () =>
);

let mute = isMuted =>
device->setMuted(isMuted)
device->Sonos.Methods.PlayerControl.Volume.mute(isMuted)
|> then_(_ => {
let message = isMuted ? "Muted speakers" : "Unmuted speakers";

Expand All @@ -50,12 +49,14 @@ let mute = isMuted =>

let play = () =>
Queue.queueWithFallback()
|> then_(({items}: Sonos.Decode.currentQueueResponse) => {
|> then_(({items}: Sonos.Decode.CurrentQueue.t) => {
let message =
switch (items->Belt.Array.length) {
| 0 => Message.emptyQueue
| _ =>
device->play() |> then_(_ => resolve(true)) |> ignore;
device->Sonos.Methods.PlayerControl.play()
|> then_(_ => resolve(true))
|> ignore;

"Start playing!";
};
Expand All @@ -76,12 +77,12 @@ let playTrack = trackNumber =>
|> resolve
| trackNumber =>
EasterEgg.Test.make(
device->selectTrack(trackNumber |> int_of_string)
device->Sonos.Methods.Track.select(trackNumber |> int_of_string)
|> then_(_ =>
Services.getCurrentTrack()
|> then_(({artist, title}: Sonos.Decode.currentTrackResponse) => {
|> then_(({artist, title}: Sonos.Decode.CurrentTrack.t) => {
Services.getPlayingState()
|> then_((state: Sonos.Decode.currentPlayingState) => {
|> then_((state: Sonos.Decode.CurrentPlayState.t) => {
switch (state) {
| Paused
| Stopped => play() |> ignore
Expand All @@ -106,7 +107,7 @@ let playTrack = trackNumber =>
)
|> catch(_ =>
Queue.queueWithFallback()
|> then_(({items}: Sonos.Decode.currentQueueResponse) => {
|> then_(({items}: Sonos.Decode.CurrentQueue.t) => {
let message =
switch (items->Belt.Array.length) {
| 0 =>
Expand Down
39 changes: 20 additions & 19 deletions src/commands/Queue.re
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
open Sonos.Methods;
open Sonos.Decode;
open Js.Promise;

let device = Config.device;

let trackPosition = (~first, ~queueAt, ()) =>
int_of_string(first) - int_of_float(queueAt) + 1 |> string_of_int;

let listTracks = (tracks: array(currentQueue)) =>
let listTracks = (tracks: array(Sonos.Decode.CurrentQueue.queueItem)) =>
tracks->Belt.Array.mapWithIndex((i, {artist, title}) =>
Utils.listNumber(i) ++ Utils.artistAndTitle(~artist, ~title)
);

let queueWithFallback = () =>
device->getQueue()
device->Sonos.Methods.Queue.get()
|> then_(queue =>
(
switch (Js.Types.classify(queue)) {
Expand All @@ -23,13 +21,13 @@ let queueWithFallback = () =>
| _ => queue
}
)
|> currentQueueResponse
|> Sonos.Decode.CurrentQueue.make
|> resolve
);

/* CLI style */
let clear = () =>
device->Sonos.Methods.flush()
device->Sonos.Methods.Queue.clear()
|> then_(_ =>
`Ok(
Slack.Block.make([
Expand Down Expand Up @@ -64,7 +62,7 @@ let handleRemoveArgs = args => {
let removeMultipleTracks = args => {
let (numberOfTracks, index) = handleRemoveArgs(args);

SonosSpecialCase.removeMultipleTracks(device, index, numberOfTracks)
Sonos.Methods.Queue.removeMultipleTracks(device, index, numberOfTracks)
|> then_(_ => {
let message =
switch (index, numberOfTracks) {
Expand All @@ -77,9 +75,9 @@ let removeMultipleTracks = args => {

let current = () =>
queueWithFallback()
|> then_(({items}) =>
|> then_(({items}: Sonos.Decode.CurrentQueue.t) =>
Services.getCurrentTrack()
|> then_(({queuePosition}) => {
|> then_(({queuePosition}: Sonos.Decode.CurrentTrack.t) => {
let numberOfTracks = items->Belt.Array.length;

let message =
Expand Down Expand Up @@ -107,7 +105,7 @@ let current = () =>

let full = () =>
queueWithFallback()
|> then_(({items}) => {
|> then_(({items}: Sonos.Decode.CurrentQueue.t) => {
let message =
switch (items->Belt.Array.length) {
| 0 => Message.emptyQueue
Expand All @@ -132,15 +130,15 @@ module Exists = {
->Spotify.getSpotifyTrack
|> then_((spotify: Spotify.WejayTrack.t) =>
queueWithFallback()
|> then_(({items}) => {
|> then_(({items}: Sonos.Decode.CurrentQueue.t) => {
/* Only checking URI is not enough, so we do a
* naive artist/track name match as well. Sonos
* sometimes uses a different track URI
* than what was pasted in Wejay because
* of different regions or markets */
let existsInQueue =
items->Belt.Array.some(item =>
Utils.sonosUriToSpotifyUri(item.uri) === trackUri
Utils.sonosUriToSpotifyUri(Some(item.uri)) === trackUri
|| item.artist === spotify.artist
&& item.title === spotify.name
);
Expand All @@ -153,12 +151,12 @@ module Exists = {

module AsLastTrack = {
let queue = track => {
device->queueAsLast(track)
device->Sonos.Methods.Queue.asLast(track)
|> then_(queuedTrack =>
Services.getCurrentTrack()
|> then_(({queuePosition}: currentTrackResponse) => {
let {firstTrackNumberEnqueued}: queueResponse =
queuedTrack->queueResponse;
|> then_(({queuePosition}: Sonos.Decode.CurrentTrack.t) => {
let {firstTrackNumberEnqueued}: Sonos.Decode.QueueTrack.t =
queuedTrack->Sonos.Decode.QueueTrack.make;

let message =
"Sweet! Your track is number *"
Expand Down Expand Up @@ -222,8 +220,11 @@ let next = track => {
)
| NotInQueue =>
Services.getCurrentTrack()
|> then_(({position, queuePosition}) =>
device->queue(parsedTrack, int_of_float(queuePosition) + 1)
|> then_(({position, queuePosition}: Sonos.Decode.CurrentTrack.t) =>
device->Sonos.Methods.Queue.atPosition(
parsedTrack,
int_of_float(queuePosition) + 1,
)
|> then_(() => {
let message =
switch (position, queuePosition) {
Expand Down Expand Up @@ -275,4 +276,4 @@ let multiple = tracks => {

resolve(`Ok(Slack.Block.make([`Section(message)])));
});
};
};
Loading

0 comments on commit 8d37304

Please sign in to comment.