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> deviceCache; private void init() { deviceCache = new HashMap>(); for(BuntiDevice device: devicesDAO.getAllDevices()) { deviceCache.put(device.getId(), device.getOptions()); } } public void updateData(int deviceId, Map options) { if(deviceCache == null) init(); Map cached = deviceCache.get(deviceId); if(cached == null ) { cached = new HashMap(); deviceCache.put(deviceId, cached); } cached.putAll(options); } public DeviceUpdate getData(int deviceId) { if(deviceCache == null) init(); Map cached = deviceCache.get(deviceId); DeviceUpdate du = new DeviceUpdate(); du.setDeviceId(deviceId); du.setOptions(cached); return du; } }