mac
2023-12-06 d830fef6a2bc2ab061e6ac2b423c4a49dda3cf21
app/src/main/java/com/hdl/photovoltaic/ui/MyPowerStationActivity.java
@@ -1,38 +1,71 @@
package com.hdl.photovoltaic.ui;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.MotionEvent;
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.linkpm.sdk.ota.bean.CloudGatewayDriversBean;
import com.hdl.linkpm.sdk.utils.HDLMD5Utils;
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.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.AppManagerUtils;
import com.hdl.photovoltaic.utils.PermissionUtils;
import com.hdl.sdk.link.HDLLinkLocalSdk;
import com.hdl.sdk.link.common.event.EventListener;
import com.hdl.sdk.link.core.bean.LinkResponse;
import com.hdl.sdk.link.core.bean.ModbusResponse;
import com.hdl.sdk.link.core.bean.eventbus.BaseEventBus;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.config.HDLLinkConfig;
import com.hdl.sdk.link.core.utils.mqtt.MqttRecvClient;
import java.nio.charset.StandardCharsets;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.List;
import okhttp3.ResponseBody;
/**
 * 我的电站界面
 */
public class MyPowerStationActivity extends CustomBaseActivity {
    private ActivityMyPowerStationBinding viewBinding;
    private int currentFragmentIndex = 0;
    private int currentFragmentIndex = 1;
    private EventListener allTopicsListener;
    private int backPressTimes;
    @Override
@@ -43,30 +76,43 @@
    @Override
    public void onBindView(Bundle savedInstanceState) {
        getWindow().setStatusBarColor(getColor(R.color.text_245EC3));
        setNotificationBarBackgroundColor(CustomColor.blue);
        //初始化
        initView();
        //初始化界面监听器
        initEvent();
        //注册监听
        registerAllTopicsListener();
        //初始化mqtt客户端
        initMqttClient();
//        dddd();
    }
    private void initEvent() {
        viewBinding.myPowerStationBottomIl1.clickTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentFragmentIndex = 0;
                bottomViewChangeOfStyle();
            }
        });
        viewBinding.myPowerStationBottomIl2.clickTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentFragmentIndex = 1;
                bottomViewChangeOfStyle();
            }
        });
        viewBinding.myMessageBottomIl2.clickTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentFragmentIndex = 2;
                bottomViewChangeOfStyle();
            }
        });
        viewBinding.myMeBottomIl3.clickTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentFragmentIndex = 3;
                bottomViewChangeOfStyle();
            }
        });
    }
@@ -80,29 +126,44 @@
    }
    private void initView() {
        viewBinding.myPowerStationBottomIl1.titleTv.setText(R.string.my_power_station_电站);
        viewBinding.myPowerStationBottomIl2.titleTv.setText(R.string.my_power_station_我的);
        viewBinding.myPowerStationBottomIl1.titleTv.setText(R.string.power_station);
        viewBinding.myMessageBottomIl2.titleTv.setText(R.string.message);
        viewBinding.myMeBottomIl3.titleTv.setText(R.string.power_station_me);
        this.bottomViewChangeOfStyle();
    }
    private void bottomViewChangeOfStyle() {
        if (this.currentFragmentIndex == 0) {
            viewBinding.myPowerStationFcv1.setVisibility(View.VISIBLE);
            viewBinding.myPowerStationFcv2.setVisibility(View.GONE);
        if (this.currentFragmentIndex == 1) {
            viewBinding.myPowerStationFcv1.setVisibility(View.VISIBLE);//显示电站界面
            viewBinding.myMessageFcv2.setVisibility(View.GONE);//隐藏消息界面
            viewBinding.myMeFcv3.setVisibility(View.GONE);//隐藏我的界面
            viewBinding.myPowerStationBottomIl1.iconIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.selectedpowerstation));
            viewBinding.myPowerStationBottomIl1.titleTv.setTextColor(getColor(R.color.text_90000000));
            viewBinding.myPowerStationBottomIl2.iconIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.unselectedme));
            viewBinding.myPowerStationBottomIl2.titleTv.setTextColor(getColor(R.color.text_65000000));
        } else {
            viewBinding.myPowerStationFcv1.setVisibility(View.GONE);
            viewBinding.myPowerStationFcv2.setVisibility(View.VISIBLE);
            viewBinding.myMessageBottomIl2.iconIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.message_unselected));
            viewBinding.myMessageBottomIl2.titleTv.setTextColor(getColor(R.color.text_65000000));
            viewBinding.myMeBottomIl3.iconIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.unselectedme));
            viewBinding.myMeBottomIl3.titleTv.setTextColor(getColor(R.color.text_65000000));
        } else if (this.currentFragmentIndex == 2) {
            viewBinding.myPowerStationFcv1.setVisibility(View.GONE);//隐藏电站界面
            viewBinding.myMessageFcv2.setVisibility(View.VISIBLE);//显示消息界面
            viewBinding.myMeFcv3.setVisibility(View.GONE);//隐藏我的界面
            viewBinding.myPowerStationBottomIl1.iconIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.unselectedpowerstation));
            viewBinding.myPowerStationBottomIl1.titleTv.setTextColor(getColor(R.color.text_65000000));
            viewBinding.myPowerStationBottomIl2.iconIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.selectedme));
            viewBinding.myPowerStationBottomIl2.titleTv.setTextColor(getColor(R.color.text_90000000));
            viewBinding.myMessageBottomIl2.iconIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.message_select));
            viewBinding.myMessageBottomIl2.titleTv.setTextColor(getColor(R.color.text_90000000));
            viewBinding.myMeBottomIl3.iconIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.unselectedme));
            viewBinding.myMeBottomIl3.titleTv.setTextColor(getColor(R.color.text_65000000));
        } else if (this.currentFragmentIndex == 3) {
            viewBinding.myPowerStationFcv1.setVisibility(View.GONE);//隐藏电站界面
            viewBinding.myMessageFcv2.setVisibility(View.GONE);//隐藏消息界面
            viewBinding.myMeFcv3.setVisibility(View.VISIBLE);//显示我的界面
            viewBinding.myPowerStationBottomIl1.iconIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.unselectedpowerstation));
            viewBinding.myPowerStationBottomIl1.titleTv.setTextColor(getColor(R.color.text_65000000));
            viewBinding.myMessageBottomIl2.iconIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.message_unselected));
            viewBinding.myMessageBottomIl2.titleTv.setTextColor(getColor(R.color.text_65000000));
            viewBinding.myMeBottomIl3.iconIv.setImageDrawable(AppCompatResources.getDrawable(_mActivity, R.drawable.selectedme));
            viewBinding.myMeBottomIl3.titleTv.setTextColor(getColor(R.color.text_90000000));
        }
    }
