From cc2e412a29d9d8e05b6baabdd62434f34d4d2eb3 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sun, 14 Apr 2024 22:25:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=BF=90=E7=BB=B4=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xypower/mpmaster/AppMaster.java | 74 ++++++++ .../java/com/xypower/mpmaster/DBHandler.java | 172 ++++++++++++++++++ .../com/xypower/mpmaster/MainActivity.java | 4 - .../com/xypower/mpmaster/MpMasterService.java | 87 ++++----- 4 files changed, 280 insertions(+), 57 deletions(-) create mode 100644 mpmaster/src/main/java/com/xypower/mpmaster/DBHandler.java diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index 764a30bc..198dce4c 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -17,6 +17,7 @@ import com.xypower.common.HttpRequest; import com.xypower.common.InetAddressUtils; import com.xypower.common.JSONUtils; import com.xypower.common.MicroPhotoContext; +import com.xypower.common.ThermalInfoUtil; import com.xypower.common.ZipUtils; import org.json.JSONArray; @@ -113,6 +114,13 @@ public class AppMaster { HttpURLConnection httpURLConnection = null; InputStream inputStream = null; + Date now = new Date(); + now.setHours(0); + now.setMinutes(0); + now.setSeconds(0); + + long startTime = now.getTime() / 1000; + long endTime = startTime + 86400 - 1; try { if (masterUrl.indexOf('?') != -1) { @@ -139,6 +147,47 @@ public class AppMaster { postParams.add(new Pair("oid", mService.getSerialNo())); postParams.add(new Pair("maintainVersion", mService.getMasterAppVersion())); + 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; + } + } + + try { + MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(mService); + if (appConfig != null) { + postParams.add(new Pair("heartbeatDuration", Integer.toString(appConfig.heartbeat))); + postParams.add(new Pair("cma", appConfig.server + ":" + Integer.toString(appConfig.port))); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + try { + postParams.add(new Pair("mainBoardTmp", ThermalInfoUtil.getCPUTermperature())); + } catch (Exception ex) { + ex.printStackTrace(); + } + + try { + String battary = getBatteryVoltage() + "V/" + getChargingBatteryVoltage() + "V"; + postParams.add(new Pair("battary", battary)); + } catch (Exception ex) { + ex.printStackTrace(); + } + // postParams(httpURLConnection.getOutputStream(), postParams); buildParams(httpURLConnection.getOutputStream(), postParams); httpURLConnection.connect(); @@ -160,6 +209,30 @@ public class AppMaster { }).start(); } + private String getBatteryVoltage() { + int val = 0; + for (int idx = 0; idx < 3; idx++) { + val = SysApi.getBatteryVoltage(); + if (val > 0) { + return Integer.toString(val / 1000) + "." + Integer.toString((val % 1000) / 100); + } + } + + return ""; + } + + private String getChargingBatteryVoltage() { + int val = 0; + for (int idx = 0; idx < 3; idx++) { + val = SysApi.getChargingBusVoltage(); + if (val > 0) { + return Integer.toString(val / 1000) + "." + Integer.toString((val % 1000) / 100); + } + } + + return ""; + } + private void process(String content) { if (TextUtils.isEmpty(content)) { return; @@ -403,6 +476,7 @@ public class AppMaster { sendResult(cid, 1, action, action + ":" + mCmdid); Context context = mService.getApplicationContext(); Log.i(TAG, "Upgrade APP: " + url); + SysApi.installApk(context, apkPath, context.getPackageName(), true); } } diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/DBHandler.java b/mpmaster/src/main/java/com/xypower/mpmaster/DBHandler.java new file mode 100644 index 00000000..b952af76 --- /dev/null +++ b/mpmaster/src/main/java/com/xypower/mpmaster/DBHandler.java @@ -0,0 +1,172 @@ +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/MainActivity.java b/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java index b70b1d08..99ca2b27 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java @@ -158,10 +158,6 @@ public class MainActivity extends AppCompatActivity { Intent intent = new Intent(context, MpMasterService.class); intent.setAction(MpMasterService.ACTION_START); intent.putExtra("cmdid", appConfig.cmdid); - intent.putExtra("server", appConfig.server); - intent.putExtra("port", appConfig.port); - intent.putExtra("protocol", appConfig.protocol); - intent.putExtra("networkProtocol", appConfig.networkProtocol); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent); diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java index 8825d277..1dfced8c 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java @@ -51,42 +51,21 @@ import java.util.Map; public class MpMasterService extends Service { public static final String TAG = "MpMaster"; - - public static final int MSG_WHAT_LOG = 10; - - public final static int MSG_WHAT_NETWORK_CHANGE = 20; - - public final static int MSG_WHAT_SERVICE_STATUS_CHANGE = 30; - public final static int MSG_WHAT_SENDING_HB = 40; - - public final static int MSG_WHAT_MAX = 1000; - - private static final String ALARM_EVENT = "com.xinyingpower.mp.MicroPhotoService.AlarmReceiver"; public static final int NOTIFICATION_ID_FOREGROUND_SERVICE = 8466503; - // public static final int NOTIFICATION_ID_FOREGROUND_SERVICE = 0; public static final String ACTION_MSG_BROADCAST = "ACT_MSG_BROADCAST"; public static final String ACTION_START = "com.xypower.mpmaster.ACT_START"; public static final String ACTION_STOP = "com.xypower.mpmaster.ACT_STOP"; public static final String ACTION_MAIN = "com.xypower.mpmaster.ACT_MAIN"; + private static final String ACTION_UPDATE_CONFIGS = "com.xypower.mpmaster.ACT_UPD_CFG"; + private static final String ACTION_HEARTBEAT = "com.xypower.mpmaster.ACT_HB"; private static final String ACTION_TAKE_PHOTO = "com.xypower.mpapp.ACT_TP"; - //private static final String ACTION_TAKE_PHOTO_MANUALLY = "ACT_TP_M"; - //private static final String ACTION_TIMEOUT = "ACT_TIMEOUT"; - private static final String EXTRA_PARAM_CHANNEL = "Channel"; - private static final String EXTRA_PARAM_PRESET = "Preset"; - private static final String EXTRA_PARAM_PHOTO_OR_VIDEO = "PhotoOrVideo"; private static final String EXTRA_PARAM_SCHEDULES = "Schedules"; private static final String EXTRA_PARAM_SCHEDULE = "Schedule_"; private static final String EXTRA_PARAM_TIME = "Time"; - // private static String EXTRA_PARAM_FILENAME = "FileName"; - private static final String EXTRA_PARAM_TIMER_UID = "TimerUid"; - // private static String EXTRA_PARAM_TIMER_TYPE = "TimerType"; - private static final String EXTRA_PARAM_TIMEOUT = "Timeout"; - private static final String EXTRA_PARAM_TIMES = "Times"; - private static final String EXTRA_PARAM_ELASPED_TIMES = "ElapsedTimes"; private static final String FOREGROUND_CHANNEL_ID = "foreground_channel_id"; public static class STATE_SERVICE { public static final int CONNECTED = 10; @@ -94,35 +73,30 @@ public class MpMasterService extends Service { } private static int mStateService = STATE_SERVICE.NOT_CONNECTED; - private boolean mMntnMode = false; private boolean mQuickHbMode = false; + private boolean mUsingAbsHbTime = false; private String mCmdid = ""; private NotificationManager mNotificationManager; - private int mQuickHeartbeatDuration = 60000; // 1m = 60 * 1000 ms - private int mHeartbeatDuration = 600000; // 10m = 10 * 60 * 1000 ms - private long mNextHeartbeatTime = 0; - - private final Map mTimers = new HashMap<>(); + private int mQuickHeartbeatDuration = 60; // 1m = 60 s + private int mHeartbeatDuration = 600; // 10m = 10 * 60s - protected long mNativeHandle = 0; private AlarmReceiver mAlarmReceiver = null; private ScreenActionReceiver mScreenaAtionReceiver = null; private UpdateReceiver mUpdateReceiver = null; private ServiceHandler mHander = null; - private Messenger mMessenger = null; private String mModelName = null; private static String mMpAppVersion = null; - private static String mMpMasterVersion = null; private String mSerialNo = null; private long mTimeToStartMpApp = 0; private long mTimeOfMpAppAlive = 1800000; // 30minutes + private int mAbsHeartbeatTimes[] = null; public MpMasterService() { } @@ -131,6 +105,7 @@ public class MpMasterService extends Service { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } + @Override public void onCreate() { super.onCreate(); @@ -170,6 +145,7 @@ public class MpMasterService extends Service { // intentFilter.addAction(ACTION_TIMEOUT); // intentFilter.addAction(ACTION_TAKE_PHOTO_MANUALLY); intentFilter.addAction(ACTION_MSG_BROADCAST); + intentFilter.addAction(ACTION_UPDATE_CONFIGS); registerReceiver(mAlarmReceiver, intentFilter); } @@ -181,7 +157,6 @@ public class MpMasterService extends Service { intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); intentFilter.addDataScheme("package"); registerReceiver(mUpdateReceiver, intentFilter); - } // AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); @@ -192,6 +167,7 @@ public class MpMasterService extends Service { registerHeartbeatTimer(); } + @Override public void onDestroy() { @@ -221,6 +197,15 @@ public class MpMasterService extends Service { } } + protected void loadConfig() { + MicroPhotoContext.MasterConfig masterConfig = MicroPhotoContext.getMasterConfig(getApplicationContext()); + mMntnMode = masterConfig.mntnMode != 0; + mQuickHbMode = masterConfig.quickHbMode != 0; + mUsingAbsHbTime = masterConfig.usingAbsHbTime != 0; + mHeartbeatDuration = masterConfig.heartbeat * 10; // minute to second + mAbsHeartbeatTimes = masterConfig.absHeartbeats; + } + public String getCmdid() { return mCmdid; } @@ -233,7 +218,6 @@ public class MpMasterService extends Service { return; } - String appPath = MicroPhotoContext.buildMpAppDir(context); long ts = System.currentTimeMillis(); if (ts - mTimeToStartMpApp < 30000) { @@ -314,8 +298,8 @@ public class MpMasterService extends Service { String masterUrl = MicroPhotoContext.DEFAULT_MASTER_URL; MicroPhotoContext.MasterConfig masterConfig = MicroPhotoContext.getMasterConfig(getApplicationContext()); - if (!TextUtils.isEmpty(masterConfig.url)) { - masterUrl = masterConfig.url; + if (!TextUtils.isEmpty(masterConfig.getUrl())) { + masterUrl = masterConfig.getUrl(); } MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(getApplicationContext()); @@ -342,13 +326,13 @@ public class MpMasterService extends Service { mService.startMaster(); mService.startMpApp(); - } else if (TextUtils.equals(ACTION_MSG_BROADCAST, action)) { - - int what = intent.getIntExtra("what", 0); - int data = intent.getIntExtra("data", 0); - - if (what == MSG_WHAT_SENDING_HB) { - // mService.sendHeartbeat(mService.mNativeHandle); + } else if (TextUtils.equals(ACTION_UPDATE_CONFIGS, action)) { + int restart = intent.getIntExtra("restart", 0); + Log.i(TAG, "UPD CFG Fired ACTION=" + action + " restart=" + restart); + if (restart != 0) { + mService.restartApp(context, context.getPackageName()); + } else { + mService.loadConfig(); } } } @@ -363,17 +347,19 @@ public class MpMasterService extends Service { } private void registerHeartbeatTimer() { - // 创建延迟意图 Intent alarmIntent = new Intent(); alarmIntent.setAction(ACTION_HEARTBEAT); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); - long timeout = mQuickHbMode ? mQuickHeartbeatDuration : mHeartbeatDuration; - alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + timeout, pendingIntent); + long timeout = mHeartbeatDuration; + if (mMntnMode && mQuickHbMode) { + timeout = mQuickHeartbeatDuration; + } else { - mNextHeartbeatTime = System.currentTimeMillis() + mHeartbeatDuration; - // alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeout, pendingIntent); + } + + alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + timeout * 1000, pendingIntent); } private static void registerPhotoTimer(Context context, int channel, int preset, long ts, long timeout, List schedules) { @@ -423,14 +409,9 @@ public class MpMasterService extends Service { connect(); registerReceiver(mScreenaAtionReceiver, mScreenaAtionReceiver.getFilter()); - if (intent.hasExtra("messenger")) { - mMessenger = intent.getParcelableExtra("messenger"); - } String appPath = MicroPhotoContext.buildAppDir(this.getApplicationContext()); - String server = intent.getStringExtra("server"); - int port = intent.getIntExtra("port", 0); String cmdid = intent.getStringExtra("cmdid"); mCmdid = cmdid;