OpenECHO
 All Classes Namespaces Files Functions Variables
EchoNode.java
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Sony Computer Science Laboratories, Inc. <info@kadecot.net>
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.sonycsl.echo.node;
17 
18 import java.io.IOException;
19 import java.net.InetAddress;
20 import java.net.SocketException;
21 import java.net.UnknownHostException;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 
32 import com.sonycsl.echo.eoj.device.airconditioner.*;
33 import com.sonycsl.echo.eoj.device.audiovisual.*;
34 import com.sonycsl.echo.eoj.device.cookinghousehold.*;
35 import com.sonycsl.echo.eoj.device.health.*;
36 import com.sonycsl.echo.eoj.device.housingfacilities.*;
37 import com.sonycsl.echo.eoj.device.managementoperation.*;
38 import com.sonycsl.echo.eoj.device.sensor.*;
39 import com.sonycsl.echo.eoj.profile.*;
40 import com.sonycsl.echo.eoj.profile.NodeProfile.Proxy;
41 
42 
43 public final class EchoNode {
44  private static HashMap<Short, DeviceProxyCreator> mProxyCreators = new HashMap<Short, DeviceProxyCreator>();
45 
46  private NodeProfile mNodeProfile;
47  private List<DeviceObject> mDevices = new ArrayList<DeviceObject>();
48  private String mAddress;
49 
50  public EchoNode(NodeProfile nodeProfile, DeviceObject[] devices) {
51  // selfNode
52  mAddress = EchoSocket.SELF_ADDRESS;
53  mNodeProfile = nodeProfile;
54  for(DeviceObject d : devices) {
55  if(isSelfNode()) {
56  d.allocateSelfDeviceInstanceCode();
57  }
58  mDevices.add(d);
59  }
60 
61  }
62 
63  public EchoNode(String address) {
64  // otherNode
65  mAddress = address;
66  mNodeProfile = new NodeProfile.Proxy();
67  }
68 
69  public void onNew() {
70  Echo.getEventListener().onNewNode(this);
71  }
72 
73  public void onFound() {
74  Echo.getEventListener().onNewNode(this);
75  }
76 
77  public boolean isSelfNode() {
78  return EchoSocket.SELF_ADDRESS.equals(mAddress);
79  }
80 
81  public boolean isProxy() {
82  return !(EchoSocket.SELF_ADDRESS.equals(mAddress));
83  }
84 
85  public InetAddress getAddress() {
86  InetAddress address = null;
87  try {
88  address = InetAddress.getByName(mAddress);
89  } catch (UnknownHostException e) {
90  // TODO Auto-generated catch block
91  e.printStackTrace();
92  }
93  return address;
94  }
95 
96  public String getAddressStr() {
97  return mAddress;
98  }
99 
101  return mNodeProfile;
102  }
103 
104  public DeviceObject addOtherDevice(short echoClassCode, byte echoInstanceCode) {
105  DeviceObject device = newOtherDevice(echoClassCode, echoInstanceCode);
106  addDevice(device);
107  return device;
108  }
109 
110  public void addDevice(DeviceObject device) {
111  if(device == null) return;
112  if(device.getNode() == this) return;
113 
114  mDevices.add(device);
115  if(isSelfNode()) {
116  device.allocateSelfDeviceInstanceCode();
117  device.setNode(this);
118  device.onNew();
119  device.onFound();
120  }
121  /*
122  short code = device.getEchoClassCode();
123  if(mDeviceGroups.containsKey(code)) {
124  List<DeviceObject> deviceList = mDeviceGroups.get(code);
125  if(deviceList.size() > 0x7F) return;
126  deviceList.add(device);
127  } else {
128  List<DeviceObject> deviceList = new ArrayList<DeviceObject>();
129  deviceList.add(device);
130  mDeviceGroups.put(code, deviceList);
131  }
132  if(mInitialized) {
133  device.initialize(this);
134  }*/
135  }
136 
137 
138  public void removeDevice(DeviceObject device) {
139  if(device == null) return;
140  if(device.getNode() != this) return;
141  mDevices.remove(device);
142  }
143 
144  public boolean containsDevice(short echoClassCode, byte echoInstanceCode) {
145  for(DeviceObject d : mDevices) {
146  if(d.getEchoClassCode() == echoClassCode
147  && d.getInstanceCode() == echoInstanceCode) {
148  return true;
149  }
150  }
151  return false;
152  }
153 
154  public boolean containsDevice(DeviceObject device) {
155  if(device == null) return false;
156  if(device.getNode() != this) return false;
157  return mDevices.contains(device);
158  }
159 
160  public EchoObject getInstance(byte classGroupCode, byte classCode, byte instanceCode) {
161  return getInstance(EchoUtils.getEchoClassCode(classGroupCode, classCode), instanceCode);
162  }
163 
164  public EchoObject getInstance(short echoClassCode, byte echoInstanceCode) {
165  if(mNodeProfile.getEchoClassCode() == echoClassCode
166  && mNodeProfile.getInstanceCode() == echoInstanceCode) {
167  return mNodeProfile;
168  }
169  return getDevice(echoClassCode, echoInstanceCode);
170  }
171 
172 
173  public boolean containsInstance(byte classGroupCode, byte classCode, byte instanceCode) {
174  short echoClassCode = EchoUtils.getEchoClassCode(classGroupCode, classCode);
175  return containsInstance(echoClassCode, instanceCode);
176  }
177 
178  public boolean containsInstance(short echoClassCode, byte echoInstanceCode) {
179  if(mNodeProfile.getEchoClassCode() == echoClassCode
180  && mNodeProfile.getInstanceCode() == echoInstanceCode) {
181  return true;
182  }
183 
184  return containsDevice(echoClassCode, echoInstanceCode);
185  }
186 
187  public DeviceObject getDevice(byte classGroupCode, byte classCode, byte instanceCode) {
188  return getDevice(EchoUtils.getEchoClassCode(classGroupCode, classCode), instanceCode);
189  }
190 
191  public DeviceObject getDevice(short echoClassCode, byte echoInstanceCode) {
192  for(DeviceObject d : mDevices) {
193  if(d.getEchoClassCode() == echoClassCode
194  && d.getInstanceCode() == echoInstanceCode) {
195  return d;
196  }
197  }
198  return null;
199  }
200 
201  public DeviceObject[] getDevices(byte classGroupCode, byte classCode) {
202  return getDevices(EchoUtils.getEchoClassCode(classGroupCode, classCode));
203  }
204 
205  public DeviceObject[] getDevices(short echoClassCode) {
206  List<DeviceObject> ret = new ArrayList<DeviceObject>();
207  for(DeviceObject d : mDevices) {
208  if(d.getEchoClassCode() == echoClassCode) {
209  ret.add(d);
210  }
211  }
212  return ret.toArray(new DeviceObject[]{});
213  }
214 
216  return (DeviceObject[]) mDevices.toArray(new DeviceObject[]{});
217  }
218 
219  private static DeviceObject newOtherDevice(short echoClassCode, byte instanceCode) {
220  if(mProxyCreators.containsKey(echoClassCode)) {
221  return mProxyCreators.get(echoClassCode).create(instanceCode);
222  }
223  switch(echoClassCode) {
224  case ActivityAmountSensor.ECHO_CLASS_CODE: return new ActivityAmountSensor.Proxy(instanceCode);
225  case AirPollutionSensor.ECHO_CLASS_CODE: return new AirPollutionSensor.Proxy(instanceCode);
226  case AirSpeedSensor.ECHO_CLASS_CODE: return new AirSpeedSensor.Proxy(instanceCode);
227  case BathHeatingStatusSensor.ECHO_CLASS_CODE: return new BathHeatingStatusSensor.Proxy(instanceCode);
228  case BathWaterLevelSensor.ECHO_CLASS_CODE: return new BathWaterLevelSensor.Proxy(instanceCode);
229  case BedPresenceSensor.ECHO_CLASS_CODE: return new BedPresenceSensor.Proxy(instanceCode);
230  case CallSensor.ECHO_CLASS_CODE: return new CallSensor.Proxy(instanceCode);
231  case CigaretteSmokeSensor.ECHO_CLASS_CODE: return new CigaretteSmokeSensor.Proxy(instanceCode);
232  case CO2Sensor.ECHO_CLASS_CODE: return new CO2Sensor.Proxy(instanceCode);
233  case CondensationSensor.ECHO_CLASS_CODE: return new CondensationSensor.Proxy(instanceCode);
234  case CrimePreventionSensor.ECHO_CLASS_CODE: return new CrimePreventionSensor.Proxy(instanceCode);
235  case CurrentValueSensor.ECHO_CLASS_CODE: return new CurrentValueSensor.Proxy(instanceCode);
236  case DifferentialPressureSensor.ECHO_CLASS_CODE: return new DifferentialPressureSensor.Proxy(instanceCode);
237  case EarthquakeSensor.ECHO_CLASS_CODE: return new EarthquakeSensor.Proxy(instanceCode);
238  case ElectricEnergySensor.ECHO_CLASS_CODE: return new ElectricEnergySensor.Proxy(instanceCode);
239  case ElectricLeakSensor.ECHO_CLASS_CODE: return new ElectricLeakSensor.Proxy(instanceCode);
240  case EmergencyButton.ECHO_CLASS_CODE: return new EmergencyButton.Proxy(instanceCode);
241  case FireSensor.ECHO_CLASS_CODE: return new FireSensor.Proxy(instanceCode);
242  case FirstAidSensor.ECHO_CLASS_CODE: return new FirstAidSensor.Proxy(instanceCode);
243  case FlameSensor.ECHO_CLASS_CODE: return new FlameSensor.Proxy(instanceCode);
244  case GasLeakSensor.ECHO_CLASS_CODE: return new GasLeakSensor.Proxy(instanceCode);
245  case GasSensor.ECHO_CLASS_CODE: return new GasSensor.Proxy(instanceCode);
246  case HumanBodyLocationSensor.ECHO_CLASS_CODE: return new HumanBodyLocationSensor.Proxy(instanceCode);
247  case HumanDetectionSensor.ECHO_CLASS_CODE: return new HumanDetectionSensor.Proxy(instanceCode);
248  case HumiditySensor.ECHO_CLASS_CODE: return new HumiditySensor.Proxy(instanceCode);
249  case IlluminanceSensor.ECHO_CLASS_CODE: return new IlluminanceSensor.Proxy(instanceCode);
250  case MailingSensor.ECHO_CLASS_CODE: return new MailingSensor.Proxy(instanceCode);
251  case MicromotionSensor.ECHO_CLASS_CODE: return new MicromotionSensor.Proxy(instanceCode);
252  case OdorSensor.ECHO_CLASS_CODE: return new OdorSensor.Proxy(instanceCode);
253  case OpenCloseSensor.ECHO_CLASS_CODE: return new OpenCloseSensor.Proxy(instanceCode);
254  case OxygenSensor.ECHO_CLASS_CODE: return new OxygenSensor.Proxy(instanceCode);
255  case PassageSensor.ECHO_CLASS_CODE: return new PassageSensor.Proxy(instanceCode);
256  case RainSensor.ECHO_CLASS_CODE: return new RainSensor.Proxy(instanceCode);
257  case SnowSensor.ECHO_CLASS_CODE: return new SnowSensor.Proxy(instanceCode);
258  case SoundSensor.ECHO_CLASS_CODE: return new SoundSensor.Proxy(instanceCode);
259  case TemperatureSensor.ECHO_CLASS_CODE: return new TemperatureSensor.Proxy(instanceCode);
260  case VisitorSensor.ECHO_CLASS_CODE: return new VisitorSensor.Proxy(instanceCode);
261  case VOCSensor.ECHO_CLASS_CODE: return new VOCSensor.Proxy(instanceCode);
262  case WaterFlowRateSensor.ECHO_CLASS_CODE: return new WaterFlowRateSensor.Proxy(instanceCode);
263  case WaterLeakSensor.ECHO_CLASS_CODE: return new WaterLeakSensor.Proxy(instanceCode);
264  case WaterLevelSensor.ECHO_CLASS_CODE: return new WaterLevelSensor.Proxy(instanceCode);
265  case WaterOverflowSensor.ECHO_CLASS_CODE: return new WaterOverflowSensor.Proxy(instanceCode);
266  case WeightSensor.ECHO_CLASS_CODE: return new WeightSensor.Proxy(instanceCode);
267  case AirCleaner.ECHO_CLASS_CODE: return new AirCleaner.Proxy(instanceCode);
268  case AirConditionerVentilationFan.ECHO_CLASS_CODE: return new AirConditionerVentilationFan.Proxy(instanceCode);
269  case ElectricHeater.ECHO_CLASS_CODE: return new ElectricHeater.Proxy(instanceCode);
270  case FanHeater.ECHO_CLASS_CODE: return new FanHeater.Proxy(instanceCode);
271  case HomeAirConditioner.ECHO_CLASS_CODE: return new HomeAirConditioner.Proxy(instanceCode);
272  case Humidifier.ECHO_CLASS_CODE: return new Humidifier.Proxy(instanceCode);
273  case PackageTypeCommercialAirConditionerIndoorUnit.ECHO_CLASS_CODE: return new PackageTypeCommercialAirConditionerIndoorUnit.Proxy(instanceCode);
274  case PackageTypeCommercialAirConditionerOutdoorUnit.ECHO_CLASS_CODE: return new PackageTypeCommercialAirConditionerOutdoorUnit.Proxy(instanceCode);
275  case VentilationFan.ECHO_CLASS_CODE: return new VentilationFan.Proxy(instanceCode);
276  case BathroomHeaterAndDryer.ECHO_CLASS_CODE: return new BathroomHeaterAndDryer.Proxy(instanceCode);
277  case Battery.ECHO_CLASS_CODE: return new Battery.Proxy(instanceCode);
278  case Buzzer.ECHO_CLASS_CODE: return new Buzzer.Proxy(instanceCode);
279  case ColdOrHotWaterHeatSourceEquipment.ECHO_CLASS_CODE: return new ColdOrHotWaterHeatSourceEquipment.Proxy(instanceCode);
280  case ElectricallyOperatedShade.ECHO_CLASS_CODE: return new ElectricallyOperatedShade.Proxy(instanceCode);
281  case ElectricLock.ECHO_CLASS_CODE: return new ElectricLock.Proxy(instanceCode);
282  case ElectricShutter.ECHO_CLASS_CODE: return new ElectricShutter.Proxy(instanceCode);
283  case ElectricStormWindow.ECHO_CLASS_CODE: return new ElectricStormWindow.Proxy(instanceCode);
284  case ElectricToiletSeat.ECHO_CLASS_CODE: return new ElectricToiletSeat.Proxy(instanceCode);
285  case ElectricVehicle.ECHO_CLASS_CODE: return new ElectricVehicle.Proxy(instanceCode);
286  case ElectricWaterHeater.ECHO_CLASS_CODE: return new ElectricWaterHeater.Proxy(instanceCode);
287  case EngineCogeneration.ECHO_CLASS_CODE: return new EngineCogeneration.Proxy(instanceCode);
288  case FloorHeater.ECHO_CLASS_CODE: return new FloorHeater.Proxy(instanceCode);
289  case FuelCell.ECHO_CLASS_CODE: return new FuelCell.Proxy(instanceCode);
290  case GasMeter.ECHO_CLASS_CODE: return new GasMeter.Proxy(instanceCode);
291  case GeneralLighting.ECHO_CLASS_CODE: return new GeneralLighting.Proxy(instanceCode);
292  case HouseholdSolarPowerGeneration.ECHO_CLASS_CODE: return new HouseholdSolarPowerGeneration.Proxy(instanceCode);
293  case InstantaneousWaterHeater.ECHO_CLASS_CODE: return new InstantaneousWaterHeater.Proxy(instanceCode);
294  case LPGasMeter.ECHO_CLASS_CODE: return new LPGasMeter.Proxy(instanceCode);
295  case PowerDistributionBoardMetering.ECHO_CLASS_CODE: return new PowerDistributionBoardMetering.Proxy(instanceCode);
296  case SmartElectricEnergyMeter.ECHO_CLASS_CODE: return new SmartElectricEnergyMeter.Proxy(instanceCode);
297  case SmartGasMeter.ECHO_CLASS_CODE: return new SmartGasMeter.Proxy(instanceCode);
298  case Sprinkler.ECHO_CLASS_CODE: return new Sprinkler.Proxy(instanceCode);
299  case WaterFlowmeter.ECHO_CLASS_CODE: return new WaterFlowmeter.Proxy(instanceCode);
300  case WattHourMeter.ECHO_CLASS_CODE: return new WattHourMeter.Proxy(instanceCode);
301  case ClothesDryer.ECHO_CLASS_CODE: return new ClothesDryer.Proxy(instanceCode);
302  case CombinationMicrowaveOven.ECHO_CLASS_CODE: return new CombinationMicrowaveOven.Proxy(instanceCode);
303  case CookingHeater.ECHO_CLASS_CODE: return new CookingHeater.Proxy(instanceCode);
304  case ElectricHotWaterPot.ECHO_CLASS_CODE: return new ElectricHotWaterPot.Proxy(instanceCode);
305  case Refrigerator.ECHO_CLASS_CODE: return new Refrigerator.Proxy(instanceCode);
306  case RiceCooker.ECHO_CLASS_CODE: return new RiceCooker.Proxy(instanceCode);
307  case WasherAndDryer.ECHO_CLASS_CODE: return new WasherAndDryer.Proxy(instanceCode);
308  case WashingMachine.ECHO_CLASS_CODE: return new WashingMachine.Proxy(instanceCode);
309  case Weighing.ECHO_CLASS_CODE: return new Weighing.Proxy(instanceCode);
310  case Controller.ECHO_CLASS_CODE: return new Controller.Proxy(instanceCode);
311  case Switch.ECHO_CLASS_CODE: return new Switch.Proxy(instanceCode);
312  case Display.ECHO_CLASS_CODE: return new Display.Proxy(instanceCode);
313  case Television.ECHO_CLASS_CODE: return new Television.Proxy(instanceCode);
314  default: return new DeviceObject.Proxy(echoClassCode, instanceCode);
315  }
316  }
317  public static void putDeviceProxyCreator(short echoClassCode, DeviceProxyCreator creator) {
318  mProxyCreators.put(echoClassCode, creator);
319  }
320  public static void removeDeviceProxyCreator(short echoClassCode) {
321  mProxyCreators.remove(echoClassCode);
322  }
323 
324  public static interface DeviceProxyCreator {
325  public DeviceObject create(byte instanceCode);
326  }
327 
328 }
DeviceObject addOtherDevice(short echoClassCode, byte echoInstanceCode)
Definition: EchoNode.java:104
DeviceObject getDevice(byte classGroupCode, byte classCode, byte instanceCode)
Definition: EchoNode.java:187
static final String SELF_ADDRESS
Definition: EchoSocket.java:52
void removeDevice(DeviceObject device)
Definition: EchoNode.java:138
DeviceObject[] getDevices()
Definition: EchoNode.java:215
DeviceObject[] getDevices(short echoClassCode)
Definition: EchoNode.java:205
boolean containsInstance(short echoClassCode, byte echoInstanceCode)
Definition: EchoNode.java:178
static void putDeviceProxyCreator(short echoClassCode, DeviceProxyCreator creator)
Definition: EchoNode.java:317
EchoObject getInstance(byte classGroupCode, byte classCode, byte instanceCode)
Definition: EchoNode.java:160
static void removeDeviceProxyCreator(short echoClassCode)
Definition: EchoNode.java:320
void addDevice(DeviceObject device)
Definition: EchoNode.java:110
boolean containsDevice(DeviceObject device)
Definition: EchoNode.java:154
boolean containsDevice(short echoClassCode, byte echoInstanceCode)
Definition: EchoNode.java:144
static short getEchoClassCode(byte classGroupCode, byte classCode)
Definition: EchoUtils.java:101
DeviceObject getDevice(short echoClassCode, byte echoInstanceCode)
Definition: EchoNode.java:191
EchoObject getInstance(short echoClassCode, byte echoInstanceCode)
Definition: EchoNode.java:164
DeviceObject[] getDevices(byte classGroupCode, byte classCode)
Definition: EchoNode.java:201
EchoNode(NodeProfile nodeProfile, DeviceObject[] devices)
Definition: EchoNode.java:50
boolean containsInstance(byte classGroupCode, byte classCode, byte instanceCode)
Definition: EchoNode.java:173