From 5ce977e846fdeca67944fdb462aa37eb7aa099aa Mon Sep 17 00:00:00 2001 From: Matthew Date: Sat, 20 Apr 2024 23:59:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=90=E7=BB=B4=E8=8E=B7=E5=8F=96=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=E6=95=B0=E6=8D=AE=E6=96=B9=E5=BC=8F=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E6=95=B0=E6=8D=AE=E5=BA=93=E5=86=B2?= =?UTF-8?q?=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xypower/common/FileUtils.java | 24 +++ mpmaster/build.gradle | 1 + mpmaster/src/main/cpp/CMakeLists.txt | 2 + mpmaster/src/main/cpp/mpmaster.cpp | 31 ++++ .../java/com/xypower/mpmaster/AppMaster.java | 65 +++++-- .../java/com/xypower/mpmaster/DBHandler.java | 172 ------------------ .../com/xypower/mpmaster/MpMasterService.java | 1 + 7 files changed, 106 insertions(+), 190 deletions(-) delete mode 100644 mpmaster/src/main/java/com/xypower/mpmaster/DBHandler.java diff --git a/common/src/main/java/com/xypower/common/FileUtils.java b/common/src/main/java/com/xypower/common/FileUtils.java index 62367094..fbd149a9 100644 --- a/common/src/main/java/com/xypower/common/FileUtils.java +++ b/common/src/main/java/com/xypower/common/FileUtils.java @@ -56,4 +56,28 @@ public class FileUtils { return true; } + public static byte[] int2Byte(int intValue) { + byte[] b = new byte[4]; + for (int i = 0; i < 4; i++) { + b[i] = (byte) (intValue >> 8 * (3 - i) & 0xFF); + //System.out.print(Integer.toBinaryString(b[i])+" "); + //System.out.print((b[i] & 0xFF) + " "); + } + return b; + } + + /** + * 4位字节数组转换为整型 + * @param b + * @return + */ + public static int byte2Int(byte[] b) { + int intValue = 0; + for (int i = 0; i < b.length; i++) { + intValue += (b[i] & 0xFF) << (8 * (3 - i)); + } + return intValue; + } + + } diff --git a/mpmaster/build.gradle b/mpmaster/build.gradle index 247bcd8c..5f3bd27e 100644 --- a/mpmaster/build.gradle +++ b/mpmaster/build.gradle @@ -27,6 +27,7 @@ android { cmake { abiFilters 'arm64-v8a' cppFlags '' + arguments "-DTERM_CORE_ROOT=" + coreroot } } } diff --git a/mpmaster/src/main/cpp/CMakeLists.txt b/mpmaster/src/main/cpp/CMakeLists.txt index e90e3102..4b8738f8 100644 --- a/mpmaster/src/main/cpp/CMakeLists.txt +++ b/mpmaster/src/main/cpp/CMakeLists.txt @@ -7,6 +7,7 @@ cmake_minimum_required(VERSION 3.18.1) # Declares and names the project. +add_definitions(-DTERMINAL_CLIENT) project("mpmaster") @@ -14,6 +15,7 @@ project("mpmaster") # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. +include_directories(${TERM_CORE_ROOT}) add_library( # Sets the name of the library. mpmaster diff --git a/mpmaster/src/main/cpp/mpmaster.cpp b/mpmaster/src/main/cpp/mpmaster.cpp index c01df002..e17feda4 100644 --- a/mpmaster/src/main/cpp/mpmaster.cpp +++ b/mpmaster/src/main/cpp/mpmaster.cpp @@ -21,10 +21,14 @@ #include #include +#include + #define IOT_PARAM_WRITE 0xAE #define IOT_PARAM_READ 0xAF #define MAX_STRING_LEN 32 +#define PATH_MPAPP_STATS "/sdcard/com.xypower.mpapp/data/stats/" + typedef struct { int cmd; @@ -52,3 +56,30 @@ Java_com_xypower_mpmaster_MpMasterService_getInt(JNIEnv* env, jclass cls, jint c return -1; } +extern "C" JNIEXPORT jintArray JNICALL +Java_com_xypower_mpmaster_MpMasterService_getStats(JNIEnv* env, jclass cls, jlong ts) { + std::string path = PATH_MPAPP_STATS; + path += std::to_string(ts); + + FILE* file = fopen(path.c_str(), "r"); + if (file == NULL) + { + return NULL; + } + + MP_STATS stats = { 0 }; + size_t length = fread(&stats, 1, sizeof(stats), file); + fclose(file); + if (length != sizeof(stats)) + { + return NULL; + } + + size_t intLength = sizeof(stats) / sizeof(jint); + jintArray result = env->NewIntArray(intLength); + env->SetIntArrayRegion(result, 0, intLength, (const jint*)&stats); + return result; + +} + + diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index 5f3a473a..3435abc8 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -218,24 +218,7 @@ public class AppMaster { } postParams.add(new Pair("freeROM", getFreeROM())); - - DBHandler dbHandler = null; - try { - dbHandler = new DBHandler(mService); - dbHandler.readStats(startTime, endTime, postParams); - dbHandler.close(); - } catch (Exception ex) { - ex.printStackTrace(); - } finally { - if (dbHandler != null) { - try { - dbHandler.close(); - } catch (Exception ex) { - ex.printStackTrace(); - } - dbHandler = null; - } - } + buildStats(startTime, postParams); try { MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(mService); @@ -278,6 +261,52 @@ public class AppMaster { } } + private void buildStats(long startTime, List> stats) { + + try { + /* + struct MP_STATS + { + int hb; + int hbAck; + int scheduledCount; + int takingTimes; + int succeededPhotos; + int failedPhotos; + int failedTimes; + int uploaded; + int devReboots; + int appReboots; + }; + */ + + int[] items = MpMasterService.getStats(startTime); + if (items == null || items.length != 10) { + return; + } + + stats.add(new Pair("numberOfHb", Integer.toString(items[0]))); + stats.add(new Pair("numberOfHbAck", Integer.toString(items[1]))); + + stats.add(new Pair("recv", Integer.toString(items[2]))); + stats.add(new Pair("photoTimes", Integer.toString(items[3]))); + stats.add(new Pair("success", Integer.toString(items[4]))); + stats.add(new Pair("failure", Integer.toString(items[5]))); + stats.add(new Pair("failedTimes", Integer.toString(items[6]))); + stats.add(new Pair("uploads", Integer.toString(items[7]))); + + if (items[8] > 0) { + stats.add(new Pair("rebootTimes", Integer.toString(items[8]))); + } + + if (items[9] > 0) { + stats.add(new Pair("i1RebootTimes", Integer.toString(items[9]))); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + } + private String getBatteryVoltage() { int val = 0; for (int idx = 0; idx < 3; idx++) { diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/DBHandler.java b/mpmaster/src/main/java/com/xypower/mpmaster/DBHandler.java deleted file mode 100644 index b952af76..00000000 --- a/mpmaster/src/main/java/com/xypower/mpmaster/DBHandler.java +++ /dev/null @@ -1,172 +0,0 @@ -package com.xypower.mpmaster; - -import android.content.ContentValues; -import android.content.Context; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.database.sqlite.SQLiteOpenHelper; -import android.util.Pair; - -import com.xypower.common.MicroPhotoContext; - -import java.util.ArrayList; -import java.util.Hashtable; -import java.util.List; -import java.util.Map; - -public class DBHandler extends SQLiteOpenHelper { - - // creating a constant variables for our database. - // below variable is for our database name. - private static final String DB_NAME = "/sdcard/" + MicroPhotoContext.PACKAGE_NAME_MPAPP + "/data/App.db"; - - // below int is our database version - private static final int DB_VERSION = 1; - - // below variable is for our table name. - private static final String TABLE_NAME_SCHEDULES = "Schedules"; - - // below variable is for our table name. - private static final String TABLE_NAME_HEARTBEATS = "Heartbeats"; - - private static final String TABLE_NAME_REBOOTS = "Reboots"; - - // creating a constructor for our database handler. - public DBHandler(Context context) { - super(context, DB_NAME, null, DB_VERSION); - } - - // below method is for creating a database by running a sqlite query - @Override - public void onCreate(SQLiteDatabase db) { - // on below line we are creating - // an sqlite query and we are - // setting our column names - // along with their data types. - } - - // we have created a new method for reading all the courses. - public boolean readStats(long startTime, long endTime, List> stats) { - // on below line we are creating a - // database for reading our database. - SQLiteDatabase db = this.getReadableDatabase(); - - // on below line we are creating a cursor with query to read data from database. - Cursor cursor = db.rawQuery("SELECT scheduleTime,channel,preset,scheduled,takingTime,result,retries,postTime FROM " + TABLE_NAME_SCHEDULES + " WHERE scheduleTime >=" + Long.toString(startTime) + " AND scheduleTime<=" + Long.toString(endTime), null); - - int scheduledCount = 0; - int takingTimes = 0; - int retries = 0; - int succeededPhotos = 0; - int failedPhotos = 0; - int failedTimes = 0; - int scheduled = 0; - int uploaded = 0; - int photoResult = 0; - long photoTime = 0; - - // moving our cursor to first position. - if (cursor.moveToFirst()) { - do { - scheduled = cursor.getInt(3); - photoTime = cursor.getLong(4); - if (scheduled != 0) - { - scheduledCount ++; - } - if (photoTime != 0) - { - takingTimes++; - - photoResult = cursor.getInt(5); - if (photoResult != 0) - { - succeededPhotos++; - } - else - { - failedPhotos++; - } - retries = cursor.getInt(6); - if (retries > 0) - { - failedTimes += retries - ((photoResult == 0) ? 0 : 1); - } - if (cursor.getInt(7) != 0) - { - uploaded++; - } - } - } while (cursor.moveToNext()); - // moving our cursor to next. - } - // at last closing our cursor - // and returning our array list. - cursor.close(); - cursor = null; - - cursor = db.rawQuery("SELECT COUNT(hb),SUM(CASE WHEN ack=0 THEN 0 ELSE 1 END) FROM " + TABLE_NAME_HEARTBEATS + " WHERE hb >=" + Long.toString(startTime) + " AND hb<=" + Long.toString(endTime), null); - - // moving our cursor to first position. - int hb = 0; - int hbAck = 0; - if (cursor.moveToFirst()) { - hb = cursor.getInt(0); - hbAck = cursor.getInt(1); - } - // at last closing our cursor - // and returning our array list. - cursor.close(); - cursor = null; - - cursor = db.rawQuery("SELECT COUNT(*),appType FROM " + TABLE_NAME_REBOOTS + " WHERE rebootTime >=" + Long.toString(startTime) + " AND rebootTime<=" + Long.toString(endTime) + " GROUP BY appType", null); - - // moving our cursor to first position. - int appReboots = 0; - int systemReboots = 0; - int appType = 0; - - if (cursor.moveToFirst()) { - do { - appType = cursor.getInt(1); - if (appType == 0) { - appReboots = cursor.getInt(0); - } else if (appType == 1) { - systemReboots = cursor.getInt(0); - } - } while (cursor.moveToNext()); - // moving our cursor to next. - } - // at last closing our cursor - // and returning our array list. - cursor.close(); - cursor = null; - - stats.add(new Pair("numberOfHb", Integer.toString(hb))); - stats.add(new Pair("numberOfHbAck", Integer.toString(hbAck))); - - stats.add(new Pair("recv", Integer.toString(scheduledCount))); - stats.add(new Pair("photoTimes", Integer.toString(takingTimes))); - stats.add(new Pair("success", Integer.toString(succeededPhotos))); - stats.add(new Pair("failure", Integer.toString(succeededPhotos))); - stats.add(new Pair("uploads", Integer.toString(uploaded))); - - if (systemReboots > 0) { - stats.add(new Pair("rebootTimes", Integer.toString(systemReboots))); - } - - if (appReboots > 0) { - stats.add(new Pair("i1RebootTimes", Integer.toString(appReboots))); - } - - return true; - } - - @Override - public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - // this method is called to check if the table exists already. - - } - - -} diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java index 2fe86968..d37bb488 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java @@ -736,6 +736,7 @@ public class MpMasterService extends Service { public native static int getInt(int cmd); + public native static int[] getStats(long ts); ////////////////////////GPS////////////////////