var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { BLANK_CONVERTER } from '../../utility/blank-converter';
import { variableFormatToByteSize } from '../../utility/type-converter-factory';
import { AbstractVariable } from '../abstract-variable-stream';
export class ModbusAccessVariable extends AbstractVariable {
    constructor(id, context) {
        super();
        this.id = id;
        this.context = context;
    }
    get converter() {
        return this.context.converter || BLANK_CONVERTER;
    }
    get config() {
        return this.context.config;
    }
    readRawInner() {
        return __awaiter(this, void 0, void 0, function* () {
            return (yield this.context.targetService.modbusRead(this.context.config)).body();
        });
    }
    writeRaw(value) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!value || !(value instanceof Uint8Array)) {
                throw new Error(`Bad parameter for ModbusAccessVariable.writeRaw(). Expecting an Uint8Array.`);
            }
            const expectedByteSize = this.config.length * variableFormatToByteSize(this.config.format);
            if (value.length !== expectedByteSize) {
                throw new Error(`Encoded value does not have the expected byte size. Expecting ${expectedByteSize} byte(s) but given ${value.length} byte(s)`);
            }
            return (yield this.context.targetService.modbusWrite({
                data: value,
                options: this.context.config
            })).body();
        });
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibW9kYnVzLWFjY2Vzcy12YXJpYWJsZS5qcyIsInNvdXJjZVJvb3QiOiJuZzovL0Bpb3RpemUvdGFwL2RhdGEvIiwic291cmNlcyI6WyJsaWIvdGFyZ2V0LXZhcmlhYmxlL21vZGJ1cy9tb2RidXMtYWNjZXNzLXZhcmlhYmxlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ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