package com.dlc.chemical.serial.port.scan

import android.app.Service
import android.content.Intent
import android.os.IBinder
import com.dlc.chemical.serial.listener.OnSerialListener
import com.rc.core.log.RcLog

/**
 * info
 *
 * @author ReiChin_
 */
class PortScanService: Service(), OnSerialListener {

    companion object {
        const val ACTION_PORT_SCAN = "com.dlc.chemical.serial:PORT_SCAN"
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        mPortScanManager.connect(this)
    }

    private val mPortScanManager by lazy {
        PortScanManager(this)
    }

    override fun onDestroy() {
        mPortScanManager.disconnect()
        super.onDestroy()
    }

    override fun onConnected(success: Boolean, message: String?) {
        RcLog.info("PortScanService#onConnected() success=${success}, message=${message}")
    }

    override fun onReceive(content: String) {
        // QRCode/IC 扫描返回
        RcLog.info("PortScanService#onReceive() QRCode/IC content=${content}")
        val intent = Intent().apply {
            putExtra("scan_content", content)
            action = ACTION_PORT_SCAN
        }
        sendBroadcast(intent)
    }

}