2021-01-10 23:37:09 +01:00
|
|
|
/* Copyright (C) 2017-2021 Andreas Shimokawa, Carsten Pfeiffer, Daniele
|
2019-02-13 20:43:30 +01:00
|
|
|
Gobbetti
|
2017-09-18 23:26:34 +02:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2019-08-13 22:04:52 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations;
|
2017-09-18 23:26:34 +02:00
|
|
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
2019-01-26 15:52:40 +01:00
|
|
|
import androidx.annotation.NonNull;
|
2023-07-22 21:27:20 +02:00
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2017-10-23 10:28:54 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip.AmazfitBipService;
|
2017-09-18 23:26:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.BLETypeConversions;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
|
2022-08-18 23:03:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.HuamiSupport;
|
2017-09-18 23:26:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
|
2019-08-13 22:04:52 +02:00
|
|
|
public class HuamiFetchDebugLogsOperation extends AbstractFetchOperation {
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(HuamiFetchDebugLogsOperation.class);
|
2017-09-18 23:26:34 +02:00
|
|
|
|
|
|
|
private FileOutputStream logOutputStream;
|
|
|
|
|
2022-08-18 23:03:28 +02:00
|
|
|
public HuamiFetchDebugLogsOperation(HuamiSupport support) {
|
2017-09-18 23:26:34 +02:00
|
|
|
super(support);
|
2019-08-13 22:04:52 +02:00
|
|
|
setName("fetch debug logs");
|
2017-09-18 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
2023-07-22 21:27:20 +02:00
|
|
|
@Override
|
|
|
|
protected String taskDescription() {
|
|
|
|
return getContext().getString(R.string.busy_task_fetch_debug_logs);
|
|
|
|
}
|
|
|
|
|
2017-09-18 23:26:34 +02:00
|
|
|
@Override
|
|
|
|
protected void startFetching(TransactionBuilder builder) {
|
|
|
|
File dir;
|
|
|
|
try {
|
|
|
|
dir = FileUtils.getExternalFilesDir();
|
|
|
|
} catch (IOException e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-03 23:31:18 +02:00
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.US);
|
2019-08-13 22:04:52 +02:00
|
|
|
String filename = "huamidebug_" + dateFormat.format(new Date()) + ".log";
|
2017-09-18 23:26:34 +02:00
|
|
|
|
|
|
|
File outputFile = new File(dir, filename );
|
|
|
|
try {
|
|
|
|
logOutputStream = new FileOutputStream(outputFile);
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("could not create file " + outputFile, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GregorianCalendar sinceWhen = BLETypeConversions.createCalendar();
|
|
|
|
sinceWhen.add(Calendar.DAY_OF_MONTH, -10);
|
2018-09-15 22:38:20 +02:00
|
|
|
startFetching(builder, AmazfitBipService.COMMAND_ACTIVITY_DATA_TYPE_DEBUGLOGS, sinceWhen);
|
2017-09-18 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String getLastSyncTimeKey() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-09-18 13:19:23 +02:00
|
|
|
protected boolean handleActivityFetchFinish(boolean success) {
|
|
|
|
LOG.info("{} data has finished", getName());
|
2017-09-18 23:26:34 +02:00
|
|
|
try {
|
|
|
|
logOutputStream.close();
|
|
|
|
logOutputStream = null;
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.warn("could not close output stream", e);
|
2022-09-18 13:19:23 +02:00
|
|
|
return false;
|
2017-09-18 23:26:34 +02:00
|
|
|
}
|
2022-09-18 13:19:23 +02:00
|
|
|
|
|
|
|
return super.handleActivityFetchFinish(success);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean validChecksum(int crc32) {
|
|
|
|
// TODO actually check it?
|
|
|
|
LOG.warn("Checksum not implemented for debug logs, assuming it's valid");
|
|
|
|
return true;
|
2017-09-18 23:26:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void handleActivityNotif(byte[] value) {
|
|
|
|
if (!isOperationRunning()) {
|
|
|
|
LOG.error("ignoring notification because operation is not running. Data length: " + value.length);
|
|
|
|
getSupport().logMessageContent(value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((byte) (lastPacketCounter + 1) == value[0]) {
|
|
|
|
lastPacketCounter++;
|
|
|
|
bufferActivityData(value);
|
|
|
|
} else {
|
2017-11-01 23:04:52 +01:00
|
|
|
GB.toast("Error " + getName() + " invalid package counter: " + value[0], Toast.LENGTH_LONG, GB.ERROR);
|
2017-10-19 21:52:38 +02:00
|
|
|
handleActivityFetchFinish(false);
|
2017-09-18 23:26:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void bufferActivityData(@NonNull byte[] value) {
|
|
|
|
try {
|
2018-05-03 23:31:18 +02:00
|
|
|
logOutputStream.write(value, 1, value.length - 1);
|
2023-07-22 21:27:20 +02:00
|
|
|
} catch (final IOException e) {
|
2017-09-18 23:26:34 +02:00
|
|
|
LOG.warn("could not write to output stream", e);
|
2017-10-19 21:52:38 +02:00
|
|
|
handleActivityFetchFinish(false);
|
2017-09-18 23:26:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|