[iot-gateway/lighting-gateway.git] / client / AndroidClients / LightingController / src / com / lightingcontroller / Zigbee / ZigbeeAssistant.java
1 /**************************************************************************************************
2 Filename: ZigBeeAssistant.java
3 Revised: $$
4 Revision: $$
6 Description: ZigBee Assistant
8 Copyright (C) {2012} Texas Instruments Incorporated - http://www.ti.com/
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
15 Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
18 Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the
21 distribution.
23 Neither the name of Texas Instruments Incorporated nor the names of
24 its contributors may be used to endorse or promote products derived
25 from this software without specific prior written permission.
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 **************************************************************************************************/
41 package com.lightingcontroller.Zigbee;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.HashMap;
46 import java.util.List;
48 import com.lightingcontroller.Zigbee.ZigbeeScene;
50 import android.app.Activity;
51 import android.graphics.Color;
52 import android.util.Log;
53 import android.widget.Toast;
55 public class ZigbeeAssistant implements Runnable {
57 public static boolean gateWayConnected = false;
58 public static boolean initializing = true;
60 private static String TAG = "ZigbeeAssistant";
61 public static ZigbeeSrpcClient zigbeeSrpcClient;
63 static Thread thread;
64 static Activity mainAct;
66 static List<ZigbeeDevice> ourDevices = new ArrayList<ZigbeeDevice>();
67 static List<ZigbeeGroup> ourGroups = new ArrayList<ZigbeeGroup>();
68 static List<ZigbeeScene> ourScenes = new ArrayList<ZigbeeScene>();
70 private static int LightDeviceIdx = 0;
72 static public HashMap<Short,Long> nwrkToIEEE = new HashMap<Short,Long>();
74 public static native int runMain();
75 public static native void setDeviceState(short networkaddr, char end, boolean state); // Set device state on/off
76 public static native void setDeviceLevel(short networkaddr, char end, char level, short transitionTime); // Set device level/dim
77 public static native void setDeviceColour(short networkaddr, char end, char hue, char saturation, short transitionTime); // Set device Color
79 public static native void bindDevices(short network_a, char end_a, int ieel_a, int ieeh_a,
80 short network_b, char end_b, int ieel_b, int ieeh_b,
81 short clusterId); // Bind Devices
83 static final byte EP_INFO_TYPE_EXISTING = 0;
84 static final byte EP_INFO_TYPE_NEW = 1;
85 static final byte EP_INFO_TYPE_UPDATED = 2;
86 static final byte EP_INFO_TYPE_REMOVED = 4;
88 static int cnt = 0;
90 public static void enableNotify(Activity activity, int timeOut)
91 {
92 ZigbeeNotification.init(activity);
93 }
95 public static List<ZigbeeDevice> getDevices()
96 {
97 return ourDevices;
98 }
100 public static void addGroup(ZigbeeDevice d, String groupName )
101 {
102 ZigbeeSrpcClient.addGroup((short) d.NetworkAddr,d.EndPoint, groupName);
103 }
105 public static void storeScene(String sceneName, int groupId )
106 {
107 ZigbeeSrpcClient.storeScene( sceneName, groupId );
108 }
110 public static void recallScene(String sceneName, int groupId )
111 {
112 ZigbeeSrpcClient.recallScene(sceneName, groupId );
113 }
115 public void descoverGroups()
116 {
117 ZigbeeSrpcClient.discoverGroups( );
118 }
120 public static List<ZigbeeGroup> getGroups()
121 {
122 return ourGroups;
123 }
125 public static List<ZigbeeScene> getScenes()
126 {
127 return ourScenes;
128 }
130 public static void newDevice(int ProfileId, int DeviceId, int NetworkAddr, char EndPoint, byte ieee[], String deviceName, byte newDevFlag)
131 {
132 //find old device
133 ZigbeeDevice device = getDevice(ieee, EndPoint);
135 if(newDevFlag != EP_INFO_TYPE_REMOVED)
136 {
137 if(device != null)
138 {
139 ZigbeeDevice newDevice = new ZigbeeDevice(ProfileId, DeviceId, NetworkAddr, EndPoint, ieee, deviceName, LightDeviceIdx++);
140 device.updateDevice(newDevice);
141 }
142 else
143 {
144 device = new ZigbeeDevice(ProfileId, DeviceId, NetworkAddr, EndPoint, ieee, deviceName, LightDeviceIdx++);
145 ourDevices.add(device);
146 }
147 }
148 else
149 {
150 if(device != null)
151 {
152 ourDevices.remove(device);
153 if(device.hasColourable || device.hasSwitchable || device.hasDimmable)
154 {
155 ZigbeeNotification.showRemoveDeviceNotification(device.Name, 5000);
156 }
157 }
158 }
160 if( (newDevFlag == EP_INFO_TYPE_NEW) && (device.hasColourable || device.hasSwitchable || device.hasDimmable))
161 {
162 ZigbeeNotification.showNewDeviceNotification(device);
163 }
164 }
166 public static void newGroup( String groupName )
167 {
168 ZigbeeGroup newGroup = new ZigbeeGroup(groupName);
169 ourGroups.add(newGroup);
171 //Group will not be created on the GW until a device is added
172 }
174 public static void newGroup(String groupName, int groupId, int status)
175 {
176 ZigbeeGroup group=null;
177 List<ZigbeeGroup> groupList = ZigbeeAssistant.getGroups();
179 //find the group
180 for(int i=0; i < groupList.size(); i++)
181 {
182 if(groupName.equals(groupList.get(i).getGroupName()))
183 {
184 group = groupList.get(i);
185 group.setGroupId(groupId);
186 group.setStatus(status);
187 break;
188 }
189 }
191 if(group == null)
192 {
193 group = new ZigbeeGroup(groupName, groupId, status);
194 ourGroups.add(group);
195 }
196 }
198 public static void newScene(String sceneName, int groupId, byte sceneId, int status)
199 {
200 ZigbeeScene scene=null;
201 List<ZigbeeScene> sceneList = ZigbeeAssistant.getScenes();
203 //find the scene
204 for(int i=0; i < sceneList.size(); i++)
205 {
206 if(sceneName.equals(sceneList.get(i).getSceneName()))
207 {
208 scene = sceneList.get(i);
209 scene.setGroupId(groupId);
210 scene.setSceneId(sceneId);
211 scene.setStatus(status);
212 break;
213 }
214 }
216 if(scene == null)
217 {
218 scene = new ZigbeeScene(sceneName, groupId, sceneId, status);
219 ourScenes.add(scene);
220 }
221 }
223 public static void newScene(String sceneName, int groupId )
224 {
225 ZigbeeScene newScene = new ZigbeeScene(sceneName, groupId);
226 if(ourScenes.contains(newScene) == false)
227 {
228 ourScenes.add(newScene);
229 }
230 //Scene will not be created on the GW until scene is stored
231 }
233 public static boolean bindDevicesOnOff(ZigbeeDevice a, ZigbeeDevice b)
234 {
235 return bindDevices(a,b,0x0006);
236 }
238 public static boolean bindDevicesAll(ZigbeeDevice a, ZigbeeDevice b)
239 {
240 bindDevices(a,b,0x0006);
241 bindDevices(a,b,0x0008);
242 bindDevices(a,b,0x0300);
243 bindDevices(a,b,0x0005);
244 bindDevices(a,b,0x0004);
246 return true;
247 }
249 public static boolean bindDevices(ZigbeeDevice a, ZigbeeDevice b, int cluster)
250 {
251 ZigbeeSrpcClient.bindDevices((short)a.NetworkAddr,a.EndPoint, a.Ieee,
252 (short)b.NetworkAddr,b.EndPoint,b.Ieee,(short)cluster);
254 return true;
255 }
257 public static void setDeviceName(String oldName, String newName)
258 {
259 int last = ourDevices.size()-1;
260 int i = last;
261 ZigbeeDevice d = null;
262 for (i = last ; i >= 0 ; i--)
263 {
264 d = ourDevices.get(i);
265 if (d.Name == oldName)
266 break;
267 }
269 if( (d != null) && (newName != null) )
270 {
271 d.Name = newName;
272 //Need to push this back to server
273 ZigbeeSrpcClient.changeDeviceName(d, newName);
274 }
275 }
277 public static void openNetwork(byte duration)
278 {
279 ZigbeeSrpcClient.openNetwork(duration);
280 }
282 public static ZigbeeDevice ifDeviceExists(int NetworkAddr, char EndPoint)
283 {
284 int last = ourDevices.size()-1;
285 int i = last;
286 ZigbeeDevice ret = null;
287 for (i = last ; i >= 0 ; i--)
288 {
289 ret = ourDevices.get(i);
290 if (ret.NetworkAddr == NetworkAddr && ret.EndPoint == EndPoint)
291 break;
292 }
293 if (i < 0)
294 return null;
295 else
296 return ret;
297 }
299 public static ZigbeeDevice getDevice(byte ieee[], char EndPoint)
300 {
301 int last = ourDevices.size()-1;
302 int i = last;
303 ZigbeeDevice ret = null;
304 for (i = last ; i >= 0 ; i--)
305 {
306 ret = ourDevices.get(i);
307 if( (Arrays.equals(ret.Ieee, ieee)) && (ret.EndPoint == EndPoint))
308 break;
309 }
310 if (i < 0)
311 return null;
312 else
313 return ret;
314 }
316 public static boolean hasAnySwitchers()
317 {
318 for (int i = 0 ; i < ourDevices.size() ; i++)
319 {
320 ZigbeeDevice d = ourDevices.get(i);
321 if (d.hasOutSwitch)
322 return true;
323 }
324 return false;
325 }
327 public ZigbeeAssistant()
328 {
329 ourDevices.clear();
330 LightDeviceIdx = 0;
331 }
333 public static String getInfoString()
334 {
335 if (ourDevices.size() == 0)
336 return "No devices detected.";
338 String s = "";
339 for (int i = 0 ; i < ourDevices.size() ; i++)
340 {
341 ZigbeeDevice d = ourDevices.get(i);
342 s += d.Name + " - 0x" + Integer.toHexString(d.NetworkAddr) + "("+(int)d.EndPoint+")\n";
343 if (d.hasSwitchable)
344 s+="\t: Switchable\n";
345 if (d.hasDimmable)
346 s+="\t: Dimmable\n";
347 if (d.hasColourable)
348 s+="\t: Colourable\n";
349 if (d.hasOutSwitch)
350 s+="\t: Switches Others\n";
351 if (d.hasOutLeveL)
352 s+="\t: controls level of Others\n";
353 if (d.hasOutColor)
354 s+="\t: controls color of Others\n";
355 if (d.hasOutScene)
356 s+="\t: controls scenes of Others\n";
357 if (d.hasOutGroup)
358 s+="\t: controls groups of Others\n";
359 }
360 return s;
361 }
363 public static ArrayList<ZigbeeDevice> getSwitchers()
364 {
365 ArrayList<ZigbeeDevice> s = new ArrayList<ZigbeeDevice>();
366 for (int i = 0 ; i < ourDevices.size() ; i++)
367 {
368 ZigbeeDevice d = ourDevices.get(i);
369 if (d.hasOutSwitch)
370 s.add(d);
371 }
372 return s;
373 }
375 public static ArrayList<ZigbeeDevice> getSwitchable()
376 {
377 ArrayList<ZigbeeDevice> s = new ArrayList<ZigbeeDevice>();
378 for (int i = 0 ; i < ourDevices.size() ; i++)
379 {
380 ZigbeeDevice d = ourDevices.get(i);
381 if (d.hasColourable || d.hasSwitchable || d.hasDimmable)
382 s.add(d);
383 }
384 return s;
385 }
387 public static void IdentifyDevice(ZigbeeDevice d, short identifyTime)
388 {
389 ZigbeeSrpcClient.IdentifyDevice((short) d.NetworkAddr, ZigbeeSrpcClient.Addr16Bit, d.EndPoint, identifyTime);
390 }
392 public static void IdentifyGroup(ZigbeeGroup g, short identifyTime)
393 {
394 ZigbeeSrpcClient.IdentifyDevice((short) g.getGroupId(), ZigbeeSrpcClient.AddrGroup, (char)0xFF, identifyTime);
395 }
397 public static void setDeviceState(ZigbeeDevice d, boolean state)
398 {
400 ZigbeeSrpcClient.setDeviceState((short) d.NetworkAddr, ZigbeeSrpcClient.Addr16Bit, d.EndPoint,state);
401 }
403 public static void setDeviceState(ZigbeeGroup g, boolean state)
404 {
405 ZigbeeSrpcClient.setDeviceState((short) g.getGroupId(), ZigbeeSrpcClient.AddrGroup, (char) 0xFF, state);
406 }
408 public static void setDeviceColour(ZigbeeDevice d, int colour)
409 {
410 float[] hsv = new float[3];
411 Color.colorToHSV(colour, hsv);
412 ZigbeeSrpcClient.setDeviceColor((short) d.NetworkAddr, ZigbeeSrpcClient.Addr16Bit, d.EndPoint,(char)((hsv[0]/360)*0xFF), (char)(hsv[1]*0xFF),(short)10);
413 }
415 public static void setDeviceHueSat(ZigbeeDevice d, byte hue, byte sat)
416 {
417 ZigbeeSrpcClient.setDeviceColor((short) d.NetworkAddr, ZigbeeSrpcClient.Addr16Bit, d.EndPoint, (char) hue, (char) sat,(short)10);
418 }
420 public static void setDeviceHueSat(ZigbeeGroup g, byte hue, byte sat)
421 {
422 ZigbeeSrpcClient.setDeviceColor((short) g.getGroupId(), ZigbeeSrpcClient.AddrGroup, (char) 0xFF, (char) hue, (char) sat,(short)10);
423 }
425 public static void setDeviceLevel(ZigbeeDevice d, int colour)
426 {
427 float[] hsv = new float[3];
428 Color.colorToHSV(colour, hsv);
429 setDeviceLevel(d,(char)((hsv[2])*0xFF));
430 }
432 public static void setDeviceLevel(ZigbeeDevice d, char level)
433 {
434 ZigbeeSrpcClient.setDeviceLevel((short) d.NetworkAddr, ZigbeeSrpcClient.Addr16Bit, d.EndPoint,level,(short)10);
435 }
437 public static void setDeviceLevel(ZigbeeGroup g, char level)
438 {
439 ZigbeeSrpcClient.setDeviceLevel((short) g.getGroupId(), ZigbeeSrpcClient.AddrGroup, (char) 0xFF,level,(short)10);
440 }
442 public static void getDeviceState(ZigbeeDevice d)
443 {
444 ZigbeeSrpcClient.getDeviceState((short) d.NetworkAddr, ZigbeeSrpcClient.Addr16Bit, d.EndPoint);
445 }
447 public static void getDeviceLevel(ZigbeeDevice d)
448 {
449 ZigbeeSrpcClient.getDeviceLevel((short) d.NetworkAddr, ZigbeeSrpcClient.Addr16Bit, d.EndPoint);
450 }
452 public static void getDeviceHue(ZigbeeDevice d)
453 {
454 ZigbeeSrpcClient.getDeviceHue((short) d.NetworkAddr, ZigbeeSrpcClient.Addr16Bit, d.EndPoint);
455 }
457 public static void getDeviceSat(ZigbeeDevice d)
458 {
459 ZigbeeSrpcClient.getDeviceSat((short) d.NetworkAddr, ZigbeeSrpcClient.Addr16Bit, d.EndPoint);
460 }
462 public void run() {
463 Log.d(TAG, "Begin Zigbee");
464 zigbeeSrpcClient = new ZigbeeSrpcClient();
465 }
466 }