|
@@ -21,11 +21,15 @@ import android.view.WindowManager;
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
+import com.hboxs.serialport.SerialPortManager;
|
|
|
import com.hboxs.serialport.SerialPortSendQueue;
|
|
|
import com.hboxs.serialport.frame.ReadCommandFrame;
|
|
|
import com.hboxs.serialport.frame.ResponseFrame;
|
|
|
+import com.hboxs.serialport.frame.WriteCommandFrame;
|
|
|
import com.hboxs.serialport.message.Message;
|
|
|
import com.hboxs.serialport.util.AsciiUtils;
|
|
|
+import com.hboxs.serialport.util.ByteUtils;
|
|
|
+import com.hboxs.serialport.util.HexUtils;
|
|
|
import com.orhanobut.hawk.Hawk;
|
|
|
import com.sunzee.R;
|
|
|
import com.sunzee.base.BaseApplication;
|
|
@@ -37,6 +41,7 @@ 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.ParameterBean;
|
|
|
import com.sunzee.model.domain.TimeRuleBean;
|
|
|
import com.sunzee.model.domain.WarringBean;
|
|
|
import com.sunzee.model.message.ApiMessageEvent;
|
|
@@ -45,9 +50,12 @@ import com.sunzee.receiver.Alarmreceiver;
|
|
|
import com.sunzee.retrofit.ApiCallback;
|
|
|
import com.sunzee.retrofit.ApiClient;
|
|
|
import com.sunzee.retrofit.ApiStores;
|
|
|
+import com.sunzee.thread.advanceparameter.ThreadPoolAdvanceParameter;
|
|
|
+import com.sunzee.thread.myservice.ThreadPoolMyservice;
|
|
|
import com.sunzee.ui.dialog.WarringDialog;
|
|
|
import com.sunzee.utils.FileUtil;
|
|
|
import com.sunzee.utils.HexadecimalUtil;
|
|
|
+import com.sunzee.utils.PreventSpeedClickUtil;
|
|
|
import com.sunzee.utils.SharedPreferencesUtils;
|
|
|
import com.sunzee.utils.TimeUtil;
|
|
|
import com.sunzee.utils.ToastUtil;
|
|
@@ -62,6 +70,7 @@ import java.io.File;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
+import java.util.Calendar;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
@@ -72,12 +81,17 @@ import java.util.TimerTask;
|
|
|
import io.reactivex.Observable;
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
|
+import io.reactivex.internal.operators.single.SingleJust;
|
|
|
import io.reactivex.observers.DisposableObserver;
|
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
import okhttp3.RequestBody;
|
|
|
|
|
|
public class MyService extends Service {
|
|
|
private static final String TAG = "MyService";
|
|
|
+ private ThreadPoolMyservice mThreadPoolMyservice = new ThreadPoolMyservice();
|
|
|
+ private boolean isRead = true;
|
|
|
+ private boolean isRegularCleaning = false;
|
|
|
+ private ThreadPoolAdvanceParameter mPoolAdvanceParameter = new ThreadPoolAdvanceParameter();
|
|
|
|
|
|
@Nullable
|
|
|
@Override
|
|
@@ -90,8 +104,6 @@ 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)
|
|
@@ -116,6 +128,8 @@ public class MyService extends Service {
|
|
|
if (first == 4) {
|
|
|
//第一次启动心跳
|
|
|
checkHeartData();
|
|
|
+ mPoolAdvanceParameter.stopAll();
|
|
|
+ mPoolAdvanceParameter.startALLRead();
|
|
|
}
|
|
|
}
|
|
|
//进程保活相关
|
|
@@ -153,7 +167,12 @@ public class MyService extends Service {
|
|
|
if (warringDialog == null) {
|
|
|
warringBeanDao = BaseApplication.getInstances().getDaoSession().getWarringBeanDao();
|
|
|
}
|
|
|
- startRead();
|
|
|
+ if (isRead) {
|
|
|
+ isRead = !isRead;
|
|
|
+ mThreadPoolMyservice.stopReadM600();
|
|
|
+ mThreadPoolMyservice.startALLRead();
|
|
|
+ }
|
|
|
+ //startRead();
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
//进程保活相关
|
|
|
//return START_STICKY ;
|
|
@@ -162,6 +181,7 @@ public class MyService extends Service {
|
|
|
@Override
|
|
|
public void onDestroy() {
|
|
|
super.onDestroy();
|
|
|
+ mThreadPoolMyservice.stopALLRead();
|
|
|
EventBus.getDefault().unregister(this);
|
|
|
}
|
|
|
|
|
@@ -284,13 +304,134 @@ public class MyService extends Service {
|
|
|
case "startClean":
|
|
|
Log.d(TAG, "onReceive:闹钟响了26 " + messageEvent.getData());
|
|
|
//开始清洗。全蹲位。
|
|
|
+ /**
|
|
|
+ * 1.首先检查是否过期了,是否有次数
|
|
|
+ */
|
|
|
+ isCleaned();
|
|
|
+ break;
|
|
|
+ case "updataParamSet":
|
|
|
+ if (!PreventSpeedClickUtil.isFastClick()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ isRegularCleaning = false;
|
|
|
+ startSet(messageEvent.getData());
|
|
|
+ break;
|
|
|
+ case "updataParamRst":
|
|
|
+ if (!PreventSpeedClickUtil.isFastClick()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ isRegularCleaning = false;
|
|
|
+ startRst(messageEvent.getData());
|
|
|
break;
|
|
|
+ case "updataParamWrite":
|
|
|
+ if (!PreventSpeedClickUtil.isFastClick()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ startWrite(messageEvent.getData());
|
|
|
default:
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void startWrite(Object kindData) {
|
|
|
+ String[] split = (String[]) kindData;
|
|
|
+ String address = split[0];
|
|
|
+ String data = split[1].trim();
|
|
|
+ String text = ByteUtils.decimal2fitHex(Integer.valueOf(data));
|
|
|
+ mThreadPoolMyservice.stopWrite();
|
|
|
+ mThreadPoolMyservice.startWrite(address, HexUtils.hexStr2BinStr(text));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startRst(Object data) {
|
|
|
+ mThreadPoolMyservice.stopRst();
|
|
|
+ mThreadPoolMyservice.startRst((String) data);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startSet(Object data) {
|
|
|
+ mThreadPoolMyservice.stopSet();
|
|
|
+ mThreadPoolMyservice.startSet((String) data);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void isCleaned() {
|
|
|
+ addSubscription(apiStores.getBackgroundTime(), new ApiCallback<HttpResult<String>>() {
|
|
|
+ @Override
|
|
|
+ public void onSuccess(HttpResult<String> model) {
|
|
|
+ try {
|
|
|
+ String timedata = model.getData();
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timedata));
|
|
|
+ //第一次就会出现报错。
|
|
|
+ Long overdueDate = (Long) Hawk.get("overdueDate");//购买的到期的时间
|
|
|
+ Long timeInMillis = calendar.getTimeInMillis();//当前时间
|
|
|
+ String data = (String) Hawk.get("remaining");
|
|
|
+ long remaining = Long.valueOf(data);
|
|
|
+ Log.d(TAG, "onSuccess: 时间过期remaining"+remaining);
|
|
|
+ //如果时间没过期,那么可以继续制作
|
|
|
+ if (overdueDate > timeInMillis) {
|
|
|
+ Log.d(TAG, "onSuccess: 没有过期");
|
|
|
+ //发送开始清洗的订单。
|
|
|
+ getCleanReport(0);
|
|
|
+ //如果还有次那么就开始购买。
|
|
|
+ } else if (remaining > 0) {
|
|
|
+ Log.d(TAG, "onSuccess: 时间过期,按次数计算");
|
|
|
+ Hawk.put("remaining", String.valueOf(remaining - 1));
|
|
|
+ //发送开始清洗的订单。
|
|
|
+ getCleanReport(0);
|
|
|
+ }else{
|
|
|
+ Log.d(TAG, "onSuccess: 全部过期,全部不能清洗");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(String msg) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFinish() {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getCleanReport(int i) {
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ params.put("clientId", Heartbeat.deviceId);
|
|
|
+ params.put("type", i + "");
|
|
|
+ params.put("water", 1 + "");
|
|
|
+ params.put("electricity", 1 + "");
|
|
|
+ addSubscription(apiStores.getCleanReport(params), new ApiCallback<HttpResult<String>>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess(HttpResult<String> model) {
|
|
|
+ Log.d(TAG, "getCleanReportonSuccess: " + model);
|
|
|
+ if ("success".equals(model.getData())) {
|
|
|
+ startClean();
|
|
|
+ } else {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(String msg) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFinish() {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startClean() {
|
|
|
+ isRegularCleaning = true;
|
|
|
+ mThreadPoolMyservice.startSetM17();
|
|
|
+ }
|
|
|
+
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
public void event(DownLoadMessageEvent messageEvent) {
|
|
|
switch (messageEvent.getType()) {
|
|
@@ -779,6 +920,23 @@ public class MyService extends Service {
|
|
|
case sendError:
|
|
|
break;
|
|
|
case ack:
|
|
|
+ Log.d(TAG, "Mevent: " + messageEvent.getName());
|
|
|
+ mThreadPoolMyservice.stopSet();
|
|
|
+ mThreadPoolMyservice.stopRst();
|
|
|
+ mThreadPoolMyservice.stopWrite();
|
|
|
+ if (messageEvent.getName().equals("M17")) {
|
|
|
+ mThreadPoolMyservice.stopSetM17();
|
|
|
+ if (isRegularCleaning) {
|
|
|
+ isRegularCleaning = false;
|
|
|
+ mThreadPoolMyservice.stopSetM8();
|
|
|
+ mThreadPoolMyservice.startSetM8();
|
|
|
+ }
|
|
|
+ } else if (messageEvent.getName().equals("M8")) {
|
|
|
+ mThreadPoolMyservice.stopSetM8();
|
|
|
+ // todo向上面报告开始清洗了。
|
|
|
+ }
|
|
|
+ mPoolAdvanceParameter.stopAll();
|
|
|
+ mPoolAdvanceParameter.startALLRead();
|
|
|
break;
|
|
|
case nak:
|
|
|
break;
|
|
@@ -787,16 +945,16 @@ public class MyService extends Service {
|
|
|
break;
|
|
|
case response:
|
|
|
ResponseFrame responseFrame = (ResponseFrame) messageEvent.getContent();
|
|
|
+ String name = messageEvent.getName();
|
|
|
if (responseFrame.isValidSum()) {
|
|
|
//通过校验
|
|
|
//1 获取本地存储的报警记录
|
|
|
//2 将本地的报警记录进行对比
|
|
|
//3 若没有的进行添加以及上传 没有的不管他
|
|
|
//4 最好走完将他覆盖
|
|
|
- if (messageEvent.getName().equals("M600")) {
|
|
|
+ if (name.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;
|
|
@@ -812,12 +970,18 @@ public class MyService extends Service {
|
|
|
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();
|
|
|
}
|
|
|
+ }else{
|
|
|
+ //通过校验
|
|
|
+ Log.d(TAG, "event: " + name);
|
|
|
+ String result = AsciiUtils.asciiByteArray2HexStr(responseFrame.getData());
|
|
|
+ ArrayList<String> results = HexadecimalUtil.flipString(result);
|
|
|
+ setAdvanced(name, results, mPoolAdvanceParameter);
|
|
|
}
|
|
|
+
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
@@ -1012,5 +1176,89 @@ public class MyService extends Service {
|
|
|
}
|
|
|
}
|
|
|
//-----------------------------------------------------------------------报警 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()) {
|
|
|
+ getDataSuccess();
|
|
|
+ threadGetParameter1.setReceive1(false);
|
|
|
+ threadGetParameter1.setReceive2(false);
|
|
|
+ threadGetParameter1.setReceive3(false);
|
|
|
+ threadGetParameter1.setReceive4(false);
|
|
|
+ threadGetParameter1.setReceive5(false);
|
|
|
+ threadGetParameter1.setReceive6(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void getDataSuccess() {
|
|
|
+ String[] values = mPoolAdvanceParameter.getValues();
|
|
|
+ ArrayList<ParameterBean> parameterBeans = new ArrayList<>();
|
|
|
+ String[] paraAddress = {
|
|
|
+ "D426", "D427", "D428", "D429",
|
|
|
+ "D430", "D431"};
|
|
|
+ for (int i = 0; i < values.length; i++) {
|
|
|
+ parameterBeans.add(new ParameterBean(paraAddress[i], values[i]));
|
|
|
+ }
|
|
|
+ updateAdvanced(parameterBeans);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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() {
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|