bunti/src/main/java/de/ctdo/bunti/dmx/DMXMixerImpl.java

98 lines
3.0 KiB
Java
Raw Normal View History

package de.ctdo.bunti.dmx;
import de.ctdo.bunti.control.DeviceChangedEvent;
2012-03-04 09:50:50 +00:00
import de.ctdo.bunti.artnet.SimpleArtNetSender;
import de.ctdo.bunti.model.BuntiDMXDevice;
import de.ctdo.bunti.model.BuntiDevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Scheduled;
2012-03-04 09:50:50 +00:00
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class DMXMixerImpl implements DMXMixer, ApplicationListener<DeviceChangedEvent> {
2012-03-07 18:17:43 +00:00
private static final Logger LOGGER = LoggerFactory.getLogger(DMXMixerImpl.class);
2012-03-08 00:22:31 +00:00
private static final int NET_SEND_INTERVAL = 100;
private String artNetDeviceAddress;
2012-03-04 09:50:50 +00:00
private final Map<Integer, Integer> dmxMap = Collections.synchronizedMap(new HashMap<Integer, Integer>());
private SimpleArtNetSender artNetSender;
private boolean hasDataChanged = true;
@Autowired
public final void setArtNetSender(SimpleArtNetSender artNetSender) {
this.artNetSender = artNetSender;
}
public void setArtNetDeviceAddress(String artNetDeviceAddress) {
this.artNetDeviceAddress = artNetDeviceAddress;
}
2012-03-04 09:50:50 +00:00
2012-03-10 20:38:05 +00:00
private void initDMXData() {
2012-03-07 23:04:03 +00:00
for (int i = DMX.DMX_CHANNEL_INDEX_MIN; i <= DMX.DMX_CHANNEL_INDEX_MAX; i++) {
2012-03-08 00:22:31 +00:00
dmxMap.put(i, DMX.DMX_CHANNEL_VALUE_MIN);
2012-03-04 09:50:50 +00:00
}
}
2012-03-08 00:22:31 +00:00
@Scheduled(fixedDelay = NET_SEND_INTERVAL)
2012-03-04 09:50:50 +00:00
public final void sendOutDMXBuffer() {
/*
2012-03-04 09:50:50 +00:00
if (dmxMap.size() == 0) {
initDMXData();
}
if (hasDataChanged) {
LOGGER.debug("sending DMX Data");
artNetSender.sendDMXData(dmxMap, artNetDeviceAddress);
2012-03-04 09:50:50 +00:00
hasDataChanged = false;
}*/
2012-03-04 09:50:50 +00:00
}
2012-03-10 20:38:05 +00:00
@Override
2012-03-07 21:12:18 +00:00
public final boolean updateDevice(BuntiDevice device, Map<String, Object> options) {
2012-03-10 20:38:05 +00:00
if(device == null || options == null || options.size() == 0) {
return false;
}
2012-03-04 09:50:50 +00:00
BuntiDMXDevice dmxDev = (BuntiDMXDevice) device;
//if (dmxDev.setValuesFromOptions(options)) {
2012-03-04 09:50:50 +00:00
dmxMap.putAll(dmxDev.getChannelData());
LOGGER.info("setValuesFromOptions on " + device);
2012-03-07 21:12:18 +00:00
return true;
//}
2012-03-04 09:50:50 +00:00
// LOGGER.info("setValuesFromOptions on " + device + " failed");
// return false;
2012-03-04 09:50:50 +00:00
}
@Override
2012-03-07 21:12:18 +00:00
public final boolean setDMX512Channel(int channel, int value) {
2012-03-04 09:50:50 +00:00
if (!DMX.checkChannelBoundaries(channel)) {
2012-03-07 21:12:18 +00:00
return false;
2012-03-04 09:50:50 +00:00
}
dmxMap.put(channel, DMX.sanitizeDMXValue(value));
hasDataChanged = true;
2012-03-07 21:12:18 +00:00
return true;
2012-03-04 09:50:50 +00:00
}
@Override
public final void onApplicationEvent(DeviceChangedEvent arg0) {
if (arg0.getDevice() instanceof BuntiDMXDevice) {
updateDevice(arg0.getDevice(), arg0.getOptions());
hasDataChanged = true;
// TODO: hier kann man z.B. auch noch direkt einmal die DMX Daten zu verschicken veranlassen
}
}
}