From 7e94a0ffe17dbac7460df998ac35962ff0cf752c Mon Sep 17 00:00:00 2001 From: mariogrip Date: Thu, 30 Oct 2014 23:03:14 +0100 Subject: [PATCH] Fix bug #1 and #2 --- app/src/main/AndroidManifest.xml | 3 + .../com/mariogrip/octodroid/Activity.java | 107 ++++++- .../java/com/mariogrip/octodroid/get.java | 280 +++++++++++------- .../java/com/mariogrip/octodroid/service.java | 104 +++++++ app/src/main/res/values/strings.xml | 3 + app/src/main/res/xml/setting.xml | 11 +- 6 files changed, 401 insertions(+), 107 deletions(-) create mode 100644 app/src/main/java/com/mariogrip/octodroid/service.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ef50c61..65766e6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,6 +20,9 @@ + diff --git a/app/src/main/java/com/mariogrip/octodroid/Activity.java b/app/src/main/java/com/mariogrip/octodroid/Activity.java index 46cd668..f2e7630 100644 --- a/app/src/main/java/com/mariogrip/octodroid/Activity.java +++ b/app/src/main/java/com/mariogrip/octodroid/Activity.java @@ -26,12 +26,22 @@ public class Activity extends ActionBarActivity { public static String jsonData_printer; protected SharedPreferences prefs; private get get_class; - protected String ip; - protected String key; + private boolean senderr = false; + protected static boolean running = false; + protected static boolean printing; + protected static boolean push = true; + protected static boolean servicerunning =false; + protected static String ip; + protected static String key; private Timer timer = new Timer(); private TimerTask timerTask; + private Timer timer2 = new Timer(); + private TimerTask timerTask2; public static boolean server_status = false; private static final int RESULT_SETTINGS = 1; + + + @TargetApi(Build.VERSION_CODES.GINGERBREAD) @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,13 +51,17 @@ protected void onCreate(Bundle savedInstanceState) { ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); + super.onCreate(savedInstanceState); setContentView(R.layout.status_tab); prefs = PreferenceManager.getDefaultSharedPreferences(Activity.this); ip = prefs.getString("ip", "localhost"); key = prefs.getString("api", "0"); + senderr = prefs.getBoolean("err", true); + push = prefs.getBoolean("push", true); get_class = new get(); Log.d("OctoPrint","test"); + running = false; runner(); AlertDialog.Builder builder = new AlertDialog.Builder(this); @@ -63,24 +77,71 @@ public void onClick(DialogInterface dialog, int id) { Activity.this.finish(); } }); - AlertDialog dialog = builder.create(); dialog.show(); + if (push) { + if (!servicerunning) { + servicerunning = true; + Intent mServiceIntent = new Intent(this, service.class); + this.startService(mServiceIntent); + } + }else{ + if (servicerunning){ + servicerunning = false; + Intent mServiceIntent = new Intent(this, service.class); + this.stopService(mServiceIntent); + } + } } + public void servererr(){ + timerTask2 = new TimerTask() { + @Override + public void run() { + get.refreshJson(ip, "job", key); + if (server_status){ + runner(); + timerTask2.cancel(); + return; + } + } + + }; + timer2.schedule(timerTask2, 0, 10000); + } + + public void logD(String e){ + Log.d("OctoDroid",e); + } public void runner(){ + get.refreshJson(ip, "job", key); + if (running){ + logD("Stopping runner, Might started twice"); + return; + } + if (!running){ + logD("OneRunStarted"); + running = true; + } timerTask = new TimerTask() { @Override public void run() { + if (!server_status){ + logD("Server Error"); + running = false; + servererr(); + timerTask.cancel(); + return; + } Activity.this.runOnUiThread(new Runnable() { public void run() { - get.refreshJson(ip, "job", key); get.refreshJson(ip, "printer", key); - + get.decodeJson(); + logD("Running runner"); if (server_status) { + Log.d("test123",get.getData("job", "printTime")); ProgressBar progress = (ProgressBar) findViewById(R.id.progressBar); - progress.setProgress(get.getProgress()); TextView texttime = (TextView) findViewById(R.id.textView11_time); TextView textpri = (TextView) findViewById(R.id.textView16_printed); TextView textest = (TextView) findViewById(R.id.textView13_est); @@ -112,6 +173,7 @@ public void run() { textfila.setText(" " + "-"); texttimel.setText(" " + "-"); textprinttime.setText(" " + get.toHumanRead(Double.parseDouble(get.getData("job", "printTime").toString()))); + progress.setProgress(get.getProgress()); } } }); @@ -153,9 +215,42 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { prefs = PreferenceManager.getDefaultSharedPreferences(Activity.this); ip = prefs.getString("ip", "localhost"); key = prefs.getString("api", "0"); + push = prefs.getBoolean("push", true); + if (push) { + if (!servicerunning) { + servicerunning = true; + Intent mServiceIntent = new Intent(this, service.class); + this.startService(mServiceIntent); + } + }else{ + if (servicerunning){ + servicerunning = false; + Intent mServiceIntent = new Intent(this, service.class); + this.stopService(mServiceIntent); + } + } break; } } + public void onPause(){ + super.onPause(); + running = false; + timerTask.cancel(); + } + public void onResume(){ + super.onResume(); + runner(); + } + public void onStop(){ + super.onStop(); + running = false; + timerTask.cancel(); + } + public void onStart(){ + super.onStart(); + runner(); + } + } diff --git a/app/src/main/java/com/mariogrip/octodroid/get.java b/app/src/main/java/com/mariogrip/octodroid/get.java index 20677d0..2489b55 100644 --- a/app/src/main/java/com/mariogrip/octodroid/get.java +++ b/app/src/main/java/com/mariogrip/octodroid/get.java @@ -23,6 +23,9 @@ public class get extends Activity{ public static String toMBGB(double bytes){ + if (!Activity.server_status){ + return "-/-"; + } String returnData; Double fileSizeInKB = bytes / 1024; Double fileSizeInMB = fileSizeInKB / 1024; @@ -40,144 +43,183 @@ public static double roundDown5(double d) { } public static String toHumanRead(double biggy) { + if (!Activity.server_status){ + return "00:00:00"; + } int hours = (int) biggy / 3600; int remainder = (int) biggy - hours * 3600; int mins = remainder / 60; remainder = remainder - mins * 60; int secs = remainder; - return hours + ":" + mins + ":" + secs; + return String.format("%02d", hours) + ":" + String.format("%02d", mins) + ":" + String.format("%02d", secs); + } + private static JSONObject jsonData_job_job; + + private static JSONObject jsonData_job_job_progress; + private static JSONObject jsonData_job_job_file; + private static JSONObject jsonData_job_job_job; + private static JSONObject jsonData_printer_printer; + private static JSONObject jsonData_printer_printer_temps; + private static boolean jsonData_printer_status; + + public static void decodeJson(){ + if (!Activity.server_status){ + return; + } + try { + jsonData_job_job = new JSONObject(jsonData_job); + jsonData_job_job_progress = new JSONObject(jsonData_job_job.getString("progress")); + jsonData_job_job_job = new JSONObject(jsonData_job_job.getString("job")); + jsonData_job_job_file = new JSONObject(jsonData_job_job_job.getString("file")); + if (jsonData_printer.equals("Printer is not operational")){ + jsonData_printer_status = false; + }else{ + jsonData_printer_status = true; + jsonData_printer_printer = new JSONObject(jsonData_printer); + jsonData_printer_printer_temps = new JSONObject(jsonData_printer_printer.getString("temps")); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + public static void decodeJsonService(){ + if (!Activity.server_status){ + return; + } + try { + jsonData_job_job = new JSONObject(jsonData_job); + jsonData_job_job_progress = new JSONObject(jsonData_job_job.getString("progress")); + } catch (JSONException e) { + e.printStackTrace(); + } } public static String getData(String job,String cmd){ + if (!Activity.server_status){ + return "-"; + } String returnData = new String(); boolean noerr; if (Activity.server_status) { try { - if (job == "job"){ - JSONObject json = new JSONObject(jsonData_job); + if (job == "job") { //Progress - if (cmd == "printTime"){ - JSONObject printTime_json = new JSONObject(json.getString("progress")); - returnData = printTime_json.getString("printTime"); - } - if (cmd == "printTimeLeft"){ - JSONObject printTime_json = new JSONObject(json.getString("progress")); - returnData = printTime_json.getString("printTimeLeft"); - } - if (cmd == "filepos"){ - JSONObject printTime_json = new JSONObject(json.getString("progress")); - returnData = printTime_json.getString("filepos"); + if (cmd == "printTime") { + returnData = jsonData_job_job_progress.getString("printTime"); } - if (cmd == "completion"){ - JSONObject printTime_json = new JSONObject(json.getString("progress")); - returnData = printTime_json.getString("completion"); + if (cmd == "printTimeLeft") { + returnData = jsonData_job_job_progress.getString("printTimeLeft"); } - //file - if (cmd == "name"){ - JSONObject printTime_json = new JSONObject(json.getString("job")); - JSONObject printTime_json2 = new JSONObject(printTime_json.getString("file")); - returnData = printTime_json2.getString("name"); + if (cmd == "filepos") { + returnData = jsonData_job_job_progress.getString("filepos"); } - if (cmd == "size"){ - JSONObject printTime_json = new JSONObject(json.getString("job")); - JSONObject printTime_json2 = new JSONObject(printTime_json.getString("file")); - String string1 = printTime_json2.getString("size"); - if (string1.equals("null") || string1.equals("")){ - returnData = "0"; - }else{ - returnData = string1; - } + if (cmd == "completion") { + returnData = jsonData_job_job_progress.getString("completion"); } - //job - if (cmd == "estimatedPrintTime"){ - JSONObject printTime_json = new JSONObject(json.getString("job")); - String string1 = printTime_json.getString("estimatedPrintTime"); + if (cmd == "name") { + String string1 = jsonData_job_job_file.getString("name"); if (string1.equals("null") || string1.equals("")){ - returnData = "0"; + returnData = "-"; }else{ returnData = string1; } } - //no under - if (cmd == "state"){ - if (json.getString("state").equals("")){ - returnData = "-"; - }else{ - returnData = json.getString("state"); - } - } - - } - if (job == "connetion"){ - - - } - if (job == "printer"){ - JSONObject json = new JSONObject(jsonData_printer); - JSONObject printTime_json = new JSONObject(json.getString("temps")); - - if (cmd == "Bactual") { - JSONObject temp = new JSONObject(printTime_json.getString("bed")); - String string1 = temp.getString("actual"); + if (cmd == "size") { + String string1 = jsonData_job_job_file.getString("size"); if (string1.equals("null") || string1.equals("")) { returnData = "0"; } else { returnData = string1; } } - if (cmd == "Btarget"){ - - JSONObject temp = new JSONObject(printTime_json.getString("bed")); - String string1 = temp.getString("target"); - if (string1.equals("null") || string1.equals("")){ - returnData = "0"; - }else{ - returnData = string1; - } - } - if (cmd == "actual") { - JSONObject temp = new JSONObject(printTime_json.getString("tool0")); - String string1 = temp.getString("actual"); + if (cmd == "estimatedPrintTime") { + String string1 = jsonData_job_job_job.getString("estimatedPrintTime"); if (string1.equals("null") || string1.equals("")) { returnData = "0"; } else { returnData = string1; } + } - if (cmd == "target"){ - JSONObject temp = new JSONObject(printTime_json.getString("tool0")); - String string1 = temp.getString("target"); - if (string1.equals("null") || string1.equals("")){ - returnData = "0"; - }else{ - returnData = string1; + //no under + if (cmd == "state") { + if (jsonData_job_job.getString("state").equals("")) { + returnData = "-"; + } else { + returnData = jsonData_job_job.getString("state"); } } - - } - if (job == "files"){ + if (job == "printer") { + if (jsonData_printer_status) { + if (cmd == "Bactual") { + JSONObject temp = new JSONObject(jsonData_printer_printer_temps.getString("bed")); + String string1 = temp.getString("actual"); + if (string1.equals("null") || string1.equals("")) { + returnData = "0"; + } else { + returnData = string1; + } + } + if (cmd == "Btarget") { - } - if (job == "version"){ - + JSONObject temp = new JSONObject(jsonData_printer_printer_temps.getString("bed")); + String string1 = temp.getString("target"); + if (string1.equals("null") || string1.equals("")) { + returnData = "0"; + } else { + returnData = string1; + } + } + if (cmd == "actual") { + JSONObject temp = new JSONObject(jsonData_printer_printer_temps.getString("tool0")); + String string1 = temp.getString("actual"); + if (string1.equals("null") || string1.equals("")) { + returnData = "0"; + } else { + returnData = string1; + } + } + if (cmd == "target") { + JSONObject temp = new JSONObject(jsonData_printer_printer_temps.getString("tool0")); + String string1 = temp.getString("target"); + if (string1.equals("null") || string1.equals("")) { + returnData = "0"; + } else { + returnData = string1; + } + } + }else{ + returnData = "0"; + } + } + if (job == "connetion") { + } + if (job == "files") { + } + if (job == "version") { + } + }catch(Exception e){ + e.printStackTrace(); + Log.e("OctoDroid", "Exception"); } - - - } catch (Exception e) { - e.printStackTrace(); - } } if (returnData.equals("null") || returnData.equals("")){ returnData = "0"; } + if (returnData.length() > 15){ + if (cmd == "state"){ + returnData = returnData.substring(0, 15); + returnData = returnData + "..."; + } + } if (returnData.length() > 20){ returnData = returnData.substring(0, 20); returnData = returnData + "..."; @@ -186,12 +228,20 @@ public static String getData(String job,String cmd){ } public static int getProgress(){ int returnData = 0; + double acom = 0; if (Activity.server_status) { JSONObject json = null; try { json = new JSONObject(jsonData_job); JSONObject printTime_json = new JSONObject(json.getString("progress")); - double acom = Double.parseDouble(printTime_json.getString("completion")); + String comp = printTime_json.getString("completion"); + if (comp.equals("") || comp.equals("null")){ + try { + acom = Double.parseDouble(comp); + }catch (NumberFormatException e){ + acom = 0; + } + } if (acom < 0){ returnData = 0; }else{ @@ -200,6 +250,7 @@ public static int getProgress(){ } catch (JSONException e) { e.printStackTrace(); + Log.e("OctoDroid","printStackTrace"); } } @@ -207,6 +258,7 @@ public static int getProgress(){ } public static void refreshJson(String ip, String api, String key){ + boolean nono = false; if (ip == null || ip.equals("")){ }else { @@ -222,30 +274,31 @@ public static void refreshJson(String ip, String api, String key){ HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); - if (statusCode == 200) { - Activity.server_status = true; - HttpEntity entity = response.getEntity(); - InputStream content = entity.getContent(); - BufferedReader reader = new BufferedReader(new InputStreamReader(content)); - String line; - while ((line = reader.readLine()) != null) { - builder.append(line); - } - } else { - Activity.server_status = false; + HttpEntity entity = response.getEntity(); + InputStream content = entity.getContent(); + BufferedReader reader = new BufferedReader(new InputStreamReader(content)); + String line; + while ((line = reader.readLine()) != null) { + builder.append(line); } } catch (ClientProtocolException e) { Log.e("OctoDroid", "ClientProtocolException"); Log.d("OctoDroid", ip); + nono = true; Activity.server_status = false; + return; } catch (IOException e) { Log.e("OctoDroid", "IOException"); Log.d("OctoDroid", ip); + nono = true; Activity.server_status = false; + return; } catch (IllegalStateException e){ Log.e("OctoDroid", "IllegalStateException"); Log.d("OctoDroid", ip); + nono = true; Activity.server_status = false; + return; } if (api.equals("job")){ jsonData_job = builder.toString(); @@ -253,6 +306,33 @@ public static void refreshJson(String ip, String api, String key){ if (api.equals("printer")){ jsonData_printer = builder.toString(); } + if (!nono) { + Activity.server_status = true; + } } } + private void sendError(String er){ + StringBuilder builder = new StringBuilder(); + HttpClient client = new DefaultHttpClient(); + HttpGet httpGet; + httpGet = new HttpGet("http://mariogrip.com/OctoPrint/api/error.php?e=" + er); + try { + HttpResponse response = client.execute(httpGet); + StatusLine statusLine = response.getStatusLine(); + int statusCode = statusLine.getStatusCode(); + if (statusCode == 200) { + HttpEntity entity = response.getEntity(); + InputStream content = entity.getContent(); + BufferedReader reader = new BufferedReader(new InputStreamReader(content)); + String line; + } + } catch (ClientProtocolException e) { + Log.e("OctoDroid", "ClientProtocolException"); + } catch (IOException e) { + Log.e("OctoDroid", "IOException"); + } catch (IllegalStateException e){ + Log.e("OctoDroid", "IllegalStateException"); + } + + } } diff --git a/app/src/main/java/com/mariogrip/octodroid/service.java b/app/src/main/java/com/mariogrip/octodroid/service.java new file mode 100644 index 0000000..4ba228e --- /dev/null +++ b/app/src/main/java/com/mariogrip/octodroid/service.java @@ -0,0 +1,104 @@ +package com.mariogrip.octodroid; + +import android.app.IntentService; +import android.app.NotificationManager; +import android.content.Context; +import android.content.Intent; +import android.media.RingtoneManager; +import android.net.Uri; +import android.support.v4.app.NotificationCompat; +import android.util.Log; + +import java.util.Timer; +import java.util.TimerTask; + +/** + * Created by mariogrip on 30.10.14. + */ + +public class service extends IntentService { + private Timer timer = new Timer(); + private TimerTask timerTask; + private Timer timer2 = new Timer(); + private TimerTask timerTask2; + private double complete; + private int intcom; + + public service() { + super("MyIntentService"); + } + + @Override + protected void onHandleIntent(Intent workIntent) { + + runner(); + } + + public void runner() { + timerTask = new TimerTask() { + @Override + public void run() { + if (Activity.server_status) { + get.refreshJson(Activity.ip, "job", Activity.key); + get.decodeJsonService(); + Log.d("OctoDroid Service", "runner" + Activity.printing); + + if (Activity.printing) { + Activity.printing = true; + timerTask.cancel(); + startPrintService(); + return; + } + if (get.getData("job", "state").equals("Printing") && Activity.printing == false) { + Activity.printing = true; + timerTask.cancel(); + startPrintService(); + return; + } + if (!get.getData("job", "state").equals("Printing") && Activity.printing == true) { + Activity.printing = false; + } + } + } + }; + timer.schedule(timerTask, 0, 3000); + + } + + protected void startPrintService() { + final int id = 1; + final NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); + Log.d("OctoDroid Service", "printService"); + mBuilder.setContentTitle("OctoDroid") + .setContentText("Print in progress") + .setSmallIcon(R.drawable.octoprint); + + timerTask2 = new TimerTask() { + @Override + public void run() { + Log.d("OctoDroid Service", "runner"); + get.refreshJson(Activity.ip, "job", Activity.key); + get.decodeJsonService(); + complete = Double.parseDouble(get.getData("job", "completion")); + + + if (!get.getData("job", "state").equals("Printing") && Activity.printing == true) { + Activity.printing = false; + Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + mBuilder.setContentText("Print complete") + .setSound(soundUri) + .setProgress(0, 0, false); + mNotifyManager.notify(id, mBuilder.build()); + timerTask2.cancel(); + runner(); + return; + } + + mBuilder.setProgress(100, (int) complete, false); + mNotifyManager.notify(id, mBuilder.build()); + } + }; + timer2.schedule(timerTask2, 0, 3000); + } +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 224e155..befbdd8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -28,6 +28,9 @@ Target Temp: Bed Current Temp: Bed Target Temp: + Send error report + Error report + Send anonymous error data diff --git a/app/src/main/res/xml/setting.xml b/app/src/main/res/xml/setting.xml index dd049ff..b009dfc 100644 --- a/app/src/main/res/xml/setting.xml +++ b/app/src/main/res/xml/setting.xml @@ -14,7 +14,7 @@ @@ -22,10 +22,19 @@ + + + + \ No newline at end of file