package com.cisdi.data.HJ212.gateway;

import java.util.HashMap;
import java.util.Map;

import org.apache.tomcat.util.buf.HexUtils;
import org.apache.tomcat.util.buf.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.cisdi.data.common.exception.BusinessException;
import com.cisdi.data.common.utils.Strings;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class Hj212VoDecode {
	private static final Logger log = LoggerFactory.getLogger(Hj212VoDecode.class);
    
	public static Hj212FrameInputVo Decode(ByteBuf in) {
		byte[] bytes = new byte[in.readableBytes()];
		in.readBytes(bytes);
		String message = new String(bytes, CRC16Util.UTF8);
		
		String body = message.substring(6, message.length() - 6);
		String end = message.substring(message.length() - 6, message.length());
		
		String crcParameter = end.substring(0, 4);
		String crcResult = CRC16Util.CRC16(body);
		
		if(Strings.equalsIgnoreCase(crcParameter, crcResult) == false){
		    log.error("hj212 crc16 check fail, input:{}, input-crc:{},cal-crc:{}", body, crcParameter, crcResult);
		    throw new BusinessException("crc16 check fail input-crc=" + crcParameter); 
		}
		// 解析body字段
		// 字段与其值用‘=’连接；在数据区中，同一项目的不同分类值间用‘，’来分隔，不同项目之间用‘；’来分隔。
		Map<String, String> map = new HashMap<String, String>();
		
		String[] bodyItems = Strings.split(body.substring(0,body.length()-2), ';');
		for (String item : bodyItems) {
		    if(item.contains(",")) {
		        String[] itemBodys = Strings.split(item, ',');
		        for (String subItem : itemBodys) {
		            if(subItem.contains("=")) {
		                int indexOp = subItem.indexOf('=');
		                map.put(subItem.substring(0, indexOp), subItem.substring(indexOp + 1));		                
		            }
		        }		        
		    }else if(item.contains("=")) {
		        int indexOp = item.indexOf('=');
                map.put(item.substring(0, indexOp), item.substring(indexOp + 1));      
		    }
        }
		
		Hj212FrameInputVo value = new Hj212FrameInputVo();
		
		value.setCn(map.get("CN"));
		value.setQn(map.get("QN"));
		value.setSt(map.get("ST"));
        value.setPw(map.get("PW"));
	    value.setMn(map.get("MN"));
		if(map.containsKey("Flag")){
			value.setFlag(Integer.valueOf(map.get("Flag")));
		}
	    value.setCp(map.get("CP").substring(2));
		
	    // &&DataTime=20190425110800
	    int indexDt = value.getCp().indexOf("DataTime");
	    if(indexDt >= 0) {
	        value.setDataTime(Long.valueOf(value.getCp().substring(indexDt + 9, indexDt + 9 + 14)));
	    }
	    
	    map.remove("CN");
	    map.remove("QN");
	    map.remove("ST");
	    map.remove("PW");
	    map.remove("MN");
	    map.remove("Flag");
	    map.remove("CP");
	    
	    value.setDataMap(map);
		
		return value;
	}
	
	public static void main(String[] args) {
	    // 实时数据
//	    String input = "##0365QN=20190425110900000;ST=22;CN=2011;PW=123456;MN=01004190001003;Flag=5;CP=&&DataTime=20190425110800;a01001-Rtd=0.0,a01001-Flag=N;a01002-Rtd=0.0,a01002-Flag=N;a01006-Rtd=101.09,a01006-Flag=N;a01007-Rtd=0.0,a01007-Flag=N;a01008-Rtd=0,a01008-Flag=N;a34001-Rtd=0.0,a34001-Flag=N;a34002-Rtd=17.8,a34002-Flag=N;a34004-Rtd=7.7,a34004-Flag=N;a50001-Rtd=30.0,a50001-Flag=N;&&0880\r\n";
//		String input="##0102QN=20211207181139971;ST=31;CN=1011;PW=123456;MN=YFLGZXJCLT0003;Flag=1;CP=&&SystemTime=20211207181139&&4980\r\n";
//		String input="##0453QN=20211207181137957;ST=31;CN=2051;PW=123456;MN=YFLGZXJCLT0003;CP=&&DataTime=20211207180000;01-ZsAvg=1.38,01-ZsMin=1.25,01-ZsMax=1.54,01-Cou=0.18,01-Avg=1.38,01-Min=1.25,01-Max=1.54;B02-Cou=130926.47,B02-Avg=218.21,B02-Min=206.41,B02-Max=230.95;S03-Avg=16.38,S03-Min=16.30,S03-Max=16.44;S05-Avg=2.31,S05-Min=2.31,S05-Max=2.32;S08-Avg=-0.16,S08-Min=-0.17,S08-Max=-0.14;S02-Avg=12.08,S02-Min=11.42,S02-Max=12.79;S01-Avg=21.17,S01-Min=21.16,S01-Max=21.18&&5280\r\n";
//		String input="##0279QN=20211207181240386;ST=31;CN=2011;PW=123456;MN=YFLGZXJCLT0003;CP=&&DataTime=20211207181148;01-ZsRtd=1.32,01-Rtd=1.32,01-Flag=N;B02-Rtd=225.99,B02-Flag=N;S03-Rtd=16.29,S03-Flag=N;S05-Rtd=2.31,S05-Flag=N;S08-Rtd=-0.16,S08-Flag=N;S02-Rtd=12.51,S02-Flag=N;S01-Rtd=21.16,S01-Flag=N&&7940\r\n";
//	    String input="##0275QN=20211208195054384;ST=31;CN=2011;PW=123456;MN=SD371300004744;CP=&&DataTime=20211208194952;01-ZsRtd=0.52,01-Rtd=0.52,01-Flag=N;B02-Rtd=38.10,B02-Flag=N;S03-Rtd=8.66,S03-Flag=N;S05-Rtd=1.23,S05-Flag=N;S08-Rtd=0.00,S08-Flag=N;S02-Rtd=1.20,S02-Flag=N;S01-Rtd=21.61,S01-Flag=N&&4040\r\n";
//		String input="##0102QN=20211208194951184;ST=31;CN=1011;PW=123456;MN=SD371300004744;Flag=1;CP=&&SystemTime=20211208194951&&7F80\r\n";
//		String input="##0275QN=20211208194952060;ST=31;CN=2011;PW=123456;MN=SD371300004744;CP=&&DataTime=20211208194853;01-ZsRtd=0.52,01-Rtd=0.52,01-Flag=N;B02-Rtd=38.09,B02-Flag=N;S03-Rtd=8.65,S03-Flag=N;S05-Rtd=1.26,S05-Flag=N;S08-Rtd=0.00,S08-Flag=N;S02-Rtd=1.20,S02-Flag=N;S01-Rtd=21.61,S01-Flag=N&&5000\r\n";
		String input="##0441QN=20211208195157432;ST=31;CN=2051;PW=123456;MN=SD371300004744;CP=&&DataTime=20211208194000;01-ZsAvg=0.52,01-ZsMin=0.52,01-ZsMax=0.52,01-Cou=0.01,01-Avg=0.52,01-Min=0.52,01-Max=0.52;B02-Cou=22469.20,B02-Avg=37.45,B02-Min=34.90,B02-Max=38.10;S03-Avg=8.66,S03-Min=8.65,S03-Max=8.68;S05-Avg=1.27,S05-Min=1.23,S05-Max=1.29;S08-Avg=0.00,S08-Min=-0.01,S08-Max=0.00;S02-Avg=1.18,S02-Min=1.10,S02-Max=1.20;S01-Avg=21.61,S01-Min=21.60,S01-Max=21.62&&F9C1\r\n";
		ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(input.getBytes(CRC16Util.UTF8));
	    
	    Hj212FrameInputVo decode = Hj212VoDecode.Decode(wrappedBuffer);
	    wrappedBuffer.release();
	    System.out.println(JSON.toJSONString(decode));
	    
//	    String inputDay = "##0638QN=20190425104000000;ST=22;CN=2051;PW=123456;MN=0100410001003;Flag=5;CP=&&DataTime=20190425104000;a01001-Min=0.0,a01001-Avg=0.0,a01001-Max=0.0,a01001-Flag=N;a01002-Min=0.0,a01002-Avg=0.0,a01002-Max=0.0,a01002-Flag=N;a01006-Min=0.00,a01006-Avg=101.08,a01006-Max=101.08,a01006-Flag=N;a01007-Min=0.0,a01007-Avg=0.0,a01007-Max=0.0,a01007-Flag=N;a01008-Min=0,a01008-Avg=0,a01008-Max=0,a01008-Flag=N;a34001-Min=0.0,a34001-Avg=0.0,a34001-Max=0.0,a34001-Flag=N;a34002-Min=0.0,a34002-Avg=21.6,a34002-Max=35.2,a34002-Flag=N;a34004-Min=0.0,a34004-Avg=10.8,a34004-Max=22.7,a34004-Flag=N;a50001-Min=0.0,a50001-Avg=30.0,a50001-Max=30.0,a50001-Flag=N;&&5B40\r\n";
//        wrappedBuffer = Unpooled.wrappedBuffer(inputDay.getBytes(CRC16Util.UTF8));
//        decode = Hj212VoDecode.Decode(wrappedBuffer);
//        wrappedBuffer.release();
//        System.out.println(JSON.toJSONString(decode));
        
        
//        // 实时数据
//        String inputTest = "##0365QN=20190425110900000;ST=22;CN=2011;PW=123456;MN=01004190001003;Flag=5;CP=&&DataTime=20190425110800;a01001-Rtd=0.0,a01001-Flag=N;a01002-Rtd=0.0,a01002-Flag=N;a01006-Rtd=101.09,a01006-Flag=N;a01007-Rtd=0.0,a01007-Flag=N;a01008-Rtd=0,a01008-Flag=N;a34001-Rtd=0.0,a34001-Flag=N;a34002-Rtd=17.8,a34002-Flag=N;a34004-Rtd=7.7,a34004-Flag=N;a50001-Rtd=30.0,a50001-Flag=N;&&0880\r\n";
//        byte[] inputTestBytes = inputTest.getBytes(CRC16Util.UTF8);
//        String hexString = HexUtils.toHexString(inputTestBytes);
//        System.out.print(hexString);
        
	}
}
