Skip to content

Commit 5fb4a62

Browse files
author
Luc van den Brand
authored
Merge pull request #3 from RUGSoftEng/development
Release 0.1 Problems regarding structure, cleanliness, and documentation.
2 parents 20892a3 + b2a7562 commit 5fb4a62

48 files changed

Lines changed: 3381 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Hestia/.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild
10+
/.idea

Hestia/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

Hestia/app/build.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
6+
defaultConfig {
7+
applicationId "com.rugged.application.hestia"
8+
minSdkVersion 15
9+
targetSdkVersion 25
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(include: ['*.jar'], dir: 'libs')
24+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25+
exclude group: 'com.android.support', module: 'support-annotations'
26+
})
27+
compile 'com.android.support:appcompat-v7:25.2.0'
28+
testCompile 'junit:junit:4.12'
29+
compile 'com.android.support:recyclerview-v7:25.2.0'
30+
}

Hestia/app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/Feiko/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.rugged.application.hestia;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.rugged.application.hestia", appContext.getPackageName());
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.rugged.application.hestia">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7+
8+
<application
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme">
14+
<activity android:name=".PeripheralListActivity">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
</activity>
20+
<activity
21+
android:name=".PeripheralActivity">
22+
</activity>
23+
</application>
24+
25+
</manifest>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.rugged.application.hestia; /**
2+
* Currently we use BufferedReader for Input
3+
* and DataOutputStream for Output
4+
*/
5+
6+
import android.util.Log;
7+
8+
import java.io.DataOutputStream;
9+
import java.io.IOException;
10+
import java.net.Socket;
11+
import json.simple.*;
12+
13+
public class Client {
14+
private static final String TAG = "Client";
15+
16+
private Socket clientSocket;
17+
private DataOutputStream outToServer;
18+
19+
public Client(String address, Integer port) {
20+
this.openClientSocket(address, port);
21+
this.openOutToServerStream();
22+
}
23+
24+
private void openClientSocket(String address, Integer port) {
25+
try {
26+
this.clientSocket = new Socket(address, port);
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
32+
private void openOutToServerStream() {
33+
try {
34+
this.outToServer = new DataOutputStream(clientSocket.getOutputStream());
35+
} catch (IOException e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
40+
public void sendActionRequest(Integer id, String action) {
41+
// Write JSON
42+
JSONObject jsonObj = new JSONObject();
43+
// Stripping the text from the new-line separators is necessary to avoid
44+
// Strings line "abc...\n" -> the "\n" would be visible
45+
jsonObj.put("type", action);
46+
jsonObj.put("id", id);
47+
JSONObject jsonAction = new JSONObject();
48+
jsonAction.put("action", jsonObj);
49+
50+
this.writeJSONToServer(jsonAction);
51+
}
52+
53+
private void writeJSONToServer(JSONObject jsonObj) {
54+
try {
55+
this.outToServer.writeBytes(jsonObj.toJSONString() + '\n');
56+
} catch (IOException e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
61+
public void closeClientSocket() {
62+
try {
63+
this.clientSocket.close();
64+
} catch (IOException e) {
65+
e.printStackTrace();
66+
}
67+
}
68+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.rugged.application.hestia;
2+
3+
import java.util.UUID;
4+
5+
public class Peripheral {
6+
private String type;
7+
private int id;
8+
9+
public int getId() {
10+
return id;
11+
}
12+
13+
public void setId(int id) {
14+
this.id = id;
15+
}
16+
17+
public String getType() {
18+
return type;
19+
}
20+
21+
public void setType(String type) {
22+
this.type = type;
23+
}
24+
25+
26+
27+
28+
29+
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.rugged.application.hestia;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.support.v4.app.Fragment;
6+
7+
import java.util.UUID;
8+
9+
public class PeripheralActivity extends SingleFragmentActivity{
10+
public static final String EXTRA_PERIPHERAl_ID =
11+
"com.rugged.application.hestia.peripheral_id";
12+
13+
public static Intent newIntent(Context packageContext, int peripheralId) {
14+
Intent intent = new Intent(packageContext, PeripheralActivity.class);
15+
intent.putExtra(EXTRA_PERIPHERAl_ID, peripheralId);
16+
return intent;
17+
}
18+
19+
@Override
20+
protected Fragment createFragment() {
21+
return new PeripheralFragment();
22+
}
23+
}
24+
25+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.rugged.application.hestia;
2+
3+
import android.os.AsyncTask;
4+
import android.os.Bundle;
5+
import android.support.v4.app.Fragment;
6+
import android.text.Editable;
7+
import android.text.TextWatcher;
8+
import android.view.LayoutInflater;
9+
import android.view.View;
10+
import android.view.ViewGroup;
11+
import android.widget.Button;
12+
import android.widget.EditText;
13+
14+
import java.util.UUID;
15+
16+
//Fragment class taking care of a specific Peripheral
17+
public class PeripheralFragment extends Fragment {
18+
private Peripheral mPeripheral;
19+
//lock and unlock button
20+
private Button onButton;
21+
private Button offButton;
22+
@Override
23+
public void onCreate(Bundle savedInstanceState) {
24+
super.onCreate(savedInstanceState);
25+
//receive the id from the PeripheralActivity
26+
int peripheralId = (int) getActivity().getIntent()
27+
.getSerializableExtra(PeripheralActivity.EXTRA_PERIPHERAl_ID);
28+
mPeripheral = PeripheralLab.get(getActivity()).getPeripheral(peripheralId);
29+
}
30+
31+
@Override
32+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
33+
Bundle savedInstanceState) {
34+
View v = inflater.inflate(R.layout.fragment_peripheral, container, false);
35+
onButton = (Button)v.findViewById(R.id.on_button);
36+
offButton = (Button)v.findViewById(R.id.off_button);
37+
38+
switch (mPeripheral.getType()) {
39+
case "Lock" :
40+
onButton.setText("Lock");
41+
offButton.setText("Unlock");
42+
break;
43+
case "Light" :
44+
onButton.setText("Light on");
45+
offButton.setText("Light off");
46+
break;
47+
default:
48+
onButton.setText("On");
49+
offButton.setText("Off");
50+
break;
51+
}
52+
53+
onButton.setOnClickListener(new View.OnClickListener() {
54+
@Override
55+
public void onClick(View view) {
56+
new SendJSONFile().execute("172.20.10.2", "openLock");
57+
}
58+
});
59+
60+
offButton.setOnClickListener(new View.OnClickListener() {
61+
@Override
62+
public void onClick(View view) {
63+
new SendJSONFile().execute("172.20.10.2", "closeLock");
64+
65+
}
66+
});
67+
return v;
68+
}
69+
70+
//Thread classes taking care of sending commands
71+
private class SendJSONFile extends AsyncTask<String,Void,Void> {
72+
@Override
73+
protected Void doInBackground(String... strings) {
74+
Client client = new Client("172.20.10.2", 8850);
75+
client.sendActionRequest(mPeripheral.getId(), "openLock");
76+
client.closeClientSocket();
77+
return null;
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)