package com.its.app;

import com.beanit.asn1bean.ber.ReverseByteArrayOutputStream;
import com.beanit.asn1bean.ber.types.*;
import com.beanit.asn1bean.ber.types.string.BerUTF8String;
import com.beanit.asn1dsrc.dsrc.*;
import com.beanit.asn1dsrc.enums.eAuthInfo;
import com.beanit.asn1dsrc.util.DsrcAsn1Utils;
import com.its.app.utils.ByteUtils;
import com.its.app.utils.SysUtils;
import com.its.dsrc.DsrcCommServerApplication;
import com.its.dsrc.entity.TbRseCtlr;
import com.its.dsrc.global.AppRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Slf4j
@SpringBootTest(classes = DsrcCommServerApplication.class)
public class DsrcCommServerApplicationTests {

    @Test
    void requestLogin() {
        int dataPacketNumber = 0;           //obj.getSeq().nextValue()
        int dataPacketPriorityNumber = 0;

        Login login = new Login();
        login.setDatexSenderTxt(new BerUTF8String("PTATMSSERVER"));
        login.setDatexDestinationTxt(new BerUTF8String("PTMCU00000001"));
//        login.setDatexLoginUserNameTxt(new BerUTF8String(runningConfig.getServerAuthUser()));
//        login.setDatexLoginPasswordTxt(new BerUTF8String(runningConfig.getServerAuthPassword()));
        login.setDatexLoginUserNameTxt(new BerUTF8String(""));
        login.setDatexLoginPasswordTxt(new BerUTF8String(""));
        login.setDatexLoginHeartbeatDurationMaxQty(new BerInteger(120));
        login.setDatexLoginResponseTimeOutQty(new BerInteger(30));
        login.setDatexLoginInitiatorCd(new BerEnum(1));
        login.setDatexLoginDatagramSizeQty(new BerInteger(2048));
        int[] encodingRules = {2, 1};
        Login.DatexLoginEncodingRulesId loginEncodingRulesId = new Login.DatexLoginEncodingRulesId();
        loginEncodingRulesId.getBerObjectIdentifier().add(new BerObjectIdentifier(encodingRules));
        login.setDatexLoginEncodingRulesId(loginEncodingRulesId);

        byte[] auth = {(byte) eAuthInfo.AI_Login.getValue() };
        PDUs loginPdu = new PDUs();
        loginPdu.setLogin(login);

        HeaderOptions headerOptions = new HeaderOptions();
        Time tm = new Time();
        Time.TimeSecondFractions tmFac = new Time.TimeSecondFractions();
        Time.TimeTimezone        tmZon = new Time.TimeTimezone();
        Calendar currTm = Calendar.getInstance();
        tm.setTimeYearQty(new BerInteger(currTm.get(Calendar.YEAR)));
        tm.setTimeMonthQty(new BerInteger(currTm.get(Calendar.MONTH)+1));
        tm.setTimeDayQty(new BerInteger(currTm.get(Calendar.DATE)));
        tm.setTimeHourQty(new BerInteger(currTm.get(Calendar.HOUR_OF_DAY)));
        tm.setTimeMinuteQty(new BerInteger(currTm.get(Calendar.MINUTE)));
        tm.setTimeSecondQty(new BerInteger(currTm.get(Calendar.SECOND)));
        tmFac.setMilliseconds(new BerInteger(currTm.get(Calendar.MILLISECOND)));
        tm.setTimeSecondFractions(tmFac);
        headerOptions.setDatexDataPacketTime(tm);

        C2CAuthenticatedMessage c2c = new C2CAuthenticatedMessage();
        c2c.setDatexAuthenticationInfoText(new BerOctetString(auth));
        c2c.setDatexDataPacketNumber(new BerInteger(dataPacketNumber));
        c2c.setDatexDataPacketPriorityNumber(new BerInteger(dataPacketPriorityNumber));
        c2c.setOptions(headerOptions);
        c2c.setPdu(loginPdu);

        log.error("{}", c2c);
    }

