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 getEnginesFromMatchFile() { ModelMatchXmlFileManager createMatchFileManager = createMatchFileManager(); ArrayList arrayList = new ArrayList(); List motorsFromMatchFile = getMotorsFromMatchFile(createMatchFileManager); if (motorsFromMatchFile != null && motorsFromMatchFile.size() > 0) { arrayList.addAll(motorsFromMatchFile); } List servosFromMatchFile = getServosFromMatchFile(createMatchFileManager); if (servosFromMatchFile != null && servosFromMatchFile.size() > 0) { arrayList.addAll(servosFromMatchFile); } return arrayList; } private List getEnginesFromPeripheralFile() { ArrayList arrayList = new ArrayList(); List motorsFromPeripheralFile = getMotorsFromPeripheralFile(); if (motorsFromPeripheralFile != null && motorsFromPeripheralFile.size() > 0) { arrayList.addAll(motorsFromPeripheralFile); } List servosFromPeripheralFile = getServosFromPeripheralFile(); if (servosFromPeripheralFile != null && servosFromPeripheralFile.size() > 0) { arrayList.addAll(servosFromPeripheralFile); } return arrayList; } private List getMotorsFromMatchFile(ModelMatchXmlFileManager modelMatchXmlFileManager) { List a = modelMatchXmlFileManager.a(); if (a == null || a.size() <= 0) { return null; } ArrayList arrayList = new ArrayList(a.size()); Iterator it = a.iterator(); while (it.hasNext()) { arrayList.add(new Motor(it.next().intValue())); } Collections.sort(arrayList); return arrayList; } private List getMotorsFromPeripheralFile() { List a = this.mPeripheralConn.a(10); if (a == null || a.size() <= 0) { return null; } ArrayList arrayList = new ArrayList(a.size()); Iterator it = a.iterator(); while (it.hasNext()) { arrayList.add(new Motor(it.next().intValue())); } Collections.sort(arrayList); return arrayList; } private List getServosFromMatchFile(ModelMatchXmlFileManager modelMatchXmlFileManager) { List b = modelMatchXmlFileManager.b(); if (b == null || b.size() <= 0) { return null; } ArrayList arrayList = new ArrayList(b.size()); Iterator it = b.iterator(); while (it.hasNext()) { arrayList.add(new Servo(it.next().intValue(), ServoMode.SERVO_MODE_ANGLE)); } Collections.sort(arrayList); return arrayList; } private List getServosFromPeripheralFile() { HashMap c = this.mPeripheralConn.c(); if (c == null || c.size() <= 0) { return null; } ArrayList arrayList = new ArrayList(c.size()); Iterator 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 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 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 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()); } }