/*jshint expr: true*/
var chai = require('chai');
var expect = chai.expect;
var mock = require('mock-require');

describe('test SerialRequestResponseTransport with D0Protocol With Ack2', function() {


    it('check output of two D0 messages', function(done){
        this.timeout(600000); // because of first install from npm

        mock('serialport', 'virtual-serialport');

        var SmartmeterObis = require('../index.js');

        var options = {
            'protocol': 'D0Protocol',
            'transport': 'SerialRequestResponseTransport',
            'transportSerialPort': '/dev/ir-usb1',
            'transportSerialBaudrate': 300,
            'protocolD0WakeupCharacters': 40,
            'protocolD0DeviceAddress': 'Bla0',
            'requestInterval': 10,
            'transportHttpRequestUrl': '',
            'debug': 0
        };

        var lastObisResult;
        var counter = 0;
        var errCounter = 0;

        function testStoreData(err, obisResult) {
            if (err) {
                expect(obisResult).to.be.null;
                errCounter++;
                console.log('ERROR: ' + err);
                return;
            }
            // nothing to do in this case because protocol is stateless
            expect(obisResult).to.be.an('object');
            expect(obisResult['1-0:1.8.1*7']).to.be.an('object');
            expect(obisResult['1-0:1.8.1*7'].rawValue).to.be.equal('0006334.294*kWh');
            expect(obisResult['1-0:1.8.1*7'].values.length).to.be.equal(1);
            expect(obisResult['1-0:1.8.1*7'].values[0].value).to.be.equal(6334.294);
            expect(obisResult['1-0:1.6.0*255']).to.be.an('object');
            expect(obisResult['1-0:1.6.0*255'].rawValue).to.be.equal('02.468*kW, 1703100930');
            expect(obisResult['1-0:1.6.0*255'].values.length).to.be.equal(2);
            expect(obisResult['1-0:1.6.0*255'].values[0].value).to.be.equal(2.468);
            expect(obisResult['1-0:1.6.0*255'].values[0].unit).to.be.equal('kW');
            expect(obisResult['1-0:1.6.0*255'].values[1].value).to.be.equal(1703100930);

            console.log('Received data ' + counter + ': ' + Object.keys(obisResult));
            if (!lastObisResult) {
                expect(counter).to.be.equal(0);
            }
            else {
                expect(counter).to.be.equal(1);
                expect(JSON.stringify(lastObisResult)).to.be.equal(JSON.stringify(obisResult));
            }

            //console.log(JSON.stringify(obisResult,null,2));
            lastObisResult = obisResult;
            counter++;
        }

        var smTransport = SmartmeterObis.init(options, testStoreData);

        var endTimer = null;
        smTransport.serialComm.on('dataToDevice', function(data) {
            var testData;
            //console.log('RECEIVED ' + data.length + ': ' + JSON.stringify(data));
            if (data === '\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\0\0\0\0\0\0\0\0') {
                //console.log('WAKE UP DONE');
            }
            else if (data === '/?Bla0!\r\n') {
                //console.log('SEND Identification Message');
                testData = Buffer.from('/ISk5MT174-0001\r\n');
                smTransport.serialComm.writeToComputer(testData);
            }
            else if (data === '\u0006050\r\n') {
                //console.log('SEND Data Message');
                testData = Buffer.from('\u0006050\r\n\u00021-0:0.9.1*255(201455)\r\n1-0:0.9.2*255(0170318)\r\n1-0:0.0.0*255(63355730)\r\n1-0:0.0.1*255(1ISK0063355730)\r\n1-0:0.1.0*255(33)\r\n1-0:0.2.0*255(1.03)\r\n0-0:C.1.0*255(63355730)\r\n0-0:C.1.6*255(FDF5)\r\n0-0:C.51.1*255(3)\r\n0-0:C.51.2*255(1140716101053)\r\n0-0:C.51.2*01(1140716101053)\r\n0-0:C.51.2*02(0131211111412)\r\n0-0:C.51.2*03(0131211111407)\r\n1-0:1.6.0*255(02.468*kW)(1703100930)\r\n1-0:1.7.0*255(0.908*kW)\r\n1-0:1.8.0*255(0008048.375*kWh)\r\n1-0:1.8.1*255(0008048.375*kWh)\r\n1-0:1.8.2*255(0000000.000*kWh)\r\n1-0:1.8.3*255(0000000.000*kWh)\r\n1-0:1.8.4*255(0000000.000*kWh)\r\n1-0:2.6.0*255(00.000*kW)(1703182015)\r\n1-0:2.7.0*255(0.000*kW)\r\n1-0:2.8.0*255(0000000.000*kWh)\r\n1-0:2.8.1*255(0000000.000*kWh)\r\n1-0:2.8.2*255(0000000.000*kWh)\r\n1-0:2.8.3*255(0000000.000*kWh)\r\n1-0:2.8.4*255(0000000.000*kWh)\r\n0-0:F.F.0*255(0000000)\r\n1-0:0.1.2*01(1703010000)\r\n1-0:0.1.2*02(1702010000)\r\n1-0:0.1.2*03(1701010000)\r\n1-0:0.1.2*04(1612010000)\r\n1-0:0.1.2*05(1611010000)\r\n1-0:0.1.2*06(1610010000)\r\n1-0:0.1.2*07(1609010000)\r\n1-0:0.1.2*08(1608010000)\r\n1-0:0.1.2*09(1607010000)\r\n1-0:0.1.2*10(1606010000)\r\n1-0:0.1.2*11(1605010000)\r\n1-0:0.1.2*12(1604010000)\r\n1-0:0.1.2*13(1603010000)\r\n1-0:0.1.2*14(1602010000)\r\n1-0:0.1.2*15(1601010000)\r\n1-0:1.6.0*01(03.258*kW)(1702102000)\r\n1-0:1.6.0*02(03.308*kW)(1701261830)\r\n1-0:1.6.0*03(02.838*kW)(1612232045)\r\n1-0:1.6.0*04(03.398*kW)(1611161800)\r\n1-0:1.6.0*05(02.964*kW)(1610132000)\r\n1-0:1.6.0*06(03.356*kW)(1609131845)\r\n1-0:1.6.0*07(03.888*kW)(1608051945)\r\n1-0:1.6.0*08(03.102*kW)(1607272045)\r\n1-0:1.6.0*09(02.844*kW)(1606152000)\r\n1-0:1.6.0*10(03.850*kW)(1605172115)\r\n1-0:1.6.0*11(03.756*kW)(1604021130)\r\n1-0:1.6.0*12(04.190*kW)(1603022015)\r\n1-0:1.6.0*13(03.764*kW)(1602101845)\r\n1-0:1.6.0*14(03.768*kW)(1601061730)\r\n1-0:1.6.0*15(03.806*kW)(1512162045)\r\n1-0:1.8.0*01(0007904.135*kWh)\r\n1-0:1.8.0*02(0007660.326*kWh)\r\n1-0:1.8.0*03(0007371.517*kWh)\r\n1-0:1.8.0*04(0007104.603*kWh)\r\n1-0:1.8.0*05(0006820.722*kWh)\r\n1-0:1.8.0*06(0006581.536*kWh)\r\n1-0:1.8.0*07(0006334.294*kWh)\r\n1-0:1.8.0*08(0006097.335*kWh)\r\n1-0:1.8.0*09(0005862.843*kWh)\r\n1-0:1.8.0*10(0005625.115*kWh)\r\n1-0:1.8.0*11(0005358.178*kWh)\r\n1-0:1.8.0*12(0005096.931*kWh)\r\n1-0:1.8.0*13(0004800.953*kWh)\r\n1-0:1.8.0*14(0004517.795*kWh)\r\n1-0:1.8.0*15(0004224.392*kWh)\r\n1-0:1.6.1*01(03.258*kW)(1702102000)\r\n1-0:1.6.1*02(03.308*kW)(1701261830)\r\n1-0:1.6.1*03(02.838*kW)(1612232045)\r\n1-0:1.6.1*04(03.398*kW)(1611161800)\r\n1-0:1.6.1*05(02.964*kW)(1610132000)\r\n1-0:1.6.1*06(03.356*kW)(1609131845)\r\n1-0:1.6.1*07(03.888*kW)(1608051945)\r\n1-0:1.6.1*08(03.102*kW)(1607272045)\r\n1-0:1.6.1*09(02.844*kW)(1606152000)\r\n1-0:1.6.1*10(03.850*kW)(1605172115)\r\n1-0:1.6.1*11(03.756*kW)(1604021130)\r\n1-0:1.6.1*12(04.190*kW)(1603022015)\r\n1-0:1.6.1*13(03.764*kW)(1602101845)\r\n1-0:1.6.1*14(03.768*kW)(1601061730)\r\n1-0:1.6.1*15(03.806*kW)(1512162045)\r\n1-0:1.6.2*01()\r\n1-0:1.6.2*02()\r\n1-0:1.6.2*03()\r\n1-0:1.6.2*04()\r\n1-0:1.6.2*05()\r\n1-0:1.6.2*06()\r\n1-0:1.6.2*07()\r\n1-0:1.6.2*08()\r\n1-0:1.6.2*09()\r\n1-0:1.6.2*10()\r\n1-0:1.6.2*11()\r\n1-0:1.6.2*12()\r\n1-0:1.6.2*13()\r\n1-0:1.6.2*14()\r\n1-0:1.6.2*15()\r\n1-0:1.6.3*01()\r\n1-0:1.6.3*02()\r\n1-0:1.6.3*03()\r\n1-0:1.6.3*04()\r\n1-0:1.6.3*05()\r\n1-0:1.6.3*06()\r\n1-0:1.6.3*07()\r\n1-0:1.6.3*08()\r\n1-0:1.6.3*09()\r\n1-0:1.6.3*10()\r\n1-0:1.6.3*11()\r\n1-0:1.6.3*12()\r\n1-0:1.6.3*13()\r\n1-0:1.6.3*14()\r\n1-0:1.6.3*15()\r\n1-0:1.6.4*01()\r\n1-0:1.6.4*02()\r\n1-0:1.6.4*03()\r\n1-0:1.6.4*04()\r\n1-0:1.6.4*05()\r\n1-0:1.6.4*06()\r\n1-0:1.6.4*07()\r\n1-0:1.6.4*08()\r\n1-0:1.6.4*09()\r\n1-0:1.6.4*10()\r\n1-0:1.6.4*11()\r\n1-0:1.6.4*12()\r\n1-0:1.6.4*13()\r\n1-0:1.6.4*14()\r\n1-0:1.6.4*15()\r\n1-0:1.8.1*01(0007904.135*kWh)\r\n1-0:1.8.1*02(0007660.326*kWh)\r\n1-0:1.8.1*03(0007371.517*kWh)\r\n1-0:1.8.1*04(0007104.603*kWh)\r\n1-0:1.8.1*05(0006820.722*kWh)\r\n1-0:1.8.1*06(0006581.536*kWh)\r\n1-0:1.8.1*07(0006334.294*kWh)\r\n1-0:1.8.1*08(0006097.335*kWh)\r\n1-0:1.8.1*09(0005862.843*kWh)\r\n1-0:1.8.1*10(0005625.115*kWh)\r\n1-0:1.8.1*11(0005358.178*kWh)\r\n1-0:1.8.1*12(0005096.931*kWh)\r\n1-0:1.8.1*13(0004800.953*kWh)\r\n1-0:1.8.1*14(0004517.795*kWh)\r\n1-0:1.8.1*15(0004224.392*kWh)\r\n1-0:1.8.2*01(0000000.000*kWh)\r\n1-0:1.8.2*02(0000000.000*kWh)\r\n1-0:1.8.2*03(0000000.000*kWh)\r\n1-0:1.8.2*04(0000000.000*kWh)\r\n1-0:1.8.2*05(0000000.000*kWh)\r\n1-0:1.8.2*06(0000000.000*kWh)\r\n1-0:1.8.2*07(0000000.000*kWh)\r\n1-0:1.8.2*08(0000000.000*kWh)\r\n1-0:1.8.2*09(0000000.000*kWh)\r\n1-0:1.8.2*10(0000000.000*kWh)\r\n1-0:1.8.2*11(0000000.000*kWh)\r\n1-0:1.8.2*12(0000000.000*kWh)\r\n1-0:1.8.2*13(0000000.000*kWh)\r\n1-0:1.8.2*14(0000000.000*kWh)\r\n1-0:1.8.2*15(0000000.000*kWh)\r\n1-0:1.8.3*01(0000000.000*kWh)\r\n1-0:1.8.3*02(0000000.000*kWh)\r\n1-0:1.8.3*03(0000000.000*kWh)\r\n1-0:1.8.3*04(0000000.000*kWh)\r\n1-0:1.8.3*05(0000000.000*kWh)\r\n1-0:1.8.3*06(0000000.000*kWh)\r\n1-0:1.8.3*07(0000000.000*kWh)\r\n1-0:1.8.3*08(0000000.000*kWh)\r\n1-0:1.8.3*09(0000000.000*kWh)\r\n1-0:1.8.3*10(0000000.000*kWh)\r\n1-0:1.8.3*11(0000000.000*kWh)\r\n1-0:1.8.3*12(0000000.000*kWh)\r\n1-0:1.8.3*13(0000000.000*kWh)\r\n1-0:1.8.3*14(0000000.000*kWh)\r\n1-0:1.8.3*15(0000000.000*kWh)\r\n1-0:1.8.4*01(0000000.000*kWh)\r\n1-0:1.8.4*02(0000000.000*kWh)\r\n1-0:1.8.4*03(0000000.000*kWh)\r\n1-0:1.8.4*04(0000000.000*kWh)\r\n1-0:1.8.4*05(0000000.000*kWh)\r\n1-0:1.8.4*06(0000000.000*kWh)\r\n1-0:1.8.4*07(0000000.000*kWh)\r\n1-0:1.8.4*08(0000000.000*kWh)\r\n1-0:1.8.4*09(0000000.000*kWh)\r\n1-0:1.8.4*10(0000000.000*kWh)\r\n1-0:1.8.4*11(0000000.000*kWh)\r\n1-0:1.8.4*12(0000000.000*kWh)\r\n1-0:1.8.4*13(0000000.000*kWh)\r\n1-0:1.8.4*14(0000000.000*kWh)\r\n1-0:1.8.4*15(0000000.000*kWh)\r\n1-0:2.6.0*01(00.000*kW)(1703010000)\r\n1-0:2.6.0*02(00.000*kW)(1702010000)\r\n1-0:2.6.0*03(00.000*kW)(1701010000)\r\n1-0:2.6.0*04(00.000*kW)(1612010000)\r\n1-0:2.6.0*05(00.000*kW)(1611010000)\r\n1-0:2.6.0*06(00.000*kW)(1610010000)\r\n1-0:2.6.0*07(00.000*kW)(1609010000)\r\n1-0:2.6.0*08(00.000*kW)(1608010000)\r\n1-0:2.6.0*09(00.000*kW)(1607010000)\r\n1-0:2.6.0*10(00.000*kW)(1606010000)\r\n1-0:2.6.0*11(00.000*kW)(1605010000)\r\n1-0:2.6.0*12(00.000*kW)(1604010000)\r\n1-0:2.6.0*13(00.000*kW)(1603010000)\r\n1-0:2.6.0*14(00.000*kW)(1602010000)\r\n1-0:2.6.0*15(00.000*kW)(1601010000)\r\n1-0:2.8.0*01(0000000.000*kWh)\r\n1-0:2.8.0*02(0000000.000*kWh)\r\n1-0:2.8.0*03(0000000.000*kWh)\r\n1-0:2.8.0*04(0000000.000*kWh)\r\n1-0:2.8.0*05(0000000.000*kWh)\r\n1-0:2.8.0*06(0000000.000*kWh)\r\n1-0:2.8.0*07(0000000.000*kWh)\r\n1-0:2.8.0*08(0000000.000*kWh)\r\n1-0:2.8.0*09(0000000.000*kWh)\r\n1-0:2.8.0*10(0000000.000*kWh)\r\n1-0:2.8.0*11(0000000.000*kWh)\r\n1-0:2.8.0*12(0000000.000*kWh)\r\n1-0:2.8.0*13(0000000.000*kWh)\r\n1-0:2.8.0*14(0000000.000*kWh)\r\n1-0:2.8.0*15(0000000.000*kWh)\r\n1-0:2.6.1*01(00.000*kW)(1703010000)\r\n1-0:2.6.1*02(00.000*kW)(1702010000)\r\n1-0:2.6.1*03(00.000*kW)(1701010000)\r\n1-0:2.6.1*04(00.000*kW)(1612010000)\r\n1-0:2.6.1*05(00.000*kW)(1611010000)\r\n1-0:2.6.1*06(00.000*kW)(1610010000)\r\n1-0:2.6.1*07(00.000*kW)(1609010000)\r\n1-0:2.6.1*08(00.000*kW)(1608010000)\r\n1-0:2.6.1*09(00.000*kW)(1607010000)\r\n1-0:2.6.1*10(00.000*kW)(1606010000)\r\n1-0:2.6.1*11(00.000*kW)(1605010000)\r\n1-0:2.6.1*12(00.000*kW)(1604010000)\r\n1-0:2.6.1*13(00.000*kW)(1603010000)\r\n1-0:2.6.1*14(00.000*kW)(1602010000)\r\n1-0:2.6.1*15(00.000*kW)(1601010000)\r\n1-0:2.6.2*01()\r\n1-0:2.6.2*02()\r\n1-0:2.6.2*03()\r\n1-0:2.6.2*04()\r\n1-0:2.6.2*05()\r\n1-0:2.6.2*06()\r\n1-0:2.6.2*07()\r\n1-0:2.6.2*08()\r\n1-0:2.6.2*09()\r\n1-0:2.6.2*10()\r\n1-0:2.6.2*11()\r\n1-0:2.6.2*12()\r\n1-0:2.6.2*13()\r\n1-0:2.6.2*14()\r\n1-0:2.6.2*15()\r\n1-0:2.6.3*01()\r\n1-0:2.6.3*02()\r\n1-0:2.6.3*03()\r\n1-0:2.6.3*04()\r\n1-0:2.6.3*05()\r\n1-0:2.6.3*06()\r\n1-0:2.6.3*07()\r\n1-0:2.6.3*08()\r\n1-0:2.6.3*09()\r\n1-0:2.6.3*10()\r\n1-0:2.6.3*11()\r\n1-0:2.6.3*12()\r\n1-0:2.6.3*13()\r\n1-0:2.6.3*14()\r\n1-0:2.6.3*15()\r\n1-0:2.6.4*01()\r\n1-0:2.6.4*02()\r\n1-0:2.6.4*03()\r\n1-0:2.6.4*04()\r\n1-0:2.6.4*05()\r\n1-0:2.6.4*06()\r\n1-0:2.6.4*07()\r\n1-0:2.6.4*08()\r\n1-0:2.6.4*09()\r\n1-0:2.6.4*10()\r\n1-0:2.6.4*11()\r\n1-0:2.6.4*12()\r\n1-0:2.6.4*13()\r\n1-0:2.6.4*14()\r\n1-0:2.6.4*15()\r\n1-0:2.8.1*01(0000000.000*kWh)\r\n1-0:2.8.1*02(0000000.000*kWh)\r\n1-0:2.8.1*03(0000000.000*kWh)\r\n1-0:2.8.1*04(0000000.000*kWh)\r\n1-0:2.8.1*05(0000000.000*kWh)\r\n1-0:2.8.1*06(0000000.000*kWh)\r\n1-0:2.8.1*07(0000000.000*kWh)\r\n1-0:2.8.1*08(0000000.000*kWh)\r\n1-0:2.8.1*09(0000000.000*kWh)\r\n1-0:2.8.1*10(0000000.000*kWh)\r\n1-0:2.8.1*11(0000000.000*kWh)\r\n1-0:2.8.1*12(0000000.000*kWh)\r\n1-0:2.8.1*13(0000000.000*kWh)\r\n1-0:2.8.1*14(0000000.000*kWh)\r\n1-0:2.8.1*15(0000000.000*kWh)\r\n1-0:2.8.2*01(0000000.000*kWh)\r\n1-0:2.8.2*02(0000000.000*kWh)\r\n1-0:2.8.2*03(0000000.000*kWh)\r\n1-0:2.8.2*04(0000000.000*kWh)\r\n1-0:2.8.2*05(0000000.000*kWh)\r\n1-0:2.8.2*06(0000000.000*kWh)\r\n1-0:2.8.2*07(0000000.000*kWh)\r\n1-0:2.8.2*08(0000000.000*kWh)\r\n1-0:2.8.2*09(0000000.000*kWh)\r\n1-0:2.8.2*10(0000000.000*kWh)\r\n1-0:2.8.2*11(0000000.000*kWh)\r\n1-0:2.8.2*12(0000000.000*kWh)\r\n1-0:2.8.2*13(0000000.000*kWh)\r\n1-0:2.8.2*14(0000000.000*kWh)\r\n1-0:2.8.2*15(0000000.000*kWh)\r\n1-0:2.8.3*01(0000000.000*kWh)\r\n1-0:2.8.3*02(0000000.000*kWh)\r\n1-0:2.8.3*03(0000000.000*kWh)\r\n1-0:2.8.3*04(0000000.000*kWh)\r\n1-0:2.8.3*05(0000000.000*kWh)\r\n1-0:2.8.3*06(0000000.000*kWh)\r\n1-0:2.8.3*07(0000000.000*kWh)\r\n1-0:2.8.3*08(0000000.000*kWh)\r\n1-0:2.8.3*09(0000000.000*kWh)\r\n1-0:2.8.3*10(0000000.000*kWh)\r\n1-0:2.8.3*11(0000000.000*kWh)\r\n1-0:2.8.3*12(0000000.000*kWh)\r\n1-0:2.8.3*13(0000000.000*kWh)\r\n1-0:2.8.3*14(0000000.000*kWh)\r\n1-0:2.8.3*15(0000000.000*kWh)\r\n1-0:2.8.4*01(0000000.000*kWh)\r\n1-0:2.8.4*02(0000000.000*kWh)\r\n1-0:2.8.4*03(0000000.000*kWh)\r\n1-0:2.8.4*04(0000000.000*kWh)\r\n1-0:2.8.4*05(0000000.000*kWh)\r\n1-0:2.8.4*06(0000000.000*kWh)\r\n1-0:2.8.4*07(0000000.000*kWh)\r\n1-0:2.8.4*08(0000000.000*kWh)\r\n1-0:2.8.4*09(0000000.000*kWh)\r\n1-0:2.8.4*10(0000000.000*kWh)\r\n1-0:2.8.4*11(0000000.000*kWh)\r\n1-0:2.8.4*12(0000000.000*kWh)\r\n1-0:2.8.4*13(0000000.000*kWh)\r\n1-0:2.8.4*14(0000000.000*kWh)\r\n1-0:2.8.4*15(0000000.000*kWh)\r\n!\r\n\u0003f');
                smTransport.serialComm.writeToComputer(testData);

                if (!endTimer) {
                    endTimer = setTimeout(function() {
                        expect(smTransport.stopRequests).to.be.false;
                        smTransport.stop(function() {
                            expect(counter).to.be.equal(2);
                            expect(errCounter).to.be.equal(0);
                            expect(smTransport.protocol.deviceManufacturer).to.be.equal('ISk');
                            expect(smTransport.protocol.commBaudrateChangeover).to.be.equal(9600);
                            setTimeout(function() {
                                expect(smTransport.serialConnected).to.be.false;
                                done();
                            }, 100);
                        });
                    }, 12000);
                }
            }
        });

        smTransport.process();

    });
});
