Skip to content

Commit

Permalink
KAA-493: Music and photo player improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvayka committed Apr 25, 2015
1 parent 974d3d1 commit 65398df
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 18 deletions.
1 change: 1 addition & 0 deletions examples/iotworld/source/musicplayer/java/build.xml
Expand Up @@ -29,6 +29,7 @@
<mkdir dir="src/build/jar"/> <mkdir dir="src/build/jar"/>
<javac srcdir="src" destdir="src/build/classes" classpathref="classpath"/> <javac srcdir="src" destdir="src/build/classes" classpathref="classpath"/>
<jar destfile="build/jar/MusicPlayer.jar"> <jar destfile="build/jar/MusicPlayer.jar">
<fileset file="res/album.png" />
<fileset file="res/logback.xml" /> <fileset file="res/logback.xml" />
<fileset dir="src/build/classes"> <fileset dir="src/build/classes">
<include name="**/*.class"/> <include name="**/*.class"/>
Expand Down
Expand Up @@ -45,8 +45,6 @@ public class MusicPlayerApplication implements DeviceEventClassFamily.Listener,


private static final String STATE_FILE_NAME = "player.state"; private static final String STATE_FILE_NAME = "player.state";


private static final String DEFAULT_DEVICE_NAME = "Raspbery Pi 2 Music Player";

private static final int UPDATE_INTERVAL = 1000; private static final int UPDATE_INTERVAL = 1000;


private static final String DEFAULT_DIR = "E:\\music\\"; private static final String DEFAULT_DIR = "E:\\music\\";
Expand All @@ -68,7 +66,6 @@ public class MusicPlayerApplication implements DeviceEventClassFamily.Listener,


private volatile Mp3Player player; private volatile Mp3Player player;
private volatile StatusUpdateThread statusUpdateThread; private volatile StatusUpdateThread statusUpdateThread;
private volatile String deviceName = DEFAULT_DEVICE_NAME;


private volatile PlaybackStatus pendingStatus = PlaybackStatus.STOPPED; private volatile PlaybackStatus pendingStatus = PlaybackStatus.STOPPED;
private Map<String, PlaybackStatusUpdate> lastUpdates = new ConcurrentHashMap<String, PlaybackStatusUpdate>(); private Map<String, PlaybackStatusUpdate> lastUpdates = new ConcurrentHashMap<String, PlaybackStatusUpdate>();
Expand Down Expand Up @@ -147,13 +144,13 @@ private void stop() {
@Override @Override
public void onEvent(DeviceInfoRequest event, String originator) { public void onEvent(DeviceInfoRequest event, String originator) {
LOG.info("Receieved info request {}", event); LOG.info("Receieved info request {}", event);
deviceECF.sendEvent(new DeviceInfoResponse(new DeviceInfo(deviceName, "UK")), originator); deviceECF.sendEvent(new DeviceInfoResponse(new DeviceInfo(state.getDeviceName(), "UK")), originator);
} }


@Override @Override
public void onEvent(DeviceChangeNameRequest event, String originator) { public void onEvent(DeviceChangeNameRequest event, String originator) {
deviceName = event.getName(); state.setDeviceName(event.getName());
deviceECF.sendEventToAll(new DeviceInfoResponse(new DeviceInfo(deviceName, "UK"))); deviceECF.sendEventToAll(new DeviceInfoResponse(new DeviceInfo(state.getDeviceName(), "UK")));
} }


@Override @Override
Expand Down
Expand Up @@ -25,6 +25,10 @@ public class MusicPlayerState {
private static final String LISTENERS_PROP_NAME = "listeners"; private static final String LISTENERS_PROP_NAME = "listeners";
private static final String LISTENERS_PROP_DEFAULT = ""; private static final String LISTENERS_PROP_DEFAULT = "";
private static final String LISTENERS_PROP_DELIMITER = ","; private static final String LISTENERS_PROP_DELIMITER = ",";

private static final String DEVICE_PROP_NAME = "device.name";
private static final String DEFAULT_DEVICE_NAME = "Raspbery Pi 2 Photo Player";



private static final Logger LOG = LoggerFactory.getLogger(MusicPlayerApplication.class); private static final Logger LOG = LoggerFactory.getLogger(MusicPlayerApplication.class);


Expand Down Expand Up @@ -106,6 +110,15 @@ public void addStatusListener(String originator) {
persist(); persist();
} }
} }

public String getDeviceName() {
return properties.getProperty(DEVICE_PROP_NAME, DEFAULT_DEVICE_NAME);
}

