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

314 lines
13 KiB
Java

package com.ubt.jimu.transport;
import android.text.TextUtils;
import android.util.Log;
import com.ubt.jimu.base.cache.Cache;
import com.ubt.jimu.base.db.DatabaseUtils;
import com.ubt.jimu.base.db.diy.DiyDBModel;
import com.ubt.jimu.base.entities.User;
import com.ubt.jimu.base.http.ApiObserver;
import com.ubt.jimu.base.http.ApiResponse;
import com.ubt.jimu.diy.DiyRobotDbHandler;
import com.ubt.jimu.transport.dao.ConfigItemDbHandler;
import com.ubt.jimu.transport.dao.TransportFileDbHandler;
import com.ubt.jimu.transport.model.ConfigItem;
import com.ubt.jimu.transport.model.TransportFile;
import com.ubt.jimu.utils.SystemUtils;
import com.ubt.jimu.widgets.LoadingView;
import com.ubtrobot.log.ALog;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import java.io.File;
import java.util.HashMap;
import java.util.List;
/* loaded from: classes2.dex */
public class Transporter {
private static final String f = "Transporter";
private static HashMap<String, Transporter> g = new HashMap<>();
private String a;
private String b;
private FileTransporter c;
private ConfigItemDbHandler d;
private TransportFileDbHandler e;
private Transporter(String str, String str2) {
new ConfigItemTransporter();
this.c = new FileTransporter();
this.d = new ConfigItemDbHandler(DatabaseUtils.getDaoSession(true).b());
this.e = new TransportFileDbHandler(DatabaseUtils.getDaoSession(true).v());
new DiyRobotDbHandler(DatabaseUtils.getDaoSession(true).d());
this.a = str;
this.b = str2;
}
public static Transporter b() {
User user = Cache.getInstance().getUser();
String valueOf = user == null ? "local" : String.valueOf(user.getUserId());
Transporter transporter = g.get(valueOf);
if (transporter == null) {
synchronized (Transporter.class) {
transporter = g.get(valueOf);
if (transporter == null) {
transporter = new Transporter(valueOf, user == null ? "" : user.getUserName());
g.put(valueOf, transporter);
}
}
}
return transporter;
}
public synchronized void a(String str, final ConfigItem configItem) {
if ("local".equals(this.a)) {
ALog.a(f).d("本地账户,不需要同步到服务器:" + this.a);
return;
}
configItem.setUploadState(1);
this.d.update(configItem);
TransportManager.getInstance().a(configItem.getUserId(), str, configItem, new ApiObserver<ApiResponse<ConfigItem>>(null) { // from class: com.ubt.jimu.transport.Transporter.2
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onComplete() {
super.onComplete();
configItem.setUploadState(0);
Transporter.this.d.update(configItem);
}
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onError(Throwable th) {
super.onError(th);
Log.e(Transporter.f, "uploadConfigItem onError:" + th.getMessage());
th.printStackTrace();
configItem.setUploadState(0);
Transporter.this.d.update(configItem);
}
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onSubscribe(Disposable disposable) {
super.onSubscribe(disposable);
}
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onNext(ApiResponse<ConfigItem> apiResponse) {
ConfigItem a;
super.onNext((AnonymousClass2) apiResponse);
ConfigItem models = apiResponse.getModels();
if (models == null || (a = Transporter.this.d.a(apiResponse.getModels())) == null) {
return;
}
a.setIsModify(false);
a.setServerID("0000");
a.setId(models.getId());
Transporter.this.d.update(a);
}
});
}
public boolean b(File file) {
TransportFile fileBean = this.e.getFileBean(this.a, file);
if (fileBean == null) {
return false;
}
long a = SystemUtils.a();
fileBean.setModifyTime(a);
fileBean.setLastUploadTime(a);
if (!fileBean.getIsModify()) {
fileBean.setIsModify(true);
}
this.e.update(fileBean);
if ("local".equals(this.a)) {
ALog.a(f).d("本地账户,不需要同步到服务器:" + this.a);
return true;
}
a("update", fileBean);
return true;
}
public Observable<ApiResponse<DiyDBModel>> a(final DiyDBModel diyDBModel) {
LoadingView loadingView = null;
if (diyDBModel == null || diyDBModel.getModelId().intValue() == 0) {
return null;
}
if ("local".equals(this.a)) {
ALog.a(f).d("本地账户,不需要同步到服务器:" + this.a);
return null;
}
if (diyDBModel.getModelId().intValue() < 1) {
this.c.a(this.a, diyDBModel);
return null;
}
this.c.a(diyDBModel);
final List<TransportFile> modelFilesDeleted = this.e.setModelFilesDeleted(this.a, diyDBModel.getCustomModelId());
return TransportManager.getInstance().a(diyDBModel.getModelCreatedId(), String.valueOf(diyDBModel.getModelId()), new ApiObserver<ApiResponse<DiyDBModel>>(loadingView) { // from class: com.ubt.jimu.transport.Transporter.5
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onNext(ApiResponse<DiyDBModel> apiResponse) {
super.onNext((AnonymousClass5) apiResponse);
Transporter.this.c.a(Transporter.this.a, diyDBModel);
Transporter.this.a("delete", (List<TransportFile>) modelFilesDeleted);
}
});
}
public boolean a(long j, String str, String str2, File file) {
long insert = this.e.insert(new TransportFile(this.a, (int) j, str, str2, "", TransportFileDbHandler.getFilePath(file), file.getName()));
if ("local".equals(this.a)) {
ALog.a(f).d("本地账户,不需要同步到服务器:" + this.a);
return true;
}
a("add", this.e.selectById(Long.valueOf(insert)));
return true;
}
public boolean a(File file) {
TransportFile fileBean = this.e.getFileBean(this.a, file);
if (fileBean == null) {
return false;
}
long a = SystemUtils.a();
fileBean.setModifyTime(a);
fileBean.setLastUploadTime(a);
fileBean.setIsDeleted(true);
fileBean.setIsModify(true);
if (file != null && file.exists()) {
file.delete();
}
this.e.update(fileBean);
if ("local".equals(this.a)) {
ALog.a(f).d("本地账户,不需要同步到服务器:" + this.a);
return true;
}
a("delete", fileBean);
return true;
}
private void a(final String str, final TransportFile transportFile) {
if (transportFile.getModelId() < 0) {
return;
}
try {
TransportManager.getInstance().a(this.a, this.b, str, transportFile, new ApiObserver<ApiResponse<TransportFile>>(null) { // from class: com.ubt.jimu.transport.Transporter.9
private void a(TransportFile transportFile2) {
TransportFile selectUnique = Transporter.this.e.selectUnique(transportFile);
if (selectUnique == null) {
return;
}
selectUnique.setFileId(transportFile2.getFileId());
selectUnique.setModifyTime(transportFile2.getModifyTime());
selectUnique.setLastUploadTime(transportFile2.getLastUploadTime());
selectUnique.setFileVersion(transportFile2.getFileVersion());
selectUnique.setIsModify(false);
Transporter.this.e.update(selectUnique);
ALog.a(Transporter.f).d(str + ":" + transportFile.getFilePath() + transportFile.getFileName() + "成功");
}
private void b(TransportFile transportFile2) {
TransportFile selectUnique = Transporter.this.e.selectUnique(transportFile);
if (selectUnique == null) {
return;
}
selectUnique.setIsModify(false);
selectUnique.setLastUploadTime(transportFile2.getLastUploadTime());
selectUnique.setModifyTime(transportFile2.getModifyTime());
selectUnique.setFileVersion(transportFile2.getFileVersion());
Transporter.this.e.update(selectUnique);
ALog.a(Transporter.f).d(str + ":" + transportFile.getFilePath() + transportFile.getFileName() + "成功");
}
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onComplete() {
super.onComplete();
}
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onError(Throwable th) {
super.onError(th);
Log.e(Transporter.f, str + ":" + transportFile.getFilePath() + transportFile.getFileName() + "失败");
}
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onNext(ApiResponse<TransportFile> apiResponse) {
TransportFile models;
super.onNext((AnonymousClass9) apiResponse);
ALog.a(Transporter.f).d(str + ":" + transportFile.getFilePath() + transportFile.getFileName() + "返回");
if (apiResponse == null || (models = apiResponse.getModels()) == null) {
return;
}
String str2 = str;
char c = 65535;
int hashCode = str2.hashCode();
if (hashCode != -1335458389) {
if (hashCode != -838846263) {
if (hashCode == 96417 && str2.equals("add")) {
c = 0;
}
} else if (str2.equals("update")) {
c = 1;
}
} else if (str2.equals("delete")) {
c = 2;
}
if (c == 0) {
a(models);
} else if (c == 1) {
b(models);
} else {
if (c != 2) {
return;
}
b(models);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/* JADX INFO: Access modifiers changed from: private */
public void a(final String str, final List<TransportFile> list) {
if (list == null || list.size() < 1 || TextUtils.isEmpty(str)) {
return;
}
try {
TransportManager.getInstance().a(this.a, this.b, str, list, new ApiObserver<ApiResponse<List<TransportFile>>>(null) { // from class: com.ubt.jimu.transport.Transporter.10
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onComplete() {
super.onComplete();
}
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onError(Throwable th) {
super.onError(th);
Log.e(Transporter.f, "上传多个文件失败:" + th.getMessage());
}
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onSubscribe(Disposable disposable) {
super.onSubscribe(disposable);
}
@Override // com.ubt.jimu.base.http.ApiObserver, io.reactivex.Observer
public void onNext(ApiResponse<List<TransportFile>> apiResponse) {
List<TransportFile> models;
super.onNext((AnonymousClass10) apiResponse);
if (apiResponse == null || (models = apiResponse.getModels()) == null || models.size() == 0) {
return;
}
Log.i(Transporter.f, "同步成功:" + str + "-" + models.size());
if ("delete".equals(str)) {
Transporter.this.e.deleteInTx(list);
Log.i(Transporter.f, "delete:" + list.size());
return;
}
for (TransportFile transportFile : list) {
transportFile.setFileId("1");
transportFile.setIsModify(false);
}
Transporter.this.e.updateInTx(list);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}