|
@@ -10,31 +10,44 @@ import android.content.Intent;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Environment;
|
|
|
+import android.os.Handler;
|
|
|
import android.os.IBinder;
|
|
|
import android.os.SystemClock;
|
|
|
import android.support.annotation.Nullable;
|
|
|
import android.support.annotation.RequiresApi;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
+import android.view.WindowManager;
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
+import com.hboxs.serialport.SerialPortSendQueue;
|
|
|
+import com.hboxs.serialport.frame.ReadCommandFrame;
|
|
|
+import com.hboxs.serialport.frame.ResponseFrame;
|
|
|
+import com.hboxs.serialport.message.Message;
|
|
|
+import com.hboxs.serialport.util.AsciiUtils;
|
|
|
import com.orhanobut.hawk.Hawk;
|
|
|
import com.sunzee.R;
|
|
|
import com.sunzee.base.BaseApplication;
|
|
|
+import com.sunzee.db.WarringBeanDao;
|
|
|
import com.sunzee.model.Heartbeat;
|
|
|
import com.sunzee.model.HttpResult;
|
|
|
+import com.sunzee.model.domain.AddAlarmRecordBean;
|
|
|
+import com.sunzee.model.domain.ErrorBean;
|
|
|
import com.sunzee.model.domain.GetAdBean;
|
|
|
import com.sunzee.model.domain.HeartbeatBean;
|
|
|
import com.sunzee.model.domain.Name;
|
|
|
import com.sunzee.model.domain.TimeRuleBean;
|
|
|
+import com.sunzee.model.domain.WarringBean;
|
|
|
import com.sunzee.model.message.ApiMessageEvent;
|
|
|
import com.sunzee.model.message.DownLoadMessageEvent;
|
|
|
import com.sunzee.receiver.Alarmreceiver;
|
|
|
import com.sunzee.retrofit.ApiCallback;
|
|
|
import com.sunzee.retrofit.ApiClient;
|
|
|
import com.sunzee.retrofit.ApiStores;
|
|
|
+import com.sunzee.ui.dialog.WarringDialog;
|
|
|
import com.sunzee.utils.FileUtil;
|
|
|
+import com.sunzee.utils.HexadecimalUtil;
|
|
|
import com.sunzee.utils.SharedPreferencesUtils;
|
|
|
import com.sunzee.utils.TimeUtil;
|
|
|
import com.sunzee.utils.ToastUtil;
|
|
@@ -46,8 +59,10 @@ import org.greenrobot.eventbus.Subscribe;
|
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -63,9 +78,6 @@ import okhttp3.RequestBody;
|
|
|
|
|
|
public class MyService extends Service {
|
|
|
private static final String TAG = "MyService";
|
|
|
- //心跳
|
|
|
- private Timer timerHeartbeat;
|
|
|
- private TimerTask taskHeartbeat;
|
|
|
|
|
|
@Nullable
|
|
|
@Override
|
|
@@ -78,6 +90,8 @@ public class MyService extends Service {
|
|
|
super.onCreate();
|
|
|
Log.d(TAG, "MyServiceonCreate: ");
|
|
|
EventBus.getDefault().register(this);
|
|
|
+ // setError( UiUtil.getStringRes(R.string.warring_lutoujiare), true);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
|
|
@@ -136,13 +150,25 @@ public class MyService extends Service {
|
|
|
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
|
|
|
//ELAPSED_REALTIME_WAKEUP表示让定时任务的出发时间从系统开机算起,并且会唤醒CPU。
|
|
|
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pi);
|
|
|
-
|
|
|
+ if (warringDialog == null) {
|
|
|
+ warringBeanDao = BaseApplication.getInstances().getDaoSession().getWarringBeanDao();
|
|
|
+ }
|
|
|
+ startRead();
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
//进程保活相关
|
|
|
-
|
|
|
//return START_STICKY ;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ EventBus.getDefault().unregister(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ //------------------------------------------------------------------------心跳 start---------------------------------
|
|
|
+ private Timer timerHeartbeat;
|
|
|
+ private TimerTask taskHeartbeat;
|
|
|
+
|
|
|
/**
|
|
|
* 开始心跳
|
|
|
*/
|
|
@@ -153,13 +179,11 @@ public class MyService extends Service {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
//经纬度不为0
|
|
|
-// if (Heartbeat.latitude != 0 && Heartbeat.longitude != 0) {
|
|
|
//系统管理不为空
|
|
|
String managerId = (String) SharedPreferencesUtils.getParam(Name.SYSTEM_ID, "");
|
|
|
if (managerId != null && !TextUtils.isEmpty(managerId)) {
|
|
|
- // sendHeart();
|
|
|
+ sendHeart();
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
};
|
|
|
timerHeartbeat.schedule(taskHeartbeat, 1000, 1000 * 60 * 5);
|
|
@@ -216,6 +240,7 @@ public class MyService extends Service {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 检测心跳数据以及开始心跳
|
|
|
*/
|
|
@@ -241,13 +266,9 @@ public class MyService extends Service {
|
|
|
timerHeartbeat = null;
|
|
|
}
|
|
|
}
|
|
|
+ //------------------------------------------------------------------------心跳 end---------------------------------
|
|
|
|
|
|
- @Override
|
|
|
- public void onDestroy() {
|
|
|
- super.onDestroy();
|
|
|
- EventBus.getDefault().unregister(this);
|
|
|
- }
|
|
|
-
|
|
|
+ //——-----------------------------------------------------------------------请求 start------------------------------
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
public void event(ApiMessageEvent messageEvent) {
|
|
|
switch (messageEvent.getName()) {
|
|
@@ -260,6 +281,10 @@ public class MyService extends Service {
|
|
|
Log.d(TAG, "event: DOWNKOADADCID");
|
|
|
downAdvertising((String) messageEvent.getData());
|
|
|
break;
|
|
|
+ case "startClean":
|
|
|
+ Log.d(TAG, "onReceive:闹钟响了26 " + messageEvent.getData());
|
|
|
+ //开始清洗。全蹲位。
|
|
|
+ break;
|
|
|
default:
|
|
|
break;
|
|
|
|
|
@@ -294,7 +319,7 @@ public class MyService extends Service {
|
|
|
}
|
|
|
getAdRuleId("1");
|
|
|
Hawk.put("showAdA", getAdBeansA);
|
|
|
- Log.d(TAG, "GTIntentService:getAdBeansA2 "+getAdBeansA);
|
|
|
+ Log.d(TAG, "GTIntentService:getAdBeansA2 " + getAdBeansA);
|
|
|
//推送给购买页和制作页更新
|
|
|
EventBus.getDefault().post(new DownLoadMessageEvent(DownLoadMessageEvent.Type.downloadAll, "", -1));
|
|
|
return;
|
|
@@ -386,7 +411,9 @@ public class MyService extends Service {
|
|
|
|
|
|
}
|
|
|
}
|
|
|
+ //——-----------------------------------------------------------------------请求 end------------------------------
|
|
|
|
|
|
+ //_------------------------------------------------------------------------广告 start---------------------------------
|
|
|
//要显示的棉花糖广告<index,name>
|
|
|
private Map<Integer, GetAdBean> showSugar;
|
|
|
//要显示的外接广告<index,name>
|
|
@@ -554,7 +581,7 @@ public class MyService extends Service {
|
|
|
if (corporationAdBeans.size() <= 0) {
|
|
|
LogUtil.e(TAG, "onNext: 推送过来的A屏数据相同,不下载");
|
|
|
Hawk.put("showAdA", getAdBeansA);
|
|
|
- Log.d(TAG, "GTIntentService:getAdBeansA 1"+getAdBeansA);
|
|
|
+ Log.d(TAG, "GTIntentService:getAdBeansA 1" + getAdBeansA);
|
|
|
//todo
|
|
|
// if (syncDialog != null) {
|
|
|
// syncDiaLogUtil.setAdState(R.string.sync_success);
|
|
@@ -710,5 +737,280 @@ public class MyService extends Service {
|
|
|
bundle.putInt("scrrentype", getAdBean.getScreenType());
|
|
|
downloadApkIntent.putExtra("download", bundle);
|
|
|
getApplicationContext().startService(downloadApkIntent);
|
|
|
+
|
|
|
+ }
|
|
|
+ //------------------------------------------------------------------------广告 end---------------------------------
|
|
|
+
|
|
|
+ //-----------------------------------------------------------------------报警 start---------------------------------
|
|
|
+ private Timer timerM600;
|
|
|
+ private TimerTask taskM600;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 开始读取报警
|
|
|
+ */
|
|
|
+ private void startRead() {
|
|
|
+ SerialPortSendQueue.clear();
|
|
|
+ timerM600 = new Timer();
|
|
|
+ taskM600 = new TimerTask() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ SerialPortSendQueue.sendCommand(new ReadCommandFrame("M600", 8), 600, "M600");
|
|
|
+ }
|
|
|
+ };
|
|
|
+ timerM600.schedule(taskM600, 0, 1000);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 停止读取报警
|
|
|
+ */
|
|
|
+ private void stopRead() {
|
|
|
+ if (timerM600 != null) {
|
|
|
+ taskM600.cancel();
|
|
|
+ timerM600.cancel();
|
|
|
+ timerM600 = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
+ public void event(Message messageEvent) {
|
|
|
+ switch (messageEvent.getType()) {
|
|
|
+ case connected:
|
|
|
+ break;
|
|
|
+ case sendError:
|
|
|
+ break;
|
|
|
+ case ack:
|
|
|
+ break;
|
|
|
+ case nak:
|
|
|
+ break;
|
|
|
+ case disconnected:
|
|
|
+ ToastUtil.showToast(messageEvent.getContent().toString());
|
|
|
+ break;
|
|
|
+ case response:
|
|
|
+ ResponseFrame responseFrame = (ResponseFrame) messageEvent.getContent();
|
|
|
+ if (responseFrame.isValidSum()) {
|
|
|
+ //通过校验
|
|
|
+ //1 获取本地存储的报警记录
|
|
|
+ //2 将本地的报警记录进行对比
|
|
|
+ //3 若没有的进行添加以及上传 没有的不管他
|
|
|
+ //4 最好走完将他覆盖
|
|
|
+ if (messageEvent.getName().equals("M600")) {
|
|
|
+ try {
|
|
|
+ String result = AsciiUtils.asciiByteArray2HexStr(responseFrame.getData());
|
|
|
+ Log.d(TAG, "resultM600=" + result + ";name" + messageEvent.getName());
|
|
|
+ List<String> strList = HexadecimalUtil.getStrList(result, 4);
|
|
|
+ if (result.length() != 16) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0, len = strList.size(); i < len; i++) {
|
|
|
+ String list = strList.get(i);
|
|
|
+ String s1 = HexadecimalUtil.hexStringToByte(list);
|
|
|
+ String s2 = HexadecimalUtil.flipString(s1, 8);
|
|
|
+ sb.append(s2);
|
|
|
+ }
|
|
|
+ String s1 = sb.toString();
|
|
|
+ List<String> lis = Arrays.asList(s1.split(""));
|
|
|
+ ArrayList<String> list = new ArrayList(lis);
|
|
|
+ list.remove(0);
|
|
|
+ Log.d(TAG, "listevent: " + list);
|
|
|
+ showErrorDialog(list);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ private boolean isOpenReadWarring = true;
|
|
|
+ private List<String> errorListM600;
|
|
|
+ //前进阻碍
|
|
|
+ private boolean isadvance;
|
|
|
+ //下降阻碍
|
|
|
+ private boolean isdecline;
|
|
|
+
|
|
|
+ private void showErrorDialog(ArrayList<String> list) {
|
|
|
+ String json = (String) SharedPreferencesUtils.getParam("error_list_M600", "");
|
|
|
+ Gson gson = new Gson();
|
|
|
+ errorListM600 = gson.fromJson(json, new TypeToken<List<String>>() {
|
|
|
+ }.getType());
|
|
|
+ if (errorListM600 == null) {
|
|
|
+ errorListM600 = new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ ArrayList<String> errorList1 = new ArrayList<>();
|
|
|
+ if (list.get(0).equals("1")) {
|
|
|
+ //M600
|
|
|
+ if (!isadvance) {
|
|
|
+ showDialog(R.string.warring_forward_obstaclesM600);
|
|
|
+ setError(0, UiUtil.getStringRes(R.string.warring_forward_obstaclesM600), errorList1, errorListM600, true);
|
|
|
+ isadvance = true;
|
|
|
+ }
|
|
|
+ } else if (list.get(0).equals("0")) {
|
|
|
+ //警告解除 弹窗消失
|
|
|
+ if (isadvance) {
|
|
|
+ if (warringDialog != null) {
|
|
|
+ warringDialog.dismiss();
|
|
|
+ isadvance = false;
|
|
|
+ //延迟三秒再设置。
|
|
|
+ new Handler().postDelayed(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ SharedPreferencesUtils.setParam("isOpenReadWarring", true);
|
|
|
+ }
|
|
|
+ }, 3000);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (list.get(1).equals("1")) {
|
|
|
+ if (!isdecline) {
|
|
|
+ //M601
|
|
|
+ showDialog(R.string.warring_falling_blockM601);
|
|
|
+ setError(1, UiUtil.getStringRes(R.string.warring_falling_blockM601), errorList1, errorListM600, true);
|
|
|
+ isdecline = true;
|
|
|
+ }
|
|
|
+ } else if (list.get(1).equals("0")) {
|
|
|
+ //警告解除 弹窗消失
|
|
|
+ if (isdecline) {
|
|
|
+ if (warringDialog != null) {
|
|
|
+ warringDialog.dismiss();
|
|
|
+ isdecline = false;
|
|
|
+ //延迟三秒再设置。
|
|
|
+ new Handler().postDelayed(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ SharedPreferencesUtils.setParam("isOpenReadWarring", true);
|
|
|
+ }
|
|
|
+ }, 3000);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (list.get(2).equals("1")) {
|
|
|
+ //M602
|
|
|
+ boolean isOpenReadWarring = (boolean) SharedPreferencesUtils.getParam("isOpenReadWarring", true);
|
|
|
+ if (isOpenReadWarring) {
|
|
|
+ showDialog(R.string.warring_x_axis_stepM602);
|
|
|
+ setError(2, UiUtil.getStringRes(R.string.warring_x_axis_stepM602), errorList1, errorListM600, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (list.get(3).equals("1")) {
|
|
|
+ //M603
|
|
|
+ boolean isOpenReadWarring = (boolean) SharedPreferencesUtils.getParam("isOpenReadWarring", true);
|
|
|
+ if (isOpenReadWarring) {
|
|
|
+ showDialog(R.string.warring_y_axis_stepM603);
|
|
|
+ setError(3, UiUtil.getStringRes(R.string.warring_y_axis_stepM603), errorList1, errorListM600, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String s = gson.toJson(errorList1);
|
|
|
+ SharedPreferencesUtils.setParam("error_list_M600", s);
|
|
|
+ }
|
|
|
+
|
|
|
+ private WarringDialog warringDialog;
|
|
|
+
|
|
|
+ private void showDialog(int strId) {
|
|
|
+ SharedPreferencesUtils.setParam("isOpenReadWarring", false);
|
|
|
+ isOpenReadWarring = (boolean) SharedPreferencesUtils.getParam("isOpenReadWarring", true);
|
|
|
+ if (warringDialog == null) {
|
|
|
+ warringDialog = new WarringDialog(this);
|
|
|
+ }
|
|
|
+ warringDialog.setTvContent(strId);
|
|
|
+ warringDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
|
|
|
+ if (!warringDialog.isShowing()) {
|
|
|
+ warringDialog.show();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private WarringBeanDao warringBeanDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param key
|
|
|
+ * @param reason
|
|
|
+ * @param errorList1 新建的
|
|
|
+ * @param errorListM 存在本地的
|
|
|
+ */
|
|
|
+ public void setError(int key, String reason, ArrayList<String> errorList1, List<String> errorListM, boolean isM660) {
|
|
|
+ long date = System.currentTimeMillis();
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date curDate = new Date(date);
|
|
|
+ String time = formatter.format(curDate);
|
|
|
+ ErrorBean errorBean = new ErrorBean();
|
|
|
+ errorBean.setTime(time);
|
|
|
+ errorBean.setThingError(reason);
|
|
|
+ errorBean.setRemark("");
|
|
|
+
|
|
|
+ AddAlarmRecordBean addAlarmRecordBean = new AddAlarmRecordBean(reason, Heartbeat.deviceId, time, "");
|
|
|
+ WarringBean warringBean = new WarringBean();
|
|
|
+ warringBean.setTime(date);
|
|
|
+ warringBean.setThingError(reason);
|
|
|
+ warringBeanDao.insert(warringBean);
|
|
|
+ errorList1.add(key + "");
|
|
|
+ if (errorListM == null) {
|
|
|
+ //只用于上传M666
|
|
|
+ Log.d(TAG + "22", "setError: 上传");
|
|
|
+ Gson gson = new Gson();
|
|
|
+ String json = gson.toJson(addAlarmRecordBean);
|
|
|
+ RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
|
|
|
+// //上传到服务器
|
|
|
+ addSubscription(apiStores.addAlarmRecord(body), new ApiCallback<HttpResult<String>>() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(HttpResult<String> model) {
|
|
|
+ if (model.getData() != null) {
|
|
|
+ Log.d(TAG, "onSuccess: " + model);
|
|
|
+ Log.d("AddAlarmRecordApi", "onNext: " + model.getData());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(String msg) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFinish() {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!errorListM.contains(key + "")) {
|
|
|
+ //不包含,上传
|
|
|
+ Log.d(TAG + "22", "setError: 上传");
|
|
|
+
|
|
|
+ Gson gson = new Gson();
|
|
|
+ String json = gson.toJson(addAlarmRecordBean);
|
|
|
+
|
|
|
+ RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
|
|
|
+
|
|
|
+ //上传到服务器
|
|
|
+ addSubscription(apiStores.addAlarmRecord(body), new ApiCallback<HttpResult<String>>() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(HttpResult<String> model) {
|
|
|
+ Log.d(TAG, "onSuccess: " + model);
|
|
|
+ if (model.getData() != null) {
|
|
|
+ Log.d("AddAlarmRecordApi", "onNext: " + model.getData());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(String msg) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFinish() {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ Log.d(TAG + "22", "setError: 不上传");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //-----------------------------------------------------------------------报警 end---------------------------------
|
|
|
+
|
|
|
}
|