package org.springblade.third.iotdevice.service.impl;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.device.entity.IotDevice;
import org.springblade.device.feign.IIotDeviceClient;
import org.springblade.third.iotdevice.service.IAepSubScribeService;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author lidexi
 * @date 2021/5/27 17:42
 */
@Service
@Slf4j
public class AepSubScribeServiceImpl implements IAepSubScribeService {

	private DataReportContext DataReportContext;
	private IIotDeviceClient iotDeviceClient;

	public AepSubScribeServiceImpl setDataReportContext(DataReportContext DataReportContext) {
		this.DataReportContext = DataReportContext;
		return this;
	}

	public AepSubScribeServiceImpl setIotDeviceClient(IIotDeviceClient iotDeviceClient){
		this.iotDeviceClient = iotDeviceClient;
		return this;
	}

	@Override
	public void dataReport(Map<String, Object> map) {
		this.DataReportContext.dataReport(map);
	}

	@Override
	public Object deviceStatusChange(Map<String,Object> map) {
		log.error("deviceOnlineOfflineReport --> " + JSONUtil.toJsonStr(map));
		String deviceId = map.get("deviceId").toString();
		IotDevice searchDevice = new IotDevice();
		searchDevice.setDeviceId(deviceId);
		IotDevice smartDevice = iotDeviceClient.detail(searchDevice);
		String eventType = map.get("eventType").toString();
//		smartDevice.setDefenseStatus(Integer.valueOf(eventType));
		iotDeviceClient.update(smartDevice);
		return R.success("成功");
	}
}
