package com.gyee.wisdom.dao.redis;

import com.gyee.wisdom.common.data.timeseries.BooleanTsData;
import com.gyee.wisdom.common.data.timeseries.DoubleTsData;
import com.gyee.wisdom.common.data.timeseries.TsDataType;
import com.gyee.wisdom.common.utils.ByteUtil;
import com.gyee.wisdom.common.utils.Hex;

import java.io.UnsupportedEncodingException;

public  class TsDataParser {

    //    typedef struct _redis_value_t {
    //        double          value;
    //        uint64_t        timestamp;
    //    } redis_value_t;在redis内存中的结构就是这样的，其实就是16个字节，前8个字节是双精度浮点数，后8个字节是时间戳，网络序。
    //    public static void main(String[] args) {
    //编码后内存值是这样的 ae 47 e1 7a 14 ae 28 40 00 00 01 7e 51 e8 57 28
    // 前面8个字节是double类型，真实值是12.34，
    // 后面8个字节是时间戳，单位毫秒，值是1642051688232，表示2022-01-13 13:28:08.232

    public static DoubleTsData parseWBString( String wbString)
            throws UnsupportedEncodingException {

        byte[] barr = wbString.getBytes("ascii");
        double val = ByteUtil.getDouble(barr,0);
        long ts = ByteUtil.getLong2(barr,8);

//        String valStr = wbString.substring(0,15);
//        String tsStr = wbString.substring(16);
//        long ts = Long.parseLong(tsStr,16);
//        byte[] barr = Hex.decodeHex(valStr.toCharArray());
//        double val = ByteUtil.getDouble(barr,0);
        return new DoubleTsData(ts, (short) 0, val);

    }


    public static DoubleTsData parseZYDoubleString( String zyString) {
            String s = zyString.trim();  //去除空格
            s = s.substring(2);
            s = s.substring(0, s.length() - 1);
            String ar[] = s.split(":");
            String tsStr = ar[0].substring(0, ar[0].length() - 1);
            long ts = Long.parseLong(tsStr) * 1000;
            double d = Double.parseDouble(ar[1]);
            return new DoubleTsData(ts, (short) 0, d);
    }

    public static BooleanTsData parseZYBooleanString( String zyString) {
            String s = zyString.trim();  //去除空格
            s = s.substring(2);
            s = s.substring(0, s.length() - 1);
            String ar[] = s.split(":");
            String tsStr = ar[0].substring(0, ar[0].length() - 1);
            long ts = Long.parseLong(tsStr) * 1000;
            double d = Double.parseDouble(ar[1]);
            if (ar[1].startsWith("0")) {
                return new BooleanTsData(ts, (short) 0, false);
            } else {
                return new BooleanTsData(ts, (short) 0, true);
            }
    }




}
