Skip to content

Commit

Permalink
add: urban computing course code
Browse files Browse the repository at this point in the history
  • Loading branch information
bingqixia committed Jul 14, 2023
0 parents commit 3b3f705
Show file tree
Hide file tree
Showing 45 changed files with 1,167 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Crawler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.json
.DS_Store
1 change: 1 addition & 0 deletions Crawler/how_to_run.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use python3 request_noise_data.py to execute python server code
51 changes: 51 additions & 0 deletions Crawler/request_noise_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import requests
import pyrebase
from datetime import datetime
import time

# Bingqi Xia
# 22300549

def initialize_firebase(config_file):
with open(config_file) as f:
config = f.readlines()

firebase = pyrebase.initialize_app(config)
return firebase

def request_noise_data():
# current noise data for monitors
url = "https://dublincityairandnoise.ie/assets/php/get-monitors.php"
# url = "https://dublincityairandnoise.ie/readings?monitor=DCC-008&dateFrom=06+Nov+2022&dateTo=06+Nov+2022"
my_referer = "https://dublincityairandnoise.ie/readings?monitor=DCC-008&dateFrom=06+Nov+2022&dateTo=06+Nov+2022"
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
accept = "application/json, text/javascript, */*; q=0.01"

headers={'referer': my_referer, "accept": accept, "user_agent": user_agent}

r = requests.get(url, headers)
noise_data = []
if r.status_code == 200:
ret = r.json()
noise_data = [x for x in ret if x['monitor_type']['category']=='noise']
return noise_data

def push_noise_data():
noise_data = request_noise_data()
firebase = initialize_firebase()

db = firebase.database()

for data in noise_data:
print(data)
results = db.child("noise_open_data") \
.child(data['location']) \
.child(data['latest_reading']['recorded_at']).set(data)


if __name__ == '__main__':
push_noise_data()
# while (datetime.now() <= datetime(2022, 11, 6, 17, 30)):
# print(datetime.now())
# push_noise_data()
# time.sleep(300)
17 changes: 17 additions & 0 deletions UrbanApplication/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*.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
.idea/
.DS_Store
5 changes: 5 additions & 0 deletions UrbanApplication/HowToRun.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1. open the project with android studio or IntelliJ Idea
2. connect android phone(Turn on developer mode) with USB, open USB debug on the phone
3. build apk and it will install in your android phone
4. open app and allow all permissions, click start record then it start collect data
5. datas will be upload firebase.
2 changes: 2 additions & 0 deletions UrbanApplication/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build
google-services.json
57 changes: 57 additions & 0 deletions UrbanApplication/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
buildscript {

repositories {
// Make sure that you have the following two repositories
google() // Google's Maven repository
mavenCentral() // Maven Central repository
}

dependencies {

// Add the dependency for the Google services Gradle plugin
classpath 'com.google.gms:google-services:4.3.14'
}
}

plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}

android {
compileSdk 28

defaultConfig {
applicationId "com.bingqi.urbanapplication"
minSdk 23
targetSdk 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'pub.devrel:easypermissions:3.0.0'

// Import the BoM for the Firebase platform
implementation platform('com.google.firebase:firebase-bom:31.0.2')
// Add the dependency for the Realtime Database library
implementation 'com.google.firebase:firebase-database'
implementation 'com.google.firebase:firebase-analytics'
}
21 changes: 21 additions & 0 deletions UrbanApplication/app/proguard-rules.pro
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.bingqi.urbanapplication;

import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.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.bingqi.urbanapplication", appContext.getPackageName());
}
}
33 changes: 33 additions & 0 deletions UrbanApplication/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.bingqi.urbanapplication">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/> <!-- GPS定位权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bingqi.urbanapplication;

import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import pub.devrel.easypermissions.EasyPermissions;

public class BaseActivity extends AppCompatActivity {
public void requestPermission(Activity context, String tip, int requestCode, String[] perms) {
EasyPermissions.requestPermissions(context, tip, requestCode, perms);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.bingqi.urbanapplication;

import android.os.Environment;
import android.util.Log;

import java.io.*;
import java.util.ArrayList;

public class CsvUtils {
private static final String TAG = "CsvUtils";

public static void saveToCsv(String fileName, ArrayList<String> content) {
try {
String folderName = null;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
if (path != null) {
folderName = path +"/CSV/";
}
}

File fileDir = new File(folderName);
Log.e(TAG, "dataDir: " + folderName);
if(!fileDir.exists()) {
fileDir.mkdirs();
}

String savedPath = folderName + fileName;
Log.e(TAG, "DataPath: " + savedPath);
File file = new File(savedPath);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}

StringBuilder sb = new StringBuilder();
for (int i = 0; i < content.size(); ++ i) {
sb.append(content.get(i));
sb.append("\n");
}

FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(sb.toString());
bw.close();
fw.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.bingqi.urbanapplication;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class DataUtils {

private static final String TAG = "DataUtils";
private DatabaseReference mDatabase;

public DataUtils(String path) {
mDatabase = FirebaseDatabase.getInstance().getReference(path);
}

public void writeData(String pathString, Object obj) {

mDatabase.child(pathString).setValue(obj);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.bingqi.urbanapplication;

import com.google.firebase.database.IgnoreExtraProperties;

@IgnoreExtraProperties
public class GpsData {
public String currentTime;
public double latitude;
public double longitude;

public GpsData() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public GpsData(String currentTime, double latitude, double longitude) {
this.currentTime = currentTime;
this.latitude = latitude;
this.longitude = longitude;
}

}
Loading

0 comments on commit 3b3f705

Please sign in to comment.