    @Test
    void requestObuStatusInfo() {
        // 상태정보 요청
        int updateDelayQty = 30;

        int[] messageId = {1, 0, 15784, 4, 0, 7};
        int subscribeSerialNbr = 22;

        try {
            byte[] controlId = {3};
            byte[] controlData = {};
            ControlDevice controlDevice = new ControlDevice();
            controlDevice.setVpbdControlID(new BerOctetString(controlId));
            controlDevice.setVpbdControlDeviceData(new BerOctetString(controlData));
            Registered registered = new Registered();
            Registered.Continuous continuous = new Registered.Continuous();
            SubscriptionMode subscriptionMode = new SubscriptionMode();

            continuous.setDatexRegisteredUpdateDelayQty(new BerInteger(updateDelayQty));
            continuous.setDatexRegisteredStartTime(DsrcAsn1Utils.getRegisterStartTime());
            continuous.setDatexRegisteredEndTime(DsrcAsn1Utils.getRegisterEndTime());
            registered.setContinuous(continuous);
            subscriptionMode.setPeriodic(registered);

            // ControlDeviceList set
            ReverseByteArrayOutputStream msgBuff = new ReverseByteArrayOutputStream(1024);
            ControlDeviceList controlDeviceList = new ControlDeviceList();
            controlDeviceList.getControlDevice().add(controlDevice);
            controlDeviceList.encode(msgBuff);

            // EndApplicationMessage set
            EndApplicationMessage endApplicationMessage =  new EndApplicationMessage();
            endApplicationMessage.setEndApplicationMessageId(new BerObjectIdentifier(messageId));
            endApplicationMessage.setEndApplicationMessageMsg(new BerAny(msgBuff.getArray()));

            SubscriptionData subscriptionData = new SubscriptionData();
            subscriptionData.setDatexSubscribePersistentBool(new BerBoolean(false));
            subscriptionData.setDatexSubscribeStatusCd(new BerEnum(0));         // 0: New, 1: Update
            subscriptionData.setDatexSubscribeMode(subscriptionMode);
            subscriptionData.setDatexSubscribePublishFormatCd(new BerEnum(3));  // 0: other, 1: ftp, 2: tftp, 3: dataPacket
            //subscriptionData.setDatexSubscriptionPriorityNbr(new BerInteger(1));
            subscriptionData.setDatexSubscriptionPriorityNbr(new BerInteger(10));
            subscriptionData.setDatexSubscribeGuaranteeBool(new BerBoolean(true));
            subscriptionData.setDatexSubscribePdu(endApplicationMessage);

            SubscriptionType subscriptionType = new SubscriptionType();
            subscriptionType.setSubscription(subscriptionData);

            Subscription subscription = new Subscription();
            subscription.setDatexSubscribeSerialNbr(new BerInteger(subscribeSerialNbr));
            subscription.setDatexSubscribeType(subscriptionType);

            PDUs pdus = new PDUs();
            pdus.setSubscription(subscription);
            byte[] auth = { (byte) eAuthInfo.AI_Subscription.getValue() };
            C2CAuthenticatedMessage c2c = new C2CAuthenticatedMessage();
            c2c.setDatexAuthenticationInfoText(new BerOctetString(auth));
            c2c.setDatexDataPacketNumber(new BerInteger(subscribeSerialNbr));
            c2c.setDatexDataPacketPriorityNumber(new BerInteger(1));
            //c2c.setOptions(DsrcAsn1Utils.getDefaultOptions());
            c2c.setOptions(new HeaderOptions());

            c2c.setPdu(pdus);

            log.info("{}", c2c);
        }
        catch(Exception e) {
            log.error("LoginDeviceService.requestObuStatusInfo: {}, Exception: {}", updateDelayQty, e);
        }
    }

