|
@@ -0,0 +1,181 @@
|
|
|
+package com.huimv.eartag2.manage2.utils;
|
|
|
+
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+public class Action {
|
|
|
+ private final double body, tMin, tMax;
|
|
|
+ private final int aMin, aMax;
|
|
|
+ private final List<Result> result = new ArrayList<>();
|
|
|
+
|
|
|
+ private static int kind(int id) {
|
|
|
+ switch (id) {
|
|
|
+ case 1:
|
|
|
+ return 1;
|
|
|
+ case 3:
|
|
|
+ return 2;
|
|
|
+ case 8:
|
|
|
+ case 10:
|
|
|
+ case 12:
|
|
|
+ return 3;
|
|
|
+ default:
|
|
|
+ return 4;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void handle(Data data, int id) throws Exception {
|
|
|
+ id = kind(id);
|
|
|
+ Result now = new Result(id, data.getAddTime(), data.getEnvTemp());
|
|
|
+ result.add(now);
|
|
|
+ }
|
|
|
+
|
|
|
+ private int nni(int idx, int id) {
|
|
|
+ for (; idx < this.result.size(); ++idx) {
|
|
|
+ if (this.result.get(idx).getId() != id) return idx;
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ private int ln4(int idx) {
|
|
|
+ for (; idx > -1; --idx) {
|
|
|
+ if (this.result.get(idx).getId() != 4) return idx;
|
|
|
+ }
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ private int ex(int idx, int cnt) throws Exception {
|
|
|
+ int s = Math.max(0, idx - cnt), e = Math.min(this.result.size(), idx + cnt), t = this.result.get(idx).getId();
|
|
|
+ for (idx = s; idx < e; ++idx) update(idx, t);
|
|
|
+ return e;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void update(int idx, int id) throws Exception {
|
|
|
+ Result tmp = this.result.get(idx);
|
|
|
+ tmp.setId(id);
|
|
|
+ this.result.set(idx, tmp);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Action(double body, double tMin, double tMax, int aMin, int aMax) {
|
|
|
+ this.body = body;
|
|
|
+ this.tMin = tMin;
|
|
|
+ this.tMax = tMax;
|
|
|
+ this.aMin = aMin;
|
|
|
+ this.aMax = aMax;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Result> analyze(List<Data> data) throws Exception {
|
|
|
+ for (Data itr : data) {
|
|
|
+ if (Math.abs(itr.getEarTemp1() - itr.getEarTemp2()) < tMin) {
|
|
|
+ if (Math.abs(itr.getEarTemp1() - body) < tMin) {
|
|
|
+ if (itr.getAct1() < aMax) handle(itr, 1);
|
|
|
+ else handle(itr, 2);
|
|
|
+ } else if (Math.abs(itr.getEarTemp1() - itr.getEnvTemp1()) < tMin) {
|
|
|
+ if (itr.getAct1() < aMax) handle(itr, 3);
|
|
|
+ else handle(itr, 4);
|
|
|
+ } else {
|
|
|
+ if (itr.getAct1() < aMax) handle(itr, 5);
|
|
|
+ else handle(itr, 6);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (Math.abs(itr.getEarTemp1() - body) < tMax) {
|
|
|
+ if (itr.getAct1() < aMin) handle(itr, 7);
|
|
|
+ else handle(itr, 8);
|
|
|
+ } else if (Math.abs(itr.getEarTemp1() - itr.getEnvTemp1()) < tMax) {
|
|
|
+ if (itr.getAct1() < aMin) handle(itr, 9);
|
|
|
+ else handle(itr, 10);
|
|
|
+ } else {
|
|
|
+ if (itr.getAct1() < aMin) handle(itr, 11);
|
|
|
+ else handle(itr, 12);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Result> filter(int sub, int exp) throws Exception {
|
|
|
+ for (int idx = 0; idx < this.result.size(); ) {
|
|
|
+ int cid = this.result.get(idx).getId();
|
|
|
+ if (cid == 4) {
|
|
|
+ int nid = nni(idx, 4);
|
|
|
+ if (nid == -1) break;
|
|
|
+ if (nid - idx <= sub) {
|
|
|
+ int pid = ln4(idx), tar;
|
|
|
+ if (pid == -1 || (nid - idx) < (idx - pid)) tar = this.result.get(nid).getId();
|
|
|
+ else tar = this.result.get(pid).getId();
|
|
|
+ for (int i = idx; i < nid; ++i) update(i, tar);
|
|
|
+ }
|
|
|
+ idx = nid;
|
|
|
+ } else if (cid == 2 || cid == 3) {
|
|
|
+ idx = ex(idx, exp);
|
|
|
+ } else {
|
|
|
+ idx = nni(idx, 1);
|
|
|
+ if (idx == -1) break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return this.result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, List<Double>> calc() {
|
|
|
+ Map<String, List<Double>> res = new HashMap<>();
|
|
|
+ if (result.size() == 0) return res;
|
|
|
+
|
|
|
+ Result result0 = this.result.get(0);
|
|
|
+ int last = result0.getTimestamp();
|
|
|
+ double temp0 = Double.parseDouble(result0.getEnvTemp());
|
|
|
+ Double maxTemp= temp0;
|
|
|
+ Double minTemp= temp0;
|
|
|
+ Double avgTemp= temp0;
|
|
|
+
|
|
|
+ for (int i = 1; i < this.result.size(); ++i) {
|
|
|
+ String key = this.result.get(i).getTime().substring(0, 10);
|
|
|
+ double temp = Double.parseDouble( this.result.get(i).getEnvTemp());
|
|
|
+
|
|
|
+ if (!res.containsKey(key)) {
|
|
|
+ ArrayList<Double> tmp = new ArrayList<>(7);
|
|
|
+ tmp.add(0.0);
|
|
|
+ tmp.add(0.0);
|
|
|
+ tmp.add(0.0);
|
|
|
+ tmp.add(0.0);
|
|
|
+
|
|
|
+ tmp.add(0.0);
|
|
|
+ tmp.add(0.0);
|
|
|
+ tmp.add(0.0);
|
|
|
+ res.put(key, tmp);
|
|
|
+ }
|
|
|
+ List<Double> list = res.get(key);
|
|
|
+ int idx = this.result.get(i).getId() - 1;
|
|
|
+ list.set(idx, list.get(idx) + (this.result.get(i).getTimestamp() - last));
|
|
|
+ avgTemp += temp;
|
|
|
+ maxTemp = maxTemp <temp ? temp :maxTemp;
|
|
|
+ minTemp = minTemp > temp ? temp :minTemp;
|
|
|
+ list.set(4,maxTemp);
|
|
|
+ list.set(5,minTemp );
|
|
|
+ list.set(6,avgTemp/(i+1));
|
|
|
+
|
|
|
+ res.replace(key, list);
|
|
|
+ last = this.result.get(i).getTimestamp();
|
|
|
+ }
|
|
|
+
|
|
|
+ for (String key : res.keySet()) {
|
|
|
+ List<Double> list = res.get(key);
|
|
|
+ double sum = list.get(0) + list.get(1) + list.get(2) + list.get(3);
|
|
|
+ list.set(0, new BigDecimal(list.get(0) * 100 / sum).setScale(2, RoundingMode.UP).doubleValue());
|
|
|
+ list.set(1, new BigDecimal(list.get(1) * 100 / sum).setScale(2, RoundingMode.UP).doubleValue());
|
|
|
+ list.set(2, new BigDecimal(list.get(2) * 100 / sum).setScale(2, RoundingMode.UP).doubleValue());
|
|
|
+ list.set(3, new BigDecimal(list.get(3) * 100 / sum).setScale(2, RoundingMode.UP).doubleValue());
|
|
|
+ list.set(4, BigDecimal.valueOf(list.get(4)).setScale(2, RoundingMode.UP).doubleValue());
|
|
|
+ list.set(5, BigDecimal.valueOf(list.get(5)).setScale(2, RoundingMode.UP).doubleValue());
|
|
|
+ list.set(6, BigDecimal.valueOf(list.get(6)).setScale(2, RoundingMode.UP).doubleValue());
|
|
|
+ res.replace(key, list);
|
|
|
+ }
|
|
|
+
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|