jimu-decompiled/sources/com/ubt/jimu/connect/model/PeripheralConnections.java
2025-05-13 19:24:51 +02:00

499 lines
18 KiB
Java

package com.ubt.jimu.connect.model;
import android.os.Environment;
import android.util.Log;
import com.ubt.jimu.base.data.ServoMode;
import com.ubt.jimu.connect.model.TopologyPartData;
import com.ubt.jimu.utils.SystemUtils;
import com.ubtech.utils.XLog;
import com.ubtrobot.jimu.robotapi.BoardInfo;
import com.ubtrobot.jimu.robotapi.PeripheralType;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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 PeripheralConnections {
private List<TopologyPartData> a = new ArrayList();
private HashMap<Integer, List<Integer>> b;
private HashMap<Integer, ServoMode> c;
private HashMap<String, String> d;
private long e;
private HashMap<Integer, ArrayList<Integer>> f;
/* renamed from: com.ubt.jimu.connect.model.PeripheralConnections$1, reason: invalid class name */
static /* synthetic */ class AnonymousClass1 {
static final /* synthetic */ int[] a = new int[TopologyPartData.TopologyPartType.values().length];
static {
try {
a[TopologyPartData.TopologyPartType.Servo.ordinal()] = 1;
} catch (NoSuchFieldError unused) {
}
try {
a[TopologyPartData.TopologyPartType.Motor.ordinal()] = 2;
} catch (NoSuchFieldError unused2) {
}
try {
a[TopologyPartData.TopologyPartType.Gyro.ordinal()] = 3;
} catch (NoSuchFieldError unused3) {
}
try {
a[TopologyPartData.TopologyPartType.Light.ordinal()] = 4;
} catch (NoSuchFieldError unused4) {
}
try {
a[TopologyPartData.TopologyPartType.Touch.ordinal()] = 5;
} catch (NoSuchFieldError unused5) {
}
try {
a[TopologyPartData.TopologyPartType.Color.ordinal()] = 6;
} catch (NoSuchFieldError unused6) {
}
try {
a[TopologyPartData.TopologyPartType.Gravity.ordinal()] = 7;
} catch (NoSuchFieldError unused7) {
}
try {
a[TopologyPartData.TopologyPartType.Speaker.ordinal()] = 8;
} catch (NoSuchFieldError unused8) {
}
try {
a[TopologyPartData.TopologyPartType.Infrared.ordinal()] = 9;
} catch (NoSuchFieldError unused9) {
}
try {
a[TopologyPartData.TopologyPartType.Compass.ordinal()] = 10;
} catch (NoSuchFieldError unused10) {
}
try {
a[TopologyPartData.TopologyPartType.EnLight.ordinal()] = 11;
} catch (NoSuchFieldError unused11) {
}
try {
a[TopologyPartData.TopologyPartType.Atmosphere.ordinal()] = 12;
} catch (NoSuchFieldError unused12) {
}
try {
a[TopologyPartData.TopologyPartType.Ultrasonic.ordinal()] = 13;
} catch (NoSuchFieldError unused13) {
}
try {
a[TopologyPartData.TopologyPartType.DigitalTube.ordinal()] = 14;
} catch (NoSuchFieldError unused14) {
}
try {
a[TopologyPartData.TopologyPartType.Temperature.ordinal()] = 15;
} catch (NoSuchFieldError unused15) {
}
try {
a[TopologyPartData.TopologyPartType.MainBoard.ordinal()] = 16;
} catch (NoSuchFieldError unused16) {
}
try {
a[TopologyPartData.TopologyPartType.MainBoard_new_low.ordinal()] = 17;
} catch (NoSuchFieldError unused17) {
}
try {
a[TopologyPartData.TopologyPartType.RgbLight.ordinal()] = 18;
} catch (NoSuchFieldError unused18) {
}
}
}
public PeripheralConnections() {
TopologyPartData.TopologyPartType topologyPartType = TopologyPartData.TopologyPartType.MainBoard;
this.b = new HashMap<>();
this.c = new HashMap<>();
this.d = new HashMap<>();
this.f = new HashMap<>();
}
public static String d(String str) {
return str + "/servos/servos.txt";
}
public long a() {
return this.e;
}
public HashMap<Integer, ArrayList<Integer>> b() {
return this.f;
}
public HashMap<Integer, ServoMode> c() {
return this.c;
}
public static PeripheralConnections b(BoardInfo boardInfo) {
PeripheralConnections peripheralConnections = new PeripheralConnections();
peripheralConnections.c(boardInfo);
return peripheralConnections;
}
private static PeripheralConnections c(String str) {
if (!new File(str + "/servos/servos.txt").exists()) {
return null;
}
PeripheralConnections peripheralConnections = new PeripheralConnections();
peripheralConnections.a(str);
return peripheralConnections;
}
public List<Integer> a(int i) {
HashMap<Integer, ArrayList<Integer>> hashMap = this.f;
if (hashMap == null || hashMap.size() <= 0) {
return null;
}
return this.f.get(Integer.valueOf(i));
}
private HashMap<Integer, ArrayList<Integer>> a(List<TopologyPartData> list) {
HashMap<Integer, ArrayList<Integer>> hashMap = new HashMap<>();
for (TopologyPartData topologyPartData : list) {
int a = a(topologyPartData.b);
if (a != -1) {
ArrayList<Integer> arrayList = hashMap.get(Integer.valueOf(a));
if (arrayList == null) {
arrayList = new ArrayList<>();
}
arrayList.add(Integer.valueOf(topologyPartData.a));
hashMap.put(Integer.valueOf(a), arrayList);
}
}
return hashMap;
}
private int b(TopologyPartData.TopologyPartType topologyPartType) {
switch (AnonymousClass1.a[topologyPartType.ordinal()]) {
case 1:
return PeripheralType.SERVO;
case 2:
return 10;
case 3:
return 3;
case 4:
return 4;
case 5:
return 2;
case 6:
return 9;
case 7:
return 5;
case 8:
return 8;
case 9:
return 1;
case 10:
return 96;
case 11:
return 12;
case 12:
return 97;
case 13:
return 6;
case 14:
return 7;
case 15:
return 11;
case 16:
return 112;
case 17:
return 113;
case 18:
return 14;
default:
Log.e("PeripheralConnections", "Invalid peripheral type. TopologyPartType:" + topologyPartType);
return -1;
}
}
private TopologyPartData.TopologyPartType b(int i) {
if (i == 14) {
return TopologyPartData.TopologyPartType.RgbLight;
}
if (i == 128) {
return TopologyPartData.TopologyPartType.Servo;
}
if (i == 96) {
return TopologyPartData.TopologyPartType.Compass;
}
if (i == 97) {
return TopologyPartData.TopologyPartType.Atmosphere;
}
if (i == 112) {
return TopologyPartData.TopologyPartType.MainBoard;
}
if (i != 113) {
switch (i) {
case 1:
return TopologyPartData.TopologyPartType.Infrared;
case 2:
return TopologyPartData.TopologyPartType.Touch;
case 3:
return TopologyPartData.TopologyPartType.Gyro;
case 4:
return TopologyPartData.TopologyPartType.Light;
case 5:
return TopologyPartData.TopologyPartType.Gravity;
case 6:
return TopologyPartData.TopologyPartType.Ultrasonic;
case 7:
return TopologyPartData.TopologyPartType.DigitalTube;
case 8:
return TopologyPartData.TopologyPartType.Speaker;
case 9:
return TopologyPartData.TopologyPartType.Color;
case 10:
return TopologyPartData.TopologyPartType.Motor;
case 11:
return TopologyPartData.TopologyPartType.Temperature;
case 12:
return TopologyPartData.TopologyPartType.EnLight;
default:
return TopologyPartData.TopologyPartType.None;
}
}
return TopologyPartData.TopologyPartType.MainBoard_new_low;
}
private void c(BoardInfo boardInfo) {
if (boardInfo == null) {
return;
}
this.a.add(new TopologyPartData(b(boardInfo.e()), 0, false));
for (Integer num : boardInfo.i()) {
this.c.put(num, ServoMode.SERVO_MODE_ANGLE);
this.a.add(new TopologyPartData(TopologyPartData.TopologyPartType.Servo, num.intValue(), false));
}
for (Map.Entry<Integer, BoardInfo.SensorBoardInfo> entry : boardInfo.h().entrySet()) {
boolean z = entry.getKey().intValue() != 10;
TopologyPartData.TopologyPartType b = b(entry.getKey().intValue());
Iterator<Integer> it = entry.getValue().b().iterator();
while (it.hasNext()) {
this.a.add(new TopologyPartData(b, it.next().intValue(), z));
}
}
}
private int a(TopologyPartData.TopologyPartType topologyPartType) {
switch (AnonymousClass1.a[topologyPartType.ordinal()]) {
case 1:
return PeripheralType.SERVO;
case 2:
return 10;
case 3:
return 3;
case 4:
return 4;
case 5:
return 2;
case 6:
return 9;
case 7:
return 5;
case 8:
return 8;
case 9:
return 1;
case 10:
return 96;
case 11:
return 12;
case 12:
return 97;
case 13:
return 6;
case 14:
return 7;
case 15:
return 11;
case 16:
return 112;
case 17:
return 113;
case 18:
return 14;
default:
return -1;
}
}
public static PeripheralConnections a(String str, String str2, boolean z) {
String str3 = Environment.getExternalStorageDirectory().getPath() + "/Android/data/com.ubt.jimu/files";
String str4 = str3 + "/default/" + str;
String str5 = str3 + "/users/" + str2 + "/";
if (!z) {
String str6 = str5 + "/default/" + str;
PeripheralConnections c = c(str6);
PeripheralConnections c2 = c(str4);
if (c == null || (c2 != null && c2.a() > c.a())) {
c = c2;
}
if (c == c2) {
XLog.c("PeripheralConnections", "Connection folder:" + str4);
return c;
}
XLog.c("PeripheralConnections", "Connection folder:" + str6);
return c;
}
String str7 = str5 + "/playerdata/" + str;
PeripheralConnections c3 = c(str7);
XLog.c("PeripheralConnections", "Connection folder:" + str7);
return c3;
}
public void a(BoardInfo boardInfo) {
BoardInfo.SensorBoardInfo sensorBoardInfo;
HashMap<Integer, BoardInfo.SensorBoardInfo> h = boardInfo.h();
for (Map.Entry<Integer, BoardInfo.SensorBoardInfo> entry : h.entrySet()) {
if (entry.getKey().intValue() != 10) {
TopologyPartData.TopologyPartType b = b(entry.getKey().intValue());
Iterator<Integer> it = entry.getValue().b().iterator();
while (it.hasNext()) {
int intValue = it.next().intValue();
if (!a(b, intValue)) {
this.a.add(new TopologyPartData(b, intValue, true));
}
}
}
}
Iterator<TopologyPartData> it2 = this.a.iterator();
while (it2.hasNext()) {
TopologyPartData next = it2.next();
int b2 = b(next.b);
if (b2 != 128 && b2 != 10 && b2 != 112 && b2 != 113 && (sensorBoardInfo = h.get(Integer.valueOf(b2))) != null && !sensorBoardInfo.b().contains(Integer.valueOf(next.a)) && next.c) {
it2.remove();
}
}
}
public void b(String str) {
String str2 = str + "/servos";
File file = new File(str2);
if (!file.exists()) {
file.mkdirs();
}
File file2 = new File(str2 + "/servos.txt");
if (!file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e) {
XLog.a("PeripheralConnections", "Create file fail!", e);
return;
}
}
String property = System.getProperty("line.separator");
try {
FileWriter fileWriter = new FileWriter(file2);
try {
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
try {
for (Map.Entry<Integer, List<Integer>> entry : this.b.entrySet()) {
bufferedWriter.write(entry.getKey() + " " + b(entry.getValue()) + property);
}
StringBuilder sb = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for (Map.Entry<Integer, ServoMode> entry2 : this.c.entrySet()) {
if (entry2.getValue() == ServoMode.SERVO_MODE_ANGLE) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(entry2.getKey());
} else {
if (sb2.length() > 0) {
sb2.append(",");
}
sb2.append(entry2.getKey());
}
}
if (sb.length() > 0) {
bufferedWriter.write("angleServos:" + sb.toString() + property);
}
if (sb2.length() > 0) {
bufferedWriter.write("turnServos:" + sb2.toString() + property);
}
int size = this.a.size();
for (int i = 0; i < size; i++) {
bufferedWriter.write("partData:" + this.a.get(i).toString() + property);
}
if (this.d.size() > 0) {
StringBuilder sb3 = new StringBuilder();
for (Map.Entry<String, String> entry3 : this.d.entrySet()) {
if (sb3.length() > 0) {
sb3.append("|");
}
sb3.append(entry3.getKey());
sb3.append(",");
sb3.append(entry3.getValue());
}
bufferedWriter.write("portConnection:" + sb3.toString() + property);
}
bufferedWriter.flush();
this.e = SystemUtils.a();
bufferedWriter.close();
fileWriter.close();
} finally {
}
} finally {
}
} catch (Exception e2) {
XLog.a("PeripheralConnections", "Save file fail!", e2);
}
}
private boolean a(TopologyPartData.TopologyPartType topologyPartType, int i) {
for (TopologyPartData topologyPartData : this.a) {
if (topologyPartData.b == topologyPartType && topologyPartData.a == i) {
return true;
}
}
return false;
}
/* JADX WARN: Multi-variable type inference failed */
/* JADX WARN: Removed duplicated region for block: B:84:0x0166 A[EXC_TOP_SPLITTER, SYNTHETIC] */
/* JADX WARN: Type inference failed for: r2v0, types: [long] */
/* JADX WARN: Type inference failed for: r2v1 */
/* JADX WARN: Type inference failed for: r2v3, types: [java.io.BufferedReader] */
/*
Code decompiled incorrectly, please refer to instructions dump.
To view partially-correct code enable 'Show inconsistent code' option in preferences
*/
public void a(java.lang.String r13) {
/*
Method dump skipped, instructions count: 362
To view this dump change 'Code comments level' option to 'DEBUG'
*/
throw new UnsupportedOperationException("Method not decompiled: com.ubt.jimu.connect.model.PeripheralConnections.a(java.lang.String):void");
}
private <T> String b(List<T> list) {
StringBuffer stringBuffer = new StringBuffer();
Iterator<T> it = list.iterator();
while (it.hasNext()) {
stringBuffer.append(it.next());
stringBuffer.append(",");
}
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
return stringBuffer.toString();
}
public ServoMode a(Integer num) {
if (this.c.containsKey(num)) {
return this.c.get(num);
}
return ServoMode.SERVO_MODE_ANGLE;
}
public void a(Integer num, ServoMode servoMode) {
this.c.put(num, servoMode);
}
}