]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - iot-gateway/lighting-gateway.git/blob - client/AndroidClients/LightingController/src/com/lightingcontroller/Zigbee/ZigbeeDevice.java
add support for control bridge FW
[iot-gateway/lighting-gateway.git] / client / AndroidClients / LightingController / src / com / lightingcontroller / Zigbee / ZigbeeDevice.java
1 /**************************************************************************************************
2   Filename:       ZigBeeDevice.java
3   Revised:        $$
4   Revision:       $$
6   Description:    ZigBee Device Class
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.
38  
39 **************************************************************************************************/
41 package com.lightingcontroller.Zigbee;
43 import android.content.Context;
44 import android.widget.TextView;
46 import com.lightingcontroller.zllMain;
49 public class ZigbeeDevice {
51         private String TAG = "ZigbeeAssistant";
52         public String Name = "Unknown Device";
53         public String Type = "Unknown Device";
54         public char EndPoint = 0;
55         public int ProfileId = 0;
56         public int DeviceId = 0;
57         public int NetworkAddr = 0;
58         public byte Ieee[] = {};
59         
60         // Prob better to do this as an array of cluster IDS.
61         // Never mind.
62         public boolean hasColourable = false;
63         public boolean hasDimmable = false;
64         public boolean hasSwitchable = false;
65         public boolean hasOutSwitch = false;
66         public boolean hasOutLeveL = false;
67         public boolean hasOutColor = false;
68         public boolean hasOutScene = false;
69         public boolean hasOutGroup = false;     
70         
71         private byte currentState = 0;
72         private byte currentLevel = 0;
73         private byte currentHue = 0;
74         private byte currentSat = 0;
75         public byte currentColorAttrsUpdated = 0;
76         private static final byte CURRENT_STATE_UPDATED = (byte) 0x01;  
77         private static final byte CURRENT_LEVEL_UPDATED = (byte) 0x02;  
78         private static final byte CURRENT_HUE_UPDATED = (byte) 0x04;    
79         private static final byte CURRENT_SAT_UPDATED = (byte) 0x08;    
80         
81         // Zigbee Home Automation Profile Identification
82         private static final int ZCL_HA_PROFILE_ID = (int) 0x0104;      
83         // Zigbee Home Automation Profile Generic Device IDs
84         private static final int ZCL_HA_DEVICEID_LEVEL_CONTROL_SWITCH            = (int) 0x0001; 
85         private static final int ZCL_HA_DEVICEID_ON_OFF_OUTPUT                   = (int) 0x0002; 
86         private static final int ZCL_HA_DEVICEID_LEVEL_CONTROLLABLE_OUTPUT       = (int) 0x0003; 
87         private static final int ZCL_HA_DEVICEID_SCENE_SELECTOR                  = (int) 0x0004; 
88         private static final int ZCL_HA_DEVICEID_CONFIGURATIOPN_TOOL             = (int) 0x0005; 
89         private static final int ZCL_HA_DEVICEID_REMOTE_CONTROL                  = (int) 0x0006; 
90         private static final int ZCL_HA_DEVICEID_COMBINED_INETRFACE              = (int) 0x0007; 
91         private static final int ZCL_HA_DEVICEID_RANGE_EXTENDER                  = (int) 0x0008; 
92         private static final int ZCL_HA_DEVICEID_MAINS_POWER_OUTLET              = (int) 0x0009;        
93         // Zigbee Home Automation Profile  Lighting Device IDs
94         private static final int ZCL_HA_DEVICEID_ON_OFF_SWITCH          = (int) 0x0000; 
95         private static final int ZCL_HA_DEVICEID_ON_OFF_LIGHT           = (int) 0x0100;
96         private static final int ZCL_HA_DEVICEID_DIMMABLE_LIGHT         = (int) 0x0101;
97         private static final int ZCL_HA_DEVICEID_COLORED_DIMMABLE_LIGHT = (int) 0x0102;
98         private static final int ZCL_HA_DEVICEID_ON_OFF_LIGHT_SWITCH    = (int) 0x0103;
99         private static final int ZCL_HA_DEVICEID_DIMMER_SWITCH          = (int) 0x0104;
100         private static final int ZCL_HA_DEVICEID_COLOR_DIMMER_SWITCH    = (int) 0x0105;
101         private static final int ZCL_HA_DEVICEID_LIGHT_SENSOR           = (int) 0x0106;
102         private static final int ZCL_HA_DEVICEID_OCCUPANCY_SENSOR       = (int) 0x0107; 
103         
104         // Zigbee Light Link Profile Identification
105         private static final int  ZLL_PROFILE_ID                        = (int) 0xc05e;
106         // ZLL Basic Lighting Device IDs
107         private static final int ZLL_DEVICEID_ON_OFF_LIGHT              = (int) 0x0000;
108         private static final int ZLL_DEVICEID_ON_OFF_PLUG_IN_UNIT       = (int) 0x0010;
109         private static final int ZLL_DEVICEID_DIMMABLE_LIGHT            = (int) 0x0100;
110         private static final int ZLL_DEVICEID_DIMMABLE_PLUG_IN_UNIT     = (int) 0x0110;
111         // ZLL Color Lighting Device IDs
112         private static final int ZLL_DEVICEID_COLOR_LIGHT                = (int) 0x0200;
113         private static final int ZLL_DEVICEID_EXTENDED_COLOR_LIGHT       = (int) 0x0210;
114         private static final int ZLL_DEVICEID_COLOR_TEMPERATURE_LIGHT    = (int) 0x0220;
115         // ZLL Lighting Remotes Device IDs
116         private static final int ZLL_DEVICEID_COLOR_CONTORLLER           = (int) 0x0800;
117         private static final int ZLL_DEVICEID_COLOR_SCENE_CONTROLLER     = (int) 0x0810;
118         private static final int ZLL_DEVICEID_NON_COLOR_CONTORLLER       = (int) 0x0820;
119         private static final int ZLL_DEVICEID_NON_COLOR_SCENE_CONTROLLER = (int) 0x0830;
120         private static final int ZLL_DEVICEID_CONTROL_BRIDGE             = (int) 0x0840;
121         private static final int ZLL_DEVICEID_ON_OFF_SENSOR              = (int) 0x0850;        
122         
123         ZigbeeDevice() {}
124         
125         ZigbeeDevice(int _ProfileId, int _DeviceId, int _NetworkAddr, char _EndPoint,  byte _ieee[], String _deviceName, int LightDeviceIdx)
126         {
127                 ProfileId = _ProfileId;
128                 DeviceId = _DeviceId;
129                 EndPoint = _EndPoint;
130                 NetworkAddr = _NetworkAddr;
131                 Ieee = _ieee;
132                 
133                 if( ProfileId == ZCL_HA_PROFILE_ID)
134                 {                       
135                         // Would be better to get the device name from the device!
136                         switch (DeviceId)
137                         {
138                         
139                         case (ZCL_HA_DEVICEID_ON_OFF_SWITCH):   
140                         {
141                                 Type = "Switch";
142                                 
143                                 //Assume for now it is a Cleode switch, later pass the vindor ID down so we can check
144                         String SwitchButtonFunction;
145                         
146                         if(_EndPoint == 1)
147                         {
148                                 SwitchButtonFunction = " EP:1(DOWN)";
149                         }
150                         else if(_EndPoint == 2)
151                         {
152                                 SwitchButtonFunction = " EP:2(LEFT)";
153                         }
154                         else if(_EndPoint == 3)
155                         {
156                                 SwitchButtonFunction = " EP:3(UP)";
157                         }
158                         else if(_EndPoint == 4)
159                         {
160                                 SwitchButtonFunction = " EP:4(RIGHT)";
161                         }            
162                         else if(_EndPoint == 5)
163                         {
164                                 SwitchButtonFunction = " EP:5(CENTER)";
165                         }
166                         else
167                         {
168                                 SwitchButtonFunction = " EP:" + Integer.toHexString(EndPoint);
169                         }
170                         
171                         Name = "Switch" + SwitchButtonFunction  /*+ " IEEE:" + Integer.toHexString((int)((long)ZigbeeAssistant.nwrkToIEEE.get((short)_NetworkAddr)))*/;
172                         hasOutSwitch = true;
173                         break;
174                         }
175                 
176                         case (ZCL_HA_DEVICEID_MAINS_POWER_OUTLET):      
177                         {
178                                 Type = "Mains Outlet"; 
179                                 Name= (LightDeviceIdx+1) + ": Smart Plug";      
180                                 hasSwitchable = true;                           
181                                 break;
182                         }
183                         case (ZCL_HA_DEVICEID_DIMMABLE_LIGHT):  
184                         {
185                                 Type = "Dimmable Light"; 
186                                 Name= (LightDeviceIdx+1) + ": Light";
187                                 hasSwitchable = true;
188                                 hasDimmable  = true;
189                                 break;
190                         }
191                         case (ZCL_HA_DEVICEID_COLORED_DIMMABLE_LIGHT):  
192                         {
193                                 Type = "Colour Dimmable Light"; 
194                                 Name= (LightDeviceIdx+1) + ": ZLight";  
195                                 hasSwitchable = true;
196                                 hasDimmable  = true;
197                                 hasColourable = true;                                   
198                                 break;
199                         }
200                         case (ZCL_HA_DEVICEID_COLOR_DIMMER_SWITCH):     
201                         {
202                                 Type = "Colour Dimmer switch"; 
203                                 Name="Colour Dimmer switch";
204                                 hasOutSwitch= true;
205                                 hasOutLeveL = true;
206                                 hasOutColor = true;                             
207                                 break;  
208                         }
209                         case (ZCL_HA_DEVICEID_OCCUPANCY_SENSOR):        
210                         {
211                                 Type = "Occumpancy Sensor"; 
212                                 Name="Occumpancy Sensor";
213                                 hasOutSwitch = true;
214                                 break;
215                         }
216                         //some manufacturers use HA profile ID and ZLL device ID's... 
217                         case (ZLL_DEVICEID_EXTENDED_COLOR_LIGHT):       
218                         {
219                                 Type = "ZLL Light"; 
220                                 Name= (LightDeviceIdx+1) + ": ZLL Light";               
221                                 break;
222                         }
223                         default:        Type = "Unknown Device"; Name="Unknown";        
224                         }                       
225                 }               
226                 else if( ProfileId == ZLL_PROFILE_ID)
227                 {
228                         switch (DeviceId)
229                         {
230                         case (ZLL_DEVICEID_ON_OFF_LIGHT):       
231                         {
232                                 //An On off Light
233                                 Type = "On/Off Light"; 
234                                 //set a default name that can be renamed later and stored in the hub
235                                 Name= (LightDeviceIdx+1) + ": On/Off Light";    
236                                 hasSwitchable = true;
237                                 hasDimmable  = false;
238                                 hasColourable = false;                  
239                                 break;
240                         }
241                         case (ZLL_DEVICEID_ON_OFF_PLUG_IN_UNIT):        
242                         {
243                                 //An On Off Plug-In unit
244                                 Type = "On/Off Plug"; 
245                                 //set a default name that can be renamed later and stored in the hub
246                                 Name= (LightDeviceIdx+1) + ": On/Off Plug";     
247                                 hasSwitchable = true;                   
248                                 break;                  
249                         }               
250                         case (ZLL_DEVICEID_DIMMABLE_LIGHT):     
251                         {
252                                 //A Dimmable Light
253                                 Type = "Dimmable Light"; 
254                                 //set a default name that can be renamed later and stored in the hub
255                                 Name= (LightDeviceIdx+1) + ": Dimmable Light";  
256                                 hasSwitchable = true;
257                                 hasDimmable  = true;
258                                 break;                  
259                         }
260                         case (0x0110):  
261                         {
262                                 //A Dimmable Plug-In unit
263                                 Type = "Dimmable Plug"; 
264                                 //set a default name that can be renamed later and stored in the hub
265                                 Name= (LightDeviceIdx+1) + ": Dimmable Plug";   
266                                 hasSwitchable = true;
267                                 hasDimmable  = true;
268                                 break;                  
269                         }               
270                         case (0x0200):  
271                         {
272                                 //A Color Light
273                                 Type = "Color Light"; 
274                                 //set a default name that can be renamed later and stored in the hub
275                                 Name= (LightDeviceIdx+1) + ": Color Light";     
276                                 hasSwitchable = true;
277                                 hasDimmable  = true;
278                                 hasColourable = true;   
279                                 break;                  
280                         }
281                         case (0x0210):  
282                         {
283                                 //An Extended Color Light
284                                 Type = "Extended Color Light"; 
285                                 //set a default name that can be renamed later and stored in the hub
286                                 Name= (LightDeviceIdx+1) + ": Extended Color Light";    
287                                 hasSwitchable = true;
288                                 hasDimmable  = true;
289                                 hasColourable = true;   
290                                 break;                  
291                         }
292                         case (0x0220):  
293                         {
294                                 //An Color Temp Light
295                                 Type = "Color Temp Light"; 
296                                 //set a default name that can be renamed later and stored in the hub
297                                 Name= (LightDeviceIdx+1) + ": Color Temp Light";        
298                                 hasSwitchable = true;
299                                 hasDimmable  = true;
300                                 hasColourable = true;   
301                                 break;                  
302                         }               
303                         case (0x0800):  
304                         {
305                                 //A Color Controller
306                                 Type = "Color Controller"; 
307                                 //set a default name that can be renamed later and stored in the hub
308                                 Name= (LightDeviceIdx+1) + ": Color Controller";        
309                                 hasOutSwitch = true;
310                                 hasOutLeveL  = true;
311                                 hasOutColor = true;     
312                                 break;                  
313                         }
314                         case (0x0810):  
315                         {
316                                 //A Color Scene Controller
317                                 Type = "Color Scene Controller"; 
318                                 //set a default name that can be renamed later and stored in the hub
319                                 Name= (LightDeviceIdx+1) + ": Color Scene Controller";  
320                                 hasOutSwitch = true;
321                                 hasOutLeveL  = true;
322                                 hasOutColor = true;     
323                                 hasOutScene = true;
324                                 hasOutGroup = true; 
325                                 break;                  
326                         }               
327                         case (0x0820):  
328                         {
329                                 //A Dimmable Scene Controller
330                                 Type = "Dimmable Scene Controller"; 
331                                 //set a default name that can be renamed later and stored in the hub
332                                 Name= (LightDeviceIdx+1) + ": Dimmable Scene Controller";       
333                                 hasOutSwitch = true;
334                                 hasOutLeveL  = true;
335                                 hasOutScene = true;
336                                 hasOutGroup = true; 
337                                 break;                  
338                         }
339                         case (0x0830):  
340                         {
341                                 //A Dimmable Controller
342                                 Type = "Dimmable Controller"; 
343                                 //set a default name that can be renamed later and stored in the hub
344                                 Name= (LightDeviceIdx+1) + ": Dimmable Controller";     
345                                 hasOutSwitch = true;
346                                 hasOutLeveL  = true;
347                                 break;                  
348                         }
349                         case (0x0840):  
350                         {
351                                 //A Control Bridge
352                                 Type = "Control Bridge"; 
353                                 //set a default name that can be renamed later and stored in the hub
354                                 Name= (LightDeviceIdx+1) + ": Control Bridge";  
355                                 hasOutSwitch = true;
356                                 hasOutLeveL  = true;
357                                 hasOutColor = true;     
358                                 hasOutScene = true;
359                                 hasOutGroup = true; 
360                                 break;                  
361                         }       
362                         case (0x0850):  
363                         {
364                                 //An On/Off Sensor
365                                 Type = "On/Off Sensor"; 
366                                 //set a default name that can be renamed later and stored in the hub
367                                 Name= (LightDeviceIdx+1) + ": On/Off Sensor";   
368                                 hasOutSwitch = true;
369                                 break;                  
370                         }
371                         
372                         default:        Type = "Unknown Device"; Name="Unknown";        
373                         }
374                 }
375                 
376                 if(!_deviceName.isEmpty())
377                 {
378                         Name = _deviceName;
379                 }
380         }
381         
382         public void updateDevice(ZigbeeDevice device)
383         {       
384                 Name = device.Name;
385                 Type = device.Type;
386                 ProfileId = device.ProfileId;
387                 DeviceId = device.DeviceId;
388                 NetworkAddr = device.NetworkAddr;
389                 hasColourable = device.hasColourable;
390                 hasDimmable = device.hasDimmable;
391                 hasSwitchable = device.hasSwitchable;
392                 hasOutLeveL = device.hasOutLeveL;
393                 hasOutScene = device.hasOutScene;
394                 hasOutGroup = device.hasOutGroup;
395         }       
396         
397         public void addCluster(boolean in, int ClusterId)
398         {
399                 if (in)
400                 {
401                         switch (ClusterId)
402                         {
403                         case (0x0006): hasSwitchable = true;                    break;
404                         case (0x0008): hasDimmable = true;                      break;
405                         case (0x0300): hasColourable = true;                    break;
406                         }
407                 } else
408                 {
409                         switch (ClusterId)
410                         {
411                         case (0x0006): hasOutSwitch = true;                     break;          
412                         case (0x0008): hasOutLeveL = true;                      break;  
413                         case (0x0300): hasOutColor = true;                      break;          
414                         case (0x0005): hasOutScene = true;                      break;  
415                         case (0x0004): hasOutGroup = true;                      break;                          
416                         }                       
417                 }
418         }
420         public byte getCurrentState() {
421                 return this.currentState;
422         }
424         public void setCurrentState(byte currentState) {
425                 this.currentState = currentState;
426                 this.currentColorAttrsUpdated |= CURRENT_STATE_UPDATED;
427         }
429         public byte getCurrentLevel() {
430                 return this.currentLevel;
431         }
433         public void setCurrentLevel(byte currentLevel) {
434                 this.currentLevel = currentLevel;
435                 this.currentColorAttrsUpdated |= CURRENT_LEVEL_UPDATED;
436         }
438         public byte getCurrentHue() {
439                 return this.currentHue;
440         }
442         public void setCurrentHue(byte currentHue) {
443                 this.currentHue = currentHue;
444                 this.currentColorAttrsUpdated |= CURRENT_HUE_UPDATED;
445         }
447         public byte getCurrentSat() {
448                 return this.currentSat;
449         }
451         public void setCurrentSat(byte currentSat) {
452                 this.currentSat = currentSat;
453                 this.currentColorAttrsUpdated |= CURRENT_SAT_UPDATED;
454         }       
455         
456         public void clearCurrentStateUpdated() {
457                 currentColorAttrsUpdated &= ~CURRENT_STATE_UPDATED;
458         }               
459         
460         public void clearCurrentLevelUpdated() {
461                 currentColorAttrsUpdated &= ~CURRENT_LEVEL_UPDATED;
462         }
463         
464         public void clearCurrentHueUpdated() {
465                 currentColorAttrsUpdated &= ~CURRENT_HUE_UPDATED;
466         }
467         
468         public void clearCurrentSatUpdated() {
469                 currentColorAttrsUpdated &= ~CURRENT_SAT_UPDATED;
470         }       
471         
472         public boolean getCurrentStateUpdated() {
473                 boolean rtn = true;
474                 
475                 if(hasSwitchable == false)
476                 {
477                         rtn = true;                     
478                 }               
479                 else if ((currentColorAttrsUpdated & CURRENT_STATE_UPDATED) == 0)
480                 {
481                         rtn = false;
482                 }
483                                 
484                 return rtn;
485         }
486         
487         public boolean getCurrentLevelUpdated() {
488                 boolean rtn = true;
489                 
490                 if(hasDimmable == false)
491                 {
492                         rtn = true;                     
493                 }               
494                 else if ((currentColorAttrsUpdated & CURRENT_LEVEL_UPDATED) == 0)
495                 {
496                         rtn = false;
497                 }               
498                 
499                 return rtn;
500         }
501         
502         public boolean getCurrentHueUpdated() {
503                 boolean rtn = true;
504                 
505                 if(this.hasColourable == false)
506                 {
507                         rtn = true;                     
508                 }                       
509                 else if ((currentColorAttrsUpdated & CURRENT_HUE_UPDATED) == 0)
510                 {
511                         rtn = false;
512                 }       
513                 
514                 return rtn;
515         }
516         
517         public boolean getCurrentSatUpdated() {
518                 boolean rtn = true;
519                 
520                 if(this.hasColourable == false)
521                 {
522                         rtn = true;                     
523                 }               
524                 else if ((currentColorAttrsUpdated & CURRENT_SAT_UPDATED) == 0)
525                 {
526                         rtn = false;
527                 }
528                 
529                 return rtn;
530         }