import { AbstractService, extendServiceContainer } from '@iotize/tap';
import { TapStreamReader, TapStreamWriter, enumKeyOrValueToNumber } from '@iotize/tap/client/impl';
import { tapnpassConverters } from '@iotize/tap/service/impl/tapnpass';
import { targetConverters } from '@iotize/tap/service/impl/target';
import { TapRequestFrame } from '@iotize/tap/client/api';
import { converters as converters$1 } from '@iotize/tap/service/core';

var SlaveServerRawConfig;
(function (SlaveServerRawConfig) {
    /* eslint-disable */
    /**
     * This file was automatically generated by json-schema-to-typescript.
     * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
     * and run json-schema-to-typescript to regenerate this file.
     */
    let Channel;
    (function (Channel) {
        Channel[Channel["DISABLED"] = 0] = "DISABLED";
        Channel[Channel["MODBUS_RTU"] = 1] = "MODBUS_RTU";
        Channel[Channel["SPI"] = 2] = "SPI";
        Channel[Channel["CAN"] = 3] = "CAN";
        Channel[Channel["MODBUS_TCP"] = 4] = "MODBUS_TCP";
    })(Channel = SlaveServerRawConfig.Channel || (SlaveServerRawConfig.Channel = {}));
})(SlaveServerRawConfig || (SlaveServerRawConfig = {}));

/**
 *   Generated file. Do not edit
 */
TapStreamReader.prototype.readSlaveServerRawConfig =
    function () {
        const model = {};
        model.channel = this.readUnsigned(1);
        model.length = this.readUnsigned(1);
        model.config = this.readBytes(model.length);
        return model;
    };
// TapStreamWriter.prototype.write(model: SlaveServerRawConfig) : TapStreamWriter{
//     return this.writeSlaveServerRawConfig(model)
// }
TapStreamWriter.prototype.writeSlaveServerRawConfig = function (model) {
    this.writeBitsInt(enumKeyOrValueToNumber(model.channel, SlaveServerRawConfig.Channel), 8);
    this.writeUnsigned(model.length, 1);
    this.writeBytes(model.config, model.length);
    return this;
};

/**
 *   Generated file. Do not edit
 */
class SlaveServerRawConfigConverter {
    encode(model, stream = new TapStreamWriter()) {
        stream.writeSlaveServerRawConfig(model);
        return stream.toBytes;
    }
    decode(data) {
        const stream = data instanceof TapStreamReader ? data : TapStreamReader.create(data);
        return stream.readSlaveServerRawConfig();
    }
}

var converters = /*#__PURE__*/Object.freeze({
    __proto__: null,
    SlaveServerRawConfigConverter: SlaveServerRawConfigConverter
});

/**
 *   Generated file. Do not edit
 */
const slaveServerRawConfig = new SlaveServerRawConfigConverter();

var serviceConverters = /*#__PURE__*/Object.freeze({
    __proto__: null,
    slaveServerRawConfig: slaveServerRawConfig
});

/**
 *   Generated file. Do not edit
 */
const allConverters = Object.assign(Object.assign({}, converters$1), serviceConverters);
const SERVICE_CALLS = {
    getProtocolConfiguration: {
        method: TapRequestFrame.MethodType.GET,
        pathAlias: '/slave-server/protocol-configuration',
        path: '/1024//92',
        responseBodyDecoder: allConverters.slaveServerRawConfig,
    },
    postProtocolConfiguration: {
        method: TapRequestFrame.MethodType.POST,
        pathAlias: '/slave-server/protocol-configuration',
        path: '/1024//92',
        bodyEncoder: allConverters.slaveServerRawConfig,
    },
    putProtocolConfiguration: {
        method: TapRequestFrame.MethodType.PUT,
        pathAlias: '/slave-server/protocol-configuration',
        path: '/1024//92',
        bodyEncoder: allConverters.slaveServerRawConfig,
    },
};

class SlaveServerService extends AbstractService {
    constructor() {
        super(...arguments);
        this.resources = SERVICE_CALLS;
    }
    /**
     * Get slave server protocol configuration (generic)
     *
     * LWM2M path: /1024//92
     *
     * @tapVersion(">=2.1")
     * @return
     */
    getProtocolConfiguration() {
        return this.serviceCallRunner.execute(this.getProtocolConfigurationCall());
    }
    /**
     *
     *
     * LWM2M path: /1024//92
     *
     * @tapVersion(">=2.1")
     * @return call options
     */
    getProtocolConfigurationCall() {
        const callOptions = Object.assign({}, this.resources.getProtocolConfiguration);
        return callOptions;
    }
    /**
     * Set slave server protocol configuration (temporary). Not available anymore.
     *
     * LWM2M path: /1024//92
     *
     * @tapVersion(">=1.0")
     * @param data input
     * @return
     */
    postProtocolConfiguration(
    /*
     *
     */
    data) {
        return this.serviceCallRunner.execute(this.postProtocolConfigurationCall(data));
    }
    /**
     *
     *
     * LWM2M path: /1024//92
     *
     * @tapVersion(">=1.0")
     * @param data input
     * @return call options
     */
    postProtocolConfigurationCall(
    /*
     *
     */
    data) {
        const callOptions = Object.assign({}, this.resources.postProtocolConfiguration);
        callOptions.body = data;
        return callOptions;
    }
    /**
     * Write slave server protocol configuration
     *
     * LWM2M path: /1024//92
     *
     * @tapVersion(">=2.1")
     * @param data input
     * @return
     */
    putProtocolConfiguration(
    /*
     *
     */
    data) {
        return this.serviceCallRunner.execute(this.putProtocolConfigurationCall(data));
    }
    /**
     *
     *
     * LWM2M path: /1024//92
     *
     * @tapVersion(">=2.1")
     * @param data input
     * @return call options
     */
    putProtocolConfigurationCall(
    /*
     *
     */
    data) {
        const callOptions = Object.assign({}, this.resources.putProtocolConfiguration);
        callOptions.body = data;
        return callOptions;
    }
}

class SlaveServerServiceExtended extends SlaveServerService {
    putCanProtocolConfiguration(canConfig) {
        return this.serviceCallRunner.execute(this.putCanProtocolConfigurationCall(canConfig));
    }
    putCanProtocolConfigurationCall(config) {
        const canConfigEncoded = targetConverters.targetCanProtocolConfiguration.encode(config);
        return this.putProtocolConfigurationCall({
            channel: SlaveServerRawConfig.Channel.CAN,
            config: canConfigEncoded,
            length: canConfigEncoded.length,
        });
    }
    putSpiProtocolConfiguration() {
        return this.serviceCallRunner.execute(this.putSpiProtocolConfigurationCall());
    }
    putSpiProtocolConfigurationCall() {
        return this.putProtocolConfigurationCall({
            channel: SlaveServerRawConfig.Channel.SPI,
            config: new Uint8Array(0),
            length: 1,
        });
    }
    putModbusRtuProtocolConfiguration(config) {
        return this.serviceCallRunner.execute(this.putModbusRtuProtocolConfigurationCall(config));
    }
    putModbusRtuProtocolConfigurationCall(tapnpassConfigApi) {
        const modbusConfigEncoded = tapnpassConverters.tapnpassProtocolConfiguration.encode(tapnpassConfigApi);
        return this.putProtocolConfigurationCall({
            channel: SlaveServerRawConfig.Channel.MODBUS_RTU,
            config: modbusConfigEncoded,
            length: modbusConfigEncoded.length,
        });
    }
    putModbusTcpProtocolConfiguration(config) {
        return this.serviceCallRunner.execute(this.putModbusTcpProtocolConfigurationCall(config));
    }
    putModbusTcpProtocolConfigurationCall(config) {
        const modbusTcpConfigEncoded = new TapStreamWriter(5)
            .writeU1(config.firstRegisterAddress || 0)
            .writeU4(config.port).toBytes;
        return this.putProtocolConfigurationCall({
            channel: SlaveServerRawConfig.Channel.MODBUS_TCP,
            config: modbusTcpConfigEncoded,
            length: modbusTcpConfigEncoded.length,
        });
    }
}

const _TAP_SERVICE_EXTENSION_SLAVESERVER_ = extendServiceContainer('slaveServer', SlaveServerServiceExtended);

/**
 *   Generated file. Do not edit
 */
const ResourceMetaData = {
    '/slave-server/protocol-configuration': {
        put: {
            minApiVersion: 2.1,
            configModeRequired: true,
            schema: {
                $ref: './definitions.yaml#/SlaveServerRawConfig',
            },
        },
    },
};

/**
 * Generated bundle index. Do not edit.
 */

export { SERVICE_CALLS as SLAVESERVER_SERVICE_CALLS, SlaveServerRawConfig, SlaveServerRawConfigConverter, ResourceMetaData as SlaveServerResourceMetaData, SlaveServerService as SlaveServerServiceBase, SlaveServerServiceExtended, _TAP_SERVICE_EXTENSION_SLAVESERVER_, converters as slaveServerConverters };
//# sourceMappingURL=iotize-tap-service-impl-slave-server.js.map