@@ -157,12 +218,16 @@
        allTopicsListener = new EventListener() {
            @Override
            public void onMessage(Object msg) {
                if (msg instanceof LinkResponse) {
                    LinkResponse response = (LinkResponse) msg;
                if (msg == null) {
                    return;
                }
                if (msg instanceof ModbusResponse) {
                    ModbusResponse response = (ModbusResponse) msg;
                    if (response.getTopic() == null) {
                        return;
                    }
                    HdlLogLogic.print("监听到的数据===========" + new Gson().toJson(response));
                    HdlLogLogic.print("监听到的数据===" + new Gson().toJson(response), false);
//                    if (response.getTopic().endsWith("custom/native/inverter/up")) {
                    HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
                    uniCallBackBaseBean.setType(response.getTopic());
@@ -170,11 +235,200 @@
                    HdlUniLogic.getInstance().sendUni(HDLUniMP.UNI_EVENT_NOTIFICATION_DEVICE_MODEL, uniCallBackBaseBean);
//                    }
                } else if (msg instanceof LinkResponse) {
                    LinkResponse linkResponse = (LinkResponse) msg;
                    if (linkResponse.getTopic() == null) {
                        return;
                    }
                    if (linkResponse.getTopic().endsWith("/ota/device/progress/up")) {
                        HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
                        uniCallBackBaseBean.setType(linkResponse.getTopic());
                        uniCallBackBaseBean.setData(linkResponse.getData());
                        HdlUniLogic.getInstance().sendUni(HDLUniMP.UNI_EVENT_REPLY_OTA_MODEL, uniCallBackBaseBean);
                    }
                }
            }
        };
        HDLLinkLocalSdk.getInstance().registerAllTopicsListener(allTopicsListener);
    }
    /**
     * 初始化mqtt客户端
     */
    public void initMqttClient() {
        HdlMqttLogic.getInstance().getMqttRemoteInfo(HomeType.A, new CloudCallBeak<MqttInfo>() {
            @Override
            public void onSuccess(MqttInfo info) {
                if (info != null) {
                    MqttRecvClient.init(_mActivity, info.getUrl(), info.getClientId(), info.getUserName(), info.getPassWord());
                }
            }
            @Override
            public void onFailure(HDLException e) {
                HdlLogLogic.print("读取mqtt远程信息失败===" + e.getMessage(), e.getCode(), true);
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMessage(BaseEventBus event) {
        if (event == null) {
            return;
        }
        if (TextUtils.isEmpty(event.getTopic())) {
            return;
        }
        //网关更新mqtt秘钥更新通知
        if (event.getTopic().contains("/custom/mqtt/secret/change")) {
            String[] topics = event.getTopic().split("/");
            //主题不符合规则不处理
            if (topics.length < 3) {
                return;
            }
            int index = -1;
            List<GatewayBean> list = HdlDeviceLogic.getInstance().getCurrentHomeGatewayList();
            for (int i = 0; i < list.size(); i++) {
                GatewayBean gatewayBean = list.get(i);
                if (gatewayBean.getGatewayId().equals(topics[2])) {
                    index = i;
                    break;
                }
            }
            if (index > -1) {
                GatewayBean gatewayBean = list.get(index);
                HdlDeviceLogic.getInstance().getDeviceRemoteInfo(UserConfigManage.getInstance().getHomeId(), gatewayBean.getSpk(), gatewayBean.getDevice_mac(), new CloudCallBeak<DeviceRemoteInfo>() {
                    @Override
                    public void onSuccess(DeviceRemoteInfo deviceRemoteInfo) {
                        if (deviceRemoteInfo != null) {
                            //更新mqtt通讯的新秘钥
                            gatewayBean.setAesKey(deviceRemoteInfo.getSecret());
                            //用之前的库,底层mqtt订阅,加解密会用到该参数;
                            HDLLinkConfig.getInstance().setAesKey(deviceRemoteInfo.getSecret());//设置mqtt通讯秘钥库
                        }
                    }
                    @Override
                    public void onFailure(HDLException e) {
                    }
                });
            }
        } else if (event.getTopic().contains(HdlOtaLogic.localDownloadProgress)) {
            //下载云端驱动或者固件文件,自己计算进度条上报到uni那边
            HDLUniMP.UniCallBackBaseBean uniCallBackBaseBean = new HDLUniMP.UniCallBackBaseBean();
            uniCallBackBaseBean.setType(event.getType());
            uniCallBackBaseBean.setData(event.getData());
            HdlUniLogic.getInstance().sendUni(HDLUniMP.UNI_EVENT_REPLY_OTA_MODEL, uniCallBackBaseBean);
            HdlLogLogic.print("进度:" + ((HdlOtaLogic.Progress) event.getData()).step + "", false);
        }
    }
    /**
     * 点击返回键的事件
     */
    @Override
    public void onBackPressed() {
        if (AppManagerUtils.getAppManager().getActivitySize() <= 1) {
            if (backPressTimes == 0) {
                HdlThreadLogic.toast(_mActivity, R.string.kill_app);
                backPressTimes = 1;
                new Thread() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } finally {
                            backPressTimes = 0;
                        }
                    }
                }.start();
                return;
            } else {
                AppManagerUtils.getAppManager().finishAllActivity();
                Process.killProcess(Process.myPid());
            }
        }
        super.onBackPressed();
    }
    private void dddd() {
//        String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName("036F6C1B", "V01.01.02");
//        String data = HdlFileLogic.getInstance().readFile(drivePathFileName);
//        String md5 = HDLMD5Utils.encodeMD5(data);//网关驱动需要
//        Log.e("", md5);
//        HdlOtaLogic.getInstance().getDeviceDriverDownloadFile("http://192.168.16.135:49152/storage/emulated/0/Android/data/com.hdl.photovoltaic/files/Documents/upgrade/drive/036F6C1B_V01.01.02.zip", new CloudCallBeak<ResponseBody>() {
//            @Override
//            public void onSuccess(ResponseBody responseBody) {
//
//                HdlOtaLogic.getInstance().disposeDownLoadFile(drivePathFileName, responseBody, md5);
//
////                //本地升级驱动文件路径
////
////                String data = HdlFileLogic.getInstance().readFile(drivePathFileName);
////                String md5 = HDLMD5Utils.encodeMD5(data);//网关驱动需要
//            }
//
//            @Override
//            public void onFailure(HDLException e) {
//
//            }
//        });
//        HdlOtaLogic.getInstance().getNewGatewayDrivers("036F6C1B", "A000", new CloudCallBeak<CloudGatewayDriversBean>() {
//            @Override
//            public void onSuccess(CloudGatewayDriversBean obj) {
//
//            }
//
//            @Override
//            public void onFailure(HDLException e) {
//
//            }
//        });
//        dowlao();
        HdlOtaLogic.getInstance().startLocalService(new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        });
    }
    void dowlao() {
        HdlOtaLogic.getInstance().getDeviceUpgradeDownloadFile("http://hdl-hz-test.oss-cn-hangzhou.aliyuncs.com/20/2023/11/1834a1d0-d2a7-4b6f-9877-6f2722e9b7bc.hdlipk_sec", new CloudCallBeak<ResponseBody>() {
            @Override
            public void onSuccess(ResponseBody responseBody) {
                String drivePathFileName = HdlFileLogic.getInstance().getDrivePathFileName("036F6C1B", "V01.01.02");
                HdlOtaLogic.getInstance().disposeDownLoadFile(drivePathFileName, responseBody, "84cd8e62d4311c90508313f0e5965dbb", HdlOtaLogic.driver_type);
//                HdlFileLogic.getInstance().writeFile(drivePathFileName, bytes);//写入新文件
                HdlLogLogic.print("写入新驱动文件到内存成功.", false);
            }
            @Override
            public void onFailure(HDLException e) {
                HdlLogLogic.print("下载驱动文件到内存失败.", false);
            }
        });
    }
}