Commit 1dee9de5 by pye52

wifi及shell指令执行失败后不再重试,并且都返回Resul.Success

parent a41816e3
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
android { android {
compileSdkVersion 29 compileSdkVersion 30
buildToolsVersion "29.0.3" buildToolsVersion "30.0.1"
defaultConfig { defaultConfig {
applicationId "com.bgycc.smartcanteen" applicationId "com.bgycc.smartcanteen"
minSdkVersion 22 minSdkVersion 22
targetSdkVersion 29 targetSdkVersion 30
versionCode 140 versionCode 141
versionName "1.4.0" versionName "1.4.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk { ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips" abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
...@@ -109,7 +109,7 @@ dependencies { ...@@ -109,7 +109,7 @@ dependencies {
def lifecycle_version = "2.2.0" def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
def work_version = "2.3.4" def work_version = "2.4.0"
implementation "androidx.work:work-runtime:$work_version" implementation "androidx.work:work-runtime:$work_version"
implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
...@@ -121,7 +121,7 @@ dependencies { ...@@ -121,7 +121,7 @@ dependencies {
def okhttp_version = "4.7.2" def okhttp_version = "4.7.2"
implementation "com.squareup.okhttp3:okhttp:$okhttp_version" implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version" implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"
def retrofit_version = "2.8.1" def retrofit_version = '2.9.0'
implementation "com.squareup.retrofit2:retrofit:$retrofit_version" implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version" implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation "com.squareup.retrofit2:converter-scalars:$retrofit_version" implementation "com.squareup.retrofit2:converter-scalars:$retrofit_version"
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
package="com.bgycc.smartcanteen"> package="com.bgycc.smartcanteen">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" />
......
...@@ -4,6 +4,7 @@ import androidx.work.BackoffPolicy; ...@@ -4,6 +4,7 @@ import androidx.work.BackoffPolicy;
import androidx.work.Data; import androidx.work.Data;
import androidx.work.ListenableWorker; import androidx.work.ListenableWorker;
import androidx.work.OneTimeWorkRequest; import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkRequest;
import com.bgycc.smartcanteen.entity.Command; import com.bgycc.smartcanteen.entity.Command;
import com.google.gson.Gson; import com.google.gson.Gson;
...@@ -66,6 +67,7 @@ public class CommandHelper { ...@@ -66,6 +67,7 @@ public class CommandHelper {
public static OneTimeWorkRequest createWorker(Gson gson, Command command, String deviceSN) { public static OneTimeWorkRequest createWorker(Gson gson, Command command, String deviceSN) {
OneTimeWorkRequest worker = null; OneTimeWorkRequest worker = null;
Class<? extends ListenableWorker> workerClass = null; Class<? extends ListenableWorker> workerClass = null;
boolean needRetry = true;
switch (command.getAction()) { switch (command.getAction()) {
case LOG_UPLOAD: case LOG_UPLOAD:
workerClass = LogCommandWorker.class; workerClass = LogCommandWorker.class;
...@@ -75,22 +77,30 @@ public class CommandHelper { ...@@ -75,22 +77,30 @@ public class CommandHelper {
break; break;
case CONFIG_WIFI: case CONFIG_WIFI:
workerClass = WifiConfigCommandWorker.class; workerClass = WifiConfigCommandWorker.class;
// wifi配置任务不应该重试
needRetry = false;
break; break;
case EXEC_SHELL: case EXEC_SHELL:
workerClass = ShellCommandWorker.class; workerClass = ShellCommandWorker.class;
// shell任务也不应该重试
needRetry = false;
break; break;
} }
if (workerClass != null) { if (workerClass != null) {
Data inputData = CommandWorker.createBaseData(gson.toJson(command), deviceSN); Data inputData = CommandWorker.createBaseData(gson.toJson(command), deviceSN);
worker = new OneTimeWorkRequest.Builder(workerClass) WorkRequest.Builder<OneTimeWorkRequest.Builder, OneTimeWorkRequest> builder;
builder = new OneTimeWorkRequest.Builder(workerClass)
.setInputData(inputData) .setInputData(inputData)
.addTag(command.getAction()) .addTag(command.getAction());
.setBackoffCriteria( if (needRetry) {
worker = builder.setBackoffCriteria(
BackoffPolicy.LINEAR, BackoffPolicy.LINEAR,
OneTimeWorkRequest.MIN_BACKOFF_MILLIS, OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
TimeUnit.MILLISECONDS TimeUnit.MILLISECONDS)
) .build();
.build(); } else {
worker = builder.build();
}
} }
return worker; return worker;
} }
......
...@@ -35,7 +35,7 @@ public class ShellCommandWorker extends CommandWorker { ...@@ -35,7 +35,7 @@ public class ShellCommandWorker extends CommandWorker {
@Override @Override
public Result doWork() { public Result doWork() {
if (commandShell.getData() == null) { if (commandShell.getData() == null) {
return failed("shell指令非法"); return success("shell指令非法");
} }
CommandShell.CommandShellData data = commandShell.getData(); CommandShell.CommandShellData data = commandShell.getData();
byte[] decryptAES = EncryptUtils.decryptHexStringAES(data.getShell(), KEY.getBytes(), TRANSFORMATION, IV.getBytes()); byte[] decryptAES = EncryptUtils.decryptHexStringAES(data.getShell(), KEY.getBytes(), TRANSFORMATION, IV.getBytes());
......
...@@ -26,7 +26,7 @@ public class WifiConfigCommandWorker extends CommandWorker { ...@@ -26,7 +26,7 @@ public class WifiConfigCommandWorker extends CommandWorker {
public Result doWork() { public Result doWork() {
CommandWifiConfig.CommandWifiConfigData data = wifiConfig.getData(); CommandWifiConfig.CommandWifiConfigData data = wifiConfig.getData();
if (data == null) { if (data == null) {
return failed(); return success();
} }
if (!NetworkUtils.isWifiEnabled()) { if (!NetworkUtils.isWifiEnabled()) {
...@@ -39,7 +39,7 @@ public class WifiConfigCommandWorker extends CommandWorker { ...@@ -39,7 +39,7 @@ public class WifiConfigCommandWorker extends CommandWorker {
Thread.sleep(DEFAULT_DELAY); Thread.sleep(DEFAULT_DELAY);
} catch (Exception ignored) { } catch (Exception ignored) {
} }
return failed(failedMessage); return success(failedMessage);
} }
} }
...@@ -73,8 +73,8 @@ public class WifiConfigCommandWorker extends CommandWorker { ...@@ -73,8 +73,8 @@ public class WifiConfigCommandWorker extends CommandWorker {
} }
} catch (Exception e) { } catch (Exception e) {
LogUtils.e(TAG, "链接wifi过程出错: " + e.getMessage(), e); LogUtils.e(TAG, "链接wifi过程出错: " + e.getMessage(), e);
return failed(e.getMessage()); return success(e.getMessage());
} }
return failed("无法连接Wifi"); return success("无法连接Wifi");
} }
} }
package com.bgycc.smartcanteen.utils; package com.bgycc.smartcanteen.utils;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.LinkProperties; import android.net.LinkProperties;
...@@ -196,6 +197,7 @@ public class NetworkUtils { ...@@ -196,6 +197,7 @@ public class NetworkUtils {
} }
} }
// 未做版本适配工作,目前使用机型都为6.0
public static void switchNetwork(ConnectivityManager.NetworkCallback callback) { public static void switchNetwork(ConnectivityManager.NetworkCallback callback) {
if (lastTryNetwork == NETWORK_ETHERNET) { if (lastTryNetwork == NETWORK_ETHERNET) {
lastTryNetwork = NETWORK_WIFI; lastTryNetwork = NETWORK_WIFI;
...@@ -244,13 +246,14 @@ public class NetworkUtils { ...@@ -244,13 +246,14 @@ public class NetworkUtils {
return connect(createWifiConfiguration(ssid, null, pwd, null)); return connect(createWifiConfiguration(ssid, null, pwd, null));
} }
@SuppressLint("MissingPermission")
public static boolean connect(WifiConfiguration config) { public static boolean connect(WifiConfiguration config) {
int netId = wifiManager.addNetwork(config); wifiManager.addNetwork(config);
if (!wifiManager.enableNetwork(netId, true)) { if (!wifiManager.enableNetwork(config.networkId, true)) {
return false; return false;
} }
wifiManager.reconnect(); wifiManager.reconnect();
return true; return false;
} }
public static boolean disconnect(int netId) { public static boolean disconnect(int netId) {
......
...@@ -9,7 +9,7 @@ buildscript { ...@@ -9,7 +9,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:4.0.0' classpath 'com.android.tools.build:gradle:4.0.1'
} }
} }
......
...@@ -2,13 +2,13 @@ apply plugin: 'com.android.application' ...@@ -2,13 +2,13 @@ apply plugin: 'com.android.application'
def signed = "Daemon.apk" def signed = "Daemon.apk"
android { android {
compileSdkVersion 29 compileSdkVersion 30
buildToolsVersion "29.0.3" buildToolsVersion "30.0.1"
defaultConfig { defaultConfig {
applicationId "com.bgycc.smartcanteen.daemon" applicationId "com.bgycc.smartcanteen.daemon"
minSdkVersion 22 minSdkVersion 22
targetSdkVersion 29 targetSdkVersion 30
versionCode rootProject.ext.daemon_verson_code versionCode rootProject.ext.daemon_verson_code
versionName rootProject.ext.daemon_verson_name versionName rootProject.ext.daemon_verson_name
...@@ -96,9 +96,14 @@ private String findAppAssetsDir() { ...@@ -96,9 +96,14 @@ private String findAppAssetsDir() {
dependencies { dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.13' testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.blankj:utilcodex:1.29.0' implementation 'com.blankj:utilcodex:1.29.0'
} }
repositories {
mavenCentral()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment