AdvertisingPresenter.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package com.sunzee.mvp.advertising;
  2. import android.os.Environment;
  3. import android.text.TextUtils;
  4. import android.util.Log;
  5. import com.google.gson.Gson;
  6. import com.orhanobut.hawk.Hawk;
  7. import com.sunzee.base.BaseActivity;
  8. import com.sunzee.base.BaseApplication;
  9. import com.sunzee.base.BasePresenter;
  10. import com.sunzee.model.Global;
  11. import com.sunzee.model.Heartbeat;
  12. import com.sunzee.model.HttpResult;
  13. import com.sunzee.model.HttpResult2;
  14. import com.sunzee.model.domain.GetAdBean;
  15. import com.sunzee.model.domain.Name;
  16. import com.sunzee.model.domain.ParameterBean;
  17. import com.sunzee.model.message.ApiMessageEvent;
  18. import com.sunzee.retrofit.ApiCallback;
  19. import com.sunzee.thread.advanceparameter.ThreadPoolAdvanceParameter;
  20. import com.sunzee.utils.AlarmManagerUtil;
  21. import com.sunzee.utils.FileUtil;
  22. import com.sunzee.utils.HexadecimalUtil;
  23. import com.sunzee.utils.ToastUtil;
  24. import com.wuxiaolong.androidutils.library.LogUtil;
  25. import org.greenrobot.eventbus.EventBus;
  26. import org.w3c.dom.Text;
  27. import java.io.File;
  28. import java.text.SimpleDateFormat;
  29. import java.util.ArrayList;
  30. import java.util.Calendar;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. import okhttp3.RequestBody;
  35. /**
  36. * 广告界面 presenter
  37. * 需求:
  38. */
  39. public class AdvertisingPresenter extends BasePresenter<AdvertisingView> {
  40. private static final String TAG = "AdvertisingPresenter";
  41. public AdvertisingPresenter(AdvertisingView view) {
  42. attachView(view);
  43. }
  44. //--------------------------------------------------------------------------广告 start----------------------------------------------------------------------------------------------
  45. public void play() {
  46. //2.读文件的数据
  47. int leftFileSize = leftVideoImageFile();
  48. int rightFileSize = rightVideoImageFile();
  49. Log.d(TAG, "initView: leftFileSize" + leftFileSize);
  50. Log.d(TAG, "initView: rightFileSize" + rightFileSize);
  51. if (leftFileSize != 0) {
  52. mvpView.playRight(playFile);
  53. }
  54. if (rightFileSize != 0) {
  55. mvpView.playLeft(playFileList);
  56. for (File file : playFileList) {
  57. Log.d(TAG, "play: " + file.getName());
  58. }
  59. }
  60. }
  61. //下载在本地的所有文件
  62. private File[] allFiles;
  63. //本地所有<文件名字,文件>的对应
  64. private Map<String, File> playList;
  65. //要播放的文件
  66. private Map<Integer, GetAdBean> showOut;
  67. //要播放的文件名字(实体)
  68. private ArrayList<GetAdBean> names;
  69. //要播放的文件列表
  70. private List<File> allPlayList;
  71. /**
  72. * 获取左边视频图片的文件
  73. *
  74. * @return
  75. */
  76. private int rightVideoImageFile() {
  77. showOut = Hawk.get("show_out", new HashMap<Integer, GetAdBean>());// 第一次为空。
  78. Log.d(TAG, "show_out rightVideoImageFile: " + showOut);
  79. this.allFiles = FileUtil.getAllFiles(Environment.getExternalStorageDirectory().getPath() + "/rightvideoimg/", "");
  80. //判断是否为空
  81. if (this.allFiles == null || this.allFiles.length <= 0) {
  82. LogUtil.d(TAG, "rightvideoimg initView: 文件为空,没有下载");
  83. return 0;
  84. }
  85. //获取本地所有文件的名字
  86. playList = new HashMap<>();
  87. //获取名字与文件对应关系
  88. for (int i = 0; i < this.allFiles.length; i++) {
  89. String name = this.allFiles[i].getName();
  90. String fileName = name.substring(0, name.length() - 4);//获取文件名
  91. LogUtil.d(TAG, "rightvideoimg initView: 本地所有的文件名:" + fileName);
  92. // 文件名 文件
  93. playList.put(fileName, this.allFiles[i]);
  94. }
  95. if (showOut == null || showOut.size() <= 0) {
  96. //没有要播放的文件
  97. LogUtil.d(TAG, "showOut initView: 没有要播放的文件");
  98. return 0;
  99. }
  100. names = new ArrayList<>();
  101. for (int i = 0; i < allFiles.length; i++) {
  102. if (showOut.get(i) != null) {
  103. String name = showOut.get(i).getId();
  104. LogUtil.d(TAG, "showOut initView: 广告选择要播放的文件名:" + name);
  105. names.add(showOut.get(i));
  106. }
  107. }
  108. if (names == null || names.size() <= 0) {
  109. // ToastUtil.showToast("请选择文件播放");
  110. LogUtil.d(TAG, "showOut initView: 没有选择播放的文件");
  111. return 0;
  112. }
  113. allPlayList = new ArrayList<>();
  114. if (playList == null || playList.size() <= 0) {
  115. LogUtil.d(TAG, "rightvideoimg initView: playList为空");
  116. return 0;
  117. }
  118. //通过名字找出要找出需要播放的文件
  119. for (int i = 0; i < names.size(); i++) {
  120. File file = playList.get(names.get(i).getId());
  121. if (file != null) {
  122. LogUtil.d(TAG, "rightvideoimg initView: 通过名字找到的播放文件:" + file.getName());
  123. allPlayList.add(file);
  124. }
  125. }
  126. if (allPlayList == null || allPlayList.size() <= 0) {
  127. LogUtil.d(TAG, "rightvideoimg showOUt initView: allPlayList为0");
  128. return 0;
  129. }
  130. Log.d(TAG, "rightVideoImageFile: " + allPlayList.size());
  131. return setTimeRules();
  132. // return allPlayList.size();
  133. }
  134. //真正要播放的文件
  135. private ArrayList<File> playFileList;
  136. //要播放的实体 用于判断是图片还是视频
  137. private ArrayList<GetAdBean> playRules;
  138. /**
  139. * 获取当前时间规则与要播放的文件
  140. */
  141. private int setTimeRules() {
  142. playFileList = new ArrayList<>();
  143. playRules = new ArrayList<>();
  144. HashMap<String, String> ad_rules_map = Hawk.get("ad_rules_map", new HashMap<String, String>());
  145. Calendar calendar = Calendar.getInstance();
  146. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  147. LogUtil.e(TAG, "judeImgVideoAndPlay: hour=" + hour);
  148. //当前时间段
  149. String timeSort = hour + "-" + (hour + 1);
  150. LogUtil.d(TAG, "judeImgVideoAndPlay:timeSort= " + timeSort);
  151. Log.d(TAG, "setTimeRules: names" + names);
  152. //获取当前时间段的要播放的id
  153. for (int i = 0; i < names.size(); i++) {
  154. for (Map.Entry<String, String> entry : ad_rules_map.entrySet()) {
  155. entry.getKey();
  156. entry.getValue();
  157. if (entry.getValue().contains(timeSort)) {
  158. if (entry.getKey().equals(names.get(i).getId())) {
  159. LogUtil.d(TAG, "judeImgVideoAndPlay: 添加要播放的文件:" + names.get(i).getId());
  160. playRules.add(names.get(i));
  161. }
  162. }
  163. }
  164. }
  165. //获取当前时间段要播放的实体
  166. for (int i = 0; i < playRules.size(); i++) {
  167. //当前时间段要播放的文件
  168. File file = playList.get(playRules.get(i).getId());
  169. if (file != null) {
  170. LogUtil.d(TAG, "judeImgVideoAndPlay: 当前时间段要播放的文件的id:" + playRules.get(i).getId());
  171. playFileList.add(file);
  172. }
  173. }
  174. LogUtil.e(TAG, "setTimeRules: playFileList.size()=" + playFileList.size());
  175. if (playFileList.size() <= 0) {
  176. LogUtil.d(TAG, "playFileList: 空");
  177. return 0;
  178. }
  179. return playFileList.size();
  180. }
  181. //A屏 要播放的文件
  182. private ArrayList<File> playFile;
  183. private ArrayList<GetAdBean> adBeans;
  184. /**
  185. * 获取右边视频图片的文件
  186. *
  187. * @return
  188. */
  189. private int leftVideoImageFile() {
  190. File[] allFiles = FileUtil.getAllFiles(Environment.getExternalStorageDirectory().getPath() + "/leftvideoimg/", "");
  191. if (allFiles == null || allFiles.length <= 0) {
  192. LogUtil.e(TAG, "playCorporationAd: 没有下载A屏文件");
  193. return 0;
  194. }
  195. adBeans = Hawk.get("showAdA", new ArrayList<GetAdBean>());//todo 第一次为空,第一次为零。
  196. Log.d(TAG, "showAdA leftVideoImageFile: " + adBeans);
  197. if (adBeans == null || adBeans.size() <= 0) {
  198. LogUtil.e(TAG, "playCorporationAd: 没有下载A屏数据");
  199. return 0;
  200. }
  201. //按照网络数据顺序添加
  202. playFile = new ArrayList<>();
  203. for (int i = 0; i < adBeans.size(); i++) {
  204. GetAdBean getAdBean = adBeans.get(i);
  205. for (int j = 0; j < allFiles.length; j++) {
  206. String name = allFiles[j].getName();
  207. LogUtil.d(TAG, "playCorporationAd: name= " + name);
  208. String fileName = name.substring(0, name.length() - 4);//获取文件名
  209. Log.d(TAG, "leftVideoImageFile: " + fileName + ":getAdBean.getId()+" + getAdBean.getId());
  210. if (fileName.equals(getAdBean.getId())) {
  211. playFile.add(allFiles[j]);
  212. }
  213. }
  214. }
  215. if (playFile == null || playFile.size() <= 0) {
  216. Log.d(TAG, "playCorporationAd: 没有匹配的广告数据");
  217. return 0;
  218. }
  219. for (int i = 0; i < playFile.size(); i++) {
  220. LogUtil.d(TAG, "playCorporationAd: 本地的 name=" + playFile.get(i).getName());
  221. LogUtil.d(TAG, "playCorporationAd: 网络的 name=" + adBeans.get(i).getId());
  222. }
  223. return playFile.size();
  224. }
  225. //--------------------------------------------------------------------------广告 end----------------------------------------------------------------------------------------------
  226. //-------------------------------------------------------------------------清洗规则 start--------------------------------------------------------------------------------------------
  227. public void requstCleanRule() {
  228. Map<String, String> params = new HashMap<>();
  229. params.put("clientId", Heartbeat.deviceId);
  230. addSubscription(apiStores.cleanRule(params), new ApiCallback<HttpResult<String>>() {
  231. @Override
  232. public void onSuccess(HttpResult<String> model) {
  233. String times = model.getData().trim();
  234. Log.d(TAG, "onSuccess: " + times);
  235. if ("尚未设定清洗规则".equals(times)) {
  236. return;
  237. }
  238. Log.d(TAG, "onSuccess: ");
  239. Log.d(TAG, "onSuccess2: " + times);
  240. if (!TextUtils.isEmpty(times)) {
  241. String[] Alltime = times.split("\\,");
  242. Global.alarmNumber = Alltime.length;
  243. for (int i = 0; i < Alltime.length; i++) {
  244. String[] data = Alltime[i].split(":");
  245. for (String datum : data) {
  246. Log.d(TAG, "onSuccess1: " + datum);
  247. }
  248. //设置定时闹钟
  249. AlarmManagerUtil.setAlarm(BaseApplication.getContext(), 1, Integer.valueOf(data[0]), Integer.valueOf(data[1]), Integer.valueOf(data[2]), i, 0, "闹钟响了26");
  250. }
  251. }
  252. }
  253. @Override
  254. public void onFailure(String msg) {
  255. Log.d(TAG, "onFailure: " + msg);
  256. }
  257. @Override
  258. public void onFinish() {
  259. }
  260. });
  261. }
  262. //-------------------------------------------------------------------------清洗规则 end--------------------------------------------------------------------------------------------
  263. //------------------------------------------------------------------请求是否已经付费了。或者说过期了。 start-----------------------------------------------
  264. public void requestCleanTimeAndRemaining() {
  265. Map<String, String> params = new HashMap<>();
  266. params.put("clientId", Heartbeat.deviceId);
  267. addSubscription(apiStores.getCleanTimeAndRemaining(params), new ApiCallback<HttpResult2<String>>() {
  268. @Override
  269. public void onSuccess(HttpResult2<String> model) {
  270. Log.d(TAG, "onSuccess11: " + model);
  271. String overdueDate = model.getOverdueDate();
  272. String remaining = model.getRemaining();
  273. if (TextUtils.isEmpty(overdueDate)) {
  274. //如果为空
  275. Hawk.put(Name.OVERDUE_DATE, 0L);
  276. } else {
  277. try {
  278. Calendar calendar = Calendar.getInstance();
  279. calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(overdueDate));
  280. Hawk.put(Name.OVERDUE_DATE, calendar.getTimeInMillis());
  281. } catch (Exception e) {
  282. e.printStackTrace();
  283. Hawk.put(Name.OVERDUE_DATE, 0L);
  284. }
  285. }
  286. if (TextUtils.isEmpty(remaining)) {
  287. Hawk.put(Name.REMAINING, null);
  288. } else {
  289. Hawk.put(Name.REMAINING, remaining);
  290. }
  291. }
  292. @Override
  293. public void onFailure(String msg) {
  294. Log.d(TAG, "onFailure: " + msg);
  295. //失败了。
  296. }
  297. @Override
  298. public void onFinish() {
  299. }
  300. });
  301. }
  302. //---------------------------------------------------------------------请求是否已经付费了。 end---------------------------------------------
  303. public void setAdvanced(String name, ArrayList<String> results, ThreadPoolAdvanceParameter threadGetParameter1) {
  304. if (name.equals("D418")) {
  305. if (!threadGetParameter1.isReceive2()) {
  306. threadGetParameter1.setReceive2(true);
  307. threadGetParameter1.stopReadD418();
  308. threadGetParameter1.getValues()[6] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(0))));
  309. threadGetParameter1.getValues()[7] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(1))));
  310. threadGetParameter1.getValues()[0] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(2))));
  311. threadGetParameter1.getValues()[1] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(3))));
  312. threadGetParameter1.getValues()[2] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(4))));
  313. threadGetParameter1.getValues()[3] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(5))));
  314. threadGetParameter1.getValues()[4] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(6))));
  315. threadGetParameter1.getValues()[5] = String.valueOf(get177Data(HexadecimalUtil.hex2dec(results.get(7))));
  316. Log.d(TAG, "D418setAdvanced: "+results.size());
  317. }
  318. } else if (name.equals("D426")) {
  319. if (!threadGetParameter1.isReceive1()) {
  320. threadGetParameter1.setReceive1(true);
  321. threadGetParameter1.stopReadD426();
  322. threadGetParameter1.getValues()[7] = String.valueOf(HexadecimalUtil.hex2dec(results.get(0)));
  323. threadGetParameter1.getValues()[8] = String.valueOf(HexadecimalUtil.hex2dec(results.get(1)));
  324. threadGetParameter1.getValues()[9] = String.valueOf(HexadecimalUtil.hex2dec(results.get(2)));
  325. threadGetParameter1.getValues()[10] = String.valueOf(HexadecimalUtil.hex2dec(results.get(3)));
  326. threadGetParameter1.getValues()[11] = String.valueOf(HexadecimalUtil.hex2dec(results.get(4)));
  327. threadGetParameter1.getValues()[12] = String.valueOf(HexadecimalUtil.hex2dec(results.get(5)));
  328. threadGetParameter1.getValues()[13] = String.valueOf(HexadecimalUtil.hex2dec(results.get(6)));
  329. threadGetParameter1.getValues()[14] = String.valueOf(HexadecimalUtil.hex2dec(results.get(7)));
  330. threadGetParameter1.getValues()[15] = String.valueOf(HexadecimalUtil.hex2dec(results.get(8)));
  331. Log.d(TAG, "D426setAdvanced: "+results.size());
  332. }
  333. }
  334. if (threadGetParameter1.isReceive1() &&
  335. threadGetParameter1.isReceive2()) {
  336. mvpView.getDataSuccess();
  337. threadGetParameter1.setReceive1(false);
  338. threadGetParameter1.setReceive2(false);
  339. }
  340. }
  341. private String get177Data(long l) {
  342. return String.valueOf(Math.round((l/ Global.num)*10));
  343. }
  344. public void updateAdvanced(ArrayList<ParameterBean> parameterBeans) {
  345. String url = "/api/app_equipment/index/updateParam/1/" + Heartbeat.deviceId + ".htm";
  346. Gson gson = new Gson();
  347. String json = gson.toJson(parameterBeans);
  348. RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
  349. addSubscription(apiStores.updateParamApi(url, body), new ApiCallback<HttpResult<String>>() {
  350. @Override
  351. public void onSuccess(HttpResult<String> model) {
  352. Log.d(TAG, "onSuccess: " + model);
  353. }
  354. @Override
  355. public void onFailure(String msg) {
  356. }
  357. @Override
  358. public void onFinish() {
  359. }
  360. });
  361. }
  362. /**
  363. * 上次局部清洗的蹲位有多少个
  364. *
  365. * @param num 数量
  366. */
  367. public void sendPositionNum(int num) {
  368. Map<String, String> params = new HashMap<>();
  369. params.put("clientId", Heartbeat.deviceId + "");
  370. params.put("num", num + "");
  371. Log.d(TAG, "sendPositionNum: " + num);
  372. addSubscription(apiStores.sendPositionNum(params), new ApiCallback<HttpResult<String>>() {
  373. @Override
  374. public void onSuccess(HttpResult<String> model) {
  375. }
  376. @Override
  377. public void onFailure(String msg) {
  378. }
  379. @Override
  380. public void onFinish() {
  381. }
  382. });
  383. }
  384. }