package com.seewo.factoryaging.p0;

import android.util.Log;
import com.seewo.factoryaging.v;
import j.p0.d.r;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: compiled from: AgingLogRecorder.kt */
/* JADX INFO: loaded from: classes.dex */
public final class b {
    private final String a;
    private Timer b;

    /* JADX INFO: compiled from: AgingLogRecorder.kt */
    public static final class a extends TimerTask {
        a() {
        }

        @Override // java.util.TimerTask, java.lang.Runnable
        public void run() {
            b.this.f();
        }
    }

    public b(String str) {
        r.f(str, "deviceName");
        this.a = str;
    }

    private final String b() {
        return this.a;
    }

    private final String c(String str) {
        return com.seewo.factoryaging.p0.a.a.c(str);
    }

    private final String d() {
        try {
            return h.c.c.e.a.a();
        } catch (Exception e) {
            e.printStackTrace();
            return "SN_ERROR";
        }
    }

    private final String e() {
        String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US).format(new Date());
        r.e(str, "format.format(Date())");
        return str;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final void f() {
        String strD = d();
        Log.i("AgingLogRecorder", "recordDeviceStatus sn: " + strD);
        String strB = b();
        String strC = c(strB);
        String strE = e();
        i(strB, strC);
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("sn", strD);
            jSONObject.put("name", strB);
            jSONObject.put("status", strC);
            jSONObject.put("time", strE);
        } catch (JSONException e) {
            Log.e("AgingLogRecorder", "recordDeviceStatus: error " + e);
        }
        try {
            FileWriter fileWriter = new FileWriter(new File(com.seewo.factoryaging.p0.a.a.b()), true);
            fileWriter.append((CharSequence) jSONObject.toString()).append((CharSequence) "\n");
            fileWriter.close();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        Log.d("AgingLogRecorder", jSONObject.toString());
    }

    private final void i(String str, String str2) {
        Object next;
        if (r.a(str2, "PASS")) {
            return;
        }
        Iterator<T> it = com.seewo.factoryaging.p0.a.a.d().iterator();
        while (true) {
            if (!it.hasNext()) {
                next = null;
                break;
            } else {
                next = it.next();
                if (r.a(((v) next).c(), str)) {
                    break;
                }
            }
        }
        v vVar = (v) next;
        if (vVar != null) {
            vVar.d(vVar.a() + 1);
        } else {
            com.seewo.factoryaging.p0.a.a.d().add(new v(str, 1, e(), str2));
        }
    }

    public final void g() {
        Timer timer = new Timer();
        this.b = timer;
        if (timer != null) {
            timer.schedule(new a(), 0L, 300000L);
        }
    }

    public final void h() {
        Timer timer = this.b;
        if (timer != null) {
            timer.cancel();
        }
        Timer timer2 = this.b;
        if (timer2 != null) {
            timer2.purge();
        }
        this.b = null;
    }
}
