jimu-decompiled/sources/com/ubt/jimu/controller/manager/CommandManager.java
2025-05-13 19:24:51 +02:00

402 lines
13 KiB
Java

package com.ubt.jimu.controller.manager;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.SystemClock;
import com.ubt.jimu.base.EngineManager;
import com.ubt.jimu.base.cache.Cache;
import com.ubt.jimu.base.data.CtrlMotionType;
import com.ubt.jimu.base.data.Engine;
import com.ubt.jimu.base.data.Servo;
import com.ubt.jimu.base.data.ServoMode;
import com.ubt.jimu.controller.data.action.ActionSequence;
import com.ubt.jimu.controller.data.action.MotorData;
import com.ubt.jimu.controller.data.action.TurnData;
import com.ubt.jimu.controller.data.command.ActionSequenceTask;
import com.ubt.jimu.controller.data.command.MotorCommand;
import com.ubt.jimu.controller.data.command.MoveCommand;
import com.ubt.jimu.controller.data.command.MoveTask;
import com.ubt.jimu.controller.data.command.TurnCommand;
import com.ubt.jimu.utils.LogUtils;
import com.ubtech.utils.XLog;
import com.ubtrobot.jimu.exception.EngineProtectException;
import com.ubtrobot.jimu.robotapi.BoardInfo;
import com.ubtrobot.jimu.robotapi.JimuException;
import com.ubtrobot.jimu.robotapi.JimuManager;
import com.ubtrobot.jimu.robotapi.MotorException;
import com.ubtrobot.jimu.robotapi.PeripheralType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.greenrobot.eventbus.EventBus;
/* loaded from: classes.dex */
public class CommandManager implements ICommandExecutor {
private static CommandManager i;
private JimuManager a;
private Handler b;
private Handler c;
private ActionSequenceTask d;
private ActionSequenceTask e;
private HandlerThread f;
private HandlerThread g;
private EngineManager h;
private CommandManager(Context context) {
context.getApplicationContext();
}
public static synchronized CommandManager a(Context context) {
CommandManager commandManager;
synchronized (CommandManager.class) {
if (i == null) {
i = new CommandManager(context);
}
commandManager = i;
}
return commandManager;
}
private void b() {
ActionSequenceTask actionSequenceTask = this.e;
if (actionSequenceTask != null) {
actionSequenceTask.cancel();
}
ActionSequenceTask actionSequenceTask2 = this.d;
if (actionSequenceTask2 != null) {
actionSequenceTask2.cancel();
}
}
private void d() {
Handler handler = this.c;
if (handler != null) {
handler.removeCallbacksAndMessages("move");
}
}
private void e(List<Servo> list) {
if (list == null || list.size() <= 0) {
return;
}
HashMap hashMap = new HashMap(list.size());
Iterator<Servo> it = list.iterator();
while (it.hasNext()) {
hashMap.put(Integer.valueOf(it.next().getId()), new TurnData(0, 0));
}
a((MoveCommand) new TurnCommand(hashMap), true);
}
private boolean f() {
BoardInfo boardInfo = Cache.getInstance().getBoardInfo();
return boardInfo != null && boardInfo.e() == 113;
}
private void g() {
if (this.a == null) {
return;
}
if (this.c == null) {
e();
}
this.c.post(new Runnable() { // from class: com.ubt.jimu.controller.manager.b
@Override // java.lang.Runnable
public final void run() {
CommandManager.this.a();
}
});
}
public void c(ActionSequence actionSequence) {
ActionSequenceTask actionSequenceTask = this.e;
if (actionSequenceTask != null) {
if (this.d != actionSequenceTask) {
this.d = actionSequenceTask;
XLog.a("woo", "setLoop false");
this.d.a(false);
this.e = null;
return;
}
return;
}
ActionSequenceTask actionSequenceTask2 = this.d;
if (actionSequenceTask2 == null || !actionSequenceTask2.a().equals(actionSequence) || this.d.g()) {
return;
}
XLog.a("woo", "stopAction ");
b(actionSequence);
this.e = null;
}
private void d(List<Engine> list) {
if (list == null || list.size() <= 0) {
return;
}
HashMap hashMap = new HashMap(list.size());
Iterator<Engine> it = list.iterator();
while (it.hasNext()) {
hashMap.put(Integer.valueOf(it.next().getId()), new MotorData(0, 0, 6553500));
}
a((MoveCommand) new MotorCommand(hashMap), true);
}
public void a(JimuManager jimuManager) {
this.a = jimuManager;
}
public void a(ActionSequence actionSequence) {
ActionSequenceTask actionSequenceTask = this.d;
if (actionSequenceTask == null || !actionSequenceTask.a().equals(actionSequence) || this.d.g()) {
b();
if (this.b == null) {
this.f = new HandlerThread("ActionThread");
this.f.start();
this.b = new Handler(this.f.getLooper());
}
LogUtils.c("playAction sequence" + actionSequence.a().get(0).c());
ActionSequenceTask actionSequenceTask2 = new ActionSequenceTask(actionSequence, this);
actionSequenceTask2.a(true);
this.b.removeCallbacksAndMessages(null);
this.b.post(actionSequenceTask2);
this.e = actionSequenceTask2;
}
}
public void b(ActionSequence actionSequence) {
if (actionSequence != null) {
b();
d(actionSequence.f());
e(actionSequence.i());
}
}
private void e() {
this.g = new HandlerThread("MoveThread");
this.g.start();
this.c = new Handler(this.g.getLooper());
}
public void b(List<Engine> list) {
b();
c();
d();
g();
d(c(list));
}
private void c() {
Handler handler = this.b;
if (handler != null) {
handler.removeCallbacksAndMessages(null);
}
}
@Override // com.ubt.jimu.controller.manager.ICommandExecutor
public void b(Map<Integer, TurnData> map) {
if (this.a == null) {
return;
}
a(map, ServoMode.SERVO_MODE_TURN);
if (map == null || map.size() <= 0) {
return;
}
Collections.sort(new ArrayList(map.entrySet()), new Comparator() { // from class: com.ubt.jimu.controller.manager.c
@Override // java.util.Comparator
public final int compare(Object obj, Object obj2) {
return CommandManager.a((Map.Entry) obj, (Map.Entry) obj2);
}
});
int[] iArr = new int[map.size()];
int[] iArr2 = new int[map.size()];
int i2 = 0;
int i3 = -1;
Iterator<Integer> it = map.keySet().iterator();
while (it.hasNext()) {
int intValue = it.next().intValue();
iArr[i2] = intValue;
iArr2[i2] = map.get(Integer.valueOf(intValue)).a();
if (i2 > 0) {
int i4 = i2 - 1;
if (iArr2[i2] != iArr2[i4]) {
a(iArr, iArr2[i4], i2, i3);
i3 = i4;
}
}
i2++;
}
a(iArr, iArr2[i2 - 1], i2, i3);
}
private List<Engine> c(List<? extends Engine> list) {
ArrayList arrayList = new ArrayList();
if (list != null && list.size() > 0) {
for (Engine engine : list) {
if (engine.getMotionType() == CtrlMotionType.motor) {
arrayList.add(engine);
}
}
}
return arrayList;
}
public void a(List<? extends Engine> list) {
b();
c();
d();
g();
d(c(list));
}
public void a(MoveCommand moveCommand, boolean z) {
if (this.c == null) {
e();
}
this.c.removeCallbacksAndMessages("move");
this.c.postAtTime(new MoveTask(moveCommand, this), z ? "stop" : "move", SystemClock.uptimeMillis());
}
public /* synthetic */ void a() {
try {
this.a.i();
} catch (JimuException e) {
e.printStackTrace();
XLog.b("Exception", e.toString());
}
}
@Override // com.ubt.jimu.controller.manager.ICommandExecutor
public int a(Map<Integer, Integer> map, int i2, int i3) {
if (this.a == null) {
return 0;
}
a(map, ServoMode.SERVO_MODE_ANGLE);
if (map != null && map.size() > 0) {
int[] iArr = new int[map.size()];
float[] fArr = new float[map.size()];
Iterator<Integer> it = map.keySet().iterator();
int i4 = 0;
while (it.hasNext()) {
iArr[i4] = it.next().intValue();
fArr[i4] = map.get(Integer.valueOf(r5)).intValue();
i4++;
}
XLog.a("woo", "run rotas ids.length: %d, servos.length: %d sportTime: %d, waitTime: %d", Integer.valueOf(iArr.length), Integer.valueOf(fArr.length), Integer.valueOf(i2), Integer.valueOf(i3));
try {
this.a.a(iArr, fArr, i2, i3);
} catch (MotorException e) {
e.printStackTrace();
XLog.b("Controller", "RequestException: %s", e.toString());
if (e.getCode() == -21 && f()) {
EventBus.b().b(a(a(iArr, e.getFailIds()), 2, "Servo is protected", PeripheralType.SERVO));
}
return e.getCode();
}
}
return 0;
}
private Integer[] a(int[] iArr, ArrayList<Integer> arrayList) {
if (arrayList != null && arrayList.size() > 0) {
Integer[] numArr = new Integer[arrayList.size()];
arrayList.toArray(numArr);
return numArr;
}
Integer[] numArr2 = new Integer[iArr.length];
for (int i2 = 0; i2 < iArr.length; i2++) {
numArr2[i2] = Integer.valueOf(iArr[i2]);
}
return numArr2;
}
private EngineProtectException a(Integer[] numArr, int i2, String str, int i3) {
HashMap hashMap = new HashMap(1);
ArrayList arrayList = new ArrayList(numArr.length);
arrayList.addAll(Arrays.asList(numArr));
hashMap.put(32, arrayList);
return new EngineProtectException(i2, str, i3, hashMap);
}
private void a(Map<Integer, ?> map, ServoMode servoMode) {
if (this.h == null || map == null || map.size() <= 0) {
return;
}
Integer[] numArr = new Integer[map.size()];
map.keySet().toArray(numArr);
for (Integer num : numArr) {
int intValue = num.intValue();
if (this.h.getServoMode(intValue) != servoMode) {
map.remove(Integer.valueOf(intValue));
}
}
}
static /* synthetic */ int a(Map.Entry entry, Map.Entry entry2) {
if (entry.getValue() == null && entry2.getValue() == null) {
return 0;
}
if (entry.getValue() == null) {
return -1;
}
if (entry2.getValue() == null) {
return 1;
}
return Integer.compare(((TurnData) entry.getValue()).a(), ((TurnData) entry2.getValue()).a());
}
private void a(int[] iArr, int i2, int i3, int i4) {
int i5 = (i3 - 1) - i4;
int[] iArr2 = new int[i5];
System.arraycopy(iArr, i4 + 1, iArr2, 0, i5);
XLog.a("woo", "runTurns ids.length: %s, speed: %d ", Integer.valueOf(iArr2.length), Integer.valueOf(i2));
try {
this.a.a(iArr2, i2);
} catch (MotorException e) {
e.printStackTrace();
XLog.b("Controller", "RequestException: %s", e.toString());
if (e.getCode() == -21 && f()) {
EventBus.b().b(a(a(iArr, e.getFailIds()), 2, "Servo is protected", PeripheralType.SERVO));
}
}
}
@Override // com.ubt.jimu.controller.manager.ICommandExecutor
public void a(Map<Integer, MotorData> map) {
if (this.a == null || map == null || map.size() <= 0) {
return;
}
int[] iArr = new int[map.size()];
int[] iArr2 = new int[map.size()];
int[] iArr3 = new int[map.size()];
Iterator<Integer> it = map.keySet().iterator();
int i2 = 0;
while (it.hasNext()) {
int intValue = it.next().intValue();
iArr[i2] = intValue;
MotorData motorData = map.get(Integer.valueOf(intValue));
iArr2[i2] = motorData.a();
iArr3[i2] = motorData.f();
i2++;
}
XLog.a("woo", "runMotors ids: %s, speeds: %s, durations: %s", Arrays.toString(iArr), Arrays.toString(iArr2), Arrays.toString(iArr3));
try {
this.a.a(iArr, iArr2, iArr3);
} catch (MotorException e) {
e.printStackTrace();
XLog.b("Controller", "RequestException: %s", e.toString());
if (e.getCode() == -21 && f()) {
EventBus.b().b(a(a(iArr, e.getFailIds()), 4, "Motor is protected", 10));
}
}
}
public void a(EngineManager engineManager) {
this.h = engineManager;
}
}