[iot-gateway/lighting-gateway.git] / client / AndroidClients / LightingController / src / com / lightingcontroller / zllMain.java
diff --git a/client/AndroidClients/LightingController/src/com/lightingcontroller/zllMain.java b/client/AndroidClients/LightingController/src/com/lightingcontroller/zllMain.java
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
+import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.lightingcontroller.Zigbee.ZigbeeAssistant;
import com.lightingcontroller.Zigbee.ZigbeeDevice;
import com.lightingcontroller.Zigbee.ZigbeeGroup;
+import com.lightingcontroller.Zigbee.ZigbeeNotification;
import com.lightingcontroller.ColourPicker;
public class zllMain extends Activity {
public static ColourPicker colourPicker;
private static ZigbeeGroup currGroup;
- private static ZigbeeDevice currDevice;
+ private static ZigbeeDevice currDevice = null;
private Spinner deviceSpinner;
- private ArrayAdapter<String> deviceSpinnerAdapter;
+ private static ArrayAdapter<String> deviceSpinnerAdapter;
private int currentDeviceSpinnerSelection=0;
ProgressDialog bar;
addItemsOnDeviceSpinner();
addListenerOnDeviceSpinnerItemSelection();
+
+ ZigbeeNotification.init(this);
}
// add items into spinner dynamically
{
deviceSpinnerAdapter.add("group: " + groupList.get(i).getGroupName());
}
+
+ if( (currDevice == null) && (devList.size() > 0) )
+ {
+ currDevice = devList.get(0);
+ }
}
public void addListenerOnDeviceSpinnerItemSelection() {
ZigbeeAssistant.getDeviceLevel(currDevice);
ZigbeeAssistant.getDeviceHue(currDevice);
ZigbeeAssistant.getDeviceSat(currDevice);
- //new waitRspTask().execute("Device Select");
+ new waitRspTask().execute("Device Select");
currGroup = null;
}
if( !currDevice.getCurrentStateUpdated() )
{
ZigbeeAssistant.getDeviceState(currDevice);
+ try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException e) {e.printStackTrace();}
}
if( !currDevice.getCurrentLevelUpdated() )
{
ZigbeeAssistant.getDeviceLevel(currDevice);
+ try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException e) {e.printStackTrace();}
}
if( !currDevice.getCurrentHueUpdated() )
{
ZigbeeAssistant.getDeviceHue(currDevice);
+ try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException e) {e.printStackTrace();}
}
if( !currDevice.getCurrentSatUpdated() )
{
ZigbeeAssistant.getDeviceSat(currDevice);
+ try { TimeUnit.MILLISECONDS.sleep(10); } catch (InterruptedException e) {e.printStackTrace();}
}
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
+ case R.id.optionMenuAddDevice:
+ {
+ new addDeviceTask().execute("Add Device");
+ }
+ break;
case R.id.optionMenuGroups:
startActivity(new Intent(zllMain.this, groupSelect.class));
break;
alert = builder.create();
alert.show();
}
- break;
+ break;
}
return true;
}
public void deviceChangeNameButton(View view)
+ {
+ deviceChangeName();
+ }
+
+ public void deviceChangeName()
{
String Title = "Set Name";
String Msg = "Please Enter the name of the device";
ZigbeeAssistant.setDeviceLevel(currGroup, (char) level);
}
}
+
+ class addDeviceTask extends AsyncTask<String , Integer, Void>
+ {
+ private boolean rspSuccess;
+ int numCurrentDevices;
+ int timeoutCnt;
+ String param;
+ @Override
+ protected void onPreExecute()
+ {
+ bar = new ProgressDialog(zllMain.this);
+ bar.setMessage("Searching For New Device");
+ bar.setIndeterminate(true);
+ bar.show();
+
+ //count number of devices
+ numCurrentDevices = ZigbeeAssistant.getDevices().size();
+
+ //open network
+ ZigbeeAssistant.openNetwork((byte)60);
+
+ }
+ @Override
+ protected Void doInBackground(String... params)
+ {
+ param = params[0];
+
+ //30s time out
+ for(timeoutCnt = 0; timeoutCnt < 60; timeoutCnt++)
+ {
+ try { TimeUnit.MILLISECONDS.sleep(500); } catch (InterruptedException e) {e.printStackTrace();}
+
+ if( ZigbeeAssistant.getDevices().size() > numCurrentDevices)
+ {
+ rspSuccess = true;
+ return null;
+ }
+ }
+
+ rspSuccess = false;
+ return null;
+ }
+ @Override
+ protected void onPostExecute(Void result)
+ {
+ bar.dismiss();
+
+ if (rspSuccess == false)
+ {
+ AlertDialog show = new AlertDialog.Builder(zllMain.this)
+ .setTitle(param)
+ .setMessage("No new devices found\n")
+ .setPositiveButton("OK",
+ new DialogInterface.OnClickListener()
+ {
+ public void onClick(DialogInterface dialoginterface,int i){
+ }
+ })
+ .show();
+ }
+ else
+ {
+ //Let notification change the name
+ }
+ }
+ }
+
+ public static void setCurrentDevice(ZigbeeDevice device)
+ {
+ currDevice = device;
+ deviceSpinnerAdapter.clear();
+ List<ZigbeeDevice> tList = ZigbeeAssistant.getDevices();
+ for (int j = 0 ; j < tList.size() ; j++)
+ {
+ if (tList.get(j).hasColourable || tList.get(j).hasSwitchable || tList.get(j).hasDimmable)
+ {
+ deviceSpinnerAdapter.add(tList.get(j).Name);
+ }
+ }
+ }
}
\ No newline at end of file