jimu-decompiled/sources/com/ubt/jimu/base/EngineManager.java
2025-05-13 19:24:51 +02:00

201 lines
7.8 KiB
Java

package com.ubt.jimu.base;
import android.content.Context;
import android.text.TextUtils;
import com.ubt.jimu.base.data.Engine;
import com.ubt.jimu.base.data.Motor;
import com.ubt.jimu.base.data.Servo;
import com.ubt.jimu.base.data.ServoMode;
import com.ubt.jimu.base.db.diy.DiyDBModel;
import com.ubt.jimu.base.db.diy.DiyHelper;
import com.ubt.jimu.base.entities.RobotLite;
import com.ubt.jimu.base.util.PathHelper;
import com.ubt.jimu.connect.model.ModelMatchXmlFileManager;
import com.ubt.jimu.connect.model.PeripheralConnections;
import com.ubt.jimu.transport3.UnityFileOperator;
import com.ubtech.utils.XLog;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
/* loaded from: classes.dex */
public class EngineManager {
private Context mContext;
private PeripheralConnections mPeripheralConn;
private RobotLite mRobotLite;
private String mUserId;
public EngineManager(Context context, RobotLite robotLite) {
this.mContext = context.getApplicationContext();
this.mRobotLite = robotLite;
}
private ModelMatchXmlFileManager createMatchFileManager() {
ModelMatchXmlFileManager modelMatchXmlFileManager = new ModelMatchXmlFileManager();
modelMatchXmlFileManager.a(PathHelper.getDataRootDir(this.mContext, this.mRobotLite, false) + File.separator + this.mRobotLite.getModelId() + ".xml");
return modelMatchXmlFileManager;
}
private List<Engine> getEnginesFromMatchFile() {
ModelMatchXmlFileManager createMatchFileManager = createMatchFileManager();
ArrayList arrayList = new ArrayList();
List<Motor> motorsFromMatchFile = getMotorsFromMatchFile(createMatchFileManager);
if (motorsFromMatchFile != null && motorsFromMatchFile.size() > 0) {
arrayList.addAll(motorsFromMatchFile);
}
List<Servo> servosFromMatchFile = getServosFromMatchFile(createMatchFileManager);
if (servosFromMatchFile != null && servosFromMatchFile.size() > 0) {
arrayList.addAll(servosFromMatchFile);
}
return arrayList;
}
private List<Engine> getEnginesFromPeripheralFile() {
ArrayList arrayList = new ArrayList();
List<Motor> motorsFromPeripheralFile = getMotorsFromPeripheralFile();
if (motorsFromPeripheralFile != null && motorsFromPeripheralFile.size() > 0) {
arrayList.addAll(motorsFromPeripheralFile);
}
List<Servo> servosFromPeripheralFile = getServosFromPeripheralFile();
if (servosFromPeripheralFile != null && servosFromPeripheralFile.size() > 0) {
arrayList.addAll(servosFromPeripheralFile);
}
return arrayList;
}
private List<Motor> getMotorsFromMatchFile(ModelMatchXmlFileManager modelMatchXmlFileManager) {
List<Integer> a = modelMatchXmlFileManager.a();
if (a == null || a.size() <= 0) {
return null;
}
ArrayList arrayList = new ArrayList(a.size());
Iterator<Integer> it = a.iterator();
while (it.hasNext()) {
arrayList.add(new Motor(it.next().intValue()));
}
Collections.sort(arrayList);
return arrayList;
}
private List<Motor> getMotorsFromPeripheralFile() {
List<Integer> a = this.mPeripheralConn.a(10);
if (a == null || a.size() <= 0) {
return null;
}
ArrayList arrayList = new ArrayList(a.size());
Iterator<Integer> it = a.iterator();
while (it.hasNext()) {
arrayList.add(new Motor(it.next().intValue()));
}
Collections.sort(arrayList);
return arrayList;
}
private List<Servo> getServosFromMatchFile(ModelMatchXmlFileManager modelMatchXmlFileManager) {
List<Integer> b = modelMatchXmlFileManager.b();
if (b == null || b.size() <= 0) {
return null;
}
ArrayList arrayList = new ArrayList(b.size());
Iterator<Integer> it = b.iterator();
while (it.hasNext()) {
arrayList.add(new Servo(it.next().intValue(), ServoMode.SERVO_MODE_ANGLE));
}
Collections.sort(arrayList);
return arrayList;
}
private List<Servo> getServosFromPeripheralFile() {
HashMap<Integer, ServoMode> c = this.mPeripheralConn.c();
if (c == null || c.size() <= 0) {
return null;
}
ArrayList arrayList = new ArrayList(c.size());
Iterator<Integer> it = c.keySet().iterator();
while (it.hasNext()) {
int intValue = it.next().intValue();
arrayList.add(new Servo(intValue, c.get(Integer.valueOf(intValue))));
}
Collections.sort(arrayList);
return arrayList;
}
public void createPeripheralConn(String str) {
if (this.mPeripheralConn == null) {
this.mPeripheralConn = PeripheralConnections.a(this.mRobotLite.getModelId(), str, !this.mRobotLite.isOfficial());
} else if (!str.equals(this.mUserId)) {
this.mPeripheralConn = PeripheralConnections.a(this.mRobotLite.getModelId(), str, !this.mRobotLite.isOfficial());
}
this.mUserId = str;
}
public List<Engine> getEngineList(String str) {
if (this.mRobotLite == null || TextUtils.isEmpty(str)) {
return null;
}
createPeripheralConn(str);
if (this.mPeripheralConn != null) {
return getEnginesFromPeripheralFile();
}
XLog.b("woo", "Robot %s has no servos.txt", this.mRobotLite.getModelId());
return getEnginesFromMatchFile();
}
public List<Motor> getMotorList(String str) {
if (this.mRobotLite == null || TextUtils.isEmpty(str)) {
return null;
}
createPeripheralConn(str);
if (this.mPeripheralConn != null) {
return getMotorsFromPeripheralFile();
}
XLog.b("woo", "Robot %s has no servos.txt", this.mRobotLite.getModelId());
return getMotorsFromMatchFile(createMatchFileManager());
}
public List<Servo> getServoList(String str) {
if (this.mRobotLite == null || TextUtils.isEmpty(str)) {
return null;
}
createPeripheralConn(str);
if (this.mPeripheralConn != null) {
return getServosFromPeripheralFile();
}
XLog.b("woo", "Robot %s has no servos.txt", this.mRobotLite.getModelId());
return getServosFromMatchFile(createMatchFileManager());
}
public ServoMode getServoMode(int i) {
PeripheralConnections peripheralConnections = this.mPeripheralConn;
return (peripheralConnections == null || i <= 0) ? ServoMode.SERVO_MODE_ANGLE : peripheralConnections.a(Integer.valueOf(i));
}
public void reloadPeripheralConn() {
this.mPeripheralConn = PeripheralConnections.a(this.mRobotLite.getModelId(), this.mUserId, !this.mRobotLite.isOfficial());
}
public void save() {
DiyDBModel queryForUUid;
if (this.mPeripheralConn != null) {
String dataRootDir = PathHelper.getDataRootDir(this.mContext, this.mRobotLite, false);
this.mPeripheralConn.b(dataRootDir);
String d = PeripheralConnections.d(dataRootDir);
if (this.mRobotLite.isOfficial() || (queryForUUid = DiyHelper.getInstance().queryForUUid(this.mRobotLite.getModelId())) == null) {
return;
}
new UnityFileOperator(queryForUUid.getModelId().intValue(), queryForUUid.getCustomModelId(), 1, d, 2).operateFile();
}
}
public void updateServoMode(Servo servo) {
PeripheralConnections peripheralConnections = this.mPeripheralConn;
if (peripheralConnections == null || servo == null) {
return;
}
peripheralConnections.a(Integer.valueOf(servo.getId()), servo.getModeType());
}
}