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

import com.alibaba.fastjson.JSONObject;
import com.zt.plat.module.qms.iot.tcpserver.core.Balance;
import com.zt.plat.module.qms.iot.tcpserver.core.Weighbridge;
import com.zt.plat.module.qms.iot.tcpserver.core.WeightUnit;
import tech.zzjc.tio.core.ChannelContext;
import tech.zzjc.tio.core.Tio;
import tech.zzjc.tio.utils.lock.MapWithLock;

import java.io.UnsupportedEncodingException;

import static com.zt.plat.module.qms.iot.tcpserver.IotDeviceType.BALANCE;

/**
 * <b>IotDataUtils</b>
 * <p>
 * 更新历史：
 * <pre> 版本         更新时间            更新者        更新内容<hr/>
 * V1.0      2019年9月16日          王兴荣         Add</pre>
 * <b>Copyright (C)  云南志者竟成科技有限公司</b> 
 * <p>
 * @author 王兴荣 &lt;xingrong_wang@will-way.cn&gt;
 * @version V1.0 
 * @since 2019年9月16日
 */
public class IotDataUtils {
        
    private static MapWithLock<String, ChannelContext> iotToConn = new MapWithLock<>();
    
    public static void hander(ChannelContext channelContext, String data) {
        IotDeviceType iotDeviceType = (IotDeviceType) channelContext.get("iotType");
        String iotCode = (String) channelContext.get("iotCode");
        String bsId = channelContext.getBsId();
        ChannelContext toChannelContext = iotToConn.get(bsId);
        switch (iotDeviceType) {
            case BALANCE:
                double weightData = 0d;
                String weightUnit = null;
                if (data.startsWith("S")) {
                    String[] splits = data.split(" ");
                    System.out.println("第一个：" + splits[0]);
                    System.out.println("第二个：" + splits[1]);
                    System.out.println("重量数据：" + splits[splits.length - 2]);
                    System.out.println("单位数据：" + splits[splits.length - 1]);
                    weightData = Double.parseDouble(splits[splits.length - 2]);
                    weightUnit = splits[splits.length - 1];
                } else {
                    data = data.replace("(", "").replace(")", "");
                    String[] splits = data.split(" ");
                    System.out.println(iotCode + "重量数据：" + splits[0]);
                    System.out.println(iotCode + "单位数据：" + splits[1]);
                    weightData = Double.parseDouble(splits[0]);
                    weightUnit = splits[1];
                }
                if (toChannelContext != null && !toChannelContext.isClosed) {
                    Balance balance = new Balance();
                    balance.setDeviceId(iotCode);
                    balance.setWeight(weightData);
                    balance.setWeightUnit(WeightUnit.getByName(weightUnit));
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("type", BALANCE);
                    jsonObject.put("data", balance);
                    IotPacket packet = new IotPacket();
                    try {
                        packet.setBody((jsonObject.toJSONString() + "\r\n").getBytes(IotPacket.CHARSET));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    Tio.send(toChannelContext, packet);
                }
                
                
                break;
            case WEIGHBRIDGE:
                String[] splits = data.split(" ");
                double weightData1 = Double.parseDouble(splits[0]);
                String weightUnit1 = splits[1];
                if (toChannelContext != null && !toChannelContext.isClosed) {
                    Weighbridge weighbridge = new Weighbridge();
                    weighbridge.setWeighbridge(weightData1);
                    weighbridge.setWeightUnit(WeightUnit.getByName(weightUnit1));
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("type", "weighbridge");
                    jsonObject.put("data", weighbridge);
                    IotPacket packet = new IotPacket();
                    try {
                        packet.setBody((jsonObject.toJSONString() + "\r\n").getBytes(IotPacket.CHARSET));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    Tio.send(toChannelContext, packet);
                }
                break;
            case INFRARED:
                //String iotCode = (String) channelContext.get("iotCode");
                Integer intIotCode = Integer.parseInt(iotCode);
                JSONObject jsonObject = new JSONObject();
                if (intIotCode % 2 == 0) {
                    JSONObject infraredRight = new JSONObject();
                    infraredRight.put("infraredRight", Integer.parseInt(data));
                    jsonObject.put("type", "infraredRight");
                    jsonObject.put("data", infraredRight);
                } else {
                    JSONObject infraredLeft = new JSONObject();
                    infraredLeft.put("infraredLeft", Integer.parseInt(data));
                    jsonObject.put("type", "infraredLeft");
                    jsonObject.put("data", infraredLeft);
                }
                if (toChannelContext != null && !toChannelContext.isClosed) {
                    IotPacket packet = new IotPacket();
                    try {
                        packet.setBody((jsonObject.toJSONString() + "\r\n").getBytes(IotPacket.CHARSET));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    Tio.send(toChannelContext, packet);
                }
                break;
            default:
                break;
        }
        
        
    }
    
    public static void addIot(String iot, ChannelContext channelContext) {
        if (iotToConn.get(iot) != null) {
            iotToConn.remove(iot);
            iotToConn.put(iot, channelContext);
        } else {
            iotToConn.put(iot, channelContext);
        }
    }
    
    public static void removeIot(ChannelContext channelContext) {
        String[] connDeviceCode = (String[]) channelContext.get("connDeviceCode");
        String connType = (String) channelContext.get("connType");
        if (connDeviceCode == null) return;
        for (String connDevice : connDeviceCode) {
            iotToConn.remove(connType + connDevice);
        }
    }
}
