All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ociweb.iot.grove.simple_analog.SimpleAnalogTransducer Maven / Gradle / Ivy

package com.ociweb.iot.grove.simple_analog;

import java.util.ArrayList;


import com.ociweb.iot.maker.FogCommandChannel;
import com.ociweb.iot.maker.IODeviceTransducer;
import com.ociweb.iot.maker.Port;
import com.ociweb.iot.transducer.AnalogListenerTransducer;
import com.ociweb.pronghorn.util.ma.MAvgRollerLong;
import static com.ociweb.pronghorn.util.ma.MAvgRollerLong.*;


/**
 * 
 * @author Ray Lo
 *
 */


//TODO: NEED TO FINISH IMPLEMENTING MOVING/ROLLING STANDEAD DEVIATION LISTENERS
public class SimpleAnalogTransducer implements IODeviceTransducer, AnalogListenerTransducer{
	private FogCommandChannel ch;
	private Port p;

	private final ArrayList aListeners;
	private final ArrayList rsListeners;
	private final ArrayList msListeners;
	private final ArrayList maListeners;

	private final ArrayList maRollers;

	/**
	 * Listeners can only be added upon construction.
	 * @param ch
	 * @param p
	 * @param ls var args of listeners specific to this SimpleAnalog device's AnalogEvent.
	 */
	public SimpleAnalogTransducer(FogCommandChannel ch, Port p, SimpleAnalogListener... ls){
		
		this.p  = p;
		this.ch = ch;

		aListeners = new ArrayList();
		rsListeners = new ArrayList();
		msListeners = new ArrayList();
		maListeners = new ArrayList();

		maRollers = new ArrayList();

		for (SimpleAnalogListener l: ls){
			aListeners.add(l);
		}
		if (ch != null){
			ch.ensurePinWriting();
		}
	}
	
	public SimpleAnalogTransducer(Port p, SimpleAnalogListener... ls){
		this.p  = p;
		this.ch = null;

		aListeners = new ArrayList();
		rsListeners = new ArrayList();
		msListeners = new ArrayList();
		maListeners = new ArrayList();

		maRollers = new ArrayList();

		for (SimpleAnalogListener l: ls){
			aListeners.add(l);
			System.out.println("Added: " + l.toString());
		}
	}
	
	public boolean setValue(int val){
		return ch.setValue(p, val);
	}

	public boolean setValueAndBlock(int val, long durationMillis){
		return ch.setValueAndBlock(p, val, durationMillis);
	}

	public SimpleAnalogTransducer registerListener(RunningStatsListener l){
		if (l instanceof RunningStdDevListener){
			rsListeners.add((RunningStdDevListener) l);
		}
		
		if (l instanceof MovingAverageListener){

		}
		return this;
	}

	public SimpleAnalogTransducer registerListener(MovingStatsListener l, int bucketSize){
		if ( l instanceof MovingAverageListener){
			maListeners.add((MovingAverageListener) l);
			maRollers.add(new MAvgRollerLong(bucketSize));
			
		}
		if (l instanceof MovingStdDevListener){
			msListeners.add((MovingStdDevListener) l);
		}
		return this;
	}
	
	

	@Override
	public void analogEvent(Port port, long time, long durationMillis, int average, int value) {
		//aListener.analogEvent(port, time, durationMillis, average, value);
		System.out.println("HERE");
		if (port.equals(p)){
			System.out.println("THERE");
			for (SimpleAnalogListener sal: aListeners){
				sal.simpleAnalogEvent(port, time, durationMillis, value);
			}
			for (int i = 0; i < maListeners.size(); i ++){
				MAvgRollerLong.roll(maRollers.get(i), value);
				maListeners.get(i).movingAverage(mean(maRollers.get(i)));
			}
			
		
			
			//TODO: HANDLE THE OTHER 3 STATS LISTENER
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy