]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - iot-gateway/lighting-gateway.git/blob - client/AndroidClients/LightingController/src/com/lightingcontroller/LightingController.java
add support for control bridge FW
[iot-gateway/lighting-gateway.git] / client / AndroidClients / LightingController / src / com / lightingcontroller / LightingController.java
1 /**************************************************************************************************
2   Filename:       LightingController.java
3   Revised:        $$
4   Revision:       $$
6   Description:    ZLL UI
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;
43 import java.util.concurrent.TimeUnit;
45 import android.app.Activity;
46 import android.app.ProgressDialog;
47 import android.os.AsyncTask;
48 import android.os.Bundle;
49 import android.widget.EditText;
50 import android.content.DialogInterface;
51 import android.content.Intent;
52 import android.content.SharedPreferences;
53 import android.app.AlertDialog;
55 import com.lightingcontroller.Zigbee.ZigbeeAssistant;
56 import com.lightingcontroller.Zigbee.ZigbeeSrpcClient;
58 public class LightingController extends Activity{
60     private static final String PREFS_NAME = "MyPrefsFile";
61     public int selectedDevice = 0;
62     ProgressDialog bar;
63     
64     public void onCreate(Bundle savedInstanceState) {
65         super.onCreate(savedInstanceState); 
66         setContentView(R.layout.main);
67        
68         new ZigbeeAssistant();                
69                 
70         new waitRspTask().execute("Connecting to gateway");
71 /*       
72         ///Open SRPC
73         if(connectSrcpGateway())
74         {               
75           ZigbeeSrpcClient.getDevices();
76           ZigbeeSrpcClient.discoverGroups();
77           ZigbeeSrpcClient.discoverScenes();
78           //wait for responses
79           try { TimeUnit.MILLISECONDS.sleep(200); } catch (InterruptedException e) {e.printStackTrace();}  
80           
81           startActivity(new Intent(LightingController.this, zllMain.class));
82           finish();
83         }
84 */        
85     }
86                 
87     
88     private boolean connectSrcpGateway()
89     { 
90         String gatewayIpAddr;
92         SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
93         gatewayIpAddr = settings.getString("gatewayIpAddr", null);    
94         
95                 if(gatewayIpAddr != null)
96                 {
97                         ZigbeeSrpcClient.setGatewayIp(gatewayIpAddr);                   
98                 }
99                 else
100                 {
101                         //get IPAddr from user
102                         setSrcpGatewayIpAddress();
103                 }
104                 
105                 if( ZigbeeSrpcClient.clientConnect() == 0)
106                 {
107                         SharedPreferences.Editor editor = settings.edit();      
108                         editor.putString("gatewayIpAddr", gatewayIpAddr);
109                 }
110                 else
111                 {
112                         //get IPAddr from user          
113                         setSrcpGatewayIpAddress();
114                         return false;
115                 }
116                 
117                 return true;
118     } 
119     
120     private void setSrcpGatewayIpAddress()
121     {   
122         final EditText t = new EditText(this);
123         t.setText("192.168.1.111");
125         new AlertDialog.Builder(this)
126                 .setTitle("Gateway Address")
127                 .setMessage("Please Enter the HA Gateway IP Address")
128                 .setView(t)
129                 .setPositiveButton("OK",             
130                 new DialogInterface.OnClickListener()
131                 {                       
132                         public void onClick(DialogInterface dialoginterface,int i){     
133                         String gatewayIpAddr;
134                                 gatewayIpAddr = t.getText().toString();
136                                 //retrieve path setting
137                                 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
138                                 settings.getString("gatewayIpAddr", gatewayIpAddr);     
139                                 
140                                 if(gatewayIpAddr != null)
141                                 {
142                                 if(connectSrcpGateway())
143                                 {                               
144                                   ZigbeeSrpcClient.getDevices();
145                                   ZigbeeSrpcClient.discoverGroups();
146                                   ZigbeeSrpcClient.discoverScenes();
147                                   //wait for responses
148                                   try { TimeUnit.MILLISECONDS.sleep(500); } catch (InterruptedException e) {e.printStackTrace();}  
149         
150                                   startActivity(new Intent(LightingController.this, zllMain.class));                            
151                                 }
152                                 }
153                         }               
154                 })
155                 .show();                        
156     } 
157     
158     
159     class waitRspTask extends AsyncTask<String , Integer, Void>
160     {
161         private boolean rspSuccess = false;
162         String param;
163         String gatewayIpAddr;
164         
165         @Override
166         protected void onPreExecute()
167         {
168             bar = new ProgressDialog(LightingController.this);
169             bar.setMessage("Finding Gateway");
170             bar.setIndeterminate(true);
171             bar.show();
172         } 
173         @Override
174         protected Void doInBackground(String... params) 
175         {
176                 param = params[0];
177                 
178                         //retrieve path setting
179                         SharedPreferences preferences = getSharedPreferences(PREFS_NAME, 0);
180                 String gatewayIpAddr = preferences.getString("gatewayIpAddr", null);
181                         
182                         if(gatewayIpAddr != null)
183                         {
184                                 ZigbeeSrpcClient.setGatewayIp(gatewayIpAddr);                                   
185                                 if(ZigbeeSrpcClient.clientConnect() == 0)
186                                 {
187                                 ZigbeeSrpcClient.getDevices();
188                                 ZigbeeSrpcClient.discoverGroups();
189                                 ZigbeeSrpcClient.discoverScenes();
190                                 //wait for responses
191                                 try { TimeUnit.MILLISECONDS.sleep(200); } catch (InterruptedException e) {e.printStackTrace();}  
192                                   
193                                 startActivity(new Intent(LightingController.this, zllMain.class));
194                                   
195                                         rspSuccess = true;
196                                 }
197                         }
198                         
199                         return null;
200         }
201         @Override
202         protected void onPostExecute(Void result) 
203         {
204             bar.dismiss();
205             
206             if (rspSuccess == false)
207                 {
208                 final EditText t = new EditText(LightingController.this);
209                 t.setText("192.168.1.111");
211                 new AlertDialog.Builder(LightingController.this)
212                         .setTitle("Gateway Address")
213                         .setMessage("Please Enter the HA Gateway IP Address")
214                         .setView(t)
215                         .setPositiveButton("OK",             
216                         new DialogInterface.OnClickListener()
217                         {                       
218                                 public void onClick(DialogInterface dialoginterface,int i){     
219                                 String gatewayIpAddr;
220                                 gatewayIpAddr = t.getText().toString();
222                                         //Store path setting
223                                 SharedPreferences preferences = getSharedPreferences(PREFS_NAME, 0);
224                                 SharedPreferences.Editor editor = preferences.edit();
225                                 editor.putString("gatewayIpAddr", gatewayIpAddr); // value to store
226                                 editor.commit();                                                                
227                                 
228                                         new waitRspTask().execute("Connecting to gateway");
229                                 }               
230                         })
231                         .setNegativeButton("Cancel", null)
232                         .show();                        
233                 }
234             else
235             {
236                 finish();
237             }
238             
239         }
240     }