public void setDeviceName(String deviceName) {
properties.setProperty(DEVICE_PROP_NAME, deviceName);
persist();
}


public Set<String> getListeners() { public Set<String> getListeners() {
return listeners; return listeners;
Expand Down
Expand Up @@ -44,8 +44,6 @@ public class PhotoPlayerApplication implements DeviceEventClassFamily.Listener,


private static final String STATE_FILE_NAME = "player.state"; private static final String STATE_FILE_NAME = "player.state";


private static final String DEFAULT_DEVICE_NAME = "Raspbery Pi 2 Photo Player";

private static final String DEFAULT_DIR = "H:\\photos\\"; private static final String DEFAULT_DIR = "H:\\photos\\";


private static final String DEFAULT_ACCESS_CODE = "DUMMY_ACCESS_CODE"; private static final String DEFAULT_ACCESS_CODE = "DUMMY_ACCESS_CODE";
Expand All @@ -65,8 +63,6 @@ public class PhotoPlayerApplication implements DeviceEventClassFamily.Listener,


private final SlideShowFrame frame; private final SlideShowFrame frame;


private volatile String deviceName = DEFAULT_DEVICE_NAME;

private volatile SlideShow player; private volatile SlideShow player;


private volatile SlideShowStatus pendingStatus = SlideShowStatus.PAUSED; private volatile SlideShowStatus pendingStatus = SlideShowStatus.PAUSED;
Expand Down Expand Up @@ -154,13 +150,13 @@ private void stop() {
@Override @Override
public void onEvent(DeviceInfoRequest event, String originator) { public void onEvent(DeviceInfoRequest event, String originator) {
LOG.info("Receieved info request {}", event); LOG.info("Receieved info request {}", event);
deviceECF.sendEvent(new DeviceInfoResponse(new DeviceInfo(deviceName, "UK")), originator); deviceECF.sendEvent(new DeviceInfoResponse(new DeviceInfo(state.getDeviceName(), "UK")), originator);
} }


@Override @Override
public void onEvent(DeviceChangeNameRequest event, String originator) { public void onEvent(DeviceChangeNameRequest event, String originator) {
deviceName = event.getName(); state.setDeviceName(event.getName());
deviceECF.sendEventToAll(new DeviceInfoResponse(new DeviceInfo(deviceName, "UK"))); deviceECF.sendEventToAll(new DeviceInfoResponse(new DeviceInfo(state.getDeviceName(), "UK")));
} }


@Override @Override
Expand Down Expand Up @@ -211,6 +207,11 @@ public void onEvent(PauseSlideShowRequest event, String originator) {
@Override @Override
public void onEvent(DeleteUploadedPhotosRequest event, String originator) { public void onEvent(DeleteUploadedPhotosRequest event, String originator) {
LOG.info("Receieved delete uploaded photos request {} from {}", event, originator); LOG.info("Receieved delete uploaded photos request {} from {}", event, originator);
if(player != null && player.getAlbumId().equals(library.getUploadsAlbumId())){
player.stop();
}
library.deleteUploadsAlbum();
photoECF.sendEvent(new PhotoAlbumsResponse(library.buildAlbumInfoList()), originator);
} }


@Override @Override
Expand Down
Expand Up @@ -134,7 +134,7 @@ public List<PhotoAlbumInfo> buildAlbumInfoList() {
} }


public String upload(PhotoUploadRequest event) { public String upload(PhotoUploadRequest event) {
String uploadAlbumPath = rootPath + File.separator + UPLOADS_ALBUM_NAME; String uploadAlbumPath = getUploadsAlbumId();
File uploadAlbumFile = new File(uploadAlbumPath); File uploadAlbumFile = new File(uploadAlbumPath);
if (!uploadAlbumFile.exists()) { if (!uploadAlbumFile.exists()) {
LOG.info("Creating upload album"); LOG.info("Creating upload album");
Expand All @@ -157,6 +157,10 @@ public String upload(PhotoUploadRequest event) {
} }
return album.getAlbumId(); return album.getAlbumId();
} }

public String getUploadsAlbumId() {
return rootPath + File.separator + UPLOADS_ALBUM_NAME;
}


private void generateThumbnails(Collection<PhotoAlbum> albums) { private void generateThumbnails(Collection<PhotoAlbum> albums) {
for(PhotoAlbum album : albums){ for(PhotoAlbum album : albums){
Expand Down Expand Up @@ -211,4 +215,18 @@ private static byte[] toByteArray(ByteBuffer bb) {
public PhotoAlbum getAlbum(String albumId) { public PhotoAlbum getAlbum(String albumId) {
return albums.get(albumId); return albums.get(albumId);
} }

public void deleteUploadsAlbum() {
String uploadDirPath = getUploadsAlbumId();
File uploadDir = new File(uploadDirPath);
for(File child : uploadDir.listFiles()){
if(!child.delete()){
LOG.warn("Failed to delete {} {}", child.getAbsolutePath(), child.isDirectory() ? "dir" : "file");
}
}
if(!uploadDir.delete()){
LOG.warn("Failed to delete {}", uploadDir.getAbsolutePath());
}
albums.remove(uploadDirPath);
}
} }
Expand Up @@ -43,6 +43,8 @@ public class SlideShow implements PreviewGenerationListener {
private volatile boolean initialized; private volatile boolean initialized;
private volatile int currentIndex; private volatile int currentIndex;
private volatile SlideShowStatus status; private volatile SlideShowStatus status;

private volatile SlideShowPanel panel;


private final ExecutorService pool = Executors.newSingleThreadExecutor(); private final ExecutorService pool = Executors.newSingleThreadExecutor();


Expand All @@ -68,6 +70,9 @@ public void pause() {
} }


public void stop() { public void stop() {
if(panel != null){
frame.remove(panel);
}
thread.stopped = true; thread.stopped = true;
thread.interrupt(); thread.interrupt();
} }
Expand Down Expand Up @@ -147,7 +152,6 @@ private class SlideShowThread extends Thread {


private static final int ALPHA_STEP = 20; private static final int ALPHA_STEP = 20;
private static final int WAIT_FOR_PREVIEW_SLEEP_TIME = 50; private static final int WAIT_FOR_PREVIEW_SLEEP_TIME = 50;
private SlideShowPanel panel;
private volatile boolean started; private volatile boolean started;
private volatile boolean paused; private volatile boolean paused;
private volatile boolean stopped; private volatile boolean stopped;
Expand Down Expand Up @@ -217,7 +221,7 @@ private int getNextIndex() {
} }


private void changeAlpha(int from, int to, int step, long duration, final BufferedImage current, final BufferedImage next) throws InterruptedException { private void changeAlpha(int from, int to, int step, long duration, final BufferedImage current, final BufferedImage next) throws InterruptedException {
final SlideShowPanel panel = this.panel; final SlideShowPanel panel = SlideShow.this.panel;
if (to == from) { if (to == from) {
return; return;
} }
Expand Down
Expand Up @@ -19,14 +19,17 @@


public class PhotoPlayerState { public class PhotoPlayerState {


private static final Logger LOG = LoggerFactory.getLogger(PhotoPlayerApplication.class);

private static final String ALBUM_ID_PROP_NAME = "album.id"; private static final String ALBUM_ID_PROP_NAME = "album.id";
private static final String GEO_FENCING_PROP_NAME = "geofencing.position"; private static final String GEO_FENCING_PROP_NAME = "geofencing.position";
private static final String OPERATION_MODE_PROP_NAME = "operation.mode"; private static final String OPERATION_MODE_PROP_NAME = "operation.mode";
private static final String LISTENERS_PROP_NAME = "listeners"; private static final String LISTENERS_PROP_NAME = "listeners";
private static final String LISTENERS_PROP_DEFAULT = ""; private static final String LISTENERS_PROP_DEFAULT = "";
private static final String LISTENERS_PROP_DELIMITER = ","; private static final String LISTENERS_PROP_DELIMITER = ",";


private static final Logger LOG = LoggerFactory.getLogger(PhotoPlayerApplication.class); private static final String DEVICE_PROP_NAME = "device.name";
private static final String DEFAULT_DEVICE_NAME = "Raspbery Pi 2 Photo Player";


private final String fileName; private final String fileName;
private final Properties properties; private final Properties properties;
Expand Down Expand Up @@ -106,6 +109,15 @@ public void addStatusListener(String originator) {
persist(); persist();
} }
} }

public String getDeviceName() {
return properties.getProperty(DEVICE_PROP_NAME, DEFAULT_DEVICE_NAME);
}

public void setDeviceName(String deviceName) {
properties.setProperty(DEVICE_PROP_NAME, deviceName);
persist();
}


public Set<String> getListeners() { public Set<String> getListeners() {
return listeners; return listeners;
Expand Down

0 comments on commit 65398df

Please sign in to comment.