From bb6c80f7f39d4b948c4927fdeaf42c02ccc98972 Mon Sep 17 00:00:00 2001 From: Matthew Date: Thu, 24 Aug 2023 22:14:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 1 + app/src/main/cpp/CMakeLists.txt | 5 +- app/src/main/cpp/MicroPhoto.cpp | 50 +++-- app/src/main/cpp/PhoneDevice.cpp | 1 - app/src/main/cpp/PhoneDevice2.cpp | 1 - app/src/main/cpp/SystemUsage.h | 201 ++++++++++++++++++ .../xinyingpower/microphoto/MainActivity.java | 1 + .../microphoto/MicroPhotoService.java | 85 ++++++-- gradle.properties | 3 +- 9 files changed, 310 insertions(+), 38 deletions(-) create mode 100644 app/src/main/cpp/SystemUsage.h diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f0bd24e3..e201e81f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ + diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 5394f73c..58acfe94 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -34,6 +34,8 @@ set(OPENCV_EXTRA_MODULES_PATH D:/Workspace/Github/opencv_contrib/modules) project("microphoto") +# message(FATAL_ERROR "OpenCV ${OpenCV_DIR}") + find_package(OpenCV REQUIRED core imgproc highgui) if(OpenCV_FOUND) include_directories(${OpenCV_INCLUDE_DIRS}) @@ -281,12 +283,13 @@ add_library( # Sets the name of the library. ${TERM_CORE_ROOT}/SpecData_I1_SHX.cpp ${TERM_CORE_ROOT}/SpecData_XY.cpp ${TERM_CORE_ROOT}/SpecData_ZJ.cpp - ${TERM_CORE_ROOT}/TermClient.cpp ${TERM_CORE_ROOT}/Timer.cpp ${TERM_CORE_ROOT}/TimerThread.cpp ${TERM_CORE_ROOT}/Utils.cpp + ${TERM_CORE_ROOT}/Client/TerminalService.cpp ${TERM_CORE_ROOT}/Client/Terminal.cpp ${TERM_CORE_ROOT}/Client/Terminal_HN.cpp + ${TERM_CORE_ROOT}/Client/Terminal_AH.cpp ${TERM_CORE_ROOT}/Client/Terminal_HEN_ZZ.cpp ${TERM_CORE_ROOT}/Client/Terminal_HEN.cpp ${TERM_CORE_ROOT}/Client/Terminal_SHX.cpp diff --git a/app/src/main/cpp/MicroPhoto.cpp b/app/src/main/cpp/MicroPhoto.cpp index fcc42eeb..af626fd4 100644 --- a/app/src/main/cpp/MicroPhoto.cpp +++ b/app/src/main/cpp/MicroPhoto.cpp @@ -1,6 +1,7 @@ #include #include -#include +#include +#include #include "TerminalDevice.h" #include "PhoneDevice.h" #include "PhoneDevice2.h" @@ -88,7 +89,7 @@ Java_com_xinyingpower_microphoto_MainActivity_takePhoto( return JNI_TRUE; } -extern "C" JNIEXPORT jboolean JNICALL +extern "C" JNIEXPORT jlong JNICALL Java_com_xinyingpower_microphoto_MicroPhotoService_init( JNIEnv* env, jobject pThis, jstring appPath, jstring ip, jint port, jstring cmdid) { @@ -107,29 +108,36 @@ Java_com_xinyingpower_microphoto_MicroPhotoService_init( JavaVM* vm = NULL; jint ret = env->GetJavaVM(&vm); // const string& appPath, const string& termId, const string& server, unsigned short port, const string& bindIp - CTermClient& service = CTermClient::GetService(); + // CTerminal* pTerminal = reinterpret_cast(handler); // CTerminalDevice* device = new CTerminalDevice(vm, pThis); // CPhoneDevice2* device = new CPhoneDevice2(vm, pThis); + + CTerminal* pTerminal = NewTerminal(GetCurrentProtocol()); + CPhoneDevice* device = new CPhoneDevice(vm, pThis); - bool res = service.InitService(appPathStr, cmdidStr, ipStr, (unsigned short)port, "", device); + device->SetListener(pTerminal); + + pTerminal->InitServerInfo(appPathStr, cmdidStr, ipStr, port); + pTerminal->SetPacketSize(1 * 1024); // 1K + bool res = pTerminal->Startup(device); env->ReleaseStringUTFChars(appPath, appPathStr); env->ReleaseStringUTFChars(ip, ipStr); env->ReleaseStringUTFChars(cmdid, cmdidStr); - return res ? JNI_TRUE : JNI_FALSE; + return reinterpret_cast(pTerminal); } extern "C" JNIEXPORT jboolean JNICALL Java_com_xinyingpower_microphoto_MicroPhotoService_notifyToTakePhoto( JNIEnv* env, - jobject pThis, jint channel, jint preset, jlong scheduleTime, jstring path, jstring fileName, jboolean sendToCma) { + jobject pThis, jlong handler, jint channel, jint preset, jlong scheduleTime, jstring path, jstring fileName, jboolean sendToCma) { if (channel < 1 || channel > 0xFF) { return JNI_FALSE; } - CTerminal* pTerminal = CTermClient::GetService().GetTerminal(); + CTerminal* pTerminal = reinterpret_cast(handler); if (pTerminal == NULL) { return JNI_FALSE; @@ -144,9 +152,10 @@ Java_com_xinyingpower_microphoto_MicroPhotoService_notifyToTakePhoto( extern "C" JNIEXPORT jboolean JNICALL Java_com_xinyingpower_microphoto_MicroPhotoService_sendHeartbeat( JNIEnv* env, - jobject pThis) { + jobject pThis, + jlong handler) { - CTerminal* pTerminal = CTermClient::GetService().GetTerminal(); + CTerminal* pTerminal = reinterpret_cast(handler); if (pTerminal == NULL) { return JNI_FALSE; @@ -161,9 +170,10 @@ Java_com_xinyingpower_microphoto_MicroPhotoService_sendHeartbeat( extern "C" JNIEXPORT jboolean JNICALL Java_com_xinyingpower_microphoto_MicroPhotoService_fireTimeout( JNIEnv* env, - jobject pThis, jlong uid) { + jobject pThis, jlong handler, jlong uid) { - IDevice* dev = CTermClient::GetService().GetDevice(); + CTerminal* pTerminal = reinterpret_cast(handler); + IDevice* dev = pTerminal->GetDevice(); if (dev == NULL) { return JNI_FALSE; @@ -176,9 +186,12 @@ Java_com_xinyingpower_microphoto_MicroPhotoService_fireTimeout( extern "C" JNIEXPORT jboolean JNICALL Java_com_xinyingpower_microphoto_MicroPhotoService_uninit( JNIEnv* env, - jobject pThis) { + jobject pThis, jlong handler) { + + CTerminal* pTerminal = reinterpret_cast(handler); + pTerminal->Shutdown(); - CTermClient::GetService().ExitService(); + delete pTerminal; return JNI_TRUE; } @@ -187,9 +200,8 @@ Java_com_xinyingpower_microphoto_MicroPhotoService_uninit( extern "C" JNIEXPORT jlong JNICALL Java_com_xinyingpower_microphoto_MicroPhotoService_getHeartbeatDuration( JNIEnv* env, - jobject pThis) { + jobject pThis, jlong handler) { - // CTermClient::GetService().ExitService(); return 60000; } @@ -197,9 +209,9 @@ Java_com_xinyingpower_microphoto_MicroPhotoService_getHeartbeatDuration( extern "C" JNIEXPORT jlongArray JNICALL Java_com_xinyingpower_microphoto_MicroPhotoService_getPhotoTimeData( JNIEnv* env, - jobject pThis) { + jobject pThis, jlong handler) { - CTerminal* pTerminal = CTermClient::GetService().GetTerminal(); + CTerminal* pTerminal = reinterpret_cast(handler); if (pTerminal == NULL) { return NULL; @@ -263,9 +275,9 @@ Java_com_xinyingpower_microphoto_MicroPhotoService_getPhotoTimeData( extern "C" JNIEXPORT jlongArray JNICALL Java_com_xinyingpower_microphoto_MicroPhotoService_getNextScheduleItem( JNIEnv* env, - jobject pThis) { + jobject pThis, jlong handler) { - CTerminal* pTerminal = CTermClient::GetService().GetTerminal(); + CTerminal* pTerminal = reinterpret_cast(handler); if (pTerminal == NULL) { return NULL; diff --git a/app/src/main/cpp/PhoneDevice.cpp b/app/src/main/cpp/PhoneDevice.cpp index 86f0ea36..674017c4 100644 --- a/app/src/main/cpp/PhoneDevice.cpp +++ b/app/src/main/cpp/PhoneDevice.cpp @@ -19,7 +19,6 @@ #define LOG_TAG "CameraTestHelpers" #include "PhoneDevice.h" -#include "TermClient.h" #include #include diff --git a/app/src/main/cpp/PhoneDevice2.cpp b/app/src/main/cpp/PhoneDevice2.cpp index ee72878b..1da7ee22 100644 --- a/app/src/main/cpp/PhoneDevice2.cpp +++ b/app/src/main/cpp/PhoneDevice2.cpp @@ -19,7 +19,6 @@ #define LOG_TAG "CameraTestHelpers" #include "PhoneDevice2.h" -#include "TermClient.h" #include #include diff --git a/app/src/main/cpp/SystemUsage.h b/app/src/main/cpp/SystemUsage.h new file mode 100644 index 00000000..3497abd2 --- /dev/null +++ b/app/src/main/cpp/SystemUsage.h @@ -0,0 +1,201 @@ +/* --------------------------------------------------------------------- + * Luiz Carlos doleron @ 2021 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero Public License version 3 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Affero Public License for more details. + * ---------------------------------------------------------------------- + */ + +#ifndef _GET_CPU_USAGE_LINUX_ +#define _GET_CPU_USAGE_LINUX_ + +#include +#include +#include +#include + +namespace get_system_usage_linux +{ + + struct CPU_stats + { + // see http://www.linuxhowtos.org/manpages/5/proc.htm + int user; + int nice; + int system; + int idle; + int iowait; + int irq; + int softirq; + int steal; + int guest; + int guest_nice; + + int get_total_idle() + const { + return idle + iowait; + } + + int get_total_active() + const { + return user + nice + system + irq + softirq + steal + guest + guest_nice; + } + + }; + + struct Memory_stats + { + int total_memory; + int available_memory; + int total_swap; + int free_swap; + + float get_memory_usage() { + const float result = static_cast(total_memory - available_memory) / total_memory; + return result; + } + + float get_swap_usage() { + const float result = static_cast(total_swap - free_swap) / total_swap; + return result; + } + + }; + + inline CPU_stats read_cpu_data() + { + CPU_stats result; + std::ifstream proc_stat("/proc/stat"); + + if (proc_stat.good()) + { + std::string line; + getline(proc_stat, line); + + unsigned int *stats_p = (unsigned int *)&result; + std::stringstream iss(line); + std::string cpu; + iss >> cpu; + while (iss >> *stats_p) + { + stats_p++; + }; + } + + proc_stat.close(); + + return result; + } + + inline int get_val(const std::string &target, const std::string &content) { + int result = -1; + std::size_t start = content.find(target); + if (start != std::string::npos) { + int begin = start + target.length(); + std::size_t end = content.find("kB", start); + std::string substr = content.substr(begin, end - begin); + result = std::stoi(substr); + } + return result; + } + + inline Memory_stats read_memory_data() + { + Memory_stats result; + std::ifstream proc_meminfo("/proc/meminfo"); + + if (proc_meminfo.good()) + { + std::string content((std::istreambuf_iterator(proc_meminfo)), + std::istreambuf_iterator()); + + result.total_memory = get_val("MemTotal:", content); + result.total_swap = get_val("SwapTotal:", content); + result.free_swap = get_val("SwapFree:", content); + result.available_memory = get_val("MemAvailable:", content); + + } + + proc_meminfo.close(); + + return result; + } + + inline float get_cpu_usage(const CPU_stats &first, const CPU_stats &second) { + const float active_time = static_cast(second.get_total_active() - first.get_total_active()); + const float idle_time = static_cast(second.get_total_idle() - first.get_total_idle()); + const float total_time = active_time + idle_time; + return active_time / total_time; + } + + inline float get_disk_usage(const std::string & disk) { + struct statvfs diskData; + + statvfs(disk.c_str(), &diskData); + + auto total = diskData.f_blocks; + auto free = diskData.f_bfree; + auto diff = total - free; + + float result = static_cast(diff) / total; + + return result; + } + + //see https://unix.stackexchange.com/questions/304845/discrepancy-between-number-of-cores-and-thermal-zones-in-sys-class-thermal/342023 + + inline int find_thermalzone_index() { + int result = 0; + bool stop = false; + // 20 must stop anyway + for (int i = 0; !stop && i < 20; ++i) { + std::ifstream thermal_file("/sys/class/thermal/thermal_zone" + std::to_string(i) + "/type"); + + if (thermal_file.good()) + { + std::string line; + getline(thermal_file, line); + + if (line.compare("x86_pkg_temp") == 0) { + result = i; + stop = true; + } + + } else { + stop = true; + } + + thermal_file.close(); + } + return result; + } + + inline int get_thermalzone_temperature(int thermal_index) { + int result = -1; + std::ifstream thermal_file("/sys/class/thermal/thermal_zone" + std::to_string(thermal_index) + "/temp"); + + if (thermal_file.good()) + { + std::string line; + getline(thermal_file, line); + + std::stringstream iss(line); + iss >> result; + } else { + throw std::invalid_argument(std::to_string(thermal_index) + " doesn't refer to a valid thermal zone."); + } + + thermal_file.close(); + + return result; + } + +} + +#endif diff --git a/app/src/main/java/com/xinyingpower/microphoto/MainActivity.java b/app/src/main/java/com/xinyingpower/microphoto/MainActivity.java index 0945b25d..ec209187 100644 --- a/app/src/main/java/com/xinyingpower/microphoto/MainActivity.java +++ b/app/src/main/java/com/xinyingpower/microphoto/MainActivity.java @@ -69,6 +69,7 @@ public class MainActivity extends AppCompatActivity { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.FOREGROUND_SERVICE, Manifest.permission.READ_PHONE_STATE, + /*Manifest.permission.PACKAGE_USAGE_STATS,*/ /*Manifest.permission.SET_TIME,*/ }; boolean needRequire = false; diff --git a/app/src/main/java/com/xinyingpower/microphoto/MicroPhotoService.java b/app/src/main/java/com/xinyingpower/microphoto/MicroPhotoService.java index 8ab45f07..27978117 100644 --- a/app/src/main/java/com/xinyingpower/microphoto/MicroPhotoService.java +++ b/app/src/main/java/com/xinyingpower/microphoto/MicroPhotoService.java @@ -25,6 +25,7 @@ import android.telephony.CellLocation; import android.telephony.PhoneStateListener; import android.telephony.ServiceState; import android.telephony.SignalStrength; +import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; import android.telephony.cdma.CdmaCellLocation; import android.telephony.gsm.GsmCellLocation; @@ -37,6 +38,7 @@ import android.widget.Toast; import com.dowse.camera.client.DSCameraManager; import java.io.File; +import java.lang.reflect.Method; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; @@ -85,6 +87,23 @@ public class MicroPhotoService extends Service { private final static String FOREGROUND_CHANNEL_ID = "foreground_channel_id"; + public static final int SIGNAL_STRENGTH_NONE_OR_UNKNOWN = 0; + + public static final int SIGNAL_STRENGTH_POOR = 1; + + public static final int SIGNAL_STRENGTH_MODERATE = 2; + + public static final int SIGNAL_STRENGTH_GOOD = 3; + + public static final int SIGNAL_STRENGTH_GREAT = 4; + + public static final int NUM_SIGNAL_STRENGTH_BINS = 5; + + //无 ,低劣的 ,适度的 ,好 ,优异的 + public static final String[] SIGNAL_STRENGTH_NAMES = { + "none", "poor", "moderate", "good", "great" + }; + public static class STATE_SERVICE { public static final int CONNECTED = 10; public static final int NOT_CONNECTED = 0; @@ -165,7 +184,7 @@ public class MicroPhotoService extends Service { // String ip = "192.168.50.50"; int port = 6891; String cmdid = "XY-ANDROIDSIM-002"; - init(appPath, ip, port, cmdid); + mHandler = init(appPath, ip, port, cmdid); // registerHeartbeatTimer(getHeartbeatDuration()); @@ -187,6 +206,9 @@ public class MicroPhotoService extends Service { private MicroPhotoService mService; + public AlarmReceiver() { + mService = null; + } public AlarmReceiver(MicroPhotoService service) { mService = service; } @@ -195,7 +217,7 @@ public class MicroPhotoService extends Service { String action = intent.getAction(); if (TextUtils.equals(ACTION_HEARTBEAT, action)) { Log.i(TAG, "receiver ACTION=" + action); - mService.sendHeartbeat(); + mService.sendHeartbeat(mService.mHandler); mService.registerHeartbeatTimer(); } else if (TextUtils.equals(ACTION_TAKE_PHOTO, action)) { long ts = intent.getLongExtra(EXTRA_PARAM_TIME, 0); @@ -207,7 +229,7 @@ public class MicroPhotoService extends Service { int channel = (int) ((val & 0xFF0000L) >> 16); int preset = (int) ((val & 0xFF00L) >> 8); - mService.notifyToTakePhoto(channel, preset, ts, mService.buildPhotoDir(channel), mService.buildPhotoFileName(channel, preset, ts), true); + mService.notifyToTakePhoto(mService.mHandler, channel, preset, ts, mService.buildPhotoDir(channel), mService.buildPhotoFileName(channel, preset, ts), true); } } @@ -228,11 +250,11 @@ public class MicroPhotoService extends Service { boolean photoOrVideo = intent.getBooleanExtra(EXTRA_PARAM_PHOTO_OR_VIDEO, true); long ts = System.currentTimeMillis() / 1000; - mService.notifyToTakePhoto(channel, preset, ts, mService.buildPhotoDir(channel), mService.buildPhotoFileName(channel, preset, ts), photoOrVideo); + mService.notifyToTakePhoto(mService.mHandler, channel, preset, ts, mService.buildPhotoDir(channel), mService.buildPhotoFileName(channel, preset, ts), photoOrVideo); } else if (TextUtils.equals(ACTION_TIMEOUT, action)) { long uid = intent.getLongExtra(EXTRA_PARAM_TIMER_UID, 0); Log.i(TAG, "Timeout:" + uid); - mService.fireTimeout(uid); + mService.fireTimeout(mService.mHandler, uid); int timeout = intent.getIntExtra(EXTRA_PARAM_TIMEOUT, 0); Long uidObj = Long.valueOf(uid); @@ -311,6 +333,30 @@ public class MicroPhotoService extends Service { return registerTimer(pendingIntent, uid, timeout); } + private void setDefaultDataSubId(int subId) { + SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); + try { + Method method = subscriptionManager.getClass().getDeclaredMethod("setDefaultDataSubId", int.class); + method.invoke(subscriptionManager, subId); + TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); + Method method1 = telephonyManager.getClass().getDeclaredMethod("setDataEnabled", boolean.class); + method1.invoke(telephonyManager, true); + } catch (Exception e) { + Log.e(TAG, "wjz debug setDefaultDataSubId: error is " + e.getMessage()); + } + } + + private int getDefaultDataSubId() { + SubscriptionManager subscriptionManager = (SubscriptionManager) getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); + try { + Method method = subscriptionManager.getClass().getDeclaredMethod("getDefaultDataSubscriptionId"); + return (int) method.invoke(subscriptionManager); + } catch (Exception e) { + Log.e(TAG, "wjz debug getDefaultDataSubId: error is " + e.getMessage()); + } + return 0; + } + public boolean registerTimer(PendingIntent pendingIntent, long uid, int timeout) { AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); @@ -339,7 +385,7 @@ public class MicroPhotoService extends Service { private boolean registerCaptureSchedule(long startTime, long baseTime) { - long[] photoTimeData = getPhotoTimeData(); + long[] photoTimeData = getPhotoTimeData(mHandler); if (photoTimeData == null) { return false; } @@ -671,7 +717,9 @@ public class MicroPhotoService extends Service { stateService = STATE_SERVICE.NOT_CONNECTED; - uninit(); + uninit(mHandler); + mHandler = 0; + DSCameraManager.getInstace().unInit(); super.onDestroy(); } @@ -711,15 +759,22 @@ public class MicroPhotoService extends Service { } + /* + TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); +// for example value of first element +CellInfoGsm cellInfoGsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0); +CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength(); +cellSignalStrengthGsm.getDbm(); + */ - protected native boolean init(String appPath, String ip, int port, String cmdid); - protected native long getHeartbeatDuration(); - protected native long[] getPhotoTimeData(); - protected native long[] getNextScheduleItem(); - protected native boolean notifyToTakePhoto(int channel, int preset, long scheduleTime, String path, String fileName, boolean sendToCma); - protected native boolean sendHeartbeat(); - protected native boolean fireTimeout(long uid); - protected native boolean uninit(); + protected native long init(String appPath, String ip, int port, String cmdid); + protected native long getHeartbeatDuration(long handler); + protected native long[] getPhotoTimeData(long handler); + protected native long[] getNextScheduleItem(long handler); + protected native boolean notifyToTakePhoto(long handler, int channel, int preset, long scheduleTime, String path, String fileName, boolean sendToCma); + protected native boolean sendHeartbeat(long handler); + protected native boolean fireTimeout(long handler, long uid); + protected native boolean uninit(long handler); protected long mHandler = 0; private AlarmReceiver alarmReceiver = null; diff --git a/gradle.properties b/gradle.properties index f24d9361..20db1957 100644 --- a/gradle.properties +++ b/gradle.properties @@ -18,6 +18,7 @@ android.nonTransitiveRClass=true android.useAndroidX=true android.enableJetifier=true -opencvsdk=D:/Workspace/deps/opencv-mobile-4.6.0-android +# opencvsdk=D:/Workspace/deps/opencv-mobile-4.8.0-android +opencvsdk=D:/Workspace/deps/opencv-v5 asioroot=D:/Workspace/deps/asio-1.28.0 evpproot=D:/Workspace/Github/evpp \ No newline at end of file