I am trying to implement hardware into the application and wonder what is the best practice for this? I want to connect the event of a card to specific mediators. The problem is phidgets uses an implementation with eventlisteners that are global, thus i need to access into these and try to find where i am in the application at the moment and thus accessing different methods as follows. Also i feel i am getting more and more away from puremvc implementation.
I have phidget hardware and its implementation in my looks like this:
- i have put the connection to phidgets as a helper
- phidget is created in the applicationfacade
- when tag is found it traces the current index in my viewstack
- a phidget manager method (org.pmc.views.managers) recieves the current viewstack and then runs a method (getting data or other behavior)
package org.pmvc.helpers
{
import com.phidgets.*;
import com.phidgets.events.*;
import mx.core.Application;
import org.pmvc.view.MyViewMediator;
public class PhidgetHelper {
private var phid:PhidgetRFID;
public function PhidgetHelper(){
phid = new PhidgetRFID();
phid.addEventListener(PhidgetEvent.CONNECT, onConnect);
phid.addEventListener(PhidgetEvent.DISCONNECT, onDisconnect);
phid.addEventListener(PhidgetEvent.DETACH, onDetach);
phid.addEventListener(PhidgetEvent.ATTACH, onAttach);
phid.addEventListener(PhidgetErrorEvent.ERROR, onError);
phid.addEventListener(PhidgetDataEvent.TAG, onTag);
phid.addEventListener(PhidgetDataEvent.TAG_LOST, onTagLoss);
phid.addEventListener(PhidgetDataEvent.OUTPUT_CHANGE, onOutputChange);
phid.open("localhost", 5001);
}
private function onError(evt:PhidgetErrorEvent):void {
trace(evt);
trace("version = " + phid.Version);
}
private function onAttach(evt:PhidgetEvent):void{
trace(evt);
phid.Antenna = true;
}
private function onDetach(evt:PhidgetEvent):void{
trace(evt);
}
private function onConnect(evt:PhidgetEvent):void{
trace(evt);
}
private function onDisconnect(evt:PhidgetEvent):void{
trace(evt);
}
private function onTag(evt:PhidgetDataEvent):void{
trace(evt);
phid.LED = true;
var iStack:Number = Application.application.vwStack.selectedIndex.toString();
trace("current state: " + iStack);
switch(iStack)
{
case(0):
break;
case(1):
trace("phidget active on state 1");
//phidgetmanager implementation 1
break;
case(2):
trace("phidget active on state 2");
//phidgetmanager implementation 2
break;
}
}
private function onTagLoss(evt:PhidgetDataEvent):void{
trace(evt);
phid.LED = false;
}
private function onOutputChange(evt:PhidgetDataEvent):void{
trace(evt);
}
}
}
How can i build this using best practice? (some events from phidgets need to be global and others (ontag, ontagloss) need to be specific for each mediator.