-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e722c72
Showing
54 changed files
with
1,509 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
plugins { | ||
id 'com.android.application' | ||
} | ||
|
||
android { | ||
namespace 'com.smartpet.mobile' | ||
compileSdk 32 | ||
|
||
defaultConfig { | ||
applicationId "com.smartpet.mobile" | ||
minSdk 26 | ||
targetSdk 32 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
buildFeatures { | ||
viewBinding true | ||
} | ||
} | ||
|
||
dependencies { | ||
|
||
implementation 'com.android.support:appcompat-v7:28.0.0' | ||
implementation 'com.android.support.constraint:constraint-layout:2.0.4' | ||
implementation 'android.arch.lifecycle:livedata:1.1.1' | ||
implementation 'android.arch.lifecycle:viewmodel:1.1.1' | ||
implementation 'android.arch.navigation:navigation-fragment:1.0.0' | ||
implementation 'android.arch.navigation:navigation-ui:1.0.0' | ||
implementation 'com.android.support:design:28.0.0' | ||
implementation 'com.android.support:gridlayout-v7:28.0.0' | ||
testImplementation 'junit:junit:4.13.2' | ||
androidTestImplementation 'com.android.support.test:runner:1.0.2' | ||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# 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 *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
25 changes: 25 additions & 0 deletions
25
app/src/androidTest/java/com/smartpet/mobile/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.smartpet.mobile; | ||
|
||
import android.content.Context; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class ExampleInstrumentedTest { | ||
@Test | ||
public void useAppContext() { | ||
// Context of the app under test. | ||
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); | ||
assertEquals("com.smartpet.mobile", appContext.getPackageName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
<application | ||
android:allowBackup="true" | ||
android:dataExtractionRules="@xml/data_extraction_rules" | ||
android:fullBackupContent="@xml/backup_rules" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.SmartPETFeederMobile" | ||
android:usesCleartextTraffic="true" | ||
|
||
tools:targetApi="27"> | ||
|
||
<activity | ||
android:name=".MainActivity" | ||
android:exported="true" | ||
android:label="@string/app_name"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
|
||
<meta-data | ||
android:name="android.app.lib_name" | ||
android:value="" /> | ||
</activity> | ||
|
||
</application> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package com.smartpet.mobile; | ||
|
||
import android.text.Spannable; | ||
import android.text.TextUtils; | ||
import android.util.Log; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.ProtocolException; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
class Failed extends Exception { | ||
public Failed(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
public class Feeder { | ||
String ip; | ||
String uri; | ||
public Feeder(String ip) throws Failed { | ||
try { | ||
HttpURLConnection conn = null; | ||
URL url = new URL("http://" + ip + ":8000"); | ||
this.uri = "http://" + ip + ":8000"; | ||
conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("GET"); | ||
conn.connect(); | ||
if (conn.getResponseCode() == 200) { | ||
this.ip = ip; | ||
} else { | ||
throw (new Failed("Connect failed. ")); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
// throw (new IOException()); | ||
throw new Failed("Connect failed. "); | ||
} | ||
} | ||
|
||
public void addFeedingPlan(int hour, int minute, int weight) throws Failed { | ||
try { | ||
StringBuffer sbf = new StringBuffer(); | ||
String strRead = null; | ||
HttpURLConnection conn = null; | ||
URL url = new URL(uri + "/api/plan"); | ||
|
||
conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("POST"); | ||
conn.setRequestProperty("Content-Type", "application/json"); | ||
|
||
conn.connect(); | ||
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8); | ||
writer.write("{\"time_h\":"); | ||
writer.write(String.valueOf(hour)); | ||
writer.write(","); | ||
writer.write("\"time_m\":"); | ||
writer.write(String.valueOf(minute)); | ||
writer.write(","); | ||
writer.write("\"weight\":"); | ||
writer.write(String.valueOf(weight)); | ||
writer.write("}"); | ||
writer.flush(); | ||
InputStream is = conn.getInputStream(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); | ||
while ((strRead = reader.readLine()) != null) { | ||
sbf.append(strRead); | ||
} | ||
reader.close(); | ||
conn.disconnect(); | ||
String results = sbf.toString(); | ||
Log.println(Log.WARN, "FEed", results); | ||
if (!results.equals("{\"result\":0}")) { | ||
throw new Failed("Server returned failed result."); | ||
} else { | ||
Log.println(Log.WARN, "Feeder", "Add feeding plan request sent."); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
throw new Failed(""); | ||
} | ||
} | ||
|
||
public void feed(int weight) throws Failed { | ||
try { | ||
StringBuffer sbf = new StringBuffer(); | ||
String strRead = null; | ||
HttpURLConnection conn = null; | ||
URL url = new URL(uri + "/api/food"); | ||
conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("POST"); | ||
conn.setRequestProperty("Content-Type", "application/json"); | ||
|
||
conn.connect(); | ||
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8); | ||
writer.write("{\"weight\": " + String.valueOf(weight) + "}"); | ||
writer.flush(); | ||
InputStream is = conn.getInputStream(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); | ||
while ((strRead = reader.readLine()) != null) { | ||
sbf.append(strRead); | ||
} | ||
reader.close(); | ||
conn.disconnect(); | ||
String results = sbf.toString(); | ||
Log.println(Log.WARN, "FEed", results); | ||
if (!results.equals("{\"result\":0}")) { | ||
throw new Failed("Server returned failed result."); | ||
} else { | ||
Log.println(Log.WARN, "Feeder", "Feeding request sent."); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
throw new Failed("Internal error."); | ||
} | ||
} | ||
public void delFeedingPlan(int hour, int minute) throws Failed { | ||
try { | ||
StringBuffer sbf = new StringBuffer(); | ||
String strRead = null; | ||
HttpURLConnection conn = null; | ||
URL url = new URL(uri + "/api/plan"); | ||
|
||
conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("DELETE"); | ||
conn.setRequestProperty("Content-Type", "application/json"); | ||
|
||
conn.connect(); | ||
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8); | ||
writer.write("{\"time_h\":"); | ||
writer.write(String.valueOf(hour)); | ||
writer.write(","); | ||
writer.write("\"time_m\":"); | ||
writer.write(String.valueOf(minute)); | ||
writer.write("}"); | ||
writer.flush(); | ||
InputStream is = conn.getInputStream(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); | ||
while ((strRead = reader.readLine()) != null) { | ||
sbf.append(strRead); | ||
} | ||
reader.close(); | ||
conn.disconnect(); | ||
String results = sbf.toString(); | ||
Log.println(Log.WARN, "FEed", results); | ||
if (!results.equals("{\"result\":0}")) { | ||
throw new Failed("Server returned failed result."); | ||
} else { | ||
Log.println(Log.WARN, "Feeder", "Add feeding plan request sent."); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
throw new Failed(""); | ||
} | ||
} | ||
} |
Oops, something went wrong.