123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- package com.sunzee.mvp.advertising;
- import android.os.Environment;
- import android.text.TextUtils;
- import android.util.Log;
- import com.google.gson.Gson;
- import com.orhanobut.hawk.Hawk;
- import com.sunzee.base.BaseActivity;
- import com.sunzee.base.BaseApplication;
- import com.sunzee.base.BasePresenter;
- import com.sunzee.model.Global;
- import com.sunzee.model.Heartbeat;
- import com.sunzee.model.HttpResult;
- import com.sunzee.model.HttpResult2;
- import com.sunzee.model.domain.GetAdBean;
- import com.sunzee.model.domain.Name;
- import com.sunzee.model.domain.ParameterBean;
- import com.sunzee.model.message.ApiMessageEvent;
- import com.sunzee.retrofit.ApiCallback;
- import com.sunzee.thread.advanceparameter.ThreadPoolAdvanceParameter;
- import com.sunzee.utils.AlarmManagerUtil;
- import com.sunzee.utils.FileUtil;
- import com.sunzee.utils.HexadecimalUtil;
- import com.sunzee.utils.ToastUtil;
- import com.wuxiaolong.androidutils.library.LogUtil;
- import org.greenrobot.eventbus.EventBus;
- import org.w3c.dom.Text;
- import java.io.File;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import okhttp3.RequestBody;
- /**
- * 广告界面 presenter
- * 需求:
- */
- public class AdvertisingPresenter extends BasePresenter<AdvertisingView> {
- private static final String TAG = "AdvertisingPresenter";
- public AdvertisingPresenter(AdvertisingView view) {
- attachView(view);
- }
- //--------------------------------------------------------------------------广告 start----------------------------------------------------------------------------------------------
- public void play() {
- //2.读文件的数据
- int leftFileSize = leftVideoImageFile();
- int rightFileSize = rightVideoImageFile();
- Log.d(TAG, "initView: leftFileSize" + leftFileSize);
- Log.d(TAG, "initView: rightFileSize" + rightFileSize);
- if (leftFileSize != 0) {
- mvpView.playRight(playFile);
- }
- if (rightFileSize != 0) {
- mvpView.playLeft(playFileList);
- for (File file : playFileList) {
- Log.d(TAG, "play: " + file.getName());
- }
- }
- }
- //下载在本地的所有文件
- private File[] allFiles;
- //本地所有<文件名字,文件>的对应
- private Map<String, File> playList;
- //要播放的文件
- private Map<Integer, GetAdBean> showOut;
- //要播放的文件名字(实体)
- private ArrayList<GetAdBean> names;
- //要播放的文件列表
- private List<File> allPlayList;
- /**
- * 获取左边视频图片的文件
- *
- * @return
- */
- private int rightVideoImageFile() {
- showOut = Hawk.get("show_out", new HashMap<Integer, GetAdBean>());// 第一次为空。
- Log.d(TAG, "show_out rightVideoImageFile: " + showOut);
- this.allFiles = FileUtil.getAllFiles(Environment.getExternalStorageDirectory().getPath() + "/rightvideoimg/", "");
- //判断是否为空
- if (this.allFiles == null || this.allFiles.length <= 0) {
- LogUtil.d(TAG, "rightvideoimg initView: 文件为空,没有下载");
- return 0;
- }
- //获取本地所有文件的名字
- playList = new HashMap<>();
- //获取名字与文件对应关系
- for (int i = 0; i < this.allFiles.length; i++) {
- String name = this.allFiles[i].getName();
- String fileName = name.substring(0, name.length() - 4);//获取文件名
- LogUtil.d(TAG, "rightvideoimg initView: 本地所有的文件名:" + fileName);
- // 文件名 文件
- playList.put(fileName, this.allFiles[i]);
- }
- if (showOut == null || showOut.size() <= 0) {
- //没有要播放的文件
- LogUtil.d(TAG, "showOut initView: 没有要播放的文件");
- return 0;
- }
- names = new ArrayList<>();
- for (int i = 0; i < allFiles.length; i++) {
- if (showOut.get(i) != null) {
- String name = showOut.get(i).getId();
- LogUtil.d(TAG, "showOut initView: 广告选择要播放的文件名:" + name);
- names.add(showOut.get(i));
- }
- }
- if (names == null || names.size() <= 0) {
- // ToastUtil.showToast("请选择文件播放");
- LogUtil.d(TAG, "showOut initView: 没有选择播放的文件");
- return 0;
- }
- allPlayList = new ArrayList<>();
- if (playList == null || playList.size() <= 0) {
- LogUtil.d(TAG, "rightvideoimg initView: playList为空");
- return 0;
- }
- //通过名字找出要找出需要播放的文件
- for (int i = 0; i < names.size(); i++) {
- File file = playList.get(names.get(i).getId());
- if (file != null) {
- LogUtil.d(TAG, "rightvideoimg initView: 通过名字找到的播放文件:" + file.getName());
- allPlayList.add(file);
- }
- }
- if (allPlayList == null || allPlayList.size() <= 0) {
- LogUtil.d(TAG, "rightvideoimg showOUt initView: allPlayList为0");
- return 0;
- }
- Log.d(TAG, "rightVideoImageFile: " + allPlayList.size());
- return setTimeRules();
- // return allPlayList.size();
- }
- //真正要播放的文件
- private ArrayList<File> playFileList;
- //要播放的实体 用于判断是图片还是视频
- private ArrayList<GetAdBean> playRules;
- /**
- * 获取当前时间规则与要播放的文件
- */
- private int setTimeRules() {
- playFileList = new ArrayList<>();
- playRules = new ArrayList<>();
- HashMap<String, String> ad_rules_map = Hawk.get("ad_rules_map", new HashMap<String, String>());
- Calendar calendar = Calendar.getInstance();
- int hour = calendar.get(Calendar.HOUR_OF_DAY);
- LogUtil.e(TAG, "judeImgVideoAndPlay: hour=" + hour);
- //当前时间段
- String timeSort = hour + "-" + (hour + 1);
- LogUtil.d(TAG, "judeImgVideoAndPlay:timeSort= " + timeSort);
- Log.d(TAG, "setTimeRules: names" + names);
- //获取当前时间段的要播放的id
- for (int i = 0; i < names.size(); i++) {
- for (Map.Entry<String, String> entry : ad_rules_map.entrySet()) {
- entry.getKey();
- entry.getValue();
- if (entry.getValue().contains(timeSort)) {
- if (entry.getKey().equals(names.get(i).getId())) {
- LogUtil.d(TAG, "judeImgVideoAndPlay: 添加要播放的文件:" + names.get(i).getId());
- playRules.add(names.get(i));
- }
- }
- }
- }
- //获取当前时间段要播放的实体
- for (int i = 0; i < playRules.size(); i++) {
- //当前时间段要播放的文件
- File file = playList.get(playRules.get(i).getId());
- if (file != null) {
- LogUtil.d(TAG, "judeImgVideoAndPlay: 当前时间段要播放的文件的id:" + playRules.get(i).getId());
- playFileList.add(file);
- }
- }
- LogUtil.e(TAG, "setTimeRules: playFileList.size()=" + playFileList.size());
- if (playFileList.size() <= 0) {
- LogUtil.d(TAG, "playFileList: 空");
- return 0;
- }
- return playFileList.size();
- }
- //A屏 要播放的文件
- private ArrayList<File> playFile;
- private ArrayList<GetAdBean> adBeans;
- /**
- * 获取右边视频图片的文件
- *
- * @return
- */
- private int leftVideoImageFile() {
- File[] allFiles = FileUtil.getAllFiles(Environment.getExternalStorageDirectory().getPath() + "/leftvideoimg/", "");
- if (allFiles == null || allFiles.length <= 0) {
- LogUtil.e(TAG, "playCorporationAd: 没有下载A屏文件");
- return 0;
- }
- adBeans = Hawk.get("showAdA", new ArrayList<GetAdBean>());//todo 第一次为空,第一次为零。
- Log.d(TAG, "showAdA leftVideoImageFile: " + adBeans);
- if (adBeans == null || adBeans.size() <= 0) {
- LogUtil.e(TAG, "playCorporationAd: 没有下载A屏数据");
- return 0;
- }
- //按照网络数据顺序添加
- playFile = new ArrayList<>();
- for (int i = 0; i < adBeans.size(); i++) {
- GetAdBean getAdBean = adBeans.get(i);
- for (int j = 0; j < allFiles.length; j++) {
- String name = allFiles[j].getName();
- LogUtil.d(TAG, "playCorporationAd: name= " + name);
- String fileName = name.substring(0, name.length() - 4);//获取文件名
- Log.d(TAG, "leftVideoImageFile: " + fileName + ":getAdBean.getId()+" + getAdBean.getId());
- if (fileName.equals(getAdBean.getId())) {
- playFile.add(allFiles[j]);
- }
- }
- }
- if (playFile == null || playFile.size() <= 0) {
- Log.d(TAG, "playCorporationAd: 没有匹配的广告数据");
- return 0;
- }
- for (int i = 0; i < playFile.size(); i++) {
- LogUtil.d(TAG, "playCorporationAd: 本地的 name=" + playFile.get(i).getName());
- LogUtil.d(TAG, "playCorporationAd: 网络的 name=" + adBeans.get(i).getId());
- }
- return playFile.size();
- }
- //--------------------------------------------------------------------------广告 end----------------------------------------------------------------------------------------------
- //-------------------------------------------------------------------------清洗规则 start--------------------------------------------------------------------------------------------
- public void requstCleanRule() {
- Map<String, String> params = new HashMap<>();
- params.put("clientId", Heartbeat.deviceId);
- addSubscription(apiStores.cleanRule(params), new ApiCallback<HttpResult<String>>() {
- @Override
- public void onSuccess(HttpResult<String> model) {
- String times = model.getData().trim();
- Log.d(TAG, "onSuccess: " + times);
- if ("尚未设定清洗规则".equals(times)) {
- return;
- }
- Log.d(TAG, "onSuccess: ");
- Log.d(TAG, "onSuccess2: " + times);
- if (!TextUtils.isEmpty(times)) {
- String[] Alltime = times.split("\\,");
- Global.alarmNumber = Alltime.length;
- for (int i = 0; i < Alltime.length; i++) {
- String[] data = Alltime[i].split(":");
- for (String datum : data) {
- Log.d(TAG, "onSuccess1: " + datum);
- }
- //设置定时闹钟
- AlarmManagerUtil.setAlarm(BaseApplication.getContext(), 1, Integer.valueOf(data[0]), Integer.valueOf(data[1]), Integer.valueOf(data[2]), i, 0, "闹钟响了26");
- }
- }
- }
- @Override
- public void onFailure(String msg) {
- Log.d(TAG, "onFailure: " + msg);
- }
- @Override
- public void onFinish() {
- }
- });
- }
- //-------------------------------------------------------------------------清洗规则 end--------------------------------------------------------------------------------------------
- //------------------------------------------------------------------请求是否已经付费了。或者说过期了。 start-----------------------------------------------
- public void requestCleanTimeAndRemaining() {
- Map<String, String> params = new HashMap<>();
- params.put("clientId", Heartbeat.deviceId);
- addSubscription(apiStores.getCleanTimeAndRemaining(params), new ApiCallback<HttpResult2<String>>() {
- @Override
- public void onSuccess(HttpResult2<String> model) {
- Log.d(TAG, "onSuccess11: " + model);
- String overdueDate = model.getOverdueDate();
- String remaining = model.getRemaining();
- if (TextUtils.isEmpty(overdueDate)) {
- //如果为空
- Hawk.put(Name.OVERDUE_DATE, null);
- } else {
- try {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(overdueDate));
- Hawk.put(Name.OVERDUE_DATE, calendar.getTimeInMillis());
- } catch (Exception e) {
- e.printStackTrace();
- Hawk.put(Name.OVERDUE_DATE, null);
- }
- }
- if (TextUtils.isEmpty(remaining)) {
- Hawk.put(Name.REMAINING, null);
- } else {
- Hawk.put(Name.REMAINING, remaining);
- }
- }
- @Override
- public void onFailure(String msg) {
- Log.d(TAG, "onFailure: " + msg);
- //失败了。
- }
- @Override
- public void onFinish() {
- }
- });
- }
- //---------------------------------------------------------------------请求是否已经付费了。 end---------------------------------------------
- public void setAdvanced(String name, ArrayList<String> results, ThreadPoolAdvanceParameter threadGetParameter1) {
- if (name.equals("D426")) {
- if (!threadGetParameter1.isReceive1()) {
- threadGetParameter1.setReceive1(true);
- threadGetParameter1.stopReadD426();
- threadGetParameter1.getValues()[0] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
- }
- } else if (name.equals("D427")) {
- if (!threadGetParameter1.isReceive2()) {
- threadGetParameter1.setReceive2(true);
- threadGetParameter1.stopReadD427();
- threadGetParameter1.getValues()[1] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
- }
- } else if (name.equals("D428")) {
- if (!threadGetParameter1.isReceive3()) {
- threadGetParameter1.setReceive3(true);
- threadGetParameter1.stopReadD428();
- threadGetParameter1.getValues()[2] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
- }
- } else if (name.equals("D429")) {
- if (!threadGetParameter1.isReceive4()) {
- threadGetParameter1.setReceive4(true);
- //参数23 D278
- threadGetParameter1.stopReadD429();
- threadGetParameter1.getValues()[3] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
- }
- } else if (name.equals("D430")) {
- if (!threadGetParameter1.isReceive5()) {
- threadGetParameter1.setReceive5(true);
- threadGetParameter1.stopReadD430();
- threadGetParameter1.getValues()[4] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
- }
- } else if (name.equals("D431")) {
- if (!threadGetParameter1.isReceive6()) {
- threadGetParameter1.setReceive6(true);
- threadGetParameter1.stopReadD431();
- threadGetParameter1.getValues()[5] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
- }
- }
- if (threadGetParameter1.isReceive1() && threadGetParameter1.isReceive2() && threadGetParameter1.isReceive3() && threadGetParameter1.isReceive4() && threadGetParameter1.isReceive5() && threadGetParameter1.isReceive6()) {
- mvpView.getDataSuccess();
- threadGetParameter1.setReceive1(false);
- threadGetParameter1.setReceive2(false);
- threadGetParameter1.setReceive3(false);
- threadGetParameter1.setReceive4(false);
- threadGetParameter1.setReceive5(false);
- threadGetParameter1.setReceive6(false);
- }
- }
- public void updateAdvanced(ArrayList<ParameterBean> parameterBeans) {
- String url = "/api/app_equipment/index/updateParam/1/" + Heartbeat.deviceId + ".htm";
- Gson gson = new Gson();
- String json = gson.toJson(parameterBeans);
- RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
- addSubscription(apiStores.updateParamApi(url, body), new ApiCallback<HttpResult<String>>() {
- @Override
- public void onSuccess(HttpResult<String> model) {
- Log.d(TAG, "onSuccess: " + model);
- }
- @Override
- public void onFailure(String msg) {
- }
- @Override
- public void onFinish() {
- }
- });
- }
- /**
- * 上次局部清洗的蹲位有多少个
- *
- * @param num 数量
- */
- public void sendPositionNum(int num) {
- Map<String, String> params = new HashMap<>();
- params.put("clientId", Heartbeat.deviceId + "");
- params.put("num", num + "");
- Log.d(TAG, "sendPositionNum: " + num);
- addSubscription(apiStores.sendPositionNum(params), new ApiCallback<HttpResult<String>>() {
- @Override
- public void onSuccess(HttpResult<String> model) {
- }
- @Override
- public void onFailure(String msg) {
- }
- @Override
- public void onFinish() {
- }
- });
- }
- }
|