Package com.grt192.controller.hauntedhouse

Source Code of com.grt192.controller.hauntedhouse.NetworkController

package com.grt192.controller.hauntedhouse;

import com.grt192.core.EventController;
import com.grt192.core.StepController;
import com.grt192.networking.GRTSingleClientServer;
import com.grt192.networking.SocketEvent;
import com.grt192.networking.SocketListener;

/**
* Network interface for Haunted House control, by console
* @author data, ajc
*/
public class NetworkController extends EventController implements SocketListener {
    public static final int PORT = 1920;
    private final int NUM_SOLENOIDS = 8;

    private HauntedHouseController[] controls;
    private HHLEDController hlc;
    GRTSingleClientServer srvr;

    //TODO perhaps the mapping is not linear as suggested here
    final int[] solToSwitch = {1,2,3,4,5,6,7,8};
    //{7,4,5,6,8,12,10,11}; //what was given to us

    /**
     * Delays array, maps Controllers to their delays
     * Format; {minExtend, maxExtend, minRetract, maxRetract}
     * The Controllers will not randomize either the Open or Closed
     * delays if the max values are negative.
     *
     **/
    final int[][] delays =
        //floor 1
        {{1000, -15000, 1000, -15000},//axes
        {5000, 15000, 5000, 15000},//skull
        {10000, 20000, 5000, 15000},//chair
        {4000, -15000, 4000, -15000},//rat
        //floor 0
        {1000, 15000, 750, -10000},//caudron
        {5000, 15000, 1000, -5000},//dog
        {3000, 10000, 5000, 15000},//skeleton
        {5000, 15000, 2000, -15000}};//clock

    //@Deprecated-- see above
//    final boolean[] randomDelay = {true, true, true, true, true, true, true};

    public NetworkController(HauntedHouseController[] hhcs) {
        controls = hhcs;
        //initLED needs to precede initControl with the current model,
        // if HHLEDController is changed to itself add all listeners,
        //this needs to change
        //initLED();
        initControl();
//        initSocket();

    }

    public NetworkController() {
//        controls = hhcs;
        //initLED needs to precede initControl with the current model,
        // if HHLEDController is changed to itself add all listeners,
        //this needs to change
        //initLED();
        initControl();
//        initSocket();

    }

    private void initControl() {
        controls = new HauntedHouseController[NUM_SOLENOIDS];

        for (int i = 0; i < controls.length; i++) {
            controls[i] = new HauntedHouseController
                    (i+1, solToSwitch[i], delays[i]);
            controls[i].start();
        }
    }

    private void initLED(){
        //this can probably take controls as a perameter, add all as listeners
        hlc = new HHLEDController();
//        hlc = new HHLEDController(controls);
        //if we do this, implement notification for all elements of controls
    }

    private void initSocket(){
        srvr = new GRTSingleClientServer(PORT);
        srvr.start();
        System.out.println("connected");
        srvr.addSocketListener(this);
    }

    public void onConnect(SocketEvent e) {
        System.out.println("Connected!");
    }

    public void onDisconnect(SocketEvent e) {
        System.out.println("Not connected.");
    }

    public void dataRecieved(SocketEvent e) {
        String s = e.getData();
        if (s == null) {
            System.out.println("Null message");
            return;
        }
        String s1 = null;
        String s2 = null;

        System.out.println("Received message:" + s);
        boolean b = s.indexOf(-1) != -1;
        if (b) {
            s1 = s.substring(0, s.indexOf(' '));
            s2 = s.substring(s.indexOf(' ') + 1);
        }

        System.out.println("Part 1:" + s1);
        System.out.println("Part 2:" + s2);
        if (s.equals("auto")) {
            for (int i = 0; i < controls.length; i++) {
                controls[i].setAuto(true);
            }
            srvr.sendData("Auto enabled.");
            return;
        } else if (s.equals("manual")) {
            for (int i = 0; i < controls.length; i++) {
                controls[i].setAuto(false);
            }
            srvr.sendData("Manual enabled.");
        } else if (s.equals("status")) {
            String str = "";
            for (int i = 0; i < controls.length; i++) {
                str += ("Mechanism " + i + ":" + controls[i].getEnabled()) + "!";
            }
            str += "Master:" + controls[0].getMaster() + "!";
            str += "Auto:" + controls[0].getAuto() + "!";
            srvr.sendData(str);
            return;
        } else if (b && s1.equals("start")) {
            if (s2.equals("all")) {
                for (int i = 0; i < controls.length; i++) {
                    controls[i].setMaster(true);
                }
                srvr.sendData("Master is on.");
                return;
            } else {
                int num = Integer.parseInt(s2);
                controls[num].setEnabled(true);
                srvr.sendData("Mechanism " + num + " started.");
                return;
            }
        } else if (b && s1.equals("stop")) {
            if (s2.equals("all")) {
                for (int i = 0; i < controls.length; i++) {
                    controls[i].setMaster(false);

                }
                srvr.sendData("Master is off.");
                return;
            } else {
                int num = Integer.parseInt(s2);
                controls[num].setEnabled(false);
                srvr.sendData("Mechanism " + num + " stopped.");
                return;
            }
        } else {
            srvr.sendData("Unknown command type.");
            return;
        }
    }
}
TOP

Related Classes of com.grt192.controller.hauntedhouse.NetworkController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.