    @Test
    void requestObuGatherInfo() {
        int[] messageId = {1, 0, 15784, 8, 0, 8};
        TbRseCtlr obj = AppRepository.getInstance().getCtlrMap().get("1001");
        log.error("{}", obj);
        int subscribeSerialNbr = 0;
        int dataPacketNumber = 1;           //obj.getSeq().nextValue()
        int dataPacketPriorityNumber = 0;
        int datexSubscriptionPriorityNbr = 10;
        try {
            // OBU 교통정보 요청
            byte[] obuIdNumber = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
            byte[] vehicleType = {0};
            byte[] obuType = {0};
            byte[] generationDate = {0,0,0,0};
            byte[] generationTime = {0,0,0};

            OBUGatherInfoList lists = new OBUGatherInfoList();
            OBUGatherInfo gatherInfo = new OBUGatherInfo();
            gatherInfo.setObuIdNumber(new BerOctetString(obuIdNumber));
            BeaconID beaconID = new BeaconID();
            beaconID.setManufacturerid(new BerInteger(obj.getManufacturerid()));
            beaconID.setIndividualid(new BerInteger(obj.getIndividualid()));
            gatherInfo.setRseIDNumber(beaconID);
            gatherInfo.setVehicleType(new BerOctetString(vehicleType));
            gatherInfo.setObuType(new BerOctetString(obuType));
            gatherInfo.setGenerationDate(new BerOctetString(generationDate));
            gatherInfo.setGenerationTime(new BerOctetString(generationTime));
            lists.getOBUGatherInfo().add(gatherInfo);

            byte[] controlId = { (byte)4 };
            ReverseByteArrayOutputStream msgBuff = new ReverseByteArrayOutputStream(1024);
            lists.encode(msgBuff);
            ControlDevice controlDevice = new ControlDevice();
            controlDevice.setVpbdControlID(new BerOctetString(controlId));
            controlDevice.setVpbdControlDeviceData(new BerOctetString(msgBuff.getArray()));

            Registered registered = new Registered();
            Registered.Continuous continuous = new Registered.Continuous();
            SubscriptionMode subscriptionMode = new SubscriptionMode();

            continuous.setDatexRegisteredUpdateDelayQty(new BerInteger(0));
            continuous.setDatexRegisteredStartTime(DsrcAsn1Utils.getRegisterStartTime());
            continuous.setDatexRegisteredEndTime(DsrcAsn1Utils.getRegisterEndTime());
            registered.setContinuous(continuous);
            subscriptionMode.setEventDriven(registered);

            // EndApplicationMessage set
            EndApplicationMessage endApplicationMessage =  new EndApplicationMessage();
            endApplicationMessage.setEndApplicationMessageId(new BerObjectIdentifier(messageId));
            endApplicationMessage.setEndApplicationMessageMsg(new BerAny(msgBuff.getArray()));

            SubscriptionData subscriptionData = new SubscriptionData();
            subscriptionData.setDatexSubscribePersistentBool(new BerBoolean(false));
            subscriptionData.setDatexSubscribeStatusCd(new BerEnum(0));         // 0: New, 1: Update
            subscriptionData.setDatexSubscribeMode(subscriptionMode);
            subscriptionData.setDatexSubscribePublishFormatCd(new BerEnum(3));  // 0: other, 1: ftp, 2: tftp, 3: dataPacket
            //subscriptionData.setDatexSubscriptionPriorityNbr(new BerInteger(1));
            subscriptionData.setDatexSubscriptionPriorityNbr(new BerInteger(datexSubscriptionPriorityNbr));
            subscriptionData.setDatexSubscribeGuaranteeBool(new BerBoolean(true));
            subscriptionData.setDatexSubscribePdu(endApplicationMessage);

            SubscriptionType subscriptionType = new SubscriptionType();
            subscriptionType.setSubscription(subscriptionData);

            Subscription subscription = new Subscription();
            //subscription.setDatexSubscribeSerialNbr(new BerInteger(subscribeSerialNbr));
            subscription.setDatexSubscribeSerialNbr(new BerInteger(subscribeSerialNbr));
            subscription.setDatexSubscribeType(subscriptionType);

            PDUs pdus = new PDUs();
            pdus.setSubscription(subscription);
            byte[] auth = { (byte) eAuthInfo.AI_Subscription.getValue() };
            C2CAuthenticatedMessage c2c = new C2CAuthenticatedMessage();
            c2c.setDatexAuthenticationInfoText(new BerOctetString(auth));
            c2c.setDatexDataPacketNumber(new BerInteger(dataPacketNumber));
            c2c.setDatexDataPacketPriorityNumber(new BerInteger(dataPacketPriorityNumber));
            c2c.setOptions(new HeaderOptions());
            c2c.setPdu(pdus);

            log.info("{}", c2c);
        }
        catch(Exception e) {
            log.error("LoginDeviceService.requestObuGatherInfo: {}, {}, Exception: {}", obj.getID(), obj.getRSE_ID(), e.toString());
        }
    }

