package nl.nikhef.safaripark.dolly;

import java.io.IOException;

import nl.nikhef.safaripark.ContextCache;
import nl.nikhef.safaripark.extra.Module;
import nl.nikhef.sfp.ddmi.DDMI;
import nl.nikhef.sfp.ddmi.DDMIContext;
import nl.nikhef.sfp.ddmi.DDMIValue;
import nl.nikhef.sfp.ddmi.DataSource;

public class ModuleValueSetter implements ValueSetter {

	private Module _mod;
	private ContextCache _ctxCache;
	
	public ModuleValueSetter(ContextCache cache, Module targetModule) {
		_mod = targetModule;
		_ctxCache = cache;
	}

	@Override
	public void setValue(DDMIValue value, byte[] data) throws IOException {
		value.writeRaw(_ctxCache.getContextFor(_mod.dev, _mod.bay), data);
	}

	@Override
	public void finished() throws IOException {
		DDMIContext ctx = _ctxCache.getContextFor(_mod.dev, _mod.bay);
		DDMI ddmi = ctx.getDDMI();
		for (DataSource ds: ddmi.getSources()) {
			ds.updateChecksums(ctx);
		}
	}

}
