Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const five = require("johnny-five"),
  2.   board = new five.Board(),
  3.   Mopidy = require("mopidy"),
  4.   mopidy = new Mopidy({
  5.     webSocketUrl: "ws://salon.local:6680/mopidy/ws/"
  6.   }),
  7.   Iconv = require('iconv').Iconv,
  8.   iconv = new Iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE'),
  9.   SHUTDOWN_TIMEOUT= 3600000,
  10.   DISPLAY_TIMEOUT = 5000,
  11.   OFF_TIMEOUT = 10000,
  12.   DISPLAY_CONTROLLER = "PCF8574T",
  13.   PODCASTS_FILE = 'podcast+file:///etc/mopidy/podcast/Podcasts.opml'
  14. ;
  15.  
  16.  
  17. board.on("ready", function() {
  18.   const lcd = new five.LCD({
  19.       controller: DISPLAY_CONTROLLER
  20.     }),
  21.     onLed = new five.Led(13),
  22.     onButton = new five.Button({pin: 12, isPullup: true}),
  23.     playLed = new five.Led(11),
  24.     playButton = new five.Button({pin: 10, isPullup: true}),
  25.     shuffleLed = new five.Led(9),
  26.     shuffleButton = new five.Button({pin: 8, isPullup: true}),
  27.     previousTrackButton = new five.Button({pin: 7, isPullup: true}),
  28.     nextTrackButton = new five.Button({pin: 6, isPullup: true}),
  29.     previousListButton = new five.Button({pin: 5, isPullup: true}),
  30.     nextListButton = new five.Button({pin: 4, isPullup: true});
  31.  
  32.   var currentPlaylist = 0,
  33.       currentPlaylistName = "",
  34.       isOn = false,
  35.       displayTimeout,
  36.       offTimeout,
  37.       shutdownTimeout;
  38.  
  39.   function getTextLine(text) {
  40.     return iconv.convert(text).toString("ASCII", 0, 19);
  41.   }
  42.  
  43.   function setShutdownTimeout() {
  44.     if (shutdownTimeout) {
  45.       cancelTimeout(shutdownTimeout)
  46.     }
  47.     cancelTimeout = setTimeout(shutdown, SHUTDOWN_TIMEOUT);
  48.   }
  49.  
  50.   function writeToLCD(line1, line2, line3, line4) {
  51.     lcd.clear();
  52.     lcd.cursor(0,0);
  53.     lcd.print(getTextLine(line1));
  54.     lcd.cursor(1,0);
  55.     lcd.print(getTextLine(line2));
  56.     lcd.cursor(2,0);
  57.     lcd.print(getTextLine(line3));
  58.     lcd.cursor(3,0);
  59.     lcd.print(getTextLine(line4));
  60.     showLcd()
  61.   }
  62.  
  63.   function showLcd() {
  64.     if (!isOn) {
  65.       return;
  66.     }
  67.     lcd.backlight();
  68.     lcd.on();
  69.     if (displayTimeout) {
  70.       clearTimeout(displayTimeout);
  71.     }
  72.     displayTimeout = setTimeout(hideLcd, DISPLAY_TIMEOUT);
  73.   }
  74.  
  75.   function hideLcd() {
  76.     lcd.noBacklight();
  77.     lcd.off();
  78.   }
  79.  
  80.   board.repl.inject({
  81.     onLed,
  82.     playLed,
  83.     shuffleLed
  84.   });
  85.  
  86.  
  87.   function loadPlaylistContents(index, playlistRef) {
  88.     function runPlaylist() {
  89.       currentPlaylist = index;
  90.       currentPlaylistName = playlistRef.name;
  91.       if (isOn) {
  92.         mopidy.playback.play();
  93.       }
  94.     }
  95.     function addPlaylistTracks(playlist) {
  96.       mopidy.tracklist.add({tracks: playlist.tracks}).then(runPlaylist);
  97.     }
  98.     switch (playlistRef.uri.substring(0,3)) {
  99.       case "m3u" :
  100.         mopidy.playlists.lookup([playlistRef.uri]).then(addPlaylistTracks)
  101.         break;
  102.       default:
  103.         mopidy.tracklist.add({uris: [playlistRef.uri]}).then(runPlaylist);
  104.     }
  105.   }
  106.  
  107.   function  loadPlaylist(index) {
  108.     writeToLCD("Loading...","","","")
  109.     mopidy.playlists.asList().then(function(playlists) {
  110.       mopidy.library.browse([PODCASTS_FILE]).then(function(podcasts) {
  111.         for (var i=0; i < podcasts.length; i++) {
  112.           playlists.push(podcasts[i]);
  113.         }
  114.         if (index >= playlists.length) {
  115.           index = 0;
  116.         } else if (index < 0) {
  117.           index = playlists.length - 1;
  118.         }
  119.         writeToLCD(playlists[index].name, "Loading...","","")
  120.         mopidy.tracklist.clear().then(function() {
  121.           loadPlaylistContents(index, playlists[index]);
  122.         })
  123.       })
  124.     })
  125.  
  126.   }
  127.   function setShuffleLed() {
  128.     mopidy.tracklist.getRandom().then(function(isRandom) {
  129.       if (isRandom) {
  130.         shuffleLed.on();
  131.       } else {
  132.         shuffleLed.off();
  133.       }
  134.     })
  135.   }
  136.  
  137.   function shutdown() {
  138.     mopidy.playback.stop();
  139.     onLed.off();
  140.     shuffleLed.off();
  141.     hideLcd();
  142.     isOn = false;
  143.   }
  144.  
  145.   function start() {
  146.     onLed.on();
  147.     setShuffleLed();
  148.     setShutdownTimeout();
  149.     isOn = true;
  150.   }
  151.  
  152.   onButton.on("down", function() {
  153.     if (isOn) {
  154.       shutdown();
  155.     } else {
  156.       start();
  157.       loadPlaylist(currentPlaylist);
  158.     };
  159.   });
  160.  
  161.   playButton.on("down", function() {
  162.     if (!isOn) {
  163.       return;
  164.     }
  165.     mopidy.playback.getState().then(function(state) {
  166.       switch (state) {
  167.         case "playing":
  168.           mopidy.playback.pause();
  169.           break;
  170.         case "paused":
  171.           mopidy.playback.play();
  172.       }
  173.       showLcd();
  174.       setShutdownTimeout();
  175.     });
  176.   })
  177.  
  178.   shuffleButton.on("down", function() {
  179.     if (!isOn) {
  180.       return;
  181.     }
  182.     mopidy.tracklist.getRandom().then(function(isRandom) {
  183.       mopidy.tracklist.setRandom([!isRandom])
  184.       setShutdownTimeout();
  185.     })
  186.   })
  187.  
  188.   nextTrackButton.on("down", function() {
  189.     if (!isOn) {
  190.       return;
  191.     }
  192.     mopidy.playback.next();
  193.     setShutdownTimeout();
  194.   })
  195.  
  196.   previousTrackButton.on("down", function() {
  197.     if (!isOn) {
  198.       return;
  199.     }
  200.     mopidy.playback.previous();
  201.     setShutdownTimeout();
  202.   })
  203.  
  204.   nextListButton.on("down", function() {
  205.     if (!isOn) {
  206.       return;
  207.     }
  208.     loadPlaylist(currentPlaylist + 1);
  209.     setShutdownTimeout();
  210.   })
  211.  
  212.   previousListButton.on("down", function() {
  213.     if (!isOn) {
  214.       return;
  215.     }
  216.     loadPlaylist(currentPlaylist - 1);
  217.     setShutdownTimeout();
  218.   })
  219.  
  220.   mopidy.on("event:playbackStateChanged", function(event) {
  221.     if (offTimeout) {
  222.       clearTimeout(offTimeout);
  223.     }
  224.     switch(event.new_state) {
  225.       case "playing":
  226.         if (!isOn) {
  227.           start();
  228.         }
  229.         playLed.on();
  230.         break;
  231.       case "stopped":
  232.         offTimeout = setTimeout(shutdown, OFF_TIMEOUT);
  233.         playLed.off();
  234.         break;
  235.       default:
  236.         playLed.off();
  237.     }
  238.   });
  239.  
  240.   mopidy.on("event:trackPlaybackStarted", function(event) {
  241.     var track = event.tl_track.track;
  242.     writeToLCD(
  243.       currentPlaylistName,
  244.       "artists" in track && track.artists.length > 0 ? track.artists[0].name : "",
  245.       "name" in track ? track.name : "",
  246.       "album" in track ? track.album.name : "",
  247.     )
  248.   });
  249.   mopidy.on("event:optionsChanged",setShuffleLed);
  250.   mopidy.tracklist.setRepeat([true]);
  251.   mopidy.on("event", console.log);
  252.   mopidy.on("state", console.log);
  253.   hideLcd();
  254. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement