package com.zt.plat.module.qms.iot.tcpserver.device.handler;

import com.alibaba.fastjson.JSONObject;
import com.zt.plat.module.qms.iot.tcpserver.IotDeviceType;
import com.zt.plat.module.qms.iot.tcpserver.IotPacket;
import com.zt.plat.module.qms.iot.tcpserver.core.ClientType;
import com.zt.plat.module.qms.iot.tcpserver.core.Command;
import com.zt.plat.module.qms.iot.tcpserver.core.IotDeviceSessionContext;
import com.zt.plat.module.qms.iot.tcpserver.handler.IotDataHander;
import com.zt.plat.module.qms.iot.tcpserver.publisher.HygrothermographPublisher;
import tech.zzjc.tio.core.ChannelContext;
import tech.zzjc.tio.core.Tio;

import java.nio.charset.Charset;

import static com.zt.plat.module.qms.iot.tcpserver.core.GroupConstant.CAA_ALL_CLIENT;

/**
 * <b>IotDeviceThermometerHandler</b>
 * 温湿度
 * <p>更新历史：</p>
 * <pre> 版本         更新时间            更新者        更新内容
 * V1.0      2021-10-14          王兴荣         Add</pre>
 * <b>Copyright (C)  云南志者竟成科技有限公司</b> 
 * @author 王兴荣 &lt;xingrong_wang@will-way.cn&gt;
 * @version V1.0 
 * @since 2021-10-14
 */
public class IotDeviceThermometerHandler implements IotDataHander {

    @Override
    public Object hander(String msgId, String data, ChannelContext channelContext) throws Exception {        
        
        if (channelContext.get("TenantId") == null) { //设备未注册成功，暂不处理数据
            return null;
        }
        
        IotDeviceSessionContext sessionContext = (IotDeviceSessionContext) channelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
        int temperature = Integer.parseInt(data.substring(6, 10), 16);
        int humidity = Integer.parseInt(data.substring(10, 14), 16);
        if (temperature > 32767) {//上传数据需除十，正数的范围为16进制0X0000-0X7FFF，负数采用正数的补码方式传输,其范围为16进制0X8000-0XFFFF
            temperature = -(65535 - temperature);
        }
//        double temperature = 0.0d;
//        if (0 < t && t <= 1000) {
//            temperature = t * 0.1d;
//        } else {
//            temperature = -(65535 - t) * 0.1d;
//        }
//        double humidity = h * 0.1d;
        String tenantId = channelContext.get("TenantId").toString();
        HygrothermographPublisher.publishEvent(Long.parseLong(sessionContext.getDeviceId()), tenantId, temperature, humidity);
//        System.out.println("温度：" + temperature * 0.1 + "°C，湿度：" + humidity * 0.1 + "%");
        JSONObject deviceData = new JSONObject();
        deviceData.put("deviceId", sessionContext.getDeviceId());
        deviceData.put("deviceType", IotDeviceType.THERMOMETER.getType());
        deviceData.put("temperature", temperature);
        deviceData.put("humidity", humidity);
        
        JSONObject json = new JSONObject();
        json.put("cmd", Command.DEVICE_DATA.getName());
        json.put("clientType", ClientType.CAA_CLIENT.getName());
        json.put("data", deviceData);
        IotPacket packet = new IotPacket();
        packet.setBody((json.toJSONString() + "\r\n").getBytes(Charset.forName(IotPacket.CHARSET)));
        Tio.sendToGroup(channelContext.getTioConfig(), CAA_ALL_CLIENT, packet);
        
        return null;
    }

}
