View Javadoc

1   package org.indi.server;
2   
3   import java.util.HashSet;
4   import java.util.Set;
5   
6   import org.indi.clientmessages.GetProperties;
7   import org.indi.objects.BlobVector;
8   import org.indi.objects.NumberVector;
9   import org.indi.objects.SwitchVector;
10  import org.indi.objects.TextVector;
11  import org.indi.objects.Vector;
12  
13  class HelperDevice extends BasicDevice {
14      private final Device device;
15      private final Set<Vector> vectors;
16  
17      public HelperDevice(IndiServer server, String name, Device device,
18  	    Set<Vector> vectors) {
19  	super(server);
20  	this.device = device;
21  	this.vectors = vectors;
22  	this.name = name;
23      }
24  
25      @Override
26      public void onGetProperties(GetProperties o) {
27  	for (Vector v : this.vectors) {
28  	    super.def(v);
29  	}
30      }
31  
32      @Override
33      public void onNew(SwitchVector vector) {
34  	if (this.vectors.contains(vector)) {
35  	    this.device.onNew(vector);
36  	}
37      }
38  
39      @Override
40      public void onNew(NumberVector vector) {
41  	if (this.vectors.contains(vector)) {
42  	    this.device.onNew(vector);
43  	}
44      }
45  
46      @Override
47      public void onNew(BlobVector vector) {
48  	if (this.vectors.contains(vector)) {
49  	    this.device.onNew(vector);
50  	}
51      }
52  
53      @Override
54      public void onNew(TextVector vector) {
55  	if (this.vectors.contains(vector)) {
56  	    this.device.onNew(vector);
57  	}
58      }
59  
60      @Override
61      public void onTimer() {
62  	this.device.onTimer();
63      }
64  
65  }
66  
67  public class SmartDevice implements Device {
68      private final Set<Vector> vectors;
69      private final HelperDevice helper;
70      public String name;
71  
72      public SmartDevice(IndiServer server, String name) {
73  	this.vectors = new HashSet<Vector>();
74  	this.helper = new HelperDevice(server, name, this, this.vectors);
75  	this.name = name;
76      }
77  
78      public void def(Vector vector) {
79  	def(vector, null);
80      }
81  
82      public void set(Vector vector) {
83  	set(vector, null);
84      }
85  
86      public void del(Vector vector) {
87  	del(vector, null);
88      }
89  
90      public void def(Vector vector, String message) {
91  	this.vectors.add(vector);
92  	this.helper.def(vector, message);
93      }
94  
95      public void set(Vector vector, String message) {
96  	if (!this.vectors.contains(vector)) {
97  	    throw new RuntimeException(vector.name);
98  	}
99  	this.helper.set(vector, message);
100     }
101 
102     public void del(Vector vector, String message) {
103 	if (!this.vectors.contains(vector)) {
104 	    throw new RuntimeException();
105 	}
106 	this.vectors.remove(vector);
107 	this.helper.del(vector, message);
108     }
109 
110     public void timer(long timeout) {
111 	this.helper.timer(timeout);
112     }
113 
114     public void msg(String message) {
115 	this.helper.msg(message);
116     }
117 
118     public void onGetProperties(GetProperties o) {
119 
120     }
121 
122     public void onNew(SwitchVector vector) {
123 
124     }
125 
126     public void onNew(NumberVector vector) {
127 
128     }
129 
130     public void onNew(BlobVector vector) {
131 
132     }
133 
134     public void onNew(TextVector vector) {
135 
136     }
137 
138     public void onTimer() {
139 
140     }
141 
142     public void onObserved(Vector vector) {
143 	this.helper.onObserved(vector);
144     }
145 
146     public String getName() {
147 	return this.name;
148     }
149 }