bunti/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java

99 lines
2.8 KiB
Java
Raw Normal View History

package de.ctdo.bunti.control;
2012-03-07 18:19:21 +00:00
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
2012-03-20 17:01:54 +00:00
import de.ctdo.bunti.dao.RoomsDAO;
import de.ctdo.bunti.model.*;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
import de.ctdo.bunti.dao.BuntiDevicesDAO;
@Component
public class BuntiControllerImpl implements BuntiController, ApplicationEventPublisherAware {
2012-03-08 00:22:31 +00:00
private static final Logger LOGGER = LoggerFactory.getLogger(BuntiControllerImpl.class);
private ApplicationEventPublisher applicationEventPublisher = null;
private BuntiDevicesDAO devicesDAO;
2012-03-20 17:01:54 +00:00
private RoomsDAO roomsDAO;
private Map<Integer, Map<String, Object>> deviceCache = new HashMap<Integer, Map<String, Object>>();
2012-03-20 17:01:54 +00:00
@Autowired
2012-03-04 09:50:50 +00:00
public final void setDevicesDAO(BuntiDevicesDAO devicesDAO) {
this.devicesDAO = devicesDAO;
}
2012-03-20 17:01:54 +00:00
@Autowired
public final void setRoomsDAO(RoomsDAO roomsDAO) {
this.roomsDAO = roomsDAO;
}
2012-03-07 18:19:21 +00:00
@Override
public final void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.applicationEventPublisher = publisher;
2012-03-07 18:19:21 +00:00
}
2012-03-07 18:19:21 +00:00
@Override
public final boolean updateDeviceData(int deviceId, Map<String, Object> options) {
BuntiDevice device = devicesDAO.getDeviceById(deviceId);
if (device != null) {
2012-03-08 00:22:31 +00:00
LOGGER.debug("publishEvent in BuntiController");
Map<String, Object> cached = deviceCache.get(deviceId);
if(cached == null ) {
cached = new HashMap<String, Object>();
deviceCache.put(deviceId, cached);
}
cached.putAll(options);
device.setValuesFromOptions(options) ;
this.applicationEventPublisher.publishEvent(new DeviceChangedEvent(this, device, options));
return true;
}
return false;
}
@Override
public DeviceUpdate getDeviceValues(int deviceId) {
Map<String, Object> cached = deviceCache.get(deviceId);
DeviceUpdate du = new DeviceUpdate();
du.setDeviceId(deviceId);
du.setOptions(cached);
return du;
}
2012-03-20 23:44:41 +00:00
@Override
public Collection<Room> getAllRooms() {
return roomsDAO.getRooms();
}
@Override
public Room getRoomById(int roomId) {
return roomsDAO.getRoom(roomId);
}
2012-03-07 18:19:21 +00:00
@Override
public Collection<BuntiDevice> getAllDevices() {
return devicesDAO.getAllDevices();
2012-03-07 18:19:21 +00:00
}
@Override
public BuntiDevice getDeviceById(int deviceId) {
return devicesDAO.getDeviceById(deviceId);
}
}