From 080ac78b22a1434572cf16c1b7839c9322dbb57b Mon Sep 17 00:00:00 2001 From: daniel Date: Sun, 1 May 2016 22:23:29 +0200 Subject: [PATCH] First commit --- .idea/.name | 1 + .idea/compiler.xml | 22 ++ .idea/copyright/profiles_settings.xml | 3 + .idea/gradle.xml | 18 ++ .idea/misc.xml | 62 ++++ .idea/modules.xml | 9 + .idea/runConfigurations.xml | 12 + .idea/vcs.xml | 6 + app/.gitignore | 1 + app/build.gradle | 39 +++ app/proguard-rules.pro | 17 ++ .../moodlightdimmer/ApplicationTest.java | 14 + app/src/main/AndroidManifest.xml | 32 ++ .../moodlightdimmer/DimmMainActivity.java | 288 ++++++++++++++++++ .../main/res/layout/activity_dimm_main.xml | 79 +++++ app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3418 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2206 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4842 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7718 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10486 bytes app/src/main/res/values-w820dp/dimens.xml | 6 + app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/dimens.xml | 5 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 11 + .../moodlightdimmer/ExampleUnitTest.java | 15 + build.gradle | 25 ++ gradle.properties | 18 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 53637 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 160 ++++++++++ gradlew.bat | 90 ++++++ settings.gradle | 1 + 33 files changed, 949 insertions(+) create mode 100644 .idea/.name create mode 100644 .idea/compiler.xml create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 .idea/vcs.xml create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/androidTest/java/com/example/daniel/moodlightdimmer/ApplicationTest.java create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/example/daniel/moodlightdimmer/DimmMainActivity.java create mode 100644 app/src/main/res/layout/activity_dimm_main.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/values-w820dp/dimens.xml create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/dimens.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/test/java/com/example/daniel/moodlightdimmer/ExampleUnitTest.java create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..66e1032 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +Moodlightdimmer \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..1bbc21d --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..c60d22d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.7 + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..b3e48f9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..f836d53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..8becbcb --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,39 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.3" + + defaultConfig { + applicationId "com.example.daniel.moodlightdimmer" + minSdkVersion 15 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:23.2.1' +} + +repositories { + maven { + url "https://repo.eclipse.org/content/repositories/paho-releases/" + } +} + + +dependencies { + compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') { + exclude module: 'support-v4' + } +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..bfc1962 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /home/daniel/Android/Sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/androidTest/java/com/example/daniel/moodlightdimmer/ApplicationTest.java b/app/src/androidTest/java/com/example/daniel/moodlightdimmer/ApplicationTest.java new file mode 100644 index 0000000..886ce0a --- /dev/null +++ b/app/src/androidTest/java/com/example/daniel/moodlightdimmer/ApplicationTest.java @@ -0,0 +1,14 @@ +package com.example.daniel.moodlightdimmer; + +import android.app.Application; +import android.test.ApplicationTestCase; + + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..dc3c3dc --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/example/daniel/moodlightdimmer/DimmMainActivity.java b/app/src/main/java/com/example/daniel/moodlightdimmer/DimmMainActivity.java new file mode 100644 index 0000000..678efaa --- /dev/null +++ b/app/src/main/java/com/example/daniel/moodlightdimmer/DimmMainActivity.java @@ -0,0 +1,288 @@ +package com.example.daniel.moodlightdimmer; + + +import android.content.Context; +import android.net.wifi.WifiInfo; +import android.net.wifi.WifiManager; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.SeekBar; +import android.widget.Spinner; +import android.widget.Toast; + +import org.eclipse.paho.android.service.MqttAndroidClient; +import org.eclipse.paho.client.mqttv3.IMqttActionListener; +import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; +import org.eclipse.paho.client.mqttv3.IMqttToken; +import org.eclipse.paho.client.mqttv3.MqttCallback; +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.eclipse.paho.client.mqttv3.MqttSecurityException; + +import java.net.NetworkInterface; +import java.util.Collections; +import java.util.List; + + +public class DimmMainActivity extends AppCompatActivity { + + private SeekBar red, green, blue; + private Button buttonSet, buttonReconnect; + private Integer color; + private MqttAndroidClient client; + private String device = "sender"; + private String publisher_topic; + private String broker = "tcp://raspi2:1883"; + private Spinner spinnerlights; + private CheckBox checkBoxInstantSet; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_dimm_main); + + red = (SeekBar) findViewById(R.id.seekBarRed); + green = (SeekBar) findViewById(R.id.seekBarGreen); + blue = (SeekBar) findViewById(R.id.seekBarBlue); + buttonSet = (Button) findViewById(R.id.buttonSet); + buttonReconnect = (Button) findViewById(R.id.buttonReconnect); + checkBoxInstantSet = (CheckBox) findViewById(R.id.checkBoxInstantSet); + + color = 0xFF000000; + spinnerlights = (Spinner) findViewById(R.id.spinnerLights); + spinnerlights.setAdapter(new ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, new String[] {"All", "Kitchen", "Automat", "South East","South West","North East","North West"} )); + + String macaddress = getWifiMacAddress(); + + publisher_topic = "kitchen/switch/android/" + macaddress; + + red.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + if (fromUser) SetButtonColor(checkBoxInstantSet.isChecked()); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + + } + }); + + green.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + if(fromUser) SetButtonColor(checkBoxInstantSet.isChecked()); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + + } + }); + + blue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + if (fromUser) SetButtonColor(checkBoxInstantSet.isChecked()); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + + } + }); + + + ConnectMQTT(); // Init Connection + + + buttonSet.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + if (client.isConnected()) { + SetButtonColor(true); + //SendCommand(publisher_topic, spinnerlights.getSelectedItem().toString() + Integer.toHexString(color)); + } + } + }); + + buttonReconnect.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + if(!client.isConnected()) ConnectMQTT(); + } + }); + + + + + } + + private void SetButtonColor(Boolean sendCommand) + { + color = (0xFF<<24) | ((int)(red.getProgress()*2.55)<<16) | ((int)(green.getProgress()*2.55)<<8) | ((int)(blue.getProgress()*2.55)); + buttonSet.setBackgroundColor(color); + if(client.isConnected() && device == "sender") + { + //String topic = publisher_topic;sf + //String payload = Integer.toHexString(color); + + if(sendCommand) SendCommand(spinnerlights.getSelectedItem().toString() + "; " +Integer.toHexString(color).substring(2), publisher_topic); + } + else if(!client.isConnected() && device == "sender") + { + Toast.makeText(this.getApplicationContext(), "Not Connected", Toast.LENGTH_SHORT); + } + return; + } + + private void ConnectMQTT() + { + String clientId = MqttClient.generateClientId(); + final MqttAndroidClient client = + new MqttAndroidClient(this.getApplicationContext(), broker, + clientId); + try { + IMqttToken token = client.connect(); + token.setActionCallback(new IMqttActionListener() { + @Override + public void onSuccess(IMqttToken asyncActionToken) { + // We are connected + // if connection was succesful, subscripe to topic if app is a receiver + Log.d("MQTT Connection", "onSuccess"); + if (device == "receiver") { + String topic = publisher_topic; + + int qos = 1; + try { + IMqttToken subToken = client.subscribe(topic, qos); + subToken.setActionCallback(new IMqttActionListener() { + @Override + public void onSuccess(IMqttToken asyncActionToken) { + Log.d("MQTT Subscription", "Successful"); + } + + @Override + public void onFailure(IMqttToken asyncActionToken, Throwable exception) { + + } + }); + + } catch (MqttSecurityException e) { + e.printStackTrace(); + } catch (MqttException e) { + e.printStackTrace(); + + } + } + } + @Override + public void onFailure(IMqttToken asyncActionToken, Throwable exception) { + // Something went wrong e.g. connection timeout or firewall problems + Log.d("FragmentActivity", "onFailure"); + } + }); + } catch (MqttException e) { + e.printStackTrace(); + } + + this.client = client; + + //Callback for message arrived and connection lost + this.client.setCallback(new MqttCallback() { + @Override + public void connectionLost(Throwable cause) { + Log.d("Connection", "Lost"); + } + + @Override + public void messageArrived(String topic, MqttMessage message) throws Exception { + try { + if(device == "receiver") { + String msg = new String(message.getPayload()); + msg = msg.substring(2); + color = 0xFF000000 | Integer.parseInt(msg, 16); + red.setProgress(((color & 0xFF0000) >> 16) * 100 / 255); + green.setProgress(((color & 0xFF00) >> 8) * 100 / 255); + blue.setProgress(((color & 0xFF)) * 100 / 255); + SetButtonColor(false); + } + } + catch (Exception e) + { + Log.d("MesageArrived", "Exeption"); + } + } + + @Override + public void deliveryComplete(IMqttDeliveryToken token) { + + } + }); + + + } + + private void SendCommand(String payload, String topic) + { + //TODO Send which lights should be set + try { + byte[] encodedPayload = payload.getBytes(); + MqttMessage msg = new MqttMessage(encodedPayload); + client.publish(topic, msg); + } + catch (MqttException e) + { + e.printStackTrace(); + } + } + + public static String getWifiMacAddress() { + try { + String interfaceName = "wlan0"; + List interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); + for (NetworkInterface intf : interfaces) { + if (!intf.getName().equalsIgnoreCase(interfaceName)){ + continue; + } + + byte[] mac = intf.getHardwareAddress(); + if (mac==null){ + return ""; + } + + StringBuilder buf = new StringBuilder(); + for (byte aMac : mac) { + buf.append(String.format("%02X:", aMac)); + } + if (buf.length()>0) { + buf.deleteCharAt(buf.length() - 1); + } + return buf.toString(); + } + } catch (Exception ex) { } // for now eat exceptions + return ""; + } + +} diff --git a/app/src/main/res/layout/activity_dimm_main.xml b/app/src/main/res/layout/activity_dimm_main.xml new file mode 100644 index 0000000..07f6a02 --- /dev/null +++ b/app/src/main/res/layout/activity_dimm_main.xml @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + + + + +