package org.nl.acs.device_driver.basedriver.standard_scanner;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.device_driver.DeviceDriver;
import org.nl.acs.device_driver.ScannerDeviceDriver;
import org.nl.acs.device_driver.driver.AbstractDeviceDriver;
import org.nl.acs.monitor.DeviceStageMonitor;
import org.nl.acs.socket.SocketConfig;
import org.nl.acs.udw.UnifiedDataAccessor;
import org.nl.acs.udw.UnifiedDataAccessorFactory;

/** 标准版扫码器 */
@Slf4j
@Data
public class StandardScannerDeviceDriver extends AbstractDeviceDriver
    implements ScannerDeviceDriver, DeviceDriver, DeviceStageMonitor {

  UnifiedDataAccessor accessor_value;

  public StandardScannerDeviceDriver() {
    this.accessor_value = UnifiedDataAccessorFactory.getAccessor(SocketConfig.udw_unit_key);
  }

  public String getIp() {
    String ip = (String) this.getDevice().getExtraValue().get("scannerIP");
    return StrUtil.isEmpty(ip) ? null : ip;
  }

  @Override
  public String readBarcode() {
    String ip = this.getIp();
    if (StrUtil.isEmpty(ip)) {
      //            throw new Exception("ip未配置");
      log.error("ip未配置");
    } else {
      String result = (String) this.accessor_value.getValue(this.getIp());
      return result;
    }
    return null;
  }

  @Override
  public void writeBarcode(String barcode) {
    String ip = this.getIp();
    if (StrUtil.isEmpty(ip)) {
      //            throw new Exception("ip未配置");
      log.error("ip未配置");
    } else {
      this.accessor_value.setValueWithPersistence(this.getIp(), barcode);
    }
  }

  @Override
  public void cleanBarcode() {
    String ip = this.getIp();
    if (StrUtil.isEmpty(ip)) {
      log.error("ip未配置");
      //            throw new Exception("ip未配置");
    } else {
      this.accessor_value.setValueWithPersistence(this.getIp(), (Object) null);
    }
  }

  @Override
  public JSONObject getDeviceStatusName() throws Exception {
    JSONObject jo = new JSONObject();
    jo.put("device_name", this.getDevice().getDevice_name());
    jo.put("isOnline", true);
    jo.put("device_type", device.getDevice_type());
    // 点击弹出
    jo.put("is_click", true);
    jo.put("ip", this.getIp());
    jo.put("container", StrUtil.isEmpty(this.readBarcode()) ? "" : this.readBarcode());
    return jo;
  }

  @Override
  public void setDeviceStatus(JSONObject data) {}
}
