BookListener.java

Java for Small Spaces
Java on a chip
by Chris Carpenter
Listing 1. BookListener.java.


/**
    BookListener monitors the 1-Wire Bus
    When an iButton is detected or removed
    I change the state of switch controlling an LED
    And notify an observer that an insertion or removal
    Event has occurred.
**/

package bookshelf.client;

import java.util.*;
import com.dalsemi.onewire.OneWireException;
import com.dalsemi.onewire.adapter.DSPortAdapter;
import com.dalsemi.onewire.adapter.OneWireIOException;
import com.dalsemi.onewire.utils.OneWireMonitorEventListener;
import com.dalsemi.onewire.utils.OneWireMonitor;
import com.dalsemi.onewire.utils.OneWireMonitorEvent;
import com.dalsemi.onewire.container.SwitchContainer;
import com.dalsemi.onewire.container.OneWireContainer;
import com.dalsemi.onewire.container.OneWireContainer14;
import com.dalsemi.onewire.container.PagedMemoryBank;

public class BookListener
    implements OneWireMonitorEventListener {
    protected OneWireMonitor owm;
    protected SwitchContainer led = null;
    protected BookObserver observer;
public BookListener(DSPortAdapter adapter, BookObserver observer)
    throws OneWireException, OneWireIOException, TooManyListeners-Exception {

    this.observer = observer;
    OneWireContainer owc = adapter.getFirstDeviceContainer();

    while ((owc != null) && (led == null)) {
        if (owc.getName().equals("DS2405")) {
            led = (SwitchContainer) owc;
        } else {
            owc = adapter.getNextDeviceContainer();
        }
    }
    // create a network monitor
    owm = new OneWireMonitor(adapter);
    // add this to the event listers
    owm.addEventListener(this);

    owm.start();
}
// Flash the LED
protected void flashLED() throws OneWireException {
    byte[] state = led.readDevice();
    boolean currentState = led.getLatchState(0, state);
    led.setLatchState(0, !currentState, led.hasSmartOn(), state);
    led.writeDevice(state);
}

protected void killWatch() {
    owm.killMonitor();
}
public void oneWireArrival(
    OneWireMonitorEvent owme) {
    System.out.println("Arrival Event: " + owme.getDeviceContainer(). getName());

    try {
        if (owme.getDeviceContainer().getName().equalsIgnoreCase("ds1971")) {
            flashLED();
    observer.newBook(readData(owme.getDeviceContainer()));
    System.out.println("Inserted book: " + readData(owme.getDeviceContainer()));
        }
    } catch (OneWireException owe) {
        System.out.println("Caught " + owe);
    }
}
public void oneWireDeparture(
    com.dalsemi.onewire.utils.OneWireMonitorEvent owme) {
    System.out.println("Removal Event: " + owme.getDeviceContainer(). getName());
    try {
        if (owme.getDeviceContainer().getName().equalsIgnoreCase("ds1971"))
            flashLED();
    } catch (OneWireException owe) {
        System.out.println("Caught " + owe);
    }
}
protected String readData(OneWireContainer owc) {
    Enumeration banks = ((OneWireContainer14) owc).getMemoryBanks();

    PagedMemoryBank mb = null;
    while (banks.hasMoreElements()) { // Get the paged memory bank
        Object bank;
        bank = banks.nextElement();
        if (bank.getClass().getName().endsWith("MemoryBankEE")) {
            mb = (PagedMemoryBank) bank;
            break;
        }
    }

    byte[] data = new byte[mb.getMaxPacketDataLength()];
    try {
        if (mb.readPagePacket(0, false, data, 0) > 0){
            StringBuffer sb = new StringBuffer();

            int i = 0;
            while((data[i] != 0) && (i < data.length))
    sb.append((char)data[i++]);
            return sb.toString();
        }
        else
            return "";
    } catch (OneWireException owe) {
        System.out.println("Exception: " + owe);
        return "error";
    }
}
}

About the Author

Chris Carpenter is a software engineer and architect at RoleModel Software, Inc. in Holly Springs, NC. He can be contacted at [email protected].