Advertisement
Guest User

This algorithm is supposed to detect biker's falls (Android)

a guest
May 10th, 2016
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.71 KB | None | 0 0
  1. package com.libertyrider.angel;
  2.  
  3. import android.hardware.Sensor;
  4. import android.util.Log;
  5. import db$k;
  6. import org.acra.ACRAConstants;
  7.  
  8. public class AlgoChute {
  9.     private static final int DT_MAX_SEUIL_ACC = 500;
  10.     private static final int DT_MAX_SEUIL_VIT = 20000;
  11.     private static final String TAG = "LibertyRider";
  12.     private double accSeuilActivity;
  13.     private Sensor accelerometer;
  14.     private boolean alarmStopAction;
  15.     boolean byPassInitState;
  16.     private double chocSeuil;
  17.     private int countState1;
  18.     private int countState2;
  19.     private int countState3;
  20.     private int countState4;
  21.     private int countTime;
  22.     private double currentAcc;
  23.     private int currentState;
  24.     private long currentTimeAcc;
  25.     private long currentTimeVit;
  26.     private int currentVit;
  27.     private boolean demo_active;
  28.     private boolean erreurUpdateAccEnable;
  29.     private boolean erreurUpdateAccFlag;
  30.     private boolean erreurUpdateVitEnable;
  31.     private boolean erreurUpdateVitFlag;
  32.     private boolean initAlgoTimeFlag;
  33.     private MyAlgoChuteListener listener;
  34.     private String msgDebug;
  35.     private int nbActivity;
  36.     private int nbChoc;
  37.     private int nbChocSeuil;
  38.     private int nbSeuilActivity;
  39.     private int nextState;
  40.     private double previousAcc;
  41.     private int previousState;
  42.     private long previousTimeAcc;
  43.     private long previousTimeVit;
  44.     private int previousVit;
  45.     private int seuilVitRunAlgo;
  46.     private int tempoActivityCheck;
  47.     private int tempoAlarme;
  48.     private int tempoChocCheck;
  49.     private long timeStartActivityCheck;
  50.     private long timeStartAlarme;
  51.     private long timeStartChocCheck;
  52.     private boolean vitEnable;
  53.  
  54.     public enum DetectionState {
  55.         INIT(-100),
  56.         ERROR(-1),
  57.         IDLE(0),
  58.         RUNNING(1),
  59.         SHOCKED_MOTIONLESS(2),
  60.         SHOCKED_INACTIVITY(3),
  61.         ALARM(4),
  62.         ACCIDENT(5),
  63.         ENDED(6);
  64.        
  65.         public final int code;
  66.  
  67.         private DetectionState(int i) {
  68.             this.code = i;
  69.         }
  70.  
  71.         public static String getName(int i) {
  72.             for (DetectionState detectionState : values()) {
  73.                 if (detectionState.code == i) {
  74.                     return detectionState.name();
  75.                 }
  76.             }
  77.             return "UNKNOWN";
  78.         }
  79.     }
  80.  
  81.     public interface MyAlgoChuteListener {
  82.         void onCountDown(int i);
  83.  
  84.         void onLogConsolMsg(String str);
  85.  
  86.         void onStateAlgoChange(long j, int i, String str);
  87.     }
  88.  
  89.     public AlgoChute() {
  90.         this.byPassInitState = false;
  91.         this.currentTimeAcc = 0;
  92.         this.previousTimeAcc = 0;
  93.         this.currentTimeVit = 0;
  94.         this.previousTimeVit = 0;
  95.         this.currentAcc = 0.0d;
  96.         this.previousAcc = 0.0d;
  97.         this.currentVit = 0;
  98.         this.previousVit = 0;
  99.         this.nextState = DetectionState.INIT.code;
  100.         this.currentState = DetectionState.INIT.code;
  101.         this.previousState = DetectionState.ERROR.code;
  102.         this.chocSeuil = 0.0d;
  103.         this.tempoChocCheck = 0;
  104.         this.nbChoc = 0;
  105.         this.nbChocSeuil = 0;
  106.         this.timeStartChocCheck = 0;
  107.         this.accSeuilActivity = 0.0d;
  108.         this.tempoActivityCheck = 0;
  109.         this.nbSeuilActivity = 0;
  110.         this.nbActivity = 0;
  111.         this.timeStartActivityCheck = 0;
  112.         this.timeStartAlarme = 0;
  113.         this.tempoAlarme = 0;
  114.         this.alarmStopAction = false;
  115.         this.seuilVitRunAlgo = 0;
  116.         this.vitEnable = true;
  117.         this.initAlgoTimeFlag = false;
  118.         this.countState2 = 0;
  119.         this.countState1 = 0;
  120.         this.countState3 = 0;
  121.         this.countState4 = 0;
  122.         this.accelerometer = null;
  123.         this.countTime = 1;
  124.         this.demo_active = false;
  125.         this.msgDebug = ACRAConstants.DEFAULT_STRING_VALUE;
  126.         this.listener = null;
  127.         algoInitDefaultParameter();
  128.     }
  129.  
  130.     public AlgoChute(Sensor sensor) {
  131.         this.byPassInitState = false;
  132.         this.currentTimeAcc = 0;
  133.         this.previousTimeAcc = 0;
  134.         this.currentTimeVit = 0;
  135.         this.previousTimeVit = 0;
  136.         this.currentAcc = 0.0d;
  137.         this.previousAcc = 0.0d;
  138.         this.currentVit = 0;
  139.         this.previousVit = 0;
  140.         this.nextState = DetectionState.INIT.code;
  141.         this.currentState = DetectionState.INIT.code;
  142.         this.previousState = DetectionState.ERROR.code;
  143.         this.chocSeuil = 0.0d;
  144.         this.tempoChocCheck = 0;
  145.         this.nbChoc = 0;
  146.         this.nbChocSeuil = 0;
  147.         this.timeStartChocCheck = 0;
  148.         this.accSeuilActivity = 0.0d;
  149.         this.tempoActivityCheck = 0;
  150.         this.nbSeuilActivity = 0;
  151.         this.nbActivity = 0;
  152.         this.timeStartActivityCheck = 0;
  153.         this.timeStartAlarme = 0;
  154.         this.tempoAlarme = 0;
  155.         this.alarmStopAction = false;
  156.         this.seuilVitRunAlgo = 0;
  157.         this.vitEnable = true;
  158.         this.initAlgoTimeFlag = false;
  159.         this.countState2 = 0;
  160.         this.countState1 = 0;
  161.         this.countState3 = 0;
  162.         this.countState4 = 0;
  163.         this.accelerometer = null;
  164.         this.countTime = 1;
  165.         this.demo_active = false;
  166.         this.msgDebug = ACRAConstants.DEFAULT_STRING_VALUE;
  167.         this.listener = null;
  168.         this.accelerometer = sensor;
  169.         algoInitDefaultParameter();
  170.     }
  171.  
  172.     private void algoInitDefaultParameter() {
  173.         boolean z;
  174.         if (this.demo_active) {
  175.             this.chocSeuil = 5.0d;
  176.             this.tempoChocCheck = 700;
  177.             this.nbChocSeuil = 3;
  178.             this.accSeuilActivity = 11.0d;
  179.             this.tempoActivityCheck = 1000;
  180.             this.nbSeuilActivity = 10;
  181.         } else {
  182.             this.chocSeuil = 20.0d;
  183.             this.tempoChocCheck = ACRAConstants.TOAST_WAIT_DURATION;
  184.             this.nbChocSeuil = 3;
  185.             this.accSeuilActivity = 11.0d;
  186.             this.tempoActivityCheck = ACRAConstants.DEFAULT_CONNECTION_TIMEOUT;
  187.             this.nbSeuilActivity = 10;
  188.         }
  189.         this.tempoAlarme = 30000;
  190.         if (this.demo_active) {
  191.             z = false;
  192.         } else {
  193.             z = true;
  194.         }
  195.         this.vitEnable = z;
  196.         this.seuilVitRunAlgo = 30;
  197.         this.erreurUpdateAccEnable = true;
  198.         this.erreurUpdateAccFlag = false;
  199.         this.erreurUpdateVitEnable = true;
  200.         this.erreurUpdateVitFlag = false;
  201.         Log.v(TAG, "Algo state changed=" + DetectionState.getName(this.currentState));
  202.     }
  203.  
  204.     public void update(long j, double d, long j2, int i) {
  205.         if (j != this.currentTimeAcc) {
  206.             this.previousTimeAcc = this.currentTimeAcc;
  207.             this.currentTimeAcc = j;
  208.             this.previousAcc = this.currentAcc;
  209.             this.currentAcc = d;
  210.         }
  211.         if (j2 != this.currentTimeVit) {
  212.             this.previousTimeVit = this.currentTimeVit;
  213.             this.currentTimeVit = j2;
  214.             this.previousVit = this.currentVit;
  215.             this.currentVit = i;
  216.         }
  217.         if (((this.currentState >= DetectionState.IDLE.code ? 1 : 0) & (!this.byPassInitState ? 1 : 0)) != 0) {
  218.             if (this.currentTimeAcc - this.previousTimeAcc > 500) {
  219.                 this.erreurUpdateAccFlag = true;
  220.             } else {
  221.                 this.erreurUpdateAccFlag = false;
  222.             }
  223.             if (this.currentTimeVit - this.previousTimeVit > 20000) {
  224.                 this.erreurUpdateVitFlag = true;
  225.             } else {
  226.                 this.erreurUpdateVitFlag = false;
  227.             }
  228.         }
  229.         stateMachine();
  230.     }
  231.  
  232.     private void stateMachine() {
  233.         boolean z;
  234.         int i = 1;
  235.         boolean z2 = this.currentVit > this.seuilVitRunAlgo || !this.vitEnable;
  236.         if (this.erreurUpdateAccFlag && this.erreurUpdateAccEnable) {
  237.             z = false;
  238.         } else {
  239.             z = true;
  240.         }
  241.         this.previousState = this.currentState;
  242.         this.currentState = this.nextState;
  243.         if (this.currentState != this.previousState) {
  244.             Log.v(TAG, "Algo state changed=" + DetectionState.getName(this.currentState));
  245.             this.listener.onLogConsolMsg("State=" + DetectionState.getName(this.currentState));
  246.             this.listener.onStateAlgoChange(this.currentTimeAcc, this.currentState, this.msgDebug);
  247.             this.msgDebug = ACRAConstants.DEFAULT_STRING_VALUE;
  248.         }
  249.         switch (this.currentState) {
  250.             case -100:
  251.                 int i2;
  252.                 this.nextState = DetectionState.INIT.code;
  253.                 if (this.currentTimeAcc != 0) {
  254.                     i2 = 1;
  255.                 } else {
  256.                     i2 = 0;
  257.                 }
  258.                 if (this.currentTimeVit == 0) {
  259.                     i = 0;
  260.                 }
  261.                 if (((i2 & i) | this.byPassInitState) != 0) {
  262.                     this.nextState = DetectionState.IDLE.code;
  263.                 }
  264.             case ACRAConstants.DEFAULT_SHARED_PREFERENCES_MODE /*0*/:
  265.                 this.nextState = DetectionState.IDLE.code;
  266.                 if (z2) {
  267.                     this.nextState = DetectionState.RUNNING.code;
  268.                 }
  269.                 if (!z) {
  270.                     this.nextState = DetectionState.ERROR.code;
  271.                 }
  272.             case db$k.View_android_focusable /*1*/:
  273.                 this.nextState = DetectionState.RUNNING.code;
  274.                 if (this.currentState != this.previousState) {
  275.                     this.countState1++;
  276.                 }
  277.                 if (this.currentAcc > this.chocSeuil && z2 && z) {
  278.                     this.nextState = 2;
  279.                     this.msgDebug = Integer.toString((int) this.currentAcc);
  280.                 }
  281.                 if (!z2) {
  282.                     this.nextState = DetectionState.IDLE.code;
  283.                 }
  284.                 if (!z) {
  285.                     this.nextState = DetectionState.ERROR.code;
  286.                 }
  287.             case db$k.View_paddingStart /*2*/:
  288.                 this.nextState = 2;
  289.                 if (this.currentState != this.previousState) {
  290.                     this.timeStartChocCheck = this.currentTimeAcc;
  291.                     this.nbChoc = 1;
  292.                     Log.v(TAG, "Process choc start");
  293.                     this.countState2++;
  294.                 }
  295.                 if (this.currentTimeAcc - this.timeStartChocCheck >= ((long) this.tempoChocCheck)) {
  296.                     Log.v(TAG, "temp=" + Long.toString(this.currentTimeAcc - this.timeStartChocCheck));
  297.                     Log.v(TAG, "nbChoc=" + this.nbChoc);
  298.                     this.listener.onLogConsolMsg("nbChoc=" + this.nbChoc);
  299.                     if (this.nbChoc > this.nbChocSeuil) {
  300.                         this.nextState = 3;
  301.                         this.msgDebug = "nbChoc=" + Integer.toString(this.nbChoc);
  302.                     } else {
  303.                         this.nextState = DetectionState.RUNNING.code;
  304.                         this.msgDebug = "nbChoc=" + Integer.toString(this.nbChoc);
  305.                     }
  306.                     this.nbChoc = 0;
  307.                 } else if (this.currentAcc > this.chocSeuil) {
  308.                     this.nbChoc++;
  309.                 }
  310.             case db$k.View_paddingEnd /*3*/:
  311.                 this.nextState = 3;
  312.                 if (this.currentState != this.previousState) {
  313.                     this.timeStartActivityCheck = this.currentTimeAcc;
  314.                     Log.v(TAG, "Process activity start");
  315.                     this.countState3++;
  316.                 }
  317.                 if (this.currentTimeAcc - this.timeStartActivityCheck >= ((long) this.tempoActivityCheck)) {
  318.                     Log.v(TAG, "temp=" + Long.toString(this.currentTimeAcc - this.timeStartActivityCheck));
  319.                     Log.v(TAG, "nbActi=" + this.nbActivity);
  320.                     this.listener.onLogConsolMsg("nbActi=" + this.nbActivity);
  321.                     if (this.nbActivity < this.nbSeuilActivity) {
  322.                         this.nextState = DetectionState.ALARM.code;
  323.                         this.msgDebug = "nbActi=" + Integer.toString(this.nbActivity);
  324.                     } else {
  325.                         this.nextState = DetectionState.RUNNING.code;
  326.                         this.msgDebug = "nbActi=" + Integer.toString(this.nbActivity);
  327.                     }
  328.                     this.nbActivity = 0;
  329.                 } else if (this.currentAcc > this.accSeuilActivity) {
  330.                     this.nbActivity++;
  331.                 }
  332.             case db$k.View_theme /*4*/:
  333.                 this.nextState = DetectionState.ALARM.code;
  334.                 if (this.currentState != this.previousState) {
  335.                     Log.v(TAG, "Alarme Start");
  336.                     this.timeStartAlarme = this.currentTimeAcc;
  337.                     this.countState4++;
  338.                     this.countTime = 1;
  339.                     this.listener.onCountDown(this.tempoAlarme / 1000);
  340.                 }
  341.                 if (this.alarmStopAction) {
  342.                     Log.v(TAG, "Alarme Stoped");
  343.                     this.nextState = DetectionState.ENDED.code;
  344.                     this.alarmStopAction = false;
  345.                     return;
  346.                 }
  347.                 long j = this.currentTimeAcc - this.timeStartAlarme;
  348.                 if (j >= ((long) this.tempoAlarme)) {
  349.                     Log.v(TAG, "Alarme Finished");
  350.                     Log.v(TAG, "temp0=" + Long.toString(this.currentTimeAcc - this.timeStartAlarme));
  351.                     this.nextState = DetectionState.ACCIDENT.code;
  352.                 } else if (j > ((long) (this.countTime * 1000))) {
  353.                     this.countTime++;
  354.                     this.listener.onCountDown(((this.tempoAlarme / 1000) - this.countTime) + 1);
  355.                 }
  356.             case ACRAConstants.MAX_SEND_REPORTS /*5*/:
  357.                 this.nextState = DetectionState.ACCIDENT.code;
  358.                 if (this.currentState != this.previousState) {
  359.                     Log.v(TAG, "Chute d\u00e9tect\u00e9e");
  360.                     this.listener.onLogConsolMsg("chute d\u00e9tect\u00e9e");
  361.                 }
  362.                 if (this.alarmStopAction) {
  363.                     this.nextState = DetectionState.ENDED.code;
  364.                 }
  365.             case db$k.Toolbar_contentInsetEnd /*6*/:
  366.                 this.nextState = DetectionState.ENDED.code;
  367.                 if (this.currentState != this.previousState) {
  368.                     Log.v(TAG, "User stop alarme");
  369.                     this.listener.onLogConsolMsg("User stop alarme");
  370.                 }
  371.             default:
  372.         }
  373.     }
  374.  
  375.     public void setDemo(boolean z) {
  376.         this.demo_active = z;
  377.     }
  378.  
  379.     public void setVitCondi(int i) {
  380.         if (i == 1) {
  381.             this.vitEnable = true;
  382.         } else {
  383.             this.vitEnable = false;
  384.         }
  385.     }
  386.  
  387.     public void setByPassInitSate(boolean z) {
  388.         this.byPassInitState = z;
  389.     }
  390.  
  391.     public void setVitErreurCondi(int i) {
  392.         if (i == 1) {
  393.             this.erreurUpdateVitEnable = true;
  394.         } else {
  395.             this.erreurUpdateVitEnable = false;
  396.         }
  397.     }
  398.  
  399.     public void setAccErreurCondi(int i) {
  400.         if (i == 1) {
  401.             this.erreurUpdateAccEnable = true;
  402.         } else {
  403.             this.erreurUpdateAccEnable = false;
  404.         }
  405.     }
  406.  
  407.     public void setByPassInitState(int i) {
  408.         if (i == 1) {
  409.             this.byPassInitState = true;
  410.         } else {
  411.             this.byPassInitState = false;
  412.         }
  413.     }
  414.  
  415.     public void setAlgoInit() {
  416.         algoInitDefaultParameter();
  417.         this.previousState = DetectionState.IDLE.code;
  418.         this.currentState = DetectionState.IDLE.code;
  419.         this.nextState = DetectionState.IDLE.code;
  420.         this.previousTimeAcc = 0;
  421.         this.previousTimeVit = 0;
  422.         this.initAlgoTimeFlag = false;
  423.     }
  424.  
  425.     public void setAlarmeStopAction() {
  426.         this.alarmStopAction = true;
  427.     }
  428.  
  429.     public int getAlgoChuteState() {
  430.         return this.currentState;
  431.     }
  432.  
  433.     public String getStatSessionString() {
  434.         return "s1:" + this.countState1 + " s2:" + this.countState2 + " s3:" + this.countState3 + " s4:" + this.countState4;
  435.     }
  436.  
  437.     public int[] getStatSessionTab() {
  438.         return new int[]{this.countState1, this.countState2, this.countState3, this.countState4};
  439.     }
  440.  
  441.     public String affAlgoParameter() {
  442.         String str = ACRAConstants.DEFAULT_STRING_VALUE;
  443.         return "cS:" + this.chocSeuil + " cT:" + this.tempoChocCheck + " cN:" + this.nbChocSeuil + " aS:" + this.accSeuilActivity + " aT:" + this.tempoActivityCheck + " aN:" + this.nbSeuilActivity + " eA:" + this.erreurUpdateAccEnable + " eV:" + this.erreurUpdateVitEnable + " vE" + this.vitEnable;
  444.     }
  445.  
  446.     public void setCustomObjectListener(MyAlgoChuteListener myAlgoChuteListener) {
  447.         this.listener = myAlgoChuteListener;
  448.     }
  449. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement