Search in sources :

Example 1 with LocoNetSlot

use of jmri.jmrix.loconet.LocoNetSlot in project JMRI by JMRI.

the class SlotMonDataModel method getValueAt.

@Override
public Object getValueAt(int row, int col) {
    LocoNetSlot s = memo.getSlotManager().slot(slotNum(row));
    String t;
    if (s == null) {
        log.error("slot pointer was null for slot row: " + row + " col: " + col);
        return null;
    }
    switch(col) {
        case // slot number
        SLOTCOLUMN:
            return Integer.valueOf(slotNum(row));
        case //
        ESTOPCOLUMN:
            // will be name of button in default GUI
            return "E Stop";
        case //
        ADDRCOLUMN:
            return Integer.valueOf(s.locoAddr());
        case //
        SPDCOLUMN:
            switch(s.consistStatus()) {
                case LnConstants.CONSIST_TOP:
                case LnConstants.CONSIST_NO:
                    if (s.speed() == 1) {
                        t = "(estop) 1";
                    } else {
                        t = "          " + s.speed();
                    }
                    // 9 comes from (estop)
                    return t.substring(t.length() - 9, t.length());
                case LnConstants.CONSIST_MID:
                case LnConstants.CONSIST_SUB:
                    return "(consist)";
                default:
                    return "<error>";
            }
        case //
        TYPECOLUMN:
            switch(s.decoderType()) {
                case LnConstants.DEC_MODE_128A:
                    return "128 step adv";
                case LnConstants.DEC_MODE_28A:
                    return " 28 step adv";
                case LnConstants.DEC_MODE_128:
                    return "128 step";
                case LnConstants.DEC_MODE_14:
                    return " 14 step";
                case LnConstants.DEC_MODE_28TRI:
                    return " 28 step trinary";
                case LnConstants.DEC_MODE_28:
                    return " 28 step";
                default:
                    return "<unknown>";
            }
        case //
        STATCOLUMN:
            switch(s.slotStatus()) {
                case LnConstants.LOCO_IN_USE:
                    return "In Use";
                case LnConstants.LOCO_IDLE:
                    return "Idle";
                case LnConstants.LOCO_COMMON:
                    return "Common";
                case LnConstants.LOCO_FREE:
                    return "Free";
                default:
                    return "<error>";
            }
        case //
        CONSCOLUMN:
            switch(s.consistStatus()) {
                case LnConstants.CONSIST_MID:
                    t = "mid(" + s.speed() + ")";
                    return t;
                case LnConstants.CONSIST_TOP:
                    return "top";
                case LnConstants.CONSIST_SUB:
                    t = "sub(" + s.speed() + ")";
                    return t;
                case LnConstants.CONSIST_NO:
                    return "none";
                default:
                    return "<error>";
            }
        case //
        DISPCOLUMN:
            // will be name of button in default GUI
            return "Free";
        case //
        DIRCOLUMN:
            return (s.isForward() ? "F" : "R");
        case //
        F0COLUMN:
            return (s.isF0() ? True : False);
        case //
        F1COLUMN:
            return (s.isF1() ? True : False);
        case //
        F2COLUMN:
            return (s.isF2() ? True : False);
        case //
        F3COLUMN:
            return (s.isF3() ? True : False);
        case //
        F4COLUMN:
            return (s.isF4() ? True : False);
        case //
        F5COLUMN:
            return (s.isF5() ? True : False);
        case //
        F6COLUMN:
            return (s.isF6() ? True : False);
        case //
        F7COLUMN:
            return (s.isF7() ? True : False);
        case //
        F8COLUMN:
            return (s.isF8() ? True : False);
        case THROTCOLUMN:
            int upper = (s.id() >> 7) & 0x7F;
            int lower = s.id() & 0x7F;
            return StringUtil.twoHexFromInt(upper) + " " + StringUtil.twoHexFromInt(lower);
        default:
            log.error("internal state inconsistent with table requst for " + row + " " + col);
            return null;
    }
}
Also used : LocoNetSlot(jmri.jmrix.loconet.LocoNetSlot)

Example 2 with LocoNetSlot

use of jmri.jmrix.loconet.LocoNetSlot in project JMRI by JMRI.

the class SlotMonDataModel method setValueAt.

