package com.seewo.remote.e;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import com.cvte.vman.R;
import com.seewo.remote.e.g.a;
import com.seewo.remote.g.m;
import com.seewo.vcommons.data.d;
import e.c.e.f.e;
import io.netty.channel.ChannelHandlerContext;
import okhttp3.internal.http2.Http2Connection;

/* JADX INFO: compiled from: LanServer.java */
/* JADX INFO: loaded from: classes.dex */
public class b {
    private Context a;
    private com.seewo.remote.e.d.a b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private com.seewo.remote.e.d.a f30c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private com.seewo.remote.e.c.b f31d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private com.seewo.remote.e.c.b f32e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private com.seewo.remote.e.e.h.b f33f;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private f.a.k.b f35h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private com.seewo.remote.e.c.a f36i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private BroadcastReceiver f37j = new a();

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private com.seewo.remote.e.i.a f38k = new C0002b();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private f.a.r.c<com.seewo.remote.e.f.a> f34g = f.a.r.a.x().v();

    /* JADX INFO: compiled from: LanServer.java */
    class a extends BroadcastReceiver {
        a() {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            Log.i("NetWorker", "onReceive action: " + intent.getAction());
            if (!"com.ifpdos.settings.action.RS232_CODE_CHANGE".equals(intent.getAction())) {
                if ("com.ifpdos.settings.action.UPDATE_DEVICE_ID".equals(intent.getAction())) {
                    b.this.f36i.a();
                }
            } else {
                com.seewo.remote.d.b.b().f(b.this.h());
                if (com.seewo.remote.d.b.b().c() == 8) {
                    b.this.f36i.a();
                }
            }
        }
    }

    /* JADX INFO: renamed from: com.seewo.remote.e.b$b, reason: collision with other inner class name */
    /* JADX INFO: compiled from: LanServer.java */
    class C0002b implements com.seewo.remote.e.i.a {
        C0002b() {
        }

        @Override // com.seewo.remote.e.i.a
        public void a(ChannelHandlerContext channelHandlerContext) {
            b.this.f36i.g(channelHandlerContext.channel());
        }

        @Override // com.seewo.remote.e.i.a
        public void b(com.seewo.remote.e.f.a aVar) {
            e.g("NetWorker", "Receive message " + aVar + "\nFrom " + aVar.a().name());
            b.this.f34g.d(aVar);
        }

        @Override // com.seewo.remote.e.i.a
        public boolean c(ChannelHandlerContext channelHandlerContext) {
            return true;
        }

        @Override // com.seewo.remote.e.i.a
        public void d(ChannelHandlerContext channelHandlerContext) {
            if (b.this.f31d != null) {
                b.this.f31d.c(channelHandlerContext.channel());
            }
            if (b.this.f32e != null) {
                b.this.f32e.c(channelHandlerContext.channel());
            }
            b.this.f36i.f(channelHandlerContext.channel());
        }
    }

    /* JADX INFO: compiled from: LanServer.java */
    static /* synthetic */ class c {
        static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[a.b.values().length];
            a = iArr;
            try {
                iArr[a.b.MESSAGE_SET.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                a[a.b.MESSAGE_GET.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
        }
    }

    public b(Context context) {
        this.a = context;
        com.seewo.remote.e.c.a aVarE = com.seewo.remote.e.c.a.e();
        this.f36i = aVarE;
        aVarE.h(context);
        com.seewo.remote.d.b.b().f(h());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void g(com.seewo.remote.e.f.a aVar) {
        com.seewo.remote.e.c.b bVar;
        if (this.a.getResources().getBoolean(R.bool.is_support_check_ops_rs232) && m.b(aVar)) {
            Log.w("NetWorker", "OPSRS232Checker intercept is true, ignore command.");
            return;
        }
        int i2 = c.a[aVar.f().ordinal()];
        if (i2 != 1) {
            if (i2 == 2 && (bVar = this.f32e) != null) {
                bVar.a(aVar);
                return;
            }
            return;
        }
        com.seewo.remote.e.c.b bVar2 = this.f31d;
        if (bVar2 != null) {
            bVar2.a(aVar);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean h() {
        return com.seewo.remote.d.b.b().c() == 8 ? d.o("persist.sys.use_common_protocol", true) : com.seewo.vcommons.data.c.c(this.a, "key_rs232_code_setting", true);
    }

    private void j() {
        Log.v("NetWorker", "sendReadyEvent");
        Intent intent = new Intent("com.benq.smartdms.action.socket_server_ready");
        intent.addFlags(Http2Connection.OKHTTP_CLIENT_WINDOW_SIZE);
        this.a.sendBroadcast(intent);
    }

    public void f() {
        com.seewo.remote.e.d.a aVar;
        Log.v("NetWorker", "tcp finish");
        com.seewo.remote.e.c.b bVar = this.f31d;
        if (bVar != null) {
            bVar.b();
        }
        com.seewo.remote.e.c.b bVar2 = this.f32e;
        if (bVar2 != null) {
            bVar2.b();
        }
        f.a.k.b bVar3 = this.f35h;
        if (bVar3 != null) {
            bVar3.dispose();
        }
        com.seewo.remote.e.e.h.b bVar4 = this.f33f;
        if (bVar4 != null) {
            bVar4.c();
            this.f33f = null;
        }
        com.seewo.remote.e.d.a aVar2 = this.b;
        if (aVar2 != null) {
            aVar2.d();
            this.b = null;
        }
        if (com.seewo.remote.d.a.i() && (aVar = this.f30c) != null) {
            aVar.d();
            this.f30c = null;
        }
        this.f38k = null;
        this.a.unregisterReceiver(this.f37j);
        this.f36i.i();
    }

    public void k() {
        Log.v("NetWorker", "tcp work");
        if (com.seewo.remote.d.a.f() && this.f33f == null) {
            com.seewo.remote.e.e.h.b bVar = new com.seewo.remote.e.e.h.b();
            this.f33f = bVar;
            bVar.start();
        }
        if (this.b == null) {
            com.seewo.remote.e.d.b bVar2 = new com.seewo.remote.e.d.b(this.a.getResources().getInteger(R.integer.tcp_port), this.f38k);
            this.b = bVar2;
            bVar2.c();
            j();
        }
        if (com.seewo.remote.d.a.i() && this.f30c == null) {
            com.seewo.remote.e.d.b bVar3 = new com.seewo.remote.e.d.b(this.a.getResources().getInteger(R.integer.tcp_port2), this.f38k);
            this.f30c = bVar3;
            bVar3.c();
        }
        if (this.f31d == null) {
            this.f31d = com.seewo.remote.e.g.a.b(this.a, a.b.MESSAGE_SET);
        }
        if (this.f32e == null) {
            this.f32e = com.seewo.remote.e.g.a.b(this.a, a.b.MESSAGE_GET);
        }
        this.f35h = this.f34g.r(f.a.q.a.b()).j(f.a.q.a.b()).n(new f.a.m.c() { // from class: com.seewo.remote.e.a
            @Override // f.a.m.c
            public final void accept(Object obj) {
                this.a.g((com.seewo.remote.e.f.a) obj);
            }
        });
        IntentFilter intentFilter = new IntentFilter("com.seewo.remote.action. ACTION_UPDATE_LAN_RS232_STATE");
        intentFilter.addAction("com.ifpdos.settings.action.RS232_CODE_CHANGE");
        intentFilter.addAction("com.ifpdos.settings.action.UPDATE_DEVICE_ID");
        this.a.registerReceiver(this.f37j, intentFilter);
    }
}
