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

53 lines
1.3 KiB
Java

package de.ctdo.bunti.control;
import de.ctdo.bunti.dao.BuntiDevicesDAO;
import de.ctdo.bunti.model.BuntiDevice;
import de.ctdo.bunti.model.DeviceUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class DeviceValueCache {
@Autowired
private BuntiDevicesDAO devicesDAO;
private Map<Integer, Map<String, Object>> deviceCache;
private void init() {
deviceCache = new HashMap<Integer, Map<String, Object>>();
for(BuntiDevice device: devicesDAO.getAllDevices()) {
deviceCache.put(device.getId(), device.getOptions());
}
}
public void updateData(int deviceId, Map<String, Object> options) {
if(deviceCache == null) init();
Map<String, Object> cached = deviceCache.get(deviceId);
if(cached == null ) {
cached = new HashMap<String, Object>();
deviceCache.put(deviceId, cached);
}
cached.putAll(options);
}
public DeviceUpdate getData(int deviceId) {
if(deviceCache == null) init();
Map<String, Object> cached = deviceCache.get(deviceId);
DeviceUpdate du = new DeviceUpdate();
du.setDeviceId(deviceId);
du.setOptions(cached);
return du;
}
}