@Override
public void setValueAt(Object value, int row, int col) {
    int status = 0;
    if (col == ESTOPCOLUMN) {
        log.debug("Start estop in slot " + row);
        // check for in use
        LocoNetSlot s = memo.getSlotManager().slot(slotNum(row));
        if (s == null) {
            log.error("slot pointer was null for slot row: " + row + " col: " + col);
            return;
        }
        if ((s.consistStatus() == LnConstants.CONSIST_SUB) || (s.consistStatus() == LnConstants.CONSIST_MID)) {
            Object[] options = { "OK", "Cancel" };
            int result = JOptionPane.showOptionDialog(null, "E-Stopping a consist MID or SUB will mess up the consist.\n\nAre you sure you want to do that?", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[1]);
            if (result == 1) {
                return;
            }
        }
        LocoNetMessage msg = new LocoNetMessage(4);
        msg.setOpCode(LnConstants.OPC_LOCO_SPD);
        msg.setElement(1, s.getSlot());
        // 1 here is estop
        msg.setElement(2, 1);
        memo.getLnTrafficController().sendLocoNetMessage(msg);
        fireTableRowsUpdated(row, row);
    } else if ((col == F0COLUMN) || (col == F1COLUMN) || (col == F2COLUMN) || (col == F3COLUMN) || (col == F4COLUMN)) {
        log.debug("F0-F4 change requested " + row);
        LocoNetSlot s = memo.getSlotManager().slot(slotNum(row));
        if (s == null) {
            log.error("slot pointer was null for slot row: " + row + " col: " + col);
            return;
        }
        boolean tempF0 = (col == F0COLUMN) ? !s.isF0() : s.isF0();
        boolean tempF1 = (col == F1COLUMN) ? !s.isF1() : s.isF1();
        boolean tempF2 = (col == F2COLUMN) ? !s.isF2() : s.isF2();
        boolean tempF3 = (col == F3COLUMN) ? !s.isF3() : s.isF3();
        boolean tempF4 = (col == F4COLUMN) ? !s.isF4() : s.isF4();
        int new_dirf = ((s.isForward() ? 0 : LnConstants.DIRF_DIR) | (tempF0 ? LnConstants.DIRF_F0 : 0) | (tempF1 ? LnConstants.DIRF_F1 : 0) | (tempF2 ? LnConstants.DIRF_F2 : 0) | (tempF3 ? LnConstants.DIRF_F3 : 0) | (tempF4 ? LnConstants.DIRF_F4 : 0));
        // set status to 'In Use' if other
        status = s.slotStatus();
        if (status != LnConstants.LOCO_IN_USE) {
            memo.getLnTrafficController().sendLocoNetMessage(s.writeStatus(LnConstants.LOCO_IN_USE));
        }
        LocoNetMessage msg = new LocoNetMessage(4);
        msg.setOpCode(LnConstants.OPC_LOCO_DIRF);
        msg.setElement(1, s.getSlot());
        // 1 here is estop
        msg.setElement(2, new_dirf);
        memo.getLnTrafficController().sendLocoNetMessage(msg);
        // Delay here allows command station time to xmit on the rails.
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            log.error(null, ex);
        }
        // reset status to original value if not previously 'in use'
        if (status != LnConstants.LOCO_IN_USE) {
            memo.getLnTrafficController().sendLocoNetMessage(s.writeStatus(status));
        }
        fireTableRowsUpdated(row, row);
    } else if ((col == F5COLUMN) || (col == F6COLUMN) || (col == F7COLUMN) || (col == F8COLUMN)) {
        log.debug("F5-F8 change requested " + row);
        LocoNetSlot s = memo.getSlotManager().slot(slotNum(row));
        if (s == null) {
            log.error("slot pointer was null for slot row: " + row + " col: " + col);
            return;
        }
        boolean tempF5 = (col == F5COLUMN) ? !s.isF5() : s.isF5();
        boolean tempF6 = (col == F6COLUMN) ? !s.isF6() : s.isF6();
        boolean tempF7 = (col == F7COLUMN) ? !s.isF7() : s.isF7();
        boolean tempF8 = (col == F8COLUMN) ? !s.isF8() : s.isF8();
        int new_snd = ((tempF8 ? LnConstants.SND_F8 : 0) | (tempF7 ? LnConstants.SND_F7 : 0) | (tempF6 ? LnConstants.SND_F6 : 0) | (tempF5 ? LnConstants.SND_F5 : 0));
        // set status to 'In Use' if other
        status = s.slotStatus();
        if (status != LnConstants.LOCO_IN_USE) {
            memo.getLnTrafficController().sendLocoNetMessage(s.writeStatus(LnConstants.LOCO_IN_USE));
        }
        LocoNetMessage msg = new LocoNetMessage(4);
        msg.setOpCode(LnConstants.OPC_LOCO_SND);
        msg.setElement(1, s.getSlot());
        // 1 here is estop
        msg.setElement(2, new_snd);
        memo.getLnTrafficController().sendLocoNetMessage(msg);
        // Delay here allows command station time to xmit on the rails.
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            log.error(null, ex);
        }
        // reset status to original value if not previously 'in use'
        if (status != LnConstants.LOCO_IN_USE) {
            memo.getLnTrafficController().sendLocoNetMessage(s.writeStatus(status));
        }
        fireTableRowsUpdated(row, row);
    } else if (col == DISPCOLUMN) {
        log.debug("Start freeing slot " + row);
        // check for in use
        LocoNetSlot s = memo.getSlotManager().slot(slotNum(row));
        if (s == null) {
            log.error("slot pointer was null for slot row: " + row + " col: " + col);
            return;
        }
        if (s.slotStatus() != LnConstants.LOCO_FREE) {
            if (s.consistStatus() != LnConstants.CONSIST_NO) {
                // Freeing a member takes it out of the consist
                // entirely (i.e., while the slot is LOCO_FREE, it
                // still reads the former consist information, but
                // the next time that loco is selected, it comes
                // back as CONSIST_NO).  Freeing the CONSIST_TOP
                // will kill the entire consist.
                Object[] options = { "OK", "Cancel" };
                int result = JOptionPane.showOptionDialog(null, "Freeing a consist member will destroy the consist.\n\nAre you sure you want to do that?", "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[1]);
                if (result == 1) {
                    return;
                }
            }
            // send status to free
            memo.getLnTrafficController().sendLocoNetMessage(s.writeStatus(LnConstants.LOCO_FREE));
        } else {
            log.debug("Slot not in use");
        }
        fireTableRowsUpdated(row, row);
    }
}
Also used : LocoNetMessage(jmri.jmrix.loconet.LocoNetMessage) LocoNetSlot(jmri.jmrix.loconet.LocoNetSlot)

