View Javadoc

1   package org.indi.examples;
2   
3   import org.indi.clientmessages.GetProperties;
4   import org.indi.objects.Light;
5   import org.indi.objects.LightVector;
6   import org.indi.objects.Permission;
7   import org.indi.objects.State;
8   import org.indi.objects.Switch;
9   import org.indi.objects.SwitchRule;
10  import org.indi.objects.SwitchVector;
11  import org.indi.objects.TransferType;
12  import org.indi.objects.Vector;
13  import org.indi.server.BasicDevice;
14  import org.indi.server.IndiServer;
15  import org.indi.server.ObserverState;
16  
17  public class Dome extends BasicDevice {
18      private final SwitchVector powerswitch;
19      private Switch connectswitch = null;
20      private final SwitchVector domeswitch;
21      private Switch openswitch = null;
22      private Switch closeswitch = null;
23      private final String powervectorname = "CONNECTION";
24      private final String domevectorname = "DOMESTATUS";
25      private final String devicename = "Dome";
26      private final String groupname = "Main Control";
27      private boolean timerRunning = false;
28  
29      public Dome(IndiServer server) {
30  	super(server);
31  	this.powerswitch = new SwitchVector(this.devicename,
32  		this.powervectorname, "Connection", this.groupname, State.Idle,
33  		Permission.ReadWrite, SwitchRule.OneOfMany, 0);
34  	this.connectswitch = new Switch("CONNECT", "Connect", Switch.State.Off);
35  	this.powerswitch.add(this.connectswitch);
36  	this.powerswitch.add(new Switch("DISCONNECT", "Disconnect",
37  		Switch.State.On));
38  	this.domeswitch = new SwitchVector(this.devicename,
39  		this.domevectorname, "Dome Status", this.groupname, State.Idle,
40  		Permission.ReadWrite, SwitchRule.OneOfMany, 0);
41  	this.openswitch = new Switch("OPEN", "Open", Switch.State.On);
42  	this.domeswitch.add(this.openswitch);
43  	this.closeswitch = new Switch("CLOSE", "Close", Switch.State.On);
44  	this.domeswitch.add(this.closeswitch);
45  	subscribe("Rain", "Rain Alert", ObserverState.All);
46      }
47  
48      @Override
49      public void onGetProperties(GetProperties o) {
50  	def(this.powerswitch);
51  	def(this.domeswitch);
52      }
53  
54      void OnNew(SwitchVector vector) {
55  	/* ignore if not ours */
56  	if (!this.devicename.equals(vector.getDevice())) {
57  	    return;
58  	}
59  	/* Connection */
60  	if (this.powervectorname.equals(vector.getName())) {
61  	    this.powerswitch.update(vector);
62  	    if (this.connectswitch.getState() == Switch.State.Off) {
63  		this.powerswitch.setState(State.Ok);
64  		set(this.powerswitch, "Dome is online.");
65  	    } else {
66  		this.powerswitch.setState(State.Idle);
67  		set(this.powerswitch, "Dome is offline.");
68  	    }
69  	    return;
70  	}
71  	/* Dome */
72  	if (this.domevectorname.equals(vector.getName())) {
73  	    if (this.powerswitch.getState() != State.Ok) {
74  		msg("Dome is offline!");
75  		return;
76  	    }
77  	    if (this.timerRunning) {
78  		msg("Command disregared; Dome already moving");
79  		return;
80  	    }
81  	    this.domeswitch.update(vector);
82  	    this.domeswitch.setState(State.Busy);
83  
84  	    if (this.openswitch.getState() == Switch.State.On) {
85  		set(this.domeswitch, "Dome is opening.");
86  	    } else {
87  		set(this.domeswitch, "Dome is closing.");
88  	    }
89  	    this.timerRunning = true;
90  	    timer(5000);
91  	}
92      }
93  
94      @Override
95      public void onTimer() {
96  	this.domeswitch.setState(State.Ok);
97  	if (this.openswitch.getState() == Switch.State.On) {
98  	    set(this.domeswitch, "Dome is open.");
99  	} else {
100 	    set(this.domeswitch, "Dome is closed.");
101 	}
102 	this.timerRunning = false;
103     }
104 
105     public void closeDome() {
106 	this.domeswitch.setState(State.Busy);
107 	set(this.domeswitch, "Rain Alert! Dome is closing...");
108 	this.timerRunning = true;
109 	this.openswitch.setState(Switch.State.Off);
110 	timer(5000);
111     }
112 
113     @Override
114     public void onObserved(Vector vector) {
115 	if (vector.getTransferType() == TransferType.Set) {
116 	    if (vector instanceof LightVector) {
117 		LightVector lv = (LightVector) vector;
118 		for (Object c : lv.getChlidren()) {
119 		    if (c instanceof Light) {
120 			Light l = (Light) c;
121 			if ("STATUS".equals(l.getName())) {
122 			    if (l.getState() == State.Alert) {
123 				closeDome();
124 			    } else {
125 				msg("Rain Alert Detected! Dome is already closed.");
126 			    }
127 			}
128 		    }
129 		}
130 	    }
131 	}
132     }
133 }