From 2e6b535af9a999092ff61a2618122093bb3124de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Wed, 24 Apr 2019 15:31:15 +0300 Subject: [PATCH 1/7] utility improve --- server_side/api/modules/utility.py | 42 ++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/server_side/api/modules/utility.py b/server_side/api/modules/utility.py index 1b82328..1227870 100644 --- a/server_side/api/modules/utility.py +++ b/server_side/api/modules/utility.py @@ -16,18 +16,50 @@ with open(db_path, 'r') as f: class Resources(Resource): def post(self): + """ + POST Data: + user_id=&type=&bill= + :return: + """ user = utils.find_by_id(users.values(), request.form['user_id']) if request.form["type"] == "electricity": + usage = user['daily_electricity_usage'] + ideal = user['ideal_electricity_usage'] + + diff = [abs(a-b) for a in usage for b in ideal] + + sum_diff = sum(diff) + + point = 10 - (sum_diff * 0.0001) + eff = point * 10 + bill = (int(request.form['bill']) * eff)/100 return { - 'daily_electricity_usage': user['daily_electricity_usage'], - 'ideal_electricity_usage': user['ideal_electricity_usage'] + 'daily_electricity_usage': usage, + 'ideal_electricity_usage': ideal, + 'points': point, + 'efficiency': eff, + 'bill': bill } elif request.form["type"] == "water": + usage = user['daily_water_usage'] + ideal = user['ideal_water_usage'] + + diff = [abs(a-b) for a in usage for b in ideal] + + sum_diff = sum(diff) + + point = 10 - (sum_diff * 0.0001) + eff = point * 10 + bill = (int(request.form['bill']) * eff)/100 + return { - 'daily_water_usage': user['daily_water_usage'], - 'ideal_water_usage': user['ideal_water_usage'] - } + 'daily_water_usage': usage, + 'ideal_water_usage': ideal, + 'point': point, + 'efficiency': eff, + 'bill': bill + } if __name__ == '__main__': api.add_resource(Resources, '/resources', '/resources/') From 9312d9d047f00e836e2cdd39f53062401e641be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yi=C4=9Fit=20=C3=87olako=C4=9Flu?= Date: Wed, 24 Apr 2019 15:42:10 +0300 Subject: [PATCH 2/7] hjkhk --- MyCity/app/build.gradle | 3 +- .../main/java/gq/yigit/mycity/QRFragment.java | 108 ++++++++++++++++++ .../java/gq/yigit/mycity/RateFragment.java | 12 +- .../mycity/navigation/TransitFragment.java | 2 +- .../yigit/mycity/tools/QRCodeGenerator.java | 4 + .../app/src/main/res/drawable-v21/qr_code.xml | 33 ++++++ .../app/src/main/res/layout/fragment_qr.xml | 14 +++ .../main/res/menu/activity_main_drawer.xml | 5 + 8 files changed, 170 insertions(+), 11 deletions(-) create mode 100644 MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java create mode 100644 MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java create mode 100644 MyCity/app/src/main/res/drawable-v21/qr_code.xml create mode 100644 MyCity/app/src/main/res/layout/fragment_qr.xml diff --git a/MyCity/app/build.gradle b/MyCity/app/build.gradle index 5f6b4eb..8461eba 100644 --- a/MyCity/app/build.gradle +++ b/MyCity/app/build.gradle @@ -22,8 +22,9 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') api 'cz.msebera.android:httpclient:4.4.1.2' + implementation 'com.google.zxing:core:3.2.0' implementation 'com.jjoe64:graphview:4.2.2' - implementation 'com.android.support:design:27.1.1' + implementation 'com.android.support:design:27.0.0' implementation 'com.android.support:appcompat-v7:27.0.0' implementation 'com.android.support:design:27.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java b/MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java new file mode 100644 index 0000000..af5a481 --- /dev/null +++ b/MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java @@ -0,0 +1,108 @@ +package gq.yigit.mycity; + +import android.content.Context; +import android.net.Uri; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + + +/** + * A simple {@link Fragment} subclass. + * Activities that contain this fragment must implement the + * {@link QRFragment.OnFragmentInteractionListener} interface + * to handle interaction events. + * Use the {@link QRFragment#newInstance} factory method to + * create an instance of this fragment. + */ +public class QRFragment extends Fragment { + // TODO: Rename parameter arguments, choose names that match + // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER + private static final String ARG_PARAM1 = "param1"; + private static final String ARG_PARAM2 = "param2"; + + // TODO: Rename and change types of parameters + private String mParam1; + private String mParam2; + + private OnFragmentInteractionListener mListener; + + public QRFragment() { + // Required empty public constructor + } + + /** + * Use this factory method to create a new instance of + * this fragment using the provided parameters. + * + * @param param1 Parameter 1. + * @param param2 Parameter 2. + * @return A new instance of fragment QRFragment. + */ + // TODO: Rename and change types and number of parameters + public static QRFragment newInstance(String param1, String param2) { + QRFragment fragment = new QRFragment(); + Bundle args = new Bundle(); + args.putString(ARG_PARAM1, param1); + args.putString(ARG_PARAM2, param2); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + mParam1 = getArguments().getString(ARG_PARAM1); + mParam2 = getArguments().getString(ARG_PARAM2); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_qr, container, false); + } + + // TODO: Rename method, update argument and hook method into UI event + public void onButtonPressed(Uri uri) { + if (mListener != null) { + mListener.onFragmentInteraction(uri); + } + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof OnFragmentInteractionListener) { + mListener = (OnFragmentInteractionListener) context; + } else { + throw new RuntimeException(context.toString() + + " must implement OnFragmentInteractionListener"); + } + } + + @Override + public void onDetach() { + super.onDetach(); + mListener = null; + } + + /** + * This interface must be implemented by activities that contain this + * fragment to allow an interaction in this fragment to be communicated + * to the activity and potentially other fragments contained in that + * activity. + *

+ * See the Android Training lesson Communicating with Other Fragments for more information. + */ + public interface OnFragmentInteractionListener { + // TODO: Update argument type and name + void onFragmentInteraction(Uri uri); + } +} diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/RateFragment.java b/MyCity/app/src/main/java/gq/yigit/mycity/RateFragment.java index 6af38eb..99116fd 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/RateFragment.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/RateFragment.java @@ -120,16 +120,10 @@ public class RateFragment extends Fragment implements WebRequest.responseListene } } }); + - if ( ContextCompat.checkSelfPermission( cntxt, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) { - ActivityCompat.requestPermissions(MainActivity.mainActivity, new String[] { Manifest.permission.ACCESS_FINE_LOCATION },1); - } - - LocationManager locationManager = (LocationManager) - getContext().getSystemService(cntxt.LOCATION_SERVICE); - Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); - String longitude = String.valueOf(loc.getLongitude()); - String latitude = String.valueOf(loc.getLatitude()); + String latitude = "39.9127897"; + String longitude = "32.8073577"; FileActions file_manager = new FileActions(); url = file_manager.readFromFile(cntxt,"server.config").trim(); diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/navigation/TransitFragment.java b/MyCity/app/src/main/java/gq/yigit/mycity/navigation/TransitFragment.java index 780b87d..88854aa 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/navigation/TransitFragment.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/navigation/TransitFragment.java @@ -102,7 +102,7 @@ public class TransitFragment extends Fragment implements WebRequest.responseList place_name = rootView.findViewById(R.id.place_name); recyclerView = rootView.findViewById(R.id.route_view); try { - latitude = "39.9127897"; + latitude = "39.9127897"; longitude = "32.8073577"; }catch (SecurityException e){ diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java b/MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java new file mode 100644 index 0000000..d1e7430 --- /dev/null +++ b/MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java @@ -0,0 +1,4 @@ +package gq.yigit.mycity.tools; + +public class QRCodeGenerator { +} diff --git a/MyCity/app/src/main/res/drawable-v21/qr_code.xml b/MyCity/app/src/main/res/drawable-v21/qr_code.xml new file mode 100644 index 0000000..ca14294 --- /dev/null +++ b/MyCity/app/src/main/res/drawable-v21/qr_code.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/MyCity/app/src/main/res/layout/fragment_qr.xml b/MyCity/app/src/main/res/layout/fragment_qr.xml new file mode 100644 index 0000000..385e6b5 --- /dev/null +++ b/MyCity/app/src/main/res/layout/fragment_qr.xml @@ -0,0 +1,14 @@ + + + + + + + \ No newline at end of file diff --git a/MyCity/app/src/main/res/menu/activity_main_drawer.xml b/MyCity/app/src/main/res/menu/activity_main_drawer.xml index ad3e68a..fef2d54 100644 --- a/MyCity/app/src/main/res/menu/activity_main_drawer.xml +++ b/MyCity/app/src/main/res/menu/activity_main_drawer.xml @@ -35,6 +35,11 @@ android:id="@+id/denunciation" android:icon="@drawable/denunciation" android:title="Denunciation"/> + + From 0abc70fc36d1dccb3c9b01664fd9b6786881582f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Wed, 24 Apr 2019 16:35:19 +0300 Subject: [PATCH 3/7] qr code halfway done --- .../main/java/gq/yigit/mycity/QRFragment.java | 73 +++++++------------ .../yigit/mycity/tools/QRCodeGenerator.java | 31 ++++++++ .../app/src/main/res/layout/fragment_qr.xml | 17 +++-- client_side/interface/UserData/socket.io | 1 - 4 files changed, 66 insertions(+), 56 deletions(-) delete mode 160000 client_side/interface/UserData/socket.io diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java b/MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java index af5a481..55e58a1 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java @@ -1,73 +1,62 @@ package gq.yigit.mycity; import android.content.Context; +import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; +import android.support.annotation.RequiresPermission; import android.support.v4.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; +import com.google.zxing.WriterException; + +import org.json.JSONException; + +import gq.yigit.mycity.tools.QRCodeGenerator; -/** - * A simple {@link Fragment} subclass. - * Activities that contain this fragment must implement the - * {@link QRFragment.OnFragmentInteractionListener} interface - * to handle interaction events. - * Use the {@link QRFragment#newInstance} factory method to - * create an instance of this fragment. - */ public class QRFragment extends Fragment { - // TODO: Rename parameter arguments, choose names that match - // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER - private static final String ARG_PARAM1 = "param1"; - private static final String ARG_PARAM2 = "param2"; - // TODO: Rename and change types of parameters - private String mParam1; - private String mParam2; private OnFragmentInteractionListener mListener; - + private ImageView qr_view; + private Bitmap qr_img; public QRFragment() { - // Required empty public constructor } - /** - * Use this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @param param1 Parameter 1. - * @param param2 Parameter 2. - * @return A new instance of fragment QRFragment. - */ - // TODO: Rename and change types and number of parameters + + public static QRFragment newInstance(String param1, String param2) { QRFragment fragment = new QRFragment(); - Bundle args = new Bundle(); - args.putString(ARG_PARAM1, param1); - args.putString(ARG_PARAM2, param2); - fragment.setArguments(args); + return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (getArguments() != null) { - mParam1 = getArguments().getString(ARG_PARAM1); - mParam2 = getArguments().getString(ARG_PARAM2); - } + } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_qr, container, false); + View rootView = inflater.inflate(R.layout.fragment_qr, container, false); + qr_view = rootView.findViewById(R.id.qr_image); + try { + qr_img = QRCodeGenerator.Generate(MainActivity.userData.getString("id"),429,429); + qr_view.setImageBitmap(qr_img); + }catch(JSONException e){ + Log.e("[ERROR]","JSON error occured while generating qr code!"); + }catch (WriterException e){ + Log.e("[ERROR]","An error occured while generating qr code!"); + } + return rootView; } - // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); @@ -91,16 +80,6 @@ public class QRFragment extends Fragment { mListener = null; } - /** - * This interface must be implemented by activities that contain this - * fragment to allow an interaction in this fragment to be communicated - * to the activity and potentially other fragments contained in that - * activity. - *

- * See the Android Training lesson Communicating with Other Fragments for more information. - */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java b/MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java index d1e7430..bdfbae5 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java @@ -1,4 +1,35 @@ package gq.yigit.mycity.tools; +import android.graphics.Bitmap; +import android.graphics.Color; +import android.net.Uri; + +import com.google.zxing.BarcodeFormat; +import com.google.zxing.MultiFormatWriter; +import com.google.zxing.WriterException; +import com.google.zxing.common.BitMatrix; + + +import java.util.Arrays; + public class QRCodeGenerator { + public static Bitmap Generate(String data, int height,int width) throws WriterException { + MultiFormatWriter writer = new MultiFormatWriter(); + String finalData = Uri.encode(data); + + // Use 1 as the height of the matrix as this is a 1D Barcode. + BitMatrix bm = writer.encode(finalData, BarcodeFormat.CODE_128, width, 1); + int bmWidth = bm.getWidth(); + + Bitmap imageBitmap = Bitmap.createBitmap(bmWidth, height, Bitmap.Config.ARGB_8888); + + for (int i = 0; i < bmWidth; i++) { + // Paint columns of width 1 + int[] column = new int[height]; + Arrays.fill(column, bm.get(i, 0) ? Color.BLACK : Color.WHITE); + imageBitmap.setPixels(column, 0, 1, i, 0, 1, height); + } + + return imageBitmap; + } } diff --git a/MyCity/app/src/main/res/layout/fragment_qr.xml b/MyCity/app/src/main/res/layout/fragment_qr.xml index 385e6b5..686a5d2 100644 --- a/MyCity/app/src/main/res/layout/fragment_qr.xml +++ b/MyCity/app/src/main/res/layout/fragment_qr.xml @@ -1,14 +1,15 @@ - - - + tools:context=".QRFragment" + android:layout_marginTop="@dimen/fragment_margin"> + \ No newline at end of file diff --git a/client_side/interface/UserData/socket.io b/client_side/interface/UserData/socket.io deleted file mode 160000 index 3020e45..0000000 --- a/client_side/interface/UserData/socket.io +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3020e455ab9a81341722e6c1e6cd8ef6b4b0ea28 From 547f6eeb73d0495b80eb2e359cd303a21aaa09cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Wed, 24 Apr 2019 16:35:19 +0300 Subject: [PATCH 4/7] qr code halfway done --- .../main/java/gq/yigit/mycity/QRFragment.java | 73 +++++++------------ .../yigit/mycity/tools/QRCodeGenerator.java | 31 ++++++++ .../app/src/main/res/layout/fragment_qr.xml | 17 +++-- client_side/interface/UserData/socket.io | 1 - 4 files changed, 66 insertions(+), 56 deletions(-) delete mode 160000 client_side/interface/UserData/socket.io diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java b/MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java index af5a481..55e58a1 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/QRFragment.java @@ -1,73 +1,62 @@ package gq.yigit.mycity; import android.content.Context; +import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; +import android.support.annotation.RequiresPermission; import android.support.v4.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; +import com.google.zxing.WriterException; + +import org.json.JSONException; + +import gq.yigit.mycity.tools.QRCodeGenerator; -/** - * A simple {@link Fragment} subclass. - * Activities that contain this fragment must implement the - * {@link QRFragment.OnFragmentInteractionListener} interface - * to handle interaction events. - * Use the {@link QRFragment#newInstance} factory method to - * create an instance of this fragment. - */ public class QRFragment extends Fragment { - // TODO: Rename parameter arguments, choose names that match - // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER - private static final String ARG_PARAM1 = "param1"; - private static final String ARG_PARAM2 = "param2"; - // TODO: Rename and change types of parameters - private String mParam1; - private String mParam2; private OnFragmentInteractionListener mListener; - + private ImageView qr_view; + private Bitmap qr_img; public QRFragment() { - // Required empty public constructor } - /** - * Use this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @param param1 Parameter 1. - * @param param2 Parameter 2. - * @return A new instance of fragment QRFragment. - */ - // TODO: Rename and change types and number of parameters + + public static QRFragment newInstance(String param1, String param2) { QRFragment fragment = new QRFragment(); - Bundle args = new Bundle(); - args.putString(ARG_PARAM1, param1); - args.putString(ARG_PARAM2, param2); - fragment.setArguments(args); + return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (getArguments() != null) { - mParam1 = getArguments().getString(ARG_PARAM1); - mParam2 = getArguments().getString(ARG_PARAM2); - } + } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_qr, container, false); + View rootView = inflater.inflate(R.layout.fragment_qr, container, false); + qr_view = rootView.findViewById(R.id.qr_image); + try { + qr_img = QRCodeGenerator.Generate(MainActivity.userData.getString("id"),429,429); + qr_view.setImageBitmap(qr_img); + }catch(JSONException e){ + Log.e("[ERROR]","JSON error occured while generating qr code!"); + }catch (WriterException e){ + Log.e("[ERROR]","An error occured while generating qr code!"); + } + return rootView; } - // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); @@ -91,16 +80,6 @@ public class QRFragment extends Fragment { mListener = null; } - /** - * This interface must be implemented by activities that contain this - * fragment to allow an interaction in this fragment to be communicated - * to the activity and potentially other fragments contained in that - * activity. - *

- * See the Android Training lesson Communicating with Other Fragments for more information. - */ public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java b/MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java index d1e7430..bdfbae5 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/tools/QRCodeGenerator.java @@ -1,4 +1,35 @@ package gq.yigit.mycity.tools; +import android.graphics.Bitmap; +import android.graphics.Color; +import android.net.Uri; + +import com.google.zxing.BarcodeFormat; +import com.google.zxing.MultiFormatWriter; +import com.google.zxing.WriterException; +import com.google.zxing.common.BitMatrix; + + +import java.util.Arrays; + public class QRCodeGenerator { + public static Bitmap Generate(String data, int height,int width) throws WriterException { + MultiFormatWriter writer = new MultiFormatWriter(); + String finalData = Uri.encode(data); + + // Use 1 as the height of the matrix as this is a 1D Barcode. + BitMatrix bm = writer.encode(finalData, BarcodeFormat.CODE_128, width, 1); + int bmWidth = bm.getWidth(); + + Bitmap imageBitmap = Bitmap.createBitmap(bmWidth, height, Bitmap.Config.ARGB_8888); + + for (int i = 0; i < bmWidth; i++) { + // Paint columns of width 1 + int[] column = new int[height]; + Arrays.fill(column, bm.get(i, 0) ? Color.BLACK : Color.WHITE); + imageBitmap.setPixels(column, 0, 1, i, 0, 1, height); + } + + return imageBitmap; + } } diff --git a/MyCity/app/src/main/res/layout/fragment_qr.xml b/MyCity/app/src/main/res/layout/fragment_qr.xml index 385e6b5..686a5d2 100644 --- a/MyCity/app/src/main/res/layout/fragment_qr.xml +++ b/MyCity/app/src/main/res/layout/fragment_qr.xml @@ -1,14 +1,15 @@ - - - + tools:context=".QRFragment" + android:layout_marginTop="@dimen/fragment_margin"> + \ No newline at end of file diff --git a/client_side/interface/UserData/socket.io b/client_side/interface/UserData/socket.io deleted file mode 160000 index 3020e45..0000000 --- a/client_side/interface/UserData/socket.io +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 3020e455ab9a81341722e6c1e6cd8ef6b4b0ea28 From ec973b4a2bafb45105b507f2fbb1ec32e536d673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20Ayd=C4=B1n?= <3feaydin@gmail.com> Date: Wed, 24 Apr 2019 17:25:03 +0300 Subject: [PATCH 5/7] =?UTF-8?q?asdn=C3=B6asdh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gq/yigit/mycity/utility/UtilityWater.java | 122 +++++++++++------- .../res/layout/fragment_utility_water.xml | 96 ++++++++++++-- 2 files changed, 163 insertions(+), 55 deletions(-) diff --git a/MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityWater.java b/MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityWater.java index b36f963..8258f15 100644 --- a/MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityWater.java +++ b/MyCity/app/src/main/java/gq/yigit/mycity/utility/UtilityWater.java @@ -1,32 +1,33 @@ package gq.yigit.mycity.utility; import android.content.Context; +import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import com.jjoe64.graphview.GraphView; +import com.jjoe64.graphview.helper.StaticLabelsFormatter; +import com.jjoe64.graphview.series.DataPoint; +import com.jjoe64.graphview.series.LineGraphSeries; +import gq.yigit.mycity.MainActivity; import gq.yigit.mycity.R; +import gq.yigit.mycity.tools.FileActions; +import gq.yigit.mycity.tools.WebRequest; +import org.json.JSONException; +import org.json.JSONObject; -/** - * A simple {@link Fragment} subclass. - * Activities that contain this fragment must implement the - * {@link UtilityWater.OnFragmentInteractionListener} interface - * to handle interaction events. - * Use the {@link UtilityWater#newInstance} factory method to - * create an instance of this fragment. - */ -public class UtilityWater extends Fragment { - // TODO: Rename parameter arguments, choose names that match - // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER - private static final String ARG_PARAM1 = "param1"; - private static final String ARG_PARAM2 = "param2"; - - // TODO: Rename and change types of parameters - private String mParam1; - private String mParam2; +import java.util.HashMap; + + +public class UtilityWater extends Fragment implements WebRequest.responseListener { + + private JSONObject waterUsage; + private GraphView graph; private OnFragmentInteractionListener mListener; @@ -34,38 +35,46 @@ public class UtilityWater extends Fragment { // Required empty public constructor } - /** - * Use this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @param param1 Parameter 1. - * @param param2 Parameter 2. - * @return A new instance of fragment UtilityWater. - */ - // TODO: Rename and change types and number of parameters + public static UtilityWater newInstance(String param1, String param2) { UtilityWater fragment = new UtilityWater(); - Bundle args = new Bundle(); - args.putString(ARG_PARAM1, param1); - args.putString(ARG_PARAM2, param2); - fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (getArguments() != null) { - mParam1 = getArguments().getString(ARG_PARAM1); - mParam2 = getArguments().getString(ARG_PARAM2); - } + } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + Bundle savedInstanceState) { // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_utility_water, container, false); + View rootView = inflater.inflate(R.layout.fragment_utility_water, container, false);; + graph = (GraphView) rootView.findViewById(R.id.utility_graph); + + + StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph); + staticLabelsFormatter.setHorizontalLabels(new String[] {"0", "2", "4", "6", "8", "10", "12", "14", "16", "18", "20", "22", "24"}); + staticLabelsFormatter.setVerticalLabels(new String[] {"0", "5", "10","15","20"}); + graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter); + + FileActions file_manager = new FileActions(); + String url = file_manager.readFromFile(getContext(),"server.config").trim(); + HashMap request = new HashMap<>(); + try { + request.put("user_id", MainActivity.userData.getString("id")); + }catch (JSONException e){ + Log.e("[ERROR]","User data not correct"); + } + request.put("type","water"); + WebRequest login_manager = new WebRequest(url + "/resources/",false,request,0); + login_manager.addListener(this); + login_manager.execute(); + + + return rootView; } // TODO: Rename method, update argument and hook method into UI event @@ -91,18 +100,39 @@ public class UtilityWater extends Fragment { mListener = null; } - /** - * This interface must be implemented by activities that contain this - * fragment to allow an interaction in this fragment to be communicated - * to the activity and potentially other fragments contained in that - * activity. - *

- * See the Android Training lesson Communicating with Other Fragments for more information. - */ + public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } + + public void receivedResponse(boolean success, String result,int reqid){ + if(success){ + try{ + waterUsage = new JSONObject(result); + LineGraphSeries series = new LineGraphSeries<>(new DataPoint[] {}); + LineGraphSeries series2 = new LineGraphSeries<>(new DataPoint[] {}); + for(int i = 0;i < waterUsage.getJSONArray("daily_water_usage").length();i++){ + series.appendData(new DataPoint(i,waterUsage.getJSONArray("daily_water_usage").getInt(i)), + true, + 100); + } + + for(int i = 0;i < waterUsage.getJSONArray("ideal_water_usage").length();i++){ + series2.appendData(new DataPoint(i,waterUsage.getJSONArray("ideal_water_usage").getInt(i)), + true, + 100); + } + series.setTitle("ideal"); + series.setColor(Color.BLUE); + series2.setTitle("usage"); + series2.setColor(Color.RED); + graph.addSeries(series); + graph.addSeries(series2); + + }catch (JSONException e){ + Log.e("[ERROR]","Cannot interpret response from water service"); + } + } + } } diff --git a/MyCity/app/src/main/res/layout/fragment_utility_water.xml b/MyCity/app/src/main/res/layout/fragment_utility_water.xml index 56550e8..77a3323 100644 --- a/MyCity/app/src/main/res/layout/fragment_utility_water.xml +++ b/MyCity/app/src/main/res/layout/fragment_utility_water.xml @@ -1,14 +1,92 @@ - + - + + + android:layout_height="match_parent"> + + + + + + + + + + + + + +