Example 3 with LocoNetSlot

use of jmri.jmrix.loconet.LocoNetSlot in project JMRI by JMRI.

the class SlotMonDataModel method slotNum.

/**
     * Returns slot number for a specific row.
     * <P>
     * This should probably use a local cache instead of counting/searching each
     * time.
     *
     * @param row Row number in the displayed table
     */
protected int slotNum(int row) {
    int slotNum;
    // need to find a used slot to have the 0th one!
    int n = -1;
    int nMin = 1;
    int nMax = 120;
    if (_systemSlots) {
        nMin = 0;
        nMax = 128;
    }
    for (slotNum = nMin; slotNum < nMax; slotNum++) {
        LocoNetSlot s = memo.getSlotManager().slot(slotNum);
        if (_allSlots || s.slotStatus() != LnConstants.LOCO_FREE || slotNum == 0 || slotNum >= 120) {
            // always show system slots if requested
            n++;
        }
        if (n == row) {
            break;
        }
    }
    return slotNum;
}
Also used : LocoNetSlot(jmri.jmrix.loconet.LocoNetSlot)

Example 4 with LocoNetSlot

use of jmri.jmrix.loconet.LocoNetSlot in project JMRI by JMRI.

the class SlotMonDataModel method estopAll.

/**
     * This is a convenience method that makes it easier for classes using this
     * model to set all in-use slots to emergency stop
     */
public void estopAll() {
    for (int slotNum = 0; slotNum < 120; slotNum++) {
        LocoNetSlot s = memo.getSlotManager().slot(slotNum);
        if (s.slotStatus() != LnConstants.LOCO_FREE && (s.consistStatus() == LnConstants.CONSIST_NO || s.consistStatus() == LnConstants.CONSIST_TOP) && s.speed() != 1) {
            // send message to estop this loco
            LocoNetMessage msg = new LocoNetMessage(4);
            msg.setOpCode(LnConstants.OPC_LOCO_SPD);
            msg.setElement(1, s.getSlot());
            // emergency stop
            msg.setElement(2, 1);
            memo.getLnTrafficController().sendLocoNetMessage(msg);
        }
    }
}
Also used : LocoNetMessage(jmri.jmrix.loconet.LocoNetMessage) LocoNetSlot(jmri.jmrix.loconet.LocoNetSlot)

Example 5 with LocoNetSlot

use of jmri.jmrix.loconet.LocoNetSlot in project JMRI by JMRI.

the class SlotMonDataModel method clearAllSlots.

//Added by Jeffrey Machacek, date: 2013 
//changed 8/22/2013
public void clearAllSlots() {
    int count = getRowCount();
    for (int row = 0; row < (count - 1); row++) {
        LocoNetSlot s = memo.getSlotManager().slot(slotNum(row));
        if ((s.slotStatus() != LnConstants.LOCO_IN_USE) && (s.consistStatus() == LnConstants.CONSIST_NO)) {
            log.debug("Freeing {} from slot {}, old status: {}", s.locoAddr(), s.getSlot(), s.slotStatus());
            memo.getLnTrafficController().sendLocoNetMessage(s.writeStatus(LnConstants.LOCO_FREE));
            fireTableRowsUpdated(row, row);
        }
        count = getRowCount();
    }
}
Also used : LocoNetSlot(jmri.jmrix.loconet.LocoNetSlot)

Aggregations

LocoNetSlot (jmri.jmrix.loconet.LocoNetSlot)6 LocoNetMessage (jmri.jmrix.loconet.LocoNetMessage)2