2023年11月22日17:12:40
支持驱动离线和在线升级
2个文件已添加
14个文件已修改
1 文件已重命名
| | |
| | | }
|
| | |
|
| | | public static int byteArrayToInt(byte[] b) {
|
| | | int i = (b[0] & 0xFF) * 256 * 256 * 256 + (b[1] & 0xFF) * 256 * 256 + (b[2] & 0xFF) * 256 + (b[3] & 0xFF);
|
| | | return i;
|
| | | return (b[0] & 0xFF) * 256 * 256 * 256 + (b[1] & 0xFF) * 256 * 256 + (b[2] & 0xFF) * 256 + (b[3] & 0xFF);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 解密
|
| | | *
|
| | | * @param contentByte 待解密待字符串hexStr
|
| | | * @param contentByte 密钥
|
| | | * @return
|
| | | */
|
| | | public static byte[] decrypt(byte[] contentByte) {
|
| | | try {
|
| | | //KEY转换
|
| | | Key key = new SecretKeySpec("HDLRDCENTER1985.".getBytes(), "AES");
|
| | | //解密
|
| | | Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
|
| | | IvParameterSpec ivps = new IvParameterSpec("HDLRDCENTER1985.".getBytes());
|
| | | cipher.init(Cipher.DECRYPT_MODE, key, ivps);
|
| | | byte[] result = cipher.doFinal(contentByte);
|
| | | return result;
|
| | | } catch (NoSuchAlgorithmException e) {
|
| | | LogUtils.e(e.getMessage());
|
| | | } catch (InvalidKeyException e) {
|
| | | LogUtils.e(e.getMessage());
|
| | | } catch (NoSuchPaddingException e) {
|
| | | LogUtils.e(e.getMessage());
|
| | | } catch (BadPaddingException e) {
|
| | | LogUtils.e(e.getMessage());
|
| | | } catch (IllegalBlockSizeException e) {
|
| | | LogUtils.e(e.getMessage());
|
| | | } catch (InvalidAlgorithmParameterException e) {
|
| | | LogUtils.e(e.getMessage());
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | }
|
| | |
| | | |
| | | /** |
| | | * Created by jlchen on 12/17/21. |
| | | * |
| | | * <p> |
| | | * 网关设备驱动 |
| | | */ |
| | | public class GatewayDriverBean implements Serializable { |
| | |
| | | private String driverNameCn;//驱动中文名称 |
| | | private String imageId;//imageId |
| | | private String version;//版本号 |
| | | private String localUrl;//升级驱动文件本地路径 |
| | | |
| | | public String getLocalUrl() { |
| | | return localUrl == null ? "" : localUrl; |
| | | } |
| | | |
| | | public void setLocalUrl(String localUrl) { |
| | | this.localUrl = localUrl; |
| | | } |
| | | |
| | | @NonNull |
| | | public String getDriverCode() { |
| | |
| | | package com.hdl.photovoltaic; |
| | | |
| | | import android.app.Application; |
| | | import android.content.ComponentName; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.content.ServiceConnection; |
| | | import android.content.res.Configuration; |
| | | import android.content.res.Resources; |
| | | import android.os.IBinder; |
| | | import android.util.DisplayMetrics; |
| | | |
| | | |
| | |
| | | import com.hdl.linkpm.sdk.utils.HDLSDKSPUtils; |
| | | import com.hdl.photovoltaic.config.AppConfigManage; |
| | | import com.hdl.photovoltaic.config.UserConfigManage; |
| | | import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer; |
| | | import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpService; |
| | | import com.hdl.photovoltaic.other.HdlFileLogic; |
| | | import com.hdl.photovoltaic.other.HdlOtaLogic; |
| | | import com.hdl.photovoltaic.ui.HomeLoginActivity; |
| | | import com.hdl.photovoltaic.uni.HDLUniMPSDKManager; |
| | | import com.hdl.photovoltaic.utils.AppManagerUtils; |
| | |
| | | //4.初始化OKHttpLog 是否开启 |
| | | setOKHttpLogOpen(isLogEnabled); |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | // /** |
| | | // * 初始化uni app mqtt |
| | | // */ |
| | |
| | | import com.google.gson.JsonObject; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.hdl.hdlhttp.HxHttp; |
| | | import com.hdl.hdlhttp.HxHttpBuilder; |
| | | import com.hdl.hdlhttp.utils.GsonConvert; |
| | | import com.hdl.linkpm.sdk.core.api.HDLCloudUserApi; |
| | | import com.hdl.linkpm.sdk.core.exception.HDLException; |
| | | import com.hdl.linkpm.sdk.core.interceptor.HDLSmartHeader; |
| | | import com.hdl.linkpm.sdk.core.response.HDLResponse; |
| | | import com.hdl.linkpm.sdk.user.bean.HDLLoginBean; |
| | | import com.hdl.linkpm.sdk.user.callback.ILoginCallBack; |
| | | import com.hdl.linkpm.sdk.utils.HDLExceptionSubmitUtils; |
| | | import com.hdl.photovoltaic.bean.HttpResponsePack; |
| | | import com.hdl.photovoltaic.config.AppConfigManage; |
| | |
| | | import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak; |
| | | import com.hdl.photovoltaic.listener.CloudCallBeak; |
| | | import com.hdl.photovoltaic.other.HdlLogLogic; |
| | | import com.hdl.photovoltaic.utils.HDLMD5Utils; |
| | | import com.hdl.photovoltaic.utils.Md5Utils; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | |
| | | import java.util.Objects; |
| | | |
| | | import io.reactivex.rxjava3.disposables.Disposable; |
| | | import io.reactivex.rxjava3.functions.Consumer; |
| | | import okhttp3.Call; |
| | | import okhttp3.Callback; |
| | | import okhttp3.FormBody; |
| | | import okhttp3.Headers; |
| | | import okhttp3.MediaType; |
| | | import okhttp3.OkHttpClient; |
| | | import okhttp3.Request; |
| | | import okhttp3.RequestBody; |
| | | import okhttp3.Response; |
| | | import okhttp3.ResponseBody; |
| | | |
| | | public class HttpClient { |
| | | |
| | |
| | | String requestUrl = HDLCloudUserApi.getRequestUrl(api); |
| | | Gson gson = new Gson(); |
| | | System.out.println("http->发送->" + requestUrl + "\r\n" + body); |
| | | return HxHttp.builder() |
| | | .url(requestUrl) |
| | | .raw(body) |
| | | .build() |
| | | .post() |
| | | .subscribeWith(new HDLResponse<String>() { |
| | | @Override |
| | | public void onResponse(String str) { |
| | | if (callBack != null) { |
| | | callBack.onSuccess(str); |
| | | return HxHttp.builder().url(requestUrl).raw(body).build().post().subscribeWith(new HDLResponse<String>() { |
| | | @Override |
| | | public void onResponse(String str) { |
| | | if (callBack != null) { |
| | | callBack.onSuccess(str); |
| | | // System.out.println("http->回复->" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); |
| | | System.out.println("http->回复->" + requestUrl + "\r\n" + str); |
| | | } |
| | | } |
| | | System.out.println("http->回复->" + requestUrl + "\r\n" + str); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | HDLExceptionSubmitUtils.submit(requestUrl, body, e); |
| | | if (callBack != null) { |
| | | callBack.onFailure(e); |
| | | System.out.println("http->回复->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}"); |
| | | } |
| | | } |
| | | }); |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | HDLExceptionSubmitUtils.submit(requestUrl, body, e); |
| | | if (callBack != null) { |
| | | callBack.onFailure(e); |
| | | System.out.println("http->回复->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}"); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | |
| | | String requestUrl = HDLCloudUserApi.getRequestUrl(api); |
| | | Gson gson = new Gson(); |
| | | System.out.println("http->发送->" + requestUrl + "\r\n" + body); |
| | | return HxHttp.builder() |
| | | .url(requestUrl) |
| | | .file(body) |
| | | .build() |
| | | .post() |
| | | .subscribeWith(new HDLResponse<String>() { |
| | | @Override |
| | | public void onResponse(String str) { |
| | | if (callBack != null) { |
| | | callBack.onSuccess(str); |
| | | return HxHttp.builder().url(requestUrl).file(body).build().post().subscribeWith(new HDLResponse<String>() { |
| | | @Override |
| | | public void onResponse(String str) { |
| | | if (callBack != null) { |
| | | callBack.onSuccess(str); |
| | | // System.out.println("http->回复->" + requestUrl + "\r\n" + gson.fromJson(str, JsonObject.class).toString()); |
| | | HdlLogLogic.print("http->回复->" + requestUrl + "\r\n" + str, true); |
| | | HdlLogLogic.print("http->回复->" + requestUrl + "\r\n" + str, true); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | HDLExceptionSubmitUtils.submit(requestUrl, body, e); |
| | | if (callBack != null) { |
| | | callBack.onFailure(e); |
| | | HdlLogLogic.print("http->回复->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", true); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 下载文件 |
| | | * |
| | | * @param callBack - |
| | | */ |
| | | public Disposable downLoadFile(String url, CloudCallBeak<ResponseBody> callBack) { |
| | | return HxHttp.builder() |
| | | .url(url) |
| | | .headers(HDLSmartHeader.IGNORE_SIGN_HEADER, 1) |
| | | .build() |
| | | .download() |
| | | .subscribe(new Consumer<ResponseBody>() { |
| | | @Override |
| | | public void accept(ResponseBody responseBody) throws Exception { |
| | | if (callBack != null) { |
| | | callBack.onSuccess(responseBody); |
| | | } |
| | | } |
| | | |
| | | }, new Consumer<Throwable>() { |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | HDLExceptionSubmitUtils.submit(requestUrl, body, e); |
| | | public void accept(Throwable throwable) throws Exception { |
| | | /** |
| | | * 网络请求失败 |
| | | * 具体可以自己实现 |
| | | */ |
| | | HDLExceptionSubmitUtils.submit(url, null, throwable); |
| | | if (callBack != null) { |
| | | callBack.onFailure(e); |
| | | HdlLogLogic.print("http->回复->" + requestUrl + "\r\n" + "\"{code=\"" + e.getCode() + "," + "\"message=\"" + e.getMsg() + "}", true); |
| | | callBack.onFailure(new HDLException(403, throwable.getMessage())); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 请求服务器(上传文件) |
| | | * 上传文件 |
| | | * |
| | | * @param api 请求接口 |
| | | * @param body 请求参数(json) |
| | | * @param callBack 回调 |
| | | */ |
| | | public Disposable requestHttpFile(String api, File body, CloudCallBeak<String> callBack) { |
| | | public Disposable uploadingFile(String api, File body, CloudCallBeak<String> callBack) { |
| | | return requestFilePost(api, body, callBack); |
| | | } |
| | | |
| | |
| | | HdlLogLogic.print("http->发送->" + fullUrl + "\r\n" + json, false); |
| | | OkHttpClient okHttpClient = new OkHttpClient(); |
| | | RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), getJson(json)); |
| | | final Request request = new Request.Builder() |
| | | .url(fullUrl)//请求的url |
| | | .addHeader("Authorization", UserConfigManage.getInstance().getHeaderPrefix() + UserConfigManage.getInstance().getToken()) |
| | | .post(requestBody) |
| | | .build(); |
| | | final Request request = new Request.Builder().url(fullUrl)//请求的url |
| | | .addHeader("Authorization", UserConfigManage.getInstance().getHeaderPrefix() + UserConfigManage.getInstance().getToken()).post(requestBody).build(); |
| | | if (isExecute) { |
| | | Response response = okHttpClient.newCall(request).execute();//同步 |
| | | if (response.isSuccessful()) { |
| | |
| | | * @param json 所有字段使用urlParameter拼接,除了appSecret |
| | | */ |
| | | private String getSign(JsonObject json, String appSecret) { |
| | | String builder = jsonToUrlParameter(json) + |
| | | appSecret; |
| | | return HDLMD5Utils.encodeMD5(builder); |
| | | String builder = jsonToUrlParameter(json) + appSecret; |
| | | return Md5Utils.encodeMD5(builder); |
| | | } |
| | | |
| | | |
| | |
| | | * @return - |
| | | */ |
| | | private boolean IfValueNeedSign(String valueStr) { |
| | | if (TextUtils.isEmpty(valueStr)) |
| | | return false; |
| | | if (TextUtils.isEmpty(valueStr)) return false; |
| | | final char[] strChar = valueStr.substring(0, 1).toCharArray(); |
| | | final char firstChar = strChar[0]; |
| | | //System.out.println("getJSONType firstChar = "+firstChar); |
| | | if (firstChar != '{' && firstChar != '[') |
| | | return true; |
| | | if (firstChar != '{' && firstChar != '[') return true; |
| | | |
| | | return false; |
| | | } |
| | |
| | | String fileName = ((HTTPSession) session).getUri(); |
| | | String data = ""; |
| | | if (fileName.equals(HdlFileLogic.getInstance().getDriveRootPath())) { |
| | | |
| | | data = HdlFileLogic.getInstance().readFile(fileName); |
| | | |
| | | } |
| | | //响应客户端 |
| | | return newFixedLengthResponse(data); |
| | |
| | | */ |
| | | public void uploadUserHeadImage_C(File file, CloudCallBeak<String> cloudCallBeak) { |
| | | String requestUrl = HttpApi.C_POST_GET_IMAGE_URL; |
| | | HttpClient.getInstance().requestHttpFile(requestUrl, file, new CloudCallBeak<String>() { |
| | | HttpClient.getInstance().uploadingFile(requestUrl, file, new CloudCallBeak<String>() { |
| | | @Override |
| | | public void onSuccess(String jsonStr) { |
| | | if (cloudCallBeak != null) { |
| | |
| | | import java.io.FileOutputStream; |
| | | import java.io.InputStream; |
| | | import java.io.InputStreamReader; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | |
| | | /** |
| | | * 获取【日志文件】全路径 |
| | | */ |
| | | public String getLogFilePath() { |
| | | public String getLogFileNamePath() { |
| | | return getCurrentHomeRootPath() + "/log.txt"; |
| | | } |
| | | |
| | |
| | | * @return 全路径 |
| | | */ |
| | | public String getDrivePathFileName(String driverCode, String version) { |
| | | String driverFileName = driverCode + "_" + version + ".zip"; |
| | | String driverFileName = driverCode + "_" + version + ".zip"; |
| | | return HdlFileLogic.getInstance().getDriveRootPath() + "/" + driverFileName; |
| | | } |
| | | |
| | |
| | | * @param fullPath 全路径 |
| | | * @param data 数据 |
| | | */ |
| | | public void writeFile(String fullPath, String data) { |
| | | public void writeFile(String fullPath, byte[] data) { |
| | | try { |
| | | File file = new File(fullPath); |
| | | if (!file.exists()) { |
| | |
| | | } |
| | | } |
| | | FileOutputStream d = new FileOutputStream(file); |
| | | d.write(data.getBytes()); |
| | | d.write(data); |
| | | d.flush(); |
| | | d.close(); |
| | | System.out.println("写入文件成功==" + fullPath); |
| | |
| | | System.out.println("写入文件有异常==" + e.getMessage()); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 写入文件 |
| | | * |
| | | * @param fullPath 全路径 |
| | | * @param data 数据 |
| | | */ |
| | | public void writeFile(String fullPath, String data) { |
| | | writeFile(fullPath, data.getBytes()); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param filePath 全路径 |
| | | * @return 数据 |
| | | */ |
| | | public String readFile(String filePath) { |
| | | public byte[] readFileByte(String filePath) { |
| | | try { |
| | | if (!isBoolean(filePath)) { |
| | | return ""; |
| | | return null; |
| | | } |
| | | String fileContent = ""; |
| | | File f = new File(filePath); |
| | | if (!f.exists()) { |
| | | return fileContent; |
| | | return null; |
| | | } |
| | | FileInputStream fis = new FileInputStream(f); |
| | | byte[] bytes = new byte[fis.available()]; |
| | | fis.read(bytes); |
| | | // byte[] bytes = new byte[fis.available()]; |
| | | byte[] readByte = new byte[1024]; |
| | | List<Byte> byteList = new ArrayList<>(); |
| | | int len = 0; |
| | | while ((len = fis.read(readByte)) != -1) { |
| | | for (int i = 0; i < len; i++) { |
| | | byteList.add(readByte[i]); |
| | | } |
| | | } |
| | | Byte[] forbByte = byteList.toArray(new Byte[0]); |
| | | byte[] bytes = new byte[forbByte.length]; |
| | | for (int i = 0; i < forbByte.length; i++) { |
| | | bytes[i] = forbByte[i]; |
| | | } |
| | | fis.close(); |
| | | System.out.println("读取文件成功===" + filePath); |
| | | fileContent = new String(bytes); |
| | | return fileContent; |
| | | return bytes; |
| | | } catch (Exception e1) { |
| | | System.out.println("读取文件有异常===" + e1.getMessage()); |
| | | return null; |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 读取文件 |
| | | * |
| | | * @param filePath 全路径 |
| | | * @return 数据 |
| | | */ |
| | | public String readFile(String filePath) { |
| | | try { |
| | | |
| | | byte[] bytes = readFileByte(filePath); |
| | | if (bytes == null) { |
| | | return ""; |
| | | } |
| | | return new String(bytes); |
| | | } catch (Exception e1) { |
| | | return ""; |
| | | } |
| | | |
| | |
| | | public static void writeLog(String strLog) { |
| | | synchronized (HdlLogLogic.class) { |
| | | strLog = TimeUtils.getTimeFromTimestamp(System.currentTimeMillis()) + "s%" + " " + strLog;//加打印时间(s%占位符,设置显示时间不同字体颜色) |
| | | HdlFileLogic.getInstance().appendFile(HdlFileLogic.getInstance().getLogFilePath(), strLog); |
| | | HdlFileLogic.getInstance().appendFile(HdlFileLogic.getInstance().getLogFileNamePath(), strLog); |
| | | } |
| | | } |
| | | |
| | |
| | | * @return 日志内容 |
| | | */ |
| | | public static String readLog() { |
| | | return HdlFileLogic.getInstance().readFile(HdlFileLogic.getInstance().getLogFilePath()); |
| | | return HdlFileLogic.getInstance().readFile(HdlFileLogic.getInstance().getLogFileNamePath()); |
| | | } |
| | | |
| | | |
| | |
| | | import com.hdl.photovoltaic.internet.api.TopicApi; |
| | | import com.hdl.photovoltaic.listener.CloudCallBeak; |
| | | import com.hdl.photovoltaic.listener.LinkCallBack; |
| | | import com.hdl.photovoltaic.utils.AesUtils; |
| | | import com.hdl.photovoltaic.utils.AppManagerUtils; |
| | | import com.hdl.photovoltaic.utils.Md5Utils; |
| | | import com.hdl.sdk.link.common.exception.HDLLinkException; |
| | | import com.hdl.sdk.link.common.utils.ByteUtils; |
| | | import com.hdl.sdk.link.core.callback.HDLLinkCallBack; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.lang.reflect.Type; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.logging.FileHandler; |
| | | |
| | | import okhttp3.ResponseBody; |
| | | |
| | | |
| | | /** |
| | |
| | | * |
| | | * @param downloadUrl 云端固件地址 |
| | | */ |
| | | public void getDeviceDriverDownloadFile(String downloadUrl, CloudCallBeak<DownloadUrlBean> callBack) { |
| | | String requestUrl = HttpApi.POST_OTA_GET_LinkDeviceFirmwareDownloadUrl; |
| | | JsonObject json = new JsonObject(); |
| | | public void getDeviceDriverDownloadFile(String downloadUrl, CloudCallBeak<ResponseBody> callBack) { |
| | | HttpClient.getInstance().downLoadFile(downloadUrl, new CloudCallBeak<ResponseBody>() { |
| | | @Override |
| | | public void onSuccess(ResponseBody s) { |
| | | |
| | | if (callBack != null) { |
| | | callBack.onSuccess(s); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | if (callBack != null) { |
| | | callBack.onFailure(e); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理下载文件数据 |
| | | * |
| | | * @param zipData 文件数据流对象 |
| | | * @param md5 检验文件MD5值 |
| | | * @return 返回解密的byte[] |
| | | */ |
| | | public byte[] disposeDownLoadFile(ResponseBody zipData, String md5) { |
| | | if (zipData == null) { |
| | | return null; |
| | | } |
| | | if (TextUtils.isEmpty(md5)) { |
| | | return null; |
| | | } |
| | | InputStream is = null; |
| | | List<Byte> byteList = new ArrayList<>(); |
| | | byte[] decrypt = null; |
| | | try { |
| | | is = zipData.byteStream(); |
| | | int total = (int) zipData.contentLength(); |
| | | byte[] buf = new byte[1024*4]; |
| | | long sum = 0; |
| | | int len = 0; |
| | | while ((len = is.read(buf)) != -1) { |
| | | sum += len; |
| | | //todo 下载中,可以自行加入进度条(可以广播出去) |
| | | int progress = (int) (sum * 1.0f / total * 100); |
| | | for (int i = 0; i < len; i++) { |
| | | byteList.add(buf[i]); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | try { |
| | | if (is != null) { |
| | | is.close(); |
| | | } |
| | | Byte[] sourceByte = byteList.toArray(new Byte[0]); |
| | | byte[] bytes = new byte[sourceByte.length]; |
| | | for (int i = 0; i < sourceByte.length; i++) { |
| | | bytes[i] = sourceByte[i]; |
| | | } |
| | | //下载文件需要解密之后再进行md5去做比对 |
| | | decrypt = AesUtils.decrypt(bytes); |
| | | if (decrypt != null) { |
| | | String fileMD5 = Md5Utils.encodeMD5(decrypt); |
| | | if (TextUtils.isEmpty(fileMD5) || !Objects.equals(fileMD5, md5)) { |
| | | //校验失败,置空 |
| | | decrypt = null; |
| | | } |
| | | } |
| | | } catch (IOException ignored) { |
| | | |
| | | } |
| | | } |
| | | return decrypt; |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | import java.util.Objects; |
| | | |
| | | import io.dcloud.feature.unimp.DCUniMPJSCallback; |
| | | import okhttp3.ResponseBody; |
| | | |
| | | /** |
| | | * uni小程序的逻辑 |
| | |
| | | File file = new File(driverFileList); |
| | | if (file.list() != null) { |
| | | for (int i = 0; i < Objects.requireNonNull(file.list()).length; i++) { |
| | | String path = Objects.requireNonNull(file.list())[i]; |
| | | String[] ary = path.split("/"); |
| | | String[] driverInfoAry = ary[ary.length - 1].split("_"); |
| | | String fileName = Objects.requireNonNull(file.list())[i]; |
| | | if (!fileName.contains(driveCode)) { |
| | | continue; |
| | | } |
| | | String[] driverInfoAry = fileName.split("_"); |
| | | GatewayDriverBean gatewayDriverBean = new GatewayDriverBean(); |
| | | gatewayDriverBean.setDriverCode(driverInfoAry[0]); |
| | | gatewayDriverBean.setVersion(driverInfoAry[1].replace(".zip", "")); |
| | | gatewayDriverBean.setLocalUrl(HdlFileLogic.getInstance().getDrivePathFileName(gatewayDriverBean.getDriverCode(), gatewayDriverBean.getVersion())); |
| | | list.add(gatewayDriverBean); |
| | | } |
| | | } |
| | |
| | | String imageId = getKeyValue("imageId", getKeyValue("data", data));//驱动镜像id |
| | | String version = getKeyValue("version", getKeyValue("data", data));//驱动版本 |
| | | String driverVersionId = getKeyValue("driverVersionId", getKeyValue("data", data));//驱动版本id |
| | | HdlOtaLogic.getInstance().getDeviceDriverDownloadUrl(driverVersionId, new CloudCallBeak<DownloadUrlBean>() { |
| | | String localUrl = getKeyValue("localUrl", getKeyValue("data", data));//储存在本地驱动文件路径 |
| | | String url = getKeyValue("url", getKeyValue("data", data));//云端上文件地址 |
| | | String md5 = getKeyValue("md5", getKeyValue("data", data));//云端上文件MD5 |
| | | HdlOtaLogic.getInstance().getDeviceDriverDownloadFile(url, new CloudCallBeak<ResponseBody>() { |
| | | @Override |
| | | public void onSuccess(DownloadUrlBean downloadUrlBean) { |
| | | if (downloadUrlBean == null) { |
| | | uniCallbackData(null, -2, "下载链接找不到", callback); |
| | | return; |
| | | } |
| | | HdlOtaLogic.getInstance().getDeviceDriverDownloadFile(downloadUrlBean.getDownloadUrl(), new CloudCallBeak<DownloadUrlBean>() { |
| | | @Override |
| | | public void onSuccess(DownloadUrlBean obj) { |
| | | Gson gson = new Gson(); |
| | | String json = gson.toJson(obj); |
| | | String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName(driverCode, version); |
| | | HdlFileLogic.getInstance().writeFile(drivePathFileName, json); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | |
| | | } |
| | | }); |
| | | public void onSuccess(ResponseBody responseBody) { |
| | | byte[] bytes = HdlOtaLogic.getInstance().disposeDownLoadFile(responseBody, md5); |
| | | HdlFileLogic.getInstance().deleteFile(localUrl);//删除旧文件 |
| | | String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName(driverCode, version); |
| | | HdlFileLogic.getInstance().writeFile(drivePathFileName, bytes);//写入新文件 |
| | | HdlLogLogic.print("写入新驱动文件到内存成功.", false); |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLException e) { |
| | | HdlLogLogic.print("下载驱动文件到内存失败.", false); |
| | | uniCallbackData(null, e.getCode(), e.getMsg(), callback); |
| | | } |
| | | }); |
| | |
| | | import androidx.appcompat.content.res.AppCompatResources; |
| | | |
| | | import android.Manifest; |
| | | import android.content.Intent; |
| | | import android.content.ComponentName; |
| | | import android.content.ServiceConnection; |
| | | import android.content.pm.PackageManager; |
| | | import android.os.Bundle; |
| | | import android.os.IBinder; |
| | | import android.os.Process; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | import android.view.View; |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.hdl.linkpm.sdk.core.exception.HDLException; |
| | | import com.hdl.linkpm.sdk.home.type.HomeType; |
| | | import com.hdl.photovoltaic.HDLApp; |
| | | import com.hdl.linkpm.sdk.ota.bean.GatewayDriverBean; |
| | | import com.hdl.photovoltaic.R; |
| | | import com.hdl.photovoltaic.base.CustomBaseActivity; |
| | | import com.hdl.photovoltaic.bean.MqttInfo; |
| | | import com.hdl.photovoltaic.config.UserConfigManage; |
| | | import com.hdl.photovoltaic.databinding.ActivityMyPowerStationBinding; |
| | | import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer; |
| | | import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpService; |
| | | import com.hdl.photovoltaic.listener.CloudCallBeak; |
| | | import com.hdl.photovoltaic.other.HdlDeviceLogic; |
| | | import com.hdl.photovoltaic.other.HdlFileLogic; |
| | | import com.hdl.photovoltaic.other.HdlLogLogic; |
| | | import com.hdl.photovoltaic.other.HdlMqttLogic; |
| | | import com.hdl.photovoltaic.other.HdlOtaLogic; |
| | | import com.hdl.photovoltaic.other.HdlThreadLogic; |
| | | import com.hdl.photovoltaic.other.HdlUniLogic; |
| | | import com.hdl.photovoltaic.ui.bean.DeviceRemoteInfo; |
| | | import com.hdl.photovoltaic.uni.HDLUniMP; |
| | | import com.hdl.photovoltaic.utils.AesUtils; |
| | | import com.hdl.photovoltaic.utils.AppManagerUtils; |
| | | import com.hdl.photovoltaic.utils.Md5Utils; |
| | | import com.hdl.photovoltaic.utils.PermissionUtils; |
| | | import com.hdl.sdk.link.HDLLinkLocalSdk; |
| | | import com.hdl.sdk.link.common.event.EventListener; |
| | |
| | | import org.greenrobot.eventbus.Subscribe; |
| | | import org.greenrobot.eventbus.ThreadMode; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | |
| | | import okhttp3.ResponseBody; |
| | | |
| | | |
| | | /** |
| | |
| | | //初始化mqtt客户端 |
| | | initMqttClient(); |
| | | |
| | | String path = HdlFileLogic.getInstance().getDriveRootPath() + "/036F6C1B.zip"; |
| | | MyNanoHttpServer.getInstance(AppManagerUtils.getAppManager().getIPAddress(HDLApp.getInstance())); |
| | | //MyNanoHttpServer.getInstance("127.0.0.1"); |
| | | Intent intentServer = new Intent(); |
| | | intentServer.setClass(HDLApp.getInstance(), MyNanoHttpService.class); |
| | | startService(intentServer); |
| | | |
| | | } |
| | | |
| | |
| | | super.onBackPressed(); |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | package com.hdl.photovoltaic.ui; |
| | | |
| | | |
| | | import android.content.ComponentName; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.content.ServiceConnection; |
| | | import android.os.Bundle; |
| | | import android.os.IBinder; |
| | | |
| | | import com.hdl.linkpm.sdk.core.exception.HDLException; |
| | | import com.hdl.photovoltaic.HDLApp; |
| | | import com.hdl.photovoltaic.R; |
| | | import com.hdl.photovoltaic.base.CustomBaseActivity; |
| | | import com.hdl.photovoltaic.config.UserConfigManage; |
| | | import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer; |
| | | import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpService; |
| | | import com.hdl.photovoltaic.listener.CloudCallBeak; |
| | | import com.hdl.photovoltaic.other.HdlOtaLogic; |
| | | import com.hdl.photovoltaic.other.HdlResidenceLogic; |
| | | import com.hdl.photovoltaic.ui.bean.HouseIdBean; |
| | | import com.hdl.photovoltaic.utils.AppManagerUtils; |
| | | |
| | | import java.util.List; |
| | | |
| | |
| | | |
| | | @Override |
| | | public void onBindView(Bundle savedInstanceState) { |
| | | |
| | | //7天内免登录 |
| | | if (UserConfigManage.getInstance().isAutoLogin()) { |
| | | // showLoading(); |
| | |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | package com.hdl.photovoltaic.ui.me; |
| | | |
| | | |
| | | import android.content.ComponentName; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.content.ServiceConnection; |
| | | import android.os.Bundle; |
| | | import android.os.IBinder; |
| | | import android.view.View; |
| | | |
| | | import com.hdl.linkpm.sdk.HDLLinkPMSdk; |
| | |
| | | import com.hdl.photovoltaic.base.CustomBaseActivity; |
| | | import com.hdl.photovoltaic.config.UserConfigManage; |
| | | import com.hdl.photovoltaic.databinding.ActivitySetBinding; |
| | | import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpServer; |
| | | import com.hdl.photovoltaic.internet.HttpServer.MyNanoHttpService; |
| | | import com.hdl.photovoltaic.listener.CloudCallBeak; |
| | | import com.hdl.photovoltaic.other.HdlAccountLogic; |
| | | import com.hdl.photovoltaic.other.HdlThreadLogic; |
| | | import com.hdl.photovoltaic.utils.AppManagerUtils; |
| | | import com.hdl.photovoltaic.widget.ConfirmationInputDialog; |
| | | |
| | | /** |
| | |
| | | |
| | | @Override |
| | | public void onBindView(Bundle savedInstanceState) { |
| | | |
| | | //初始化 |
| | | initView(); |
| | | //初始化界面监听器 |
| | |
| | | |
| | | private void initView() { |
| | | viewBinding.toolbarTopFragmentHouseListRl.topTitleTv.setText(R.string.me_set); |
| | | viewBinding.toolbarTopFragmentHouseListRl.topTitleTv.setTextColor(getResources().getColor(R.color.text_030D1C,null)); |
| | | viewBinding.toolbarTopFragmentHouseListRl.topTitleTv.setTextColor(getResources().getColor(R.color.text_030D1C, null)); |
| | | viewBinding.toolbarTopFragmentHouseListRl.topBackBtn.setVisibility(View.VISIBLE); |
| | | viewBinding.toolbarTopFragmentHouseListRl.topBarView.setBackgroundColor(getResources().getColor(R.color.text_FFFFFFFF,null)); |
| | | viewBinding.toolbarTopFragmentHouseListRl.topBarView.setBackgroundColor(getResources().getColor(R.color.text_FFFFFFFF, null)); |
| | | viewBinding.setNicknameIl.sllLlRlNameTv.setText(R.string.set_nickname); |
| | | viewBinding.setChangePasswordIl.sllLlRlNameTv.setText(R.string.home_login_change_password); |
| | | viewBinding.setCloseAccountIl.sllLlRlNameTv.setText(R.string.set_close_account); |
| | |
| | | public void onSuccess(Boolean obj) { |
| | | UserConfigManage.getInstance().setUserName(str); |
| | | UserConfigManage.getInstance().Save(); |
| | | HdlThreadLogic.tipFlashingBox(_mActivity, true, getString(R.string.home_login_change_name_succeed),0); |
| | | HdlThreadLogic.tipFlashingBox(_mActivity, true, getString(R.string.home_login_change_name_succeed), 0); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 退出登录请求的方法 |
| | | */ |
| | |
| | | if (value > 1) { |
| | | HdlFileLogic.getInstance().deleteDirectory(HdlFileLogic.getInstance().getCurrentUserRootPath()); |
| | | } else { |
| | | HdlFileLogic.getInstance().deleteFile(HdlFileLogic.getInstance().getLogFilePath()); |
| | | HdlFileLogic.getInstance().deleteFile(HdlFileLogic.getInstance().getLogFileNamePath()); |
| | | } |
| | | viewBinding.replyContentTv.setText(""); |
| | | } |
| | |
| | | viewBinding.homeIdTv.setText(homeId); |
| | | viewBinding.homeNameTv.setText(homeName); |
| | | |
| | | HdlFileLogic.getInstance().delFileLien(HdlFileLogic.getInstance().getLogFilePath(), 2); |
| | | HdlFileLogic.getInstance().delFileLien(HdlFileLogic.getInstance().getLogFileNamePath(), 2); |
| | | String content = HdlLogLogic.readLog(); |
| | | int startIndex = 0; |
| | | int endIndex; |
New file |
| | |
| | | package com.hdl.photovoltaic.utils; |
| | | |
| | | import android.annotation.SuppressLint; |
| | | import android.text.TextUtils; |
| | | |
| | | import com.hdl.sdk.link.common.utils.LogUtils; |
| | | |
| | | import java.security.InvalidAlgorithmParameterException; |
| | | import java.security.InvalidKeyException; |
| | | import java.security.Key; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.security.SecureRandom; |
| | | |
| | | import javax.crypto.BadPaddingException; |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.IllegalBlockSizeException; |
| | | import javax.crypto.KeyGenerator; |
| | | import javax.crypto.NoSuchPaddingException; |
| | | import javax.crypto.SecretKey; |
| | | import javax.crypto.spec.IvParameterSpec; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | |
| | | import io.dcloud.common.adapter.util.CryptoProvider; |
| | | |
| | | /** |
| | | * AES加密解密 |
| | | */ |
| | | public class AesUtils { |
| | | |
| | | private static final String SHA1PRNG = "HDLRDCENTER1985."; // SHA1PRNG 强随机种子算法 |
| | | private static final String AES = "AES"; //AES 加密 |
| | | private static final String CIPHERMODE = "AES/CBC/PKCS7Padding"; //AES算法/CBC模式/PKCS5Padding填充模式 |
| | | |
| | | /** |
| | | * 解密 |
| | | * |
| | | * @param contentByte 待解密待字符串hexStr |
| | | * @return 解密后byte[]数组 |
| | | */ |
| | | public static byte[] decrypt(byte[] contentByte) { |
| | | try { |
| | | //KEY转换 |
| | | Key key = new SecretKeySpec(SHA1PRNG.getBytes(), AES); |
| | | //解密 |
| | | Cipher cipher = Cipher.getInstance(CIPHERMODE); |
| | | IvParameterSpec ivps = new IvParameterSpec(SHA1PRNG.getBytes()); |
| | | cipher.init(Cipher.DECRYPT_MODE, key, ivps); |
| | | return cipher.doFinal(contentByte); |
| | | } catch (NoSuchAlgorithmException |
| | | | InvalidKeyException |
| | | | NoSuchPaddingException |
| | | | BadPaddingException |
| | | | IllegalBlockSizeException |
| | | | InvalidAlgorithmParameterException e) { |
| | | LogUtils.e(e.getMessage()); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 加密 |
| | | */ |
| | | public static String encrypt(String key, String cleartext) { |
| | | if (TextUtils.isEmpty(cleartext)) { |
| | | return cleartext; |
| | | } |
| | | try { |
| | | byte[] result = encrypt(key, cleartext.getBytes()); |
| | | return parseByte2HexStr(result); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | | */ |
| | | public static byte[] encrypt(String key, byte[] clear) throws Exception { |
| | | byte[] raw = getRawKey(key.getBytes()); |
| | | SecretKeySpec keySpec = new SecretKeySpec(raw, AES); |
| | | Cipher cipher = Cipher.getInstance(CIPHERMODE); |
| | | cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(new |
| | | byte[cipher.getBlockSize()])); |
| | | return cipher.doFinal(clear); |
| | | } |
| | | |
| | | /** |
| | | * 解密 |
| | | */ |
| | | public static String decrypt(String key, String encrypted) { |
| | | if (TextUtils.isEmpty(encrypted)) { |
| | | return encrypted; |
| | | } |
| | | try { |
| | | byte[] enc = parseHexStr2Byte(encrypted); |
| | | byte[] result = decrypt(key, enc); |
| | | return new String(result); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 解密 |
| | | */ |
| | | public static byte[] decrypt(String key, byte[] encrypted) throws Exception { |
| | | byte[] raw = getRawKey(key.getBytes()); |
| | | SecretKeySpec keySpec = new SecretKeySpec(raw, AES); |
| | | Cipher cipher = Cipher.getInstance(CIPHERMODE); |
| | | cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(new |
| | | byte[cipher.getBlockSize()])); |
| | | return cipher.doFinal(encrypted); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 处理密钥 |
| | | */ |
| | | @SuppressLint("DeletedProvider") |
| | | public static byte[] getRawKey(byte[] seed) throws Exception { |
| | | KeyGenerator kgen = KeyGenerator.getInstance(AES); |
| | | //for android |
| | | SecureRandom sr = null; |
| | | // 在4.2以上版本中,SecureRandom获取方式发生了改变 |
| | | if (android.os.Build.VERSION.SDK_INT >= 17) { |
| | | sr = SecureRandom.getInstance(SHA1PRNG, new CryptoProvider()); |
| | | } else { |
| | | sr = SecureRandom.getInstance(SHA1PRNG); |
| | | } |
| | | |
| | | sr.setSeed(seed); |
| | | kgen.init(128, sr); //128bits,192bits,256bits |
| | | //AES中128位密钥,加密轮次为10轮;192位密钥,加密轮次为12轮;256位密钥,加密轮次为14轮。 |
| | | SecretKey secretKey = kgen.generateKey(); |
| | | return secretKey.getEncoded(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将二进制转换成16进制 |
| | | */ |
| | | public static String parseByte2HexStr(byte[] buf) { |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (int i = 0; i < buf.length; i++) { |
| | | String hex = Integer.toHexString(buf[i] & 0xFF); |
| | | if (hex.length() == 1) { |
| | | hex = '0' + hex; |
| | | } |
| | | sb.append(hex.toUpperCase()); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 将16进制转换为二进制 |
| | | */ |
| | | public static byte[] parseHexStr2Byte(String hexStr) { |
| | | if (hexStr.length() < 1) |
| | | return null; |
| | | byte[] result = new byte[hexStr.length() / 2]; |
| | | for (int i = 0; i < hexStr.length() / 2; i++) { |
| | | int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); |
| | | int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), |
| | | 16); |
| | | result[i] = (byte) (high * 16 + low); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.hdl.photovoltaic.utils;
|
| | |
|
| | | import com.hdl.sdk.link.common.utils.LogUtils;
|
| | |
|
| | | import java.io.UnsupportedEncodingException;
|
| | | import java.security.InvalidAlgorithmParameterException;
|
| | | import java.security.InvalidKeyException;
|
| | | import java.security.Key;
|
| | | import java.security.NoSuchAlgorithmException;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Arrays;
|
| | | import java.util.List;
|
| | |
|
| | | import javax.crypto.BadPaddingException;
|
| | | import javax.crypto.Cipher;
|
| | | import javax.crypto.IllegalBlockSizeException;
|
| | | import javax.crypto.NoSuchPaddingException;
|
| | | import javax.crypto.spec.IvParameterSpec;
|
| | | import javax.crypto.spec.SecretKeySpec;
|
| | |
|
| | | /**
|
| | | * Created by Tong on 2021/9/23.
|
| | | */
|
| | | public class ByteUtils {
|
| | |
|
| | | public static byte[] toByteArray(List<Byte> list) {
|
| | | Byte[] temps = list.toArray(new Byte[0]);
|
| | | byte[] result = new byte[temps.length];
|
| | | for (int i = 0; i < result.length; i++) {
|
| | | result[i] = temps[i];
|
| | | }
|
| | | return result;
|
| | |
|
| | | }
|
| | |
|
| | |
|
| | | public static List<Byte> toByteList(byte[] bytes) {
|
| | | final List<Byte> list = new ArrayList<>();
|
| | | for (byte aByte : bytes) {
|
| | | list.add(aByte);
|
| | | }
|
| | | return list;
|
| | |
|
| | | }
|
| | |
|
| | | public static byte[] getRangeBytes(List<Byte> list, int start, int end) {
|
| | | Byte[] temps = Arrays.copyOfRange(list.toArray(new Byte[0]), start, end);
|
| | | byte[] result = new byte[temps.length];
|
| | | for (int i = 0; i < temps.length; i++) {
|
| | | result[i] = temps[i];
|
| | | }
|
| | | return result;
|
| | |
|
| | | }
|
| | |
|
| | | public static byte[] copyBytes(byte bytes[], int index, int length) {
|
| | | byte[] result = new byte[length];
|
| | | for (int i = 0; i < result.length; i++) {
|
| | | result[i] = bytes[index + i];
|
| | | }
|
| | | return result;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 拼接byte
|
| | | */
|
| | | public static byte[] concatBytes(byte[] bt1, byte[] bt2) {
|
| | | if (bt1 == null) {
|
| | | return bt2;
|
| | | }
|
| | | if (bt2 == null) {
|
| | | return bt1;
|
| | | }
|
| | | byte[] bt3 = new byte[bt1.length + bt2.length];
|
| | | System.arraycopy(bt1, 0, bt3, 0, bt1.length);
|
| | | System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
|
| | | return bt3;
|
| | | }
|
| | |
|
| | |
|
| | | public boolean endWith(Byte[] src, byte[] target) {
|
| | | if (src.length < target.length) {
|
| | | return false;
|
| | | }
|
| | | for (int i = 0; i < target.length; i++) {
|
| | | if (target[target.length - i - 1] != src[src.length - i - 1]) {
|
| | | return false;
|
| | | }
|
| | | }
|
| | | return true;
|
| | | }
|
| | |
|
| | |
|
| | | public static int byteIndexOf(byte[] searched, byte[] find, int start) {
|
| | | boolean matched;
|
| | | int end = find.length - 1;
|
| | | int skip = 0;
|
| | | for (int index = start; index <= searched.length - find.length; ++index) {
|
| | | matched = true;
|
| | | if (find[0] != searched[index] || find[end] != searched[index + end]) continue;
|
| | | else skip++;
|
| | | if (end > 10)
|
| | | if (find[skip] != searched[index + skip] || find[end - skip] != searched[index + end - skip])
|
| | | continue;
|
| | | else skip++;
|
| | | for (int subIndex = skip; subIndex < find.length - skip; ++subIndex) {
|
| | | if (find[subIndex] != searched[index + subIndex]) {
|
| | | matched = false;
|
| | | break;
|
| | | }
|
| | | }
|
| | | if (matched) {
|
| | | return index;
|
| | | }
|
| | | }
|
| | | return -1;
|
| | |
|
| | | }
|
| | |
|
| | | public static int getByteIndexOf(byte[] sources, byte[] src) {
|
| | | return getByteIndexOf(sources, src, 0, sources.length);
|
| | | }
|
| | |
|
| | | //判断一个byte数值在另外一个byte数组中对应的游标值
|
| | | public static int getByteIndexOf(byte[] sources, byte[] src, int startIndex) {
|
| | | return getByteIndexOf(sources, src, startIndex, sources.length);
|
| | | }
|
| | |
|
| | |
|
| | | //判断一个byte数值在另外一个byte数组中对应的游标值,指定开始的游标和结束的游标位置
|
| | | public static int getByteIndexOf(byte[] sources, byte[] src, int startIndex, int endIndex) {
|
| | |
|
| | | if (sources == null || src == null || sources.length == 0 || src.length == 0) {
|
| | | return -1;
|
| | | }
|
| | |
|
| | | if (endIndex > sources.length) {
|
| | | endIndex = sources.length;
|
| | | }
|
| | |
|
| | | int i, j;
|
| | | for (i = startIndex; i < endIndex; i++) {
|
| | | if (sources[i] == src[0] && i + src.length < endIndex) {
|
| | | for (j = 1; j < src.length; j++) {
|
| | | if (sources[i + j] != src[j]) {
|
| | | break;
|
| | | }
|
| | | }
|
| | |
|
| | | if (j == src.length) {
|
| | | return i;
|
| | | }
|
| | | }
|
| | | }
|
| | | return -1;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 字符串to Bytes
|
| | | *
|
| | | * @param str 字符串
|
| | | * @return
|
| | | */
|
| | | public static byte[] stringToBytes(String str) {
|
| | | try {
|
| | | // 使用指定的字符集将此字符串编码为byte序列并存到一个byte数组中
|
| | | return str.getBytes("utf-8");
|
| | | } catch (UnsupportedEncodingException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return new byte[]{};
|
| | | }
|
| | |
|
| | | public static String byte2hex(byte[] bytes) {
|
| | | StringBuilder sb = new StringBuilder();
|
| | | String tmp = null;
|
| | | for (byte b : bytes) {
|
| | | //将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制
|
| | | tmp = Integer.toHexString(0xFF & b);
|
| | | if (tmp.length() == 1) {
|
| | | tmp = "0" + tmp;
|
| | | }
|
| | | sb.append(tmp + " ");
|
| | | }
|
| | | return sb.toString();
|
| | | }
|
| | |
|
| | |
|
| | | public static int bytes2int(byte[] bytes) {
|
| | | return bytes[3] & 0xFF | //
|
| | | (bytes[2] & 0xFF) << 8 | //
|
| | | (bytes[1] & 0xFF) << 16 | //
|
| | | (bytes[0] & 0xFF) << 24; //
|
| | | }
|
| | |
|
| | |
|
| | | public static byte[] intToByteArray(int i) {
|
| | | byte[] result = new byte[4];
|
| | | result[0] = (byte) ((i >> 24) & 0xFF);
|
| | | result[1] = (byte) ((i >> 16) & 0xFF);
|
| | | result[2] = (byte) ((i >> 8) & 0xFF);
|
| | | result[3] = (byte) (i & 0xFF);
|
| | | return result;
|
| | | }
|
| | |
|
| | | public static int byteArrayToInt(byte[] b) {
|
| | | int i = (b[0] & 0xFF) * 256 * 256 * 256 + (b[1] & 0xFF) * 256 * 256 + (b[2] & 0xFF) * 256 + (b[3] & 0xFF);
|
| | | return i;
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | }
|
File was renamed from app/src/main/java/com/hdl/photovoltaic/utils/HDLMD5Utils.java |
| | |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.math.BigInteger; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.security.MessageDigest; |
| | | import java.security.NoSuchAlgorithmException; |
| | | |
| | | public class HDLMD5Utils { |
| | | public class Md5Utils { |
| | | /** |
| | | * MD5转字符串 |
| | | */ |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | public final static String md5(String plainText) { |
| | | |
| | | // 返回字符串 |
| | | |
| | | String md5Str = null; |
| | | |
| | | try { |
| | | |
| | | // 操作字符串 |
| | | |
| | | StringBuffer buf = new StringBuffer(); |
| | | |
| | | MessageDigest md = |
| | | |
| | | MessageDigest.getInstance("MD5"); |
| | | |
| | | // 添加要进行计算摘要的信息,使用 plainText 的 byte |
| | | |
| | | |
| | | md.update(plainText.getBytes()); |
| | | |
| | | // 计算出摘要,完成哈希计算。 |
| | | |
| | | byte b[] = md.digest(); |
| | | |
| | | int i; |
| | | |
| | | for (int offset = 0; offset < b.length; offset++) { |
| | | |
| | | i = b[offset]; |
| | | |
| | | if (i < 0) { |
| | | |
| | | i += 256; |
| | | |
| | | } |
| | | |
| | | if (i < 16) { |
| | | |
| | | buf.append("0"); |
| | | |
| | | } |
| | | |
| | | // 将整型 十进制 i |
| | | |
| | | |
| | | buf.append(Integer.toHexString(i)); |
| | | |
| | | } |
| | | |
| | | // 32位的加密 |
| | | |
| | | md5Str = buf.toString(); |
| | | |
| | | // 16位的加密 |
| | | |
| | | // md5Str = buf.toString().md5Strstring(8,24); |
| | | |
| | | } catch (Exception e) { |
| | | |
| | | e.printStackTrace(); |
| | | |
| | | } |
| | | |
| | | return md5Str; |
| | | |
| | | } |
| | | } |