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

113 lines
2.7 KiB
Java
Raw Normal View History

package de.ctdo.bunti.dmx;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import de.ctdo.bunti.DeviceChangedEvent;
import de.ctdo.bunti.dao.BuntiDevicesDAO;
import de.ctdo.bunti.model.*;
@Component
public class DMXMixerImpl implements DMXMixer {
private Logger logger = LoggerFactory.getLogger(getClass());
private final int TICKS_BETWEEN_DMX_SEND = 20;
// private final String ARTNET_DEVICE_ADDRESS = "192.168.0.90";
private int[] dmx512databuffer = new int[DMX.DMX_CHANNELS_MAX+1];
// private int sequenceID = 0;
private long ticksLastBufferFlush = 0;
private BuntiDevicesDAO devicesDAO;
@Autowired
public void setDevicesDAO(BuntiDevicesDAO devicesDAO) {
this.devicesDAO = devicesDAO;
}
private void sendOutDMXBuffer() {
StringBuilder sb = new StringBuilder();
sb.append("DMX Data: ");
byte[] arr = new byte[dmx512databuffer.length];
for (int i = 0; i < dmx512databuffer.length; i++) {
arr[i] = (byte)dmx512databuffer[i];
sb.append(i);
sb.append("=");
sb.append(dmx512databuffer[i]);
sb.append(", ");
}
// sequenceID++;
logger.debug(sb.toString());
}
public void sendOutDMXBufferIfNeeded() {
if(ticksLastBufferFlush + TICKS_BETWEEN_DMX_SEND < System.currentTimeMillis()) {
// mergeDevicesIntoBuffer();
sendOutDMXBuffer();
ticksLastBufferFlush = System.currentTimeMillis();
}
}
private void updateDevice(BuntiDevice device, Map<String, Object> options) {
BuntiDMXDevice dmxDev = (BuntiDMXDevice)device;
boolean retval = dmxDev.setValuesFromOptions(options);
if( retval ) {
dmxDev.mergeDMXData(dmx512databuffer);
sendOutDMXBufferIfNeeded();
} else {
logger.error("setValuesFromOptions failed");
}
}
// private void mergeDevicesIntoBuffer() {
//
// for (BuntiDMXDevice buntiDMXDevice : devicesDAO.getAllDMXDevices()) {
// long lastchanged = buntiDMXDevice.getLastChanged();
// long lastSend = buntiDMXDevice.getLastSendOut();
//
// if (lastchanged >= lastSend) {
// buntiDMXDevice.mergeDMXData(dmx512databuffer);
// logger.debug("merged " + buntiDMXDevice + " into dmx buffer");
// buntiDMXDevice.setSendOutNow();
// }
//
// }
// }
// public void setDMX512Channel(int channel, int value) {
// if(!DMX.checkChannelBoundaries(channel)) return;
// value = DMX.sanitizeDMXValue(value);
//
// dmx512databuffer[channel] = value;
//
// sendOutDMXBufferIfNeeded();
// }
@Override
public void onApplicationEvent(DeviceChangedEvent arg0) {
if( arg0.getDevice() instanceof BuntiDMXDevice) {
updateDevice(arg0.getDevice(), arg0.getOptions());
}
}
}