import java.io.*; import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; import sophware.home.util.Timer; import sophware.home.lang.*; import sophware.home.util.*; import sophware.home.logic.*; import sophware.home.ui.*; import sophware.home.io.*; import sophware.home.net.*; import sophware.home.x10.*; import sophware.home.insteon.*; public class HomeExample extends JFrame implements ActionListener, WindowListener { static final long serialVersionUID = 1L; static Home m_home; HomeCommandFrame m_commands = null; public static void main(String args[]) { DEBUG.setLevel(args); // looks for debug= where level 0-9 try { /** * uncomment this if you don't want error output * going to stderr * * Home.m_errorStream = new PrintStream(new FileOutputStream("error.log",true)); */ new HomeExample(); } catch (Exception e) { e.printStackTrace(System.err); } } public HomeExample() throws Exception { try { /** * uncomment this line if you want to use the network * to connect to your PLC on another computer. * your may need to update the hostname and port * to match where the network server is running * * see NetworkServerExample for server code * * m_home = new NetworkHome("hostname",8001); */ /** * if your computer is directly connect to Insteon PLC * use this line. May need to update COM port. */ m_home = new Home(new InsteonPLCTransceiver(new SerialAccess("COM1"))); m_home.start(); /** * Define your X10 devices */ ReadOnlyDevice frontMotion = new ReadOnlyDevice(new X10Address("M1"),"front motion"); ReadOnlyDevice rearMotion = new ReadOnlyDevice(new X10Address("M2"),"rear motion"); LampDevice frontFlood = new LampDevice(new X10Address("F1"),"front flood"); LampDevice rearFlood = new LampDevice(new X10Address("F2"),"rear flood"); // add devices to the home m_home.add(frontMotion); m_home.add(rearMotion); m_home.add(frontFlood); m_home.add(rearFlood); /** * create a macro to trun on both floods for two * minutes anytime motion is detected or reset the * timer if lights are already on */ // variable to track current status of lights Variable floodStatus = new Variable("floods","OFF"); // motion will be ON if either device is ON BinaryGroup motion = new BinaryGroup("outside motion",BinaryGroup.OR); motion.add(frontMotion); motion.add(rearMotion); // setup a group of conditions all of which must be // true (AND) for the group to be true BinaryGroup triggerLightsOn = new BinaryGroup("trigger lights on",BinaryGroup.AND); triggerLightsOn.add(new Condition(motion,BinaryGroup.ON)); triggerLightsOn.add(new Condition(floodStatus,"OFF")); // create a two minute timer Timer floodTimer = new Timer("flood timer",120); // setup a macro to turn on floods, set variable to ON // and start timer Vector steps = new Vector(); steps.add(frontFlood.getAddress().getOnCommand()); steps.add(rearFlood.getAddress().getOnCommand()); steps.add(new VariableAssignment(floodStatus,"ON")); steps.add(floodTimer); Macro turnFloodsOn = new Macro("turn floods on",new Condition(triggerLightsOn,BinaryObject.ON),steps); steps.removeAllElements(); // need to tell what to do if we detect motion and // lights are already on BinaryGroup triggerTimerReset = new BinaryGroup("trigger timer reset", BinaryGroup.AND); triggerTimerReset.add(new Condition(motion,BinaryGroup.ON)); triggerTimerReset.add(new Condition(floodStatus,"ON")); steps.add(floodTimer); Macro resetTimer = new Macro("reset timer",new Condition(triggerTimerReset,BinaryObject.ON),steps); steps.removeAllElements(); // turn floods back off when timer expires steps.add(frontFlood.getAddress().getOffCommand()); steps.add(rearFlood.getAddress().getOffCommand()); steps.add(new VariableAssignment(floodStatus,"OFF")); Macro turnFloodsOf = new Macro("reset timer",new Condition(floodTimer,BinaryObject.OFF),steps); steps.removeAllElements(); } catch (HomeException e) { e.printStackTrace(System.err); System.err.println("failed to initialize my home"); System.exit(1); } setTitle("My Home"); JMenuBar bar = new JMenuBar(); JMenu menu = new JMenu("File"); JMenuItem item = new JMenuItem("Close"); item.addActionListener(this); menu.add(item); bar.add(menu); menu = new JMenu("Tools"); item = new JMenuItem("Home Commands"); item.addActionListener(this); menu.add(item); bar.add(menu); setJMenuBar(bar); add(new JScrollPane(new DeviceTable(m_home))); setDefaultCloseOperation(DISPOSE_ON_CLOSE); addWindowListener(this); pack(); setVisible(true); } public void windowActivated(WindowEvent event) {} public void windowClosed(WindowEvent event) { if (event.getSource() == this) { try { m_home.stop(); } catch (Exception ignore) {}; System.exit(0); } else if (event.getSource() == m_commands) { m_commands = null; } } public void windowClosing(WindowEvent event) {} public void windowDeactivated(WindowEvent event) {} public void windowDeiconified(WindowEvent event) {} public void windowIconified(WindowEvent event) {} public void windowOpened(WindowEvent event) {} public void actionPerformed(ActionEvent event) { if (event.getActionCommand().equals("Close")) { try { m_home.stop(); } catch (Exception ignore) {}; System.exit(0); } else if (event.getActionCommand().equals("Home Commands")) { if (m_commands == null) { m_commands = new HomeCommandFrame(m_home); m_commands.addWindowListener(this); } else { m_commands.toFront(); } } } }