package com.ubt.jimu.connect.model; import android.util.Log; import com.ubt.jimu.JimuApplication; import com.ubt.jimu.base.cache.Cache; 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.User; import com.ubt.jimu.transport3.UnityFileOperator; import com.ubtech.utils.XLog; import com.ubtrobot.jimu.connection.RequestException; import com.ubtrobot.jimu.robotapi.BluetoothSpeakerInfo; import com.ubtrobot.jimu.robotapi.BoardInfo; import com.ubtrobot.jimu.robotapi.JimuException; import com.ubtrobot.jimu.robotapi.JimuManager; import com.ubtrobot.jimu.robotapi.PeripheralType; import com.ubtrobot.log.ALog; import java.io.File; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /* loaded from: classes.dex */ public class ModelMatchModel { private ArrayList b = new ArrayList<>(); private JimuManager a = JimuApplication.l().f(); private String c() { User user = Cache.getInstance().getUser(); if (user == null) { return "local"; } return user.getUserId() + ""; } public ArrayList a() { return this.b; } public BoardInfo b() throws JimuException { BoardInfo l; try { try { if (!this.a.h()) { this.a.c(); throw new JimuException(-1, "ERROR_CODE_NOT_JIMU_DEVICE"); } this.a.n(); try { Thread.sleep(2000L); } catch (InterruptedException e) { e.printStackTrace(); Thread.currentThread().interrupt(); } try { l = this.a.l(); } catch (JimuException e2) { XLog.b("ModelMatchModel", "Read mother board info fail! Retry one time"); try { if (e2.getCode() == 238) { this.a.b(); } Thread.sleep(1000L); } catch (InterruptedException e3) { e3.printStackTrace(); Thread.currentThread().interrupt(); } try { l = this.a.l(); } catch (JimuException unused) { XLog.c("ModelMatchModel", "read board fail! goto connect fail page"); throw new JimuException(-2, "MATCH_RESULT_ERROR_READ_CODE_FAIL"); } } if (l != null) { return l; } XLog.b("ModelMatchModel", "Read mother board info fail! "); throw new JimuException(-2, "MATCH_RESULT_ERROR_READ_CODE_FAIL"); } catch (JimuException e4) { XLog.c("ModelMatchModel", "read board fail! goto connect fail page"); throw new JimuException(-2, e4.getMessage()); } } catch (RequestException e5) { XLog.c("ModelMatchModel", "read board fail! goto connect fail page"); throw new JimuException(-2, e5.getMessage()); } } public void a(String str, boolean z, MatchResultCallback matchResultCallback) { BoardInfo l; int i; XLog.c("ModelMatchModel", "start match model"); try { try { if (!this.a.h()) { this.a.c(); matchResultCallback.a(-1, null); return; } this.a.n(); try { Thread.sleep(2000L); } catch (InterruptedException unused) { Thread.currentThread().interrupt(); } try { l = this.a.l(); } catch (JimuException e) { XLog.b("ModelMatchModel", "Read mother board info fail! Retry one time"); try { if (e.getCode() == 238) { this.a.b(); } Thread.sleep(1000L); } catch (InterruptedException e2) { e2.printStackTrace(); Thread.currentThread().interrupt(); } try { l = this.a.l(); } catch (JimuException unused2) { XLog.c("ModelMatchModel", "read board fail! goto connect fail page"); matchResultCallback.a(-2, null); return; } } if (l == null) { XLog.b("ModelMatchModel", "Read mother board info fail! "); matchResultCallback.a(-2, null); return; } if (l.l()) { XLog.c("ModelMatchModel", "peripheral ID duplication"); i = -4; } else { i = 0; } this.b = a(l, str, z); Iterator it = this.b.iterator(); while (it.hasNext()) { Component next = it.next(); if (next.b() != ErrorType.NONE) { XLog.c("ModelMatchModel", "Has some component not match. component:" + next); i = -3; } } matchResultCallback.a(i, l); } catch (JimuException unused3) { XLog.c("ModelMatchModel", "read board fail! goto connect fail page"); matchResultCallback.a(-2, null); } } catch (RequestException unused4) { XLog.c("ModelMatchModel", "read board fail! goto connect fail page"); matchResultCallback.a(-2, null); } } private List c(List list, List list2) { if (list == null) { return new ArrayList(); } ArrayList arrayList = new ArrayList(list); if (list2 == null) { return arrayList; } arrayList.removeAll(list2); return arrayList; } private List b(List list, List list2) { if (list2 == null) { return new ArrayList(); } ArrayList arrayList = new ArrayList(list2); if (list == null) { return arrayList; } arrayList.removeAll(list); return arrayList; } public void b(BoardInfo boardInfo, String str, boolean z) { String c = c(); if (a(z, str)) { a(str, c, boardInfo, z); a(str, c, boardInfo); } else { b(str, c, boardInfo, z); } } private static void b(String str, String str2, BoardInfo boardInfo, boolean z) { String c; PeripheralConnections a = PeripheralConnections.a(str, str2, z); if (a == null) { return; } a.a(boardInfo); if (z) { c = FilePathUtils.a(str, str2); } else { c = FilePathUtils.c(str, str2); } a.b(c); a(str, true, PeripheralConnections.d(c), 2); } public ArrayList a(BoardInfo boardInfo, String str, boolean z) { String a; ArrayList arrayList = new ArrayList<>(); if (boardInfo == null) { XLog.b("ModelMatchModel", "Board info is null"); return arrayList; } if (a(z, str)) { return a(boardInfo); } if (z) { a = FilePathUtils.a(str, c()); } else { a = FilePathUtils.a(str); String c = FilePathUtils.c(str, c()); File file = new File(a); File file2 = new File(c); if (!file.exists() || (file2.exists() && file2.lastModified() > file.lastModified())) { a = c; } } XLog.c("ModelMatchModel", "Model file folder path:" + a); XLog.c("ModelMatchModel", "robotName:" + str + " isDiyModel:" + z); ArrayList a2 = a(boardInfo, a, str, z); StringBuilder sb = new StringBuilder(); sb.append("componentList:"); sb.append(a(a2)); XLog.c("ModelMatchModel", sb.toString()); a(a2, boardInfo); Iterator it = a2.iterator(); while (it.hasNext()) { Component next = it.next(); if (next.d() == 8 && next.b() != ErrorType.LACK) { if (next.b() == ErrorType.NONE) { next.a(ErrorType.PROMPT); } try { BluetoothSpeakerInfo a3 = this.a.a(next.c()); if (a3 == null) { Log.w("ModelMatchModel", "Not read any speaker info, Please check has speaker connected"); } else { ((BluetoothSpeakerComponent) next).b(a3.a()); } } catch (JimuException e) { XLog.a("ModelMatchModel", e.getMessage(), e); } } } return a2; } public ArrayList a(BoardInfo boardInfo) { ArrayList arrayList = new ArrayList<>(); arrayList.add(a(boardInfo.e(), 1, ErrorType.NONE, boardInfo.d())); List i = boardInfo.i(); HashMap h = boardInfo.h(); a(arrayList, PeripheralType.SERVO, i, boardInfo.j()); for (Map.Entry entry : h.entrySet()) { BoardInfo.SensorBoardInfo value = entry.getValue(); if (value != null) { a(arrayList, entry.getKey().intValue(), value.b(), value.c()); } } a(arrayList, boardInfo); Iterator it = arrayList.iterator(); while (it.hasNext()) { Component next = it.next(); if (next.d() == 8 && next.b() != ErrorType.LACK) { try { ((BluetoothSpeakerComponent) next).b(this.a.a(next.c()).a()); } catch (JimuException e) { XLog.a("ModelMatchModel", e.getMessage(), e); } } } return arrayList; } private void a(ArrayList arrayList, int i, List list, String str) { Iterator it = list.iterator(); while (it.hasNext()) { arrayList.add(a(i, it.next().intValue(), ErrorType.NONE, str)); } } private String a(ArrayList arrayList) { String str = ""; for (int i = 0; i < arrayList.size(); i++) { str = (str + arrayList.get(i)) + "\n"; } return str; } private void a(ArrayList arrayList, BoardInfo boardInfo) { if (boardInfo.a().size() != 0) { for (Integer num : boardInfo.a()) { Iterator it = arrayList.iterator(); while (it.hasNext()) { Component next = it.next(); if (next.d() == 128 && next.c() == num.intValue()) { it.remove(); } } arrayList.add(a(PeripheralType.SERVO, num.intValue(), ErrorType.ID_DUPLICATION)); } } for (Map.Entry entry : boardInfo.h().entrySet()) { if (entry.getValue().a().size() != 0) { for (Integer num2 : entry.getValue().a()) { Iterator it2 = arrayList.iterator(); while (it2.hasNext()) { Component next2 = it2.next(); if (next2.d() == entry.getKey().intValue() && next2.c() == num2.intValue()) { it2.remove(); } } if (entry.getKey().intValue() == 8) { arrayList.add(a(entry.getKey().intValue(), num2.intValue(), ErrorType.TOO_MANY)); } else { arrayList.add(a(entry.getKey().intValue(), num2.intValue(), ErrorType.ID_DUPLICATION)); } } } } } private ArrayList a(BoardInfo boardInfo, String str, String str2, boolean z) { int i; HashMap> b; PeripheralConnections peripheralConnections; String str3; int i2; ArrayList arrayList = new ArrayList<>(); String str4 = "ModelMatchModel"; if (boardInfo == null) { XLog.b("ModelMatchModel", "Board is null, not compare to model file."); return arrayList; } String str5 = str + "/" + str2 + ".xml"; if (!z) { str5 = FilePathUtils.a(str2) + "/" + str2 + ".xml"; } XLog.a("ModelMatchModel", "xmlPath:" + str5); ModelMatchXmlFileManager modelMatchXmlFileManager = new ModelMatchXmlFileManager(); modelMatchXmlFileManager.a(str5); List b2 = modelMatchXmlFileManager.b(); List a = modelMatchXmlFileManager.a(); List i3 = boardInfo.i(); HashMap h = boardInfo.h(); arrayList.add(a(boardInfo.e(), 1, ErrorType.NONE, boardInfo.d())); Iterator it = a(b2, i3).iterator(); while (true) { boolean hasNext = it.hasNext(); i = PeripheralType.SERVO; if (!hasNext) { break; } arrayList.add(a(PeripheralType.SERVO, it.next().intValue(), ErrorType.NONE, boardInfo.j())); } for (Integer num : c(b2, i3)) { if (boardInfo.a().contains(num)) { Component a2 = a(PeripheralType.SERVO, num.intValue(), ErrorType.ID_DUPLICATION); a2.a(boardInfo.j()); arrayList.add(a2); } else { arrayList.add(a(PeripheralType.SERVO, num.intValue(), ErrorType.LACK)); } } Iterator it2 = b(b2, i3).iterator(); while (it2.hasNext()) { Component a3 = a(PeripheralType.SERVO, it2.next().intValue(), ErrorType.EXTRA); a3.a(boardInfo.j()); arrayList.add(a3); } int i4 = 10; BoardInfo.SensorBoardInfo sensorBoardInfo = h.get(10); List b3 = sensorBoardInfo != null ? sensorBoardInfo.b() : null; Iterator it3 = a(a, b3).iterator(); while (it3.hasNext()) { arrayList.add(a(10, it3.next().intValue(), ErrorType.NONE, sensorBoardInfo.c())); } Iterator it4 = c(a, b3).iterator(); while (it4.hasNext()) { arrayList.add(a(10, it4.next().intValue(), ErrorType.LACK)); } List b4 = b(a, b3); if (b4 != null && b4.size() != 0) { Iterator it5 = b4.iterator(); while (it5.hasNext()) { Component a4 = a(10, it5.next().intValue(), ErrorType.EXTRA); if (sensorBoardInfo != null) { a4.a(sensorBoardInfo.c()); } arrayList.add(a4); } } PeripheralConnections a5 = PeripheralConnections.a(str2, c(), z); if (a5 == null) { XLog.b("ModelMatchModel", "Get peripheral connections fail by read connections file!"); b = new HashMap<>(); } else { b = a5.b(); } HashMap> hashMap = b; Field[] declaredFields = PeripheralType.class.getDeclaredFields(); int length = declaredFields.length; int i5 = 0; while (i5 < length) { try { i2 = declaredFields[i5].getInt(PeripheralType.class); } catch (IllegalAccessException e) { e = e; peripheralConnections = a5; } if (i2 != -1 && i2 != i4 && i2 != 112 && i2 != 113) { ArrayList arrayList2 = hashMap.get(Integer.valueOf(i2)); if (i2 == i && arrayList2 != null) { Iterator it6 = arrayList2.iterator(); while (it6.hasNext()) { int intValue = it6.next().intValue(); ServoMode servoMode = ServoMode.SERVO_MODE_ANGLE; if (a5 != null) { a5.a(Integer.valueOf(intValue)); } Iterator it7 = arrayList.iterator(); while (true) { if (it7.hasNext()) { Component next = it7.next(); if (i == next.d() && intValue == next.c()) { ALog.a(str4).d("ServoId:" + intValue + " Mode:" + servoMode); ((ServoComponent) next).a(servoMode); break; } i = PeripheralType.SERVO; } } i = PeripheralType.SERVO; } } else { BoardInfo.SensorBoardInfo sensorBoardInfo2 = h.get(Integer.valueOf(i2)); if (sensorBoardInfo2 != null) { List b5 = sensorBoardInfo2.b(); if (i2 == 8 && b5.size() > 1) { Component a6 = a(i2, 1, ErrorType.TOO_MANY); a6.a(sensorBoardInfo2.c()); arrayList.add(a6); } else { Iterator it8 = a(arrayList2, b5).iterator(); while (it8.hasNext()) { int intValue2 = it8.next().intValue(); peripheralConnections = a5; if (i2 == 8) { try { str3 = str4; if (b5.size() == 1) { try { arrayList.add(a(i2, intValue2, ErrorType.PROMPT, sensorBoardInfo2.c())); a5 = peripheralConnections; str4 = str3; } catch (IllegalAccessException e2) { e = e2; e.printStackTrace(); i5++; a5 = peripheralConnections; str4 = str3; i4 = 10; i = PeripheralType.SERVO; } } } catch (IllegalAccessException e3) { e = e3; str3 = str4; e.printStackTrace(); i5++; a5 = peripheralConnections; str4 = str3; i4 = 10; i = PeripheralType.SERVO; } } else { str3 = str4; } arrayList.add(a(i2, intValue2, ErrorType.NONE, sensorBoardInfo2.c())); a5 = peripheralConnections; str4 = str3; } peripheralConnections = a5; str3 = str4; Iterator it9 = c(arrayList2, b5).iterator(); while (it9.hasNext()) { arrayList.add(a(i2, it9.next().intValue(), ErrorType.LACK)); } Iterator it10 = b(arrayList2, b5).iterator(); while (it10.hasNext()) { Component a7 = a(i2, it10.next().intValue(), ErrorType.EXTRA); a7.a(sensorBoardInfo2.c()); arrayList.add(a7); } i5++; a5 = peripheralConnections; str4 = str3; i4 = 10; i = PeripheralType.SERVO; } } else if (arrayList2 != null) { Iterator it11 = arrayList2.iterator(); while (it11.hasNext()) { arrayList.add(a(i2, it11.next().intValue(), ErrorType.LACK)); } } } } peripheralConnections = a5; str3 = str4; i5++; a5 = peripheralConnections; str4 = str3; i4 = 10; i = PeripheralType.SERVO; } return arrayList; } private Component a(int i, int i2, ErrorType errorType, String str) { if (i == 8) { BluetoothSpeakerComponent bluetoothSpeakerComponent = new BluetoothSpeakerComponent(i2, errorType); bluetoothSpeakerComponent.a(str); return bluetoothSpeakerComponent; } if (i == 128) { ServoComponent servoComponent = new ServoComponent(i2, errorType); servoComponent.a(str); return servoComponent; } Component component = new Component(i, i2, errorType); component.a(str); return component; } private Component a(int i, int i2, ErrorType errorType) { if (i == 8) { return new BluetoothSpeakerComponent(i2, errorType); } if (i == 128) { return new ServoComponent(i2, errorType); } return new Component(i, i2, errorType); } private List a(List list, List list2) { ArrayList arrayList = new ArrayList(); if (list != null && list2 != null) { arrayList.addAll(list); arrayList.retainAll(list2); } return arrayList; } public static boolean a(boolean z, String str) { if (z) { return a(str); } return false; } public static boolean a(String str) { return !new File(FilePathUtils.b(str, Cache.getInstance().getUserId())).exists(); } private static void a(String str, String str2, BoardInfo boardInfo) { String str3 = FilePathUtils.a(str, str2) + "/" + str + ".xml"; new ModelMatchXmlFileManager().a(boardInfo, str3); a(str, true, str3, 1); } private static void a(String str, boolean z, String str2, int i) { DiyDBModel queryForUUid = DiyHelper.getInstance().queryForUUid(str); if (queryForUUid == null) { Log.w("ModelMatchModel", "Query null for diy model " + str + " from database"); return; } new UnityFileOperator(queryForUUid.getModelId().intValue(), str, z ? 1 : 0, str2, i).operateFile(); } private static void a(String str, String str2, BoardInfo boardInfo, boolean z) { String c; PeripheralConnections b = PeripheralConnections.b(boardInfo); if (z) { c = FilePathUtils.a(str, str2); } else { c = FilePathUtils.c(str, str2); } b.b(c); a(str, true, PeripheralConnections.d(c), 1); } }