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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibW9kYnVzLWFjY2Vzcy12YXJpYWJsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uLy4uLy4uL2RhdGEvc3JjL2xpYi90YXJnZXQtdmFyaWFibGUvbW9kYnVzL21vZGJ1cy1hY2Nlc3MtdmFyaWFibGUudHMiXSwibmFtZXM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