package com.willalp.control.netty.service.impl;

import com.willalp.control.netty.model.ControlDataModel;
import com.willalp.control.netty.service.CommandControlService;
import com.willalp.control.netty.service.CommandNettyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author xian
 */
@Service
public class CommandControlServiceImpl implements CommandControlService {
    protected final static Logger logger = LoggerFactory.getLogger(CommandControlServiceImpl.class);

    @Resource
    private ControlDataModel commandDataControl;

    @Resource
    private CommandNettyService commandNettyService;


    @Override
    public String[] dismantlingCommand(String str) {
        if (str.length() <= 18) {
            return null;
        }
        //针头
        String pinHead = str.substring(0, 2);
        //通讯地址
        String txAddress = str.substring(2, 12);
        //指令类型
        String type = str.substring(12, 14);
        //指令内容
        String content = str.substring(14, str.length() - 4);
        //返回结果
        String result = str.substring(str.length() - 4).substring(0, 2);
        //针尾
        String pinTail = str.substring(str.length() - 2);
        String[] strings = new String[6];
        strings[0] = pinHead;
        strings[1] = txAddress;
        strings[2] = type;
        strings[3] = content;
        strings[4] = result;
        strings[5] = pinTail;
        return strings;

    }




    /**
     * 传入一个长度为偶数的字符 然后 从第 1 和 2 位数开始 一起放入数组的第一个下标处
     * 以此类推
     * 解析成 每俩位数为一组的数组
     *
     * @param str
     * @return
     */
    @Override
    public String[] parsingInstructions(String str) {
        if (null == str) {
            return null;
        }
        //字符长度只能为偶数
        if (0 != str.length() % 2) {
            return null;
        }
        String[] sArray = new String[str.length() / 2];
        int head = 0;
        int tail = 2;
        for (int i = 0; i < sArray.length; i++) {
            sArray[i] = str.substring(head, tail);
            //解析为每2位为一组
            head += 2;
            tail += 2;
        }
        return sArray;
    }
}