    @Test
    void packetEncoding() {
        TbRseCtlr obj = AppRepository.getInstance().getCtlrMap().get("1001");
        List<String> lists = new ArrayList<>();
//        lists.add("3068800101815f305d800102810100820100a31ba719800207e681010c82010983010a84011f85010fa6048202013aa435a133800c474a41544d53534552564552810d50544d4355303030303030303182008300a40306015185013c86011e870101880202408202e773");
//        lists.add("3081ce8001018181c43081c1800106810101820100a300a481b3a581b0800100a181aaa081a7800100810100a24ba149a047800100a120800207e681010c82010983010a84011f85010fa603800100a706800109810100a2208002083381010c82010983010a84011f85010fa603800100a70680010981010083010384010a8501ffa649800628fb28080008a13f303d303b80200000000000000000000000000000000000000000000000000000000000000000a10680010081010082010083010084040000000085030000008202694d");
//        lists.add("30819480010181818a308187800106810102820100a300a47aa578800101a173a071800100810100a24ba249a04780011ea120800207e681010c82010983010a84011f85010fa603800100a706800109810100a2208002083381010c82010983010a84011f85010fa603800100a70680010981010083010384010a8501ffa613800628fb28040007a1093007300580010381008202842f");
//        lists.add("302180010181183016800108810103820100a300a409a807800108a102810082028dc0");
//        lists.add("3082012b800101818201203082011c800106810104820100a300a482010da5820109800102a1820102a081ff800100810100a24ba249a04780011ea120800207e681010c82010983010a84011f85010fa603800100a706800109810100a2208002083381010c82010983010a84011f85010fa603800100a70680010981010083010384010a8501ffa681a0800628fb28040007a1819530819230818f800102818189308186800d50544d43553030303030303031810166a3723070800102810101820e3230323231323039313033313135a30a80020105810401101001a44c3011800101a10c300a800201058104011010053011800102a10c300a800201058104011010023011800102a10c300a800201058104011010033011800103a10c300a80020105810401101023820279e1");
//        lists.add("303c80010181333031800140810105820100a300a424a6228001ffa11da01b3019800101810100820100a30ea10c800628fb28050001a10230008202e944");    // error
//        lists.add("302180010181183016800108810106820100a300a409a807800105a102830082027dff");
//        lists.add("302180010181183016800108810107820100a300a409a807800106a10283008202edea");
//        lists.add("302180010181183016800108810108820100a300a409a807800107a10283008202dee2");
//        lists.add("302180010181183016800108810109820100a300a409a807800108a102830082024fe7");
//        lists.add("30218001018118301680010881010a820100a300a409a807800109a102830082027f2a");
//        lists.add("30218001018118301680010881010b820100a300a409a80780010aa10283008202ef3f");
//        lists.add("30218001018118301680010881010c820100a300a409a80780010ba102830082021eb0");
//        lists.add("30218001018118301680010881010d820100a300a409a80780010ca102830082024e54");
//        lists.add("30218001018118301680010881010e820100a300a409a80780010da102830082027e99");
//        lists.add("30218001018118301680010881010f820100a300a409a80780010ea10283008202ee8c");
//        lists.add("302180010181183016800108810110820100a300a409a80780010fa102830082021889");
//        lists.add("302180010181183016800108810111820100a300a409a807800110a102830082024a4d");
//        lists.add("302180010181183016800108810112820100a300a409a807800111a102830082027a80");
//        lists.add("302180010181183016800108810113820100a300a409a807800112a10283008202ea95");
//        lists.add("302180010181183016800108810114820100a300a409a807800113a102830082021b1a");
//        lists.add("302180010181183016800108810115820100a300a409a807800114a102830082024bfe");
//        lists.add("302180010181183016800108810116820100a300a409a807800115a102830082027b33");
//        lists.add("302180010181183016800108810117820100a300a409a807800116a10283008202eb26");
//        lists.add("302180010181183016800108810118820100a300a409a807800117a10283008202d82e");
//        lists.add("302180010181183016800108810119820100a300a409a807800118a10283008202492b");
//        lists.add("30218001018118301680010881011a820100a300a409a807800119a1028300820279e6");
//        lists.add("30218001018118301680010881011b820100a300a409a80780011aa10283008202e9f3");
//        lists.add("30218001018118301680010881011c820100a300a409a80780011ba10283008202187c");
//        lists.add("3081c98001018181bf3081bc80014081011d820100a300a481aea681ab8001ffa181a5a081a230819f800101810101820100a38193a18190800628fb28050001a181853081823043800101810e3230323231323039313033323135a200830101a4298027534bebb7b0ec9584ed8c8ced8ab8ec959eec82bceab1b0eba6ac2035ebb68420ec868cec9a942e303b800102810e3230323231323039313033323135a200830101a421801feab5b0ebacb8eab590ec82bceab1b0eba6ac2032ebb68420ec868cec9a942e820269f5");
//        lists.add("30218001018118301680010881011e820100a300a409a80780011da102830082027855");
//        lists.add("3037800101812e302c80010181010182010aa300a41fa01d800d50544d43553030303030303032810c505441544d535345525645528202ea54");
//        lists.add("3023800101811a301880010881010082010aa300a40ba809800102a10480025101820205e0");
//        lists.add("3025800101811c301a80010881010182010aa300a40da80b800102a10682041012f5708202f990");
        lists.add("3081a780010181819d30819a80010181010882010aa300a4818ca58189800101a18183a08180800100810100a229a227a02580013ca120800207e681010c82010983010e840121850108a603820100a70680010981010083010384010a8501ffa644800628fb28050001a13a3038303680016f81056655335f62a2150c01410c01350c016d0c014f0c016b0c014d0c0170830108a410a10e80010081075fa80468cf411682008202375d");
//        lists.add("3025800101811c301a80010881010282010aa300a40da80b800102a10682041012f5708202caa3");
//        lists.add("3025800101811c301a80010881010382010aa300a40da80b800102a10682041012f57082021bb3");
//        lists.add("30218001018118301680010881010482010aa300a409a807800102a10283008202d7e1");
//        lists.add("307d8001018174307280010181010582010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203461396336343931626130336133356666643135613064633735356237363832a10a8002010581040110100282010183010184042022120985031433088202a22a");
//        lists.add("307d8001018174307280010181010682010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203237313130643834623235383062633535376231656432663036666263323065a10a80020105810401101002820101830101840420221209850314330882023b86");
//        lists.add("307d8001018174307280010181010782010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203963636634346535376362336566373835363333306639663234663861633365a10a80020105810401101002820101830101840420221209850314330882022aa5");
//        lists.add("307d8001018174307280010181010882010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203835303535376531623661393933306366376566336639643962356135353836a10a800201058104011010028201068301018404202212098503143308820282db");
//        lists.add("307d8001018174307280010181010982010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80206535343832626230656330653239656263333561626264646433616662303566a10a80020105810401101002820103830101840420221209850314330882021b5a");
//        lists.add("307d8001018174307280010181010a82010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203964303462373136636266383634663661623366366639353130653831653135a10a8002010581040110100282010383010184042022120985031433088202ab27");
//        lists.add("307d8001018174307280010181010b82010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203564363233666334663261383838363530346437323537393933613564306230a10a80020105810401101002820101830101840420221209850314330982025341");
//        lists.add("307d8001018174307280010181010c82010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80206265623031363838383532326361356265393337313834393461643563326338a10a80020105810401101002820101830101840420221209850314330982024957");
//        lists.add("307d8001018174307280010181010d82010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203664333861636435616438633262373137646436616261616363636537666130a10a8002010581040110100282010183010184042022120985031433098202bbb2");
//        lists.add("307d8001018174307280010181010e82010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203930653435373432303865643062613739366630363233626431333035636231a10a80020105810401101002820101830101840420221209850314331182029d41");
//        lists.add("307d8001018174307280010181010f82010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203432666331333237383736666461376435343763666564316230616637306534a10a8002010581040110100282010183010184042022120985031433118202ffa9");
//        lists.add("307d8001018174307280010181011082010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203037333037366463316663316138356135656439303763613533303161646339a10a8002010581040110100282010183010184042022120985031433128202ce89");
//        lists.add("307d8001018174307280010181011182010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80206135623563333135643961616438643761613336313733623464373230376433a10a80020105810401101002820101830101840420221209850314331382029fc8");
//        lists.add("307d8001018174307280010181011282010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203661393531616434346336366533616130636363316166656135643138623561a10a80020105810401101002820101830101840420221209850314331682025e6d");
//        lists.add("307d8001018174307280010181011382010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80206664383162626533336337633830653136373331633461346263626665333863a10a80020105810401101002820101830101840420221209850314332282023249");
//        lists.add("307d8001018174307280010181011482010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80206266303633306166326262363438323961376661363533396237626535353235a10a800201058104011010028201018301018404202212098503143325820270d7");
//        lists.add("307d8001018174307280010181011582010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80203562343731356562333264303432636336643262343534656564363666623666a10a8002010581040110100282010183010184042022120985031433268202f8ac");
//        lists.add("307d8001018174307280010181011682010aa300a465a6638001ffa15ea05c305a8001648101648201ffa34fa14d800628fb28080008a1433041303f80206462393331613834326164646265653336303439363939323566343332316266a10a80020105810401101002820101830101840420221209850314333882023396");
//        lists.add("30798001018170306e80010181011782010aa300a461a65f8001ffa15aa05830568001648101648201ffa34ba149800628fb28040007a13f303d303b80010381363034800d474a4d4355303030303030303281040a0a010ca31d301ba00a8002010581040110100281010082040185c80a83010184016682021eaa");

        for (String data : lists) {
            byte[] inBytes = ByteUtils.hexToByteArray(data);
            DatexDataPacket datexDataPacket = new DatexDataPacket();
            try {
                datexDataPacket.decode(new ByteArrayInputStream(inBytes, 0, inBytes.length));
                BerOctetString berOctetString = datexDataPacket.getDatexData();
                C2CAuthenticatedMessage c2c = new C2CAuthenticatedMessage();
                c2c.decode(new ByteArrayInputStream(berOctetString.value, 0, berOctetString.value.length));
                log.info("{}", c2c);

                String ipAddress = "127.0.0.1";
                Subscription subscription = c2c.getPdu().getSubscription();
                if (subscription != null) {
                    Registered registered = null;
                    int subscribeSerialNbr = subscription.getDatexSubscribeSerialNbr().value.intValue();
                    SubscriptionData subscriptionData = subscription.getDatexSubscribeType().getSubscription();

                    if (subscriptionData.getDatexSubscribeMode().getEventDriven() != null) {
                        registered = subscriptionData.getDatexSubscribeMode().getEventDriven();
                        log.info("SubscriptionRegisterService.response: {}. EventDriven. subscribeSerialNbr: {}", ipAddress, subscribeSerialNbr);
                    }
                    else if (subscriptionData.getDatexSubscribeMode().getPeriodic() != null) {
                        registered = subscriptionData.getDatexSubscribeMode().getPeriodic();
                        log.info("SubscriptionRegisterService.response: {}. Periodic. subscribeSerialNbr: {}", ipAddress, subscribeSerialNbr);
                    }
                    int objectId = DsrcAsn1Utils.getObjectId(subscriptionData.getDatexSubscribePdu().getEndApplicationMessageId().value);
                    log.error("{}", objectId);
                    log.error("{}", SysUtils.byteArrayToHex(subscriptionData.getDatexSubscribePdu().getEndApplicationMessageMsg().value));

                    if (registered != null) {
                        //log.error("{}", registered);
                        ByteArrayInputStream berInputStream = new ByteArrayInputStream(subscriptionData.getDatexSubscribePdu().getEndApplicationMessageMsg().value);
                        MultiMediaDataList dataOrg = new MultiMediaDataList();
                        dataOrg.decode(berInputStream);

//                        ControlDeviceList devices = new ControlDeviceList();
//                        ControlDevice device = new ControlDevice();
                        log.info("xxxxxxxxxxxxxxxxxxxxx");
                        //device.decode(berInputStream);
                        log.info("{}", dataOrg);
                        List<MultiMediaData> dataLists = dataOrg.getMultiMediaData();
                        for (MultiMediaData media : dataLists) {
                            log.error("{}", media);
                        }
//                        for (ControlDevice controlDevice : list) {
//                            if (controlDevice.getVpbdControlDeviceData() != null) {
//                                eControlDevice e = eControlDevice.getByValue(controlDevice.getVpbdControlID().value[0]);
//                                log.info("SubscriptionRegisterService.response: {}", e.toString());
//                                switch(e) {
//                                    case Control_Command:        //(0x01, "Control_Command"),
//                                        break;
//                                    case Control_BasicInfo:      //(0x02, "Control_BasicInfo"),
//                                        break;
//                                    case Control_StatusRequest:  //(0x03, "Control_StatusRequest"),
//                                        ControlDeviceService.getInstance().decoding_ControlDevice_Status_ITTelecom(obj, controlDevice);
//                                        break;
//                                    case Control_InfoRequest:    //(0x04, "Control_InfoRequest"),
//                                        DSRCTrafficInfoRequest dsrcTrafficInfoRequest = new DSRCTrafficInfoRequest();
//                                        ByteArrayInputStream deviceStream = new ByteArrayInputStream(controlDevice.getVpbdControlDeviceData().value);
//                                        dsrcTrafficInfoRequest.decode(deviceStream);
//                                        //log.info("SubscriptionRegisterService.response: {}", dsrcTrafficInfoRequest.toString());
//                                        break;
//                                    case Control_EnterDirInfo:   //(0x05, "Control_EnterDirInfo"),
//                                        break;
//                                    case Control_DisconnectInfo: //(0x06, "Control_DisconnectInfo");
//                                        break;
//                                }
//                            }
//                        }
                    }
                }

            } catch (Exception e) {
                log.error("Exception: {}, {}", e, data);
            }
        }
    }
}
