View Javadoc

1   /*
2   Copyright (C) 2007 Dirk Huenniger
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8   
9   This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  
19  package org.indi.examples;
20  
21  import java.io.IOException;
22  
23  import org.indi.clientmessages.GetProperties;
24  import org.indi.objects.Permission;
25  import org.indi.objects.State;
26  import org.indi.objects.Switch;
27  import org.indi.objects.SwitchRule;
28  import org.indi.objects.SwitchVector;
29  import org.indi.server.BasicDevice;
30  import org.indi.server.IndiServer;
31  
32  /**
33   * A very simple device that can just be connected and discoonected
34   * 
35   * @author Dirk Hünniger
36   * 
37   */
38  public class SimpleDevice extends BasicDevice {
39      /**
40       * The Vector hosting the connect /disconnect switch
41       */
42      private final SwitchVector powerswitch;
43      /**
44       * the name of the connection vector
45       */
46      private final String vectorname = "CONNECTION";
47      /**
48       * the switch to connect/disconnect the device
49       */
50      private Switch connectswitch = null;
51      /**
52       * the name of the device
53       */
54      private final String name = "Simple Device";
55  
56      /**
57       * class constructor
58       * 
59       * @param server
60       *                the server to host the device
61       */
62      public SimpleDevice(IndiServer server) {
63  	super(server);
64  	this.powerswitch = new SwitchVector(this.name, this.vectorname,
65  		"Connection", "Main Control", State.Idle, Permission.ReadWrite,
66  		SwitchRule.OneOfMany, 0);
67  	this.connectswitch = new Switch("CONNECT", "Connect", Switch.State.Off);
68  	this.powerswitch.add(this.connectswitch);
69  	this.powerswitch.add(new Switch("DISCONNECT", "Disconnect",
70  		Switch.State.On));
71      }
72  
73      @Override
74      public void onGetProperties(GetProperties o) {
75  	def(this.powerswitch);
76      }
77  
78      @Override
79      public void onNew(SwitchVector vector) {
80  	if ((this.vectorname.equals(vector.getName()))
81  		&& (this.name.equals(vector.getDevice()))) {
82  	    this.powerswitch.update(vector);
83  	}
84  	switch (this.connectswitch.getState()) {
85  	case On:
86  	    this.powerswitch.setState(State.Ok);
87  	    set(this.powerswitch, "Connection to Simple Device is successful.");
88  	    break;
89  	case Off:
90  	    this.powerswitch.setState(State.Idle);
91  	    set(this.powerswitch, "Simple Device has been disconneced.");
92  	    break;
93  	}
94      }
95  
96      /**
97       * @param args
98       * @throws IOException
99       */
100     public static void main(String[] args) throws IOException {
101 	IndiServer s = new IndiServer();
102 	new SimpleDevice(s);
103 	while (true) {
104 	    s.reactor.handleEvents(10);
105 	}
106     }
107 
108 }