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

org.robokind.client.basic.RkCameraConnector Maven / Gradle / Ivy

The newest version!
package org.robokind.client.basic;

import java.net.URISyntaxException;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Session;
import org.robokind.api.messaging.services.ServiceCommand;
import org.robokind.api.messaging.services.ServiceError;
import org.robokind.impl.messaging.services.PortableServiceCommand;
import org.jflux.api.core.util.EmptyAdapter;
import org.jflux.api.messaging.rk.MessageAsyncReceiver;
import org.jflux.api.messaging.rk.MessageSender;
import org.robokind.api.vision.ImageEvent;
import org.robokind.api.vision.config.CameraServiceConfig;
import org.robokind.api.vision.messaging.RemoteImageServiceClient;
import org.robokind.client.basic.ConnectionContext.RkServiceConnector;
import org.robokind.impl.messaging.ServiceErrorRecord;
import org.robokind.impl.vision.ImageRecord;

/**
 *
 * @author Jason G. Pallack 
 */
final class RkCameraConnector extends RkServiceConnector{
    final static String CMD_SENDER = "cmCommandSender";
    final static String CONFIG_SENDER = "cmConfigSender";
    final static String ERROR_RECEIVER = "cmErrorReceiver";
    final static String EVENT_RECEIVER = "cmEventReceiver";
    
    static RkCameraConnector theRkCameraConnector;
    
    private String myCommandDest = "camera0Command";
    private String myConfigDest = "camera0Command"; //Same as command
    private String myErrorDest = "camera0Error";
    private String myEventDest = "camera0Event";
    
    static synchronized RkCameraConnector getConnector(){
        if(theRkCameraConnector == null){
            theRkCameraConnector = new RkCameraConnector();
        }
        return theRkCameraConnector;
    }
    
    @Override
    protected synchronized void addConnection(Session session) 
            throws JMSException, URISyntaxException{
        if(myConnectionContext == null || myConnectionsFlag){
            return;
        }
        
        readCameraId();
        
        Destination cmdDest = ConnectionContext.getQueue(myCommandDest);
        Destination confDest = ConnectionContext.getQueue(myConfigDest);
        Destination errDest = ConnectionContext.getTopic(myErrorDest);
        Destination evtDest = ConnectionContext.getTopic(myEventDest);
        
        myConnectionContext.addSender(CMD_SENDER, session, cmdDest, 
                new EmptyAdapter());
        myConnectionContext.addSender(CONFIG_SENDER, session, confDest, 
                new EmptyAdapter());
        myConnectionContext.addAsyncReceiver(ERROR_RECEIVER, session, errDest,
                ServiceErrorRecord.class, ServiceErrorRecord.SCHEMA$,
                new EmptyAdapter());
        myConnectionContext.addAsyncReceiver(EVENT_RECEIVER, session, evtDest,
                ImageRecord.class, ImageRecord.SCHEMA$, new EmptyAdapter());
        
        myConnectionsFlag = true;
    }
    
    synchronized RemoteImageServiceClient buildRemoteClient(){
        if(myConnectionContext == null || !myConnectionsFlag){
            return null;
        }
        MessageSender cmdSender = 
                myConnectionContext.getSender(CMD_SENDER);
        MessageSender confSender = 
                myConnectionContext.getSender(CONFIG_SENDER);
        MessageAsyncReceiver errReceiver = 
                myConnectionContext.getAsyncReceiver(ERROR_RECEIVER);
        MessageAsyncReceiver evtReceiver = 
                myConnectionContext.getAsyncReceiver(EVENT_RECEIVER);
        
        RemoteImageServiceClient client = 
                new RemoteImageServiceClient(CameraServiceConfig.class, 
                "imageServiceId", "remoteImageServiceId", cmdSender,
                confSender, errReceiver, new PortableServiceCommand.Factory(),
                evtReceiver);
        
        return client;
    }
    
    private synchronized void readCameraId() {
        String cameraId = UserSettings.getCameraId();
        
        if(cameraId.equals("0")) {
            myCommandDest = myCommandDest.replace("1", cameraId);
            myConfigDest = myConfigDest.replace("1", cameraId);
            myErrorDest = myErrorDest.replace("1", cameraId);
            myEventDest = myEventDest.replace("1", cameraId);
        } else if(cameraId.equals("1")) {
            myCommandDest = myCommandDest.replace("0", cameraId);
            myConfigDest = myConfigDest.replace("0", cameraId);
            myErrorDest = myErrorDest.replace("0", cameraId);
            myEventDest = myEventDest.replace("0", cameraId);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy