"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ref_napi_1 = __importDefault(require("ref-napi"));
const dll_js_1 = __importDefault(require("./dll.js"));
const utils_js_1 = require("./utils.js");
const w_char_js_1 = __importDefault(require("./w_char.js"));
class EZInterface {
    constructor(host = '127.0.0.1', port = 5123, timeout = 10000, dll_path = '') {
        this.dll = (0, dll_js_1.default)(dll_path);
        this.clientLogonEx(host, port, timeout);
    }
    goodResult(res) {
        if (res === 0) {
            return true;
        }
        else {
            throw new Error(this.dll.ResultString(res) || '');
        }
    }
    testConnection() {
        return this.goodResult(this.dll.TestConnection());
    }
    clientLogonEx(host, port = 5123, timeout = 10000) {
        this.goodResult(this.dll.ClientLogonEx(1, 1, host, port, 5124, timeout, 0, 0, 0));
    }
    clientLogoff() {
        this.goodResult(this.dll.ClientLogoff());
    }
    setDateTime(dt) {
        this.goodResult(this.dll.SetDateTime((0, utils_js_1.toOADate)(dt)));
    }
    // ------------------------ PUMPS ------------------------
    getPumpsCount() {
        const ret = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetPumpsCount(ret));
        return ret.readInt32LE();
    }
    getPumpByOrdinal(index) {
        const id = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetPumpByOrdinal(index, id));
        return id.readInt32LE();
    }
    getPumpPropertiesEx(id) {
        const number = ref_napi_1.default.alloc('int32*');
        const name = ref_napi_1.default.alloc(w_char_js_1.default);
        const physical_number = ref_napi_1.default.alloc('int16*');
        const side = ref_napi_1.default.alloc('int16*');
        const address = ref_napi_1.default.alloc('int16*');
        const price_level_1 = ref_napi_1.default.alloc('int16*');
        const price_level_2 = ref_napi_1.default.alloc('int16*');
        const price_dps_format = ref_napi_1.default.alloc('int16*');
        const volume_dsp_format = ref_napi_1.default.alloc('int16*');
        const value_dsp_format = ref_napi_1.default.alloc('int16*');
        const type = ref_napi_1.default.alloc('int16*');
        const port_id = ref_napi_1.default.alloc('int32*');
        const attendant_id = ref_napi_1.default.alloc('int32*');
        const auth_mode = ref_napi_1.default.alloc('int16*');
        const stack_mode = ref_napi_1.default.alloc('int16*');
        const prepay_allowed = ref_napi_1.default.alloc('int16*');
        const preauth_allowed = ref_napi_1.default.alloc('int16*');
        const slot_zig_bee_id = ref_napi_1.default.alloc('int32*');
        const mux_slot_zig_bee_id = ref_napi_1.default.alloc('int32*');
        const price_control = ref_napi_1.default.alloc('int16*');
        const has_preset = ref_napi_1.default.alloc('int16*');
        this.goodResult(this.dll.GetPumpPropertiesEx(id, number, name, physical_number, side, address, price_level_1, price_level_2, price_dps_format, volume_dsp_format, value_dsp_format, type, port_id, attendant_id, auth_mode, stack_mode, prepay_allowed, preauth_allowed, slot_zig_bee_id, mux_slot_zig_bee_id, price_control, has_preset));
        return {
            number: number.readInt32LE(),
            name: name.deref(),
            physical_number: physical_number.readInt16LE(),
            side: side.readInt16LE(),
            address: address.readInt16LE(),
            price_level_1: price_level_1.readInt16LE(),
            price_level_2: price_level_2.readInt16LE(),
            price_dps_format: price_dps_format.readInt16LE(),
            volume_dsp_format: volume_dsp_format.readInt16LE(),
            value_dsp_format: value_dsp_format.readInt16LE(),
            type: type.readInt16LE(),
            port_id: port_id.readInt32LE(),
            attendant_id: attendant_id.readInt32LE(),
            auth_mode: auth_mode.readInt16LE(),
            stack_mode: stack_mode.readInt16LE(),
            prepay_allowed: prepay_allowed.readInt16LE(),
            preauth_allowed: preauth_allowed.readInt16LE(),
            slot_zig_bee_id: slot_zig_bee_id.readInt32LE(),
            mux_slot_zig_bee_id: mux_slot_zig_bee_id.readInt32LE(),
            price_control: price_control.readInt16LE(),
            has_preset: has_preset.readInt16LE(),
        };
    }
    getPumpHoseByNumber(bomba, bico) {
        const ret = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetPumpHoseByNumber(bomba, bico, ret));
        return ret.readInt32LE();
    }
    getAllPumpStatuses() {
        const pump_states = ref_napi_1.default.alloc(w_char_js_1.default);
        const current_hose = ref_napi_1.default.alloc(w_char_js_1.default);
        const deliveries_count = ref_napi_1.default.alloc(w_char_js_1.default);
        this.goodResult(this.dll.GetAllPumpStatuses(pump_states, current_hose, deliveries_count));
        return {
            pump_states: pump_states.deref(),
            current_hose: current_hose.deref(),
            deliveries_count: deliveries_count.deref(),
        };
    }
    // -------------------- DELIVERIES ----------------------
    getDeliveriesCount() {
        const ret = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetDeliveriesCount(ret));
        return ret.readInt32LE();
    }
    getDeliveryByOrdinal(index) {
        const id = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetDeliveryByOrdinal(index, id));
        return id.readInt32LE();
    }
    getDeliveryPropertiesEx4(id) {
        const hose_id = ref_napi_1.default.alloc('int32*');
        const state = ref_napi_1.default.alloc('int16*');
        const dtype = ref_napi_1.default.alloc('int16*');
        const volume = ref_napi_1.default.alloc('double*');
        const price_level = ref_napi_1.default.alloc('int16*');
        const price = ref_napi_1.default.alloc('double*');
        const value = ref_napi_1.default.alloc('double*');
        const volume_2 = ref_napi_1.default.alloc('double*');
        const completed_dt = ref_napi_1.default.alloc('double*');
        const locked_by = ref_napi_1.default.alloc('int32*');
        const reserved_by = ref_napi_1.default.alloc('int32*');
        const attendant_id = ref_napi_1.default.alloc('int32*');
        const age = ref_napi_1.default.alloc('int32*');
        const cleared_dt = ref_napi_1.default.alloc('double*');
        const old_volume_etot = ref_napi_1.default.alloc('double*');
        const oldvolume2_etot = ref_napi_1.default.alloc('double*');
        const oldvalue_etot = ref_napi_1.default.alloc('double*');
        const newvolume_etot = ref_napi_1.default.alloc('double*');
        const newvolume2_etot = ref_napi_1.default.alloc('double*');
        const newvalue_etot = ref_napi_1.default.alloc('double*');
        const tag = ref_napi_1.default.alloc('int64*');
        const duration = ref_napi_1.default.alloc('int32*');
        const card_cliente_id = ref_napi_1.default.alloc('int32*');
        const peak_flow_rate = ref_napi_1.default.alloc('double*');
        this.goodResult(this.dll.GetDeliveryPropertiesEx4(id, hose_id, state, dtype, volume, price_level, price, value, volume_2, completed_dt, locked_by, reserved_by, attendant_id, age, cleared_dt, old_volume_etot, oldvolume2_etot, oldvalue_etot, newvolume_etot, newvolume2_etot, newvalue_etot, tag, duration, card_cliente_id, peak_flow_rate));
        return {
            id: id,
            hose_id: hose_id.readInt32LE(),
            state: state.readInt16LE(),
            dtype: dtype.readInt16LE(),
            volume: volume.readDoubleLE(),
            price_level: price_level.readInt16LE(),
            price: price.readDoubleLE(),
            value: value.readDoubleLE(),
            volume_2: volume_2.readDoubleLE(),
            completed_dt: (0, utils_js_1.fromOADate)(completed_dt.readDoubleLE()),
            locked_by: locked_by.readInt32LE(),
            reserved_by: reserved_by.readInt32LE(),
            attendant_id: attendant_id.readInt32LE(),
            age: age.readInt32LE(),
            cleared_dt: (0, utils_js_1.fromOADate)(cleared_dt.readDoubleLE()),
            old_volume_etot: old_volume_etot.readDoubleLE(),
            oldvolume2_etot: oldvolume2_etot.readDoubleLE(),
            oldvalue_etot: oldvalue_etot.readDoubleLE(),
            newvolume_etot: newvolume_etot.readDoubleLE(),
            newvolume2_etot: newvolume2_etot.readDoubleLE(),
            newvalue_etot: newvalue_etot.readDoubleLE(),
            tag: tag.readInt64LE(),
            duration: duration.readInt32LE(),
            card_cliente_id: card_cliente_id.readInt32LE(),
            peak_flow_rate: peak_flow_rate.readDoubleLE(),
        };
    }
    lockDelivery(id) {
        return this.goodResult(this.dll.LockDelivery(id));
    }
    clearDelivery(id, dtype) {
        return this.goodResult(this.dll.ClearDelivery(id, dtype));
    }
    // ------------------- HOSES ------------------------
    getHosesCount() {
        const ret = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetHosesCount(ret));
        return ret.readInt32LE();
    }
    getHoseByOrdinal(index) {
        const id = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetHoseByOrdinal(index, id));
        return id.readInt32LE();
    }
    getHosePropertiesEx2(id) {
        const number = ref_napi_1.default.alloc('int32*');
        const pump_id = ref_napi_1.default.alloc('int32*');
        const tank_id = ref_napi_1.default.alloc('int32*');
        const physical_number = ref_napi_1.default.alloc('int32*');
        const mtr_theo_value = ref_napi_1.default.alloc('double*');
        const mtr_theo_volume = ref_napi_1.default.alloc('double*');
        const mtr_elec_value = ref_napi_1.default.alloc('double*');
        const mtr_elec_volume = ref_napi_1.default.alloc('double*');
        const uve_antenna = ref_napi_1.default.alloc('int16*');
        const price_1 = ref_napi_1.default.alloc('double*');
        const price_2 = ref_napi_1.default.alloc('double*');
        const enabled = ref_napi_1.default.alloc('int16*');
        this.goodResult(this.dll.GetHosePropertiesEx2(id, number, pump_id, tank_id, physical_number, mtr_theo_value, mtr_theo_volume, mtr_elec_value, mtr_elec_volume, uve_antenna, price_1, price_2, enabled));
        return {
            number: number.readInt32LE(),
            pump_id: pump_id.readInt32LE(),
            tank_id: tank_id.readInt32LE(),
            physical_number: physical_number.readInt32LE(),
            mtr_theo_value: mtr_theo_value.readDoubleLE(),
            mtr_theo_volume: mtr_theo_volume.readDoubleLE(),
            mtr_elec_value: mtr_elec_value.readDoubleLE(),
            mtr_elec_volume: mtr_elec_volume.readDoubleLE(),
            uve_antenna: uve_antenna.readInt16LE(),
            price_1: price_1.readDoubleLE(),
            price_2: price_2.readDoubleLE(),
            enabled: enabled.readInt16LE(),
        };
    }
    getHoseSummaryEx(id) {
        const number = ref_napi_1.default.alloc('int32*');
        const physical_number = ref_napi_1.default.alloc('int32*');
        const pump_id = ref_napi_1.default.alloc('int32*');
        const pump_number = ref_napi_1.default.alloc('int32*');
        const pump_name = ref_napi_1.default.alloc(w_char_js_1.default);
        const tank_id = ref_napi_1.default.alloc('int32*');
        const tank_number = ref_napi_1.default.alloc('int32*');
        const tank_name = ref_napi_1.default.alloc(w_char_js_1.default);
        const grade_id = ref_napi_1.default.alloc('int32*');
        const grade_number = ref_napi_1.default.alloc('int32*');
        const grade_name = ref_napi_1.default.alloc(w_char_js_1.default);
        const grade_short_name = ref_napi_1.default.alloc(w_char_js_1.default);
        const grade_code = ref_napi_1.default.alloc(w_char_js_1.default);
        const mtr_theo_value = ref_napi_1.default.alloc('double*');
        const mtr_theo_volume = ref_napi_1.default.alloc('double*');
        const mtr_elec_value = ref_napi_1.default.alloc('double*');
        const mtr_elec_volume = ref_napi_1.default.alloc('double*');
        const price_1 = ref_napi_1.default.alloc('double*');
        const price_2 = ref_napi_1.default.alloc('double*');
        const enabled = ref_napi_1.default.alloc('int16*');
        this.goodResult(this.dll.GetHoseSummaryEx(id, number, physical_number, pump_id, pump_number, pump_name, tank_id, tank_number, tank_name, grade_id, grade_number, grade_name, grade_short_name, grade_code, mtr_theo_value, mtr_theo_volume, mtr_elec_value, mtr_elec_volume, price_1, price_2, enabled));
        return {
            number: number.readInt32LE(),
            physical_number: physical_number.readInt32LE(),
            pump_id: pump_id.readInt32LE(),
            pump_number: pump_number.readInt32LE(),
            pump_name: pump_name.deref(),
            tank_id: tank_id.readInt32LE(),
            tank_number: tank_number.readInt32LE(),
            tank_name: tank_name.deref(),
            grade_id: grade_id.readInt32LE(),
            grade_number: grade_number.readInt32LE(),
            grade_name: grade_name.deref(),
            grade_short_name: grade_short_name.deref(),
            grade_code: grade_code.deref(),
            mtr_theo_value: mtr_theo_value.readDoubleLE(),
            mtr_theo_volume: mtr_theo_volume.readDoubleLE(),
            mtr_elec_value: mtr_elec_value.readDoubleLE(),
            mtr_elec_volume: mtr_elec_volume.readDoubleLE(),
            price_1: price_1.readDoubleLE(),
            price_2: price_2.readDoubleLE(),
            enabled: enabled.readInt16LE(),
        };
    }
    SetHosePrices(hose_id, duration_type, price_type, price_1, price2) {
        return this.goodResult(this.dll.SetHosePrices(hose_id, duration_type, price_type, price_1, price2));
    }
    // ------------------- GRADES --------------------------------
    getGradesCount() {
        const ret = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetGradesCount(ret));
        return ret.readInt32LE();
    }
    getGradeByOrdinal(index) {
        const id = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetGradeByOrdinal(index, id));
        return id.readInt32LE();
    }
    getGradePropertiesEx(id) {
        const number = ref_napi_1.default.alloc('int32*');
        const name = ref_napi_1.default.alloc(w_char_js_1.default);
        const short_name = ref_napi_1.default.alloc(w_char_js_1.default);
        const code = ref_napi_1.default.alloc(w_char_js_1.default);
        const type = ref_napi_1.default.alloc('int16*');
        this.goodResult(this.dll.GetGradePropertiesEx(id, number, name, short_name, code, type));
        return {
            number: number.readInt32LE(),
            name: name.deref(),
            short_name: short_name.deref(),
            code: code.deref(),
            type: type.readInt16LE(),
        };
    }
    setGradePropertiesEx(id, number, name, short_name, code, type) {
        return this.goodResult(this.dll.SetGradePropertiesEx(id, number, name, short_name, code, type));
    }
    deleteGrade(id) {
        return this.goodResult(this.dll.DeleteGrade(id));
    }
    setGradePrice(id, level, price) {
        return this.goodResult(this.dll.SetGradePrice(id, level, price));
    }
    getGradePrice(id, level) {
        const price = ref_napi_1.default.alloc('double*');
        this.goodResult(this.dll.GetGradePrice(id, level, price));
        return price;
    }
    // ------------------------- TANKS -------------------------
    getTanksCount() {
        const ret = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetTanksCount(ret));
        return ret.readInt32LE();
    }
    getTankByOrdinal(index) {
        const id = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetTankByOrdinal(index, id));
        return id.readInt32LE();
    }
    getTankPropertiesEx(id) {
        const number = ref_napi_1.default.alloc('int32*');
        const name = ref_napi_1.default.alloc(w_char_js_1.default);
        const grade_id = ref_napi_1.default.alloc('int32*');
        const type = ref_napi_1.default.alloc('int16*');
        const capacity = ref_napi_1.default.alloc('double*');
        const diameter = ref_napi_1.default.alloc('double*');
        const theo_volume = ref_napi_1.default.alloc('double*');
        const gauge_volume = ref_napi_1.default.alloc('double*');
        const gauge_tc_volume = ref_napi_1.default.alloc('double*');
        const gauge_ull_lage = ref_napi_1.default.alloc('double*');
        const gauge_temperature = ref_napi_1.default.alloc('double*');
        const gauge_level = ref_napi_1.default.alloc('double*');
        const gauge_water_volume = ref_napi_1.default.alloc('double*');
        const gauge_water_level = ref_napi_1.default.alloc('double*');
        const gauge_id = ref_napi_1.default.alloc('int32*');
        const probe_no = ref_napi_1.default.alloc('int16*');
        const gauge_alarms_mark = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetTankPropertiesEx(id, number, name, grade_id, type, capacity, diameter, theo_volume, gauge_volume, gauge_tc_volume, gauge_ull_lage, gauge_temperature, gauge_level, gauge_water_volume, gauge_water_level, gauge_id, probe_no, gauge_alarms_mark));
        return {
            number: number.readInt32LE(),
            name: name.deref(),
            grade_id: grade_id.readInt32LE(),
            type: type.readInt16LE(),
            capacity: capacity.readDoubleLE(),
            diameter: diameter.readDoubleLE(),
            theo_volume: theo_volume.readDoubleLE(),
            gauge_volume: gauge_volume.readDoubleLE(),
            gauge_tc_volume: gauge_tc_volume.readDoubleLE(),
            gauge_ull_lage: gauge_ull_lage.readDoubleLE(),
            gauge_temperature: gauge_temperature.readDoubleLE(),
            gauge_level: gauge_level.readDoubleLE(),
            gauge_water_volume: gauge_water_volume.readDoubleLE(),
            gauge_water_level: gauge_water_level.readDoubleLE(),
            gauge_id: gauge_id.readInt32LE(),
            probe_no: probe_no.readInt16LE(),
            gauge_alarms_mark: gauge_alarms_mark.readInt32LE(),
        };
    }
    // ------------------- ATTENDANTS ------------------------
    getAttendantsCount() {
        const ret = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetAttendantsCount(ret));
        return ret.readInt32LE();
    }
    getAttendantByOrdinal(index) {
        const id = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetAttendantByOrdinal(index, id));
        return id.readInt32LE();
    }
    getAttendantPropertiesEx(id) {
        const number = ref_napi_1.default.alloc('int32*');
        const name = ref_napi_1.default.alloc(w_char_js_1.default);
        const short_name = ref_napi_1.default.alloc(w_char_js_1.default);
        const password = ref_napi_1.default.alloc(w_char_js_1.default);
        const tag = ref_napi_1.default.alloc(w_char_js_1.default);
        const shiftastart = ref_napi_1.default.alloc('int16*');
        const shiftaend = ref_napi_1.default.alloc('int16*');
        const shiftbstart = ref_napi_1.default.alloc('int16*');
        const shiftbend = ref_napi_1.default.alloc('int16*');
        const enabled = ref_napi_1.default.alloc('int16*');
        this.goodResult(this.dll.GetAttendantPropertiesEx(id, number, name, short_name, password, tag, shiftastart, shiftaend, shiftbstart, shiftbend, enabled));
        return {
            number: number.readInt32LE(),
            name: name.deref(),
            short_name: short_name.deref(),
            password: password.deref(),
            tag: tag.deref(),
            shiftastart: shiftastart.readInt16LE(),
            shiftaend: shiftaend.readInt16LE(),
            shiftbstart: shiftbstart.readInt16LE(),
            shiftbend: shiftbend.readInt16LE(),
            enabled: enabled.readInt16LE(),
        };
    }
    // ------------------- Card Clients ------------------------
    getCardClientsCount() {
        const ret = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetCardClientsCount(ret));
        return ret.readInt32LE();
    }
    getCardClientByOrdinal(index) {
        const id = ref_napi_1.default.alloc('int32*');
        this.goodResult(this.dll.GetCardClientByOrdinal(index, id));
        return id.readInt32LE();
    }
    getCardClientPropertiesEx2(id) {
        const number = ref_napi_1.default.alloc('int32*');
        const name = ref_napi_1.default.alloc(w_char_js_1.default);
        const tag = ref_napi_1.default.alloc(w_char_js_1.default);
        const enabled = ref_napi_1.default.alloc('int16*');
        const price_level = ref_napi_1.default.alloc('int16*');
        const plate = ref_napi_1.default.alloc(w_char_js_1.default);
        const grade_type = ref_napi_1.default.alloc('int16*');
        const card_type = ref_napi_1.default.alloc('int16*');
        const limit_type = ref_napi_1.default.alloc('int16*');
        const limit = ref_napi_1.default.alloc('double*');
        const entry_type = ref_napi_1.default.alloc('int16*');
        const expiration_date = ref_napi_1.default.alloc('double*');
        const parent_id = ref_napi_1.default.alloc('int64*');
        this.goodResult(this.dll.GetCardClientPropertiesEx2(id, number, name, tag, enabled, price_level, plate, grade_type, card_type, limit_type, limit, entry_type, expiration_date, parent_id));
        return {
            number: number.readInt32LE(),
            name: name.deref(),
            tag: tag.deref(),
            enabled: enabled.readInt16LE(),
            price_level: price_level.readInt16LE(),
            plate: plate.deref(),
            grade_type: grade_type.readInt16LE(),
            card_type: card_type.readInt16LE(),
            limit_type: limit_type.readInt16LE(),
            limit: limit.readDoubleLE(),
            entry_type: entry_type.readInt16LE(),
            expiration_date: (0, utils_js_1.fromOADate)(expiration_date.readDoubleLE()),
            parent_id: parent_id.readInt64LE(),
        };
    }
}
exports.default = EZInterface;
