001/*
002 * Copyright (C) Cross The Road Electronics.  All rights reserved.
003 * License information can be found in CTRE_LICENSE.txt
004 * For support and suggestions contact support@ctr-electronics.com or file
005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
006 */
007package com.ctre.phoenixpro.configs;
008
009import java.util.concurrent.locks.Lock;
010import java.util.concurrent.locks.ReentrantLock;
011
012import com.ctre.phoenix6.StatusCode;
013import com.ctre.phoenix6.configs.jni.ConfigJNI;
014import com.ctre.phoenixpro.hardware.DeviceIdentifier;
015import com.ctre.phoenix6.jni.ErrorReportingJNI;
016
017/**
018 * @deprecated Classes in the phoenixpro package will be removed in 2024.
019 *             Users should instead use classes from the phoenix6 package.
020 */
021@Deprecated(forRemoval = true)
022public class ParentConfigurator {
023    /**
024     * The most amount of time to wait for a config.
025     */
026    public double defaultTimeoutSeconds = 0.050;
027
028    private final DeviceIdentifier deviceIdentifier;
029    private final Lock _lck = new ReentrantLock();
030
031    private final ConfigJNI jni = new ConfigJNI();
032
033    protected ParentConfigurator(DeviceIdentifier deviceIdentifier) {
034        this.deviceIdentifier = deviceIdentifier;
035    }
036
037    protected StatusCode setConfigsPrivate(String serialString, double timeoutSeconds, boolean futureProofConfigs,
038            boolean overrideIfDuplicate) {
039        StatusCode status;
040        _lck.lock();
041        /* make sure we always unlock */
042        try {
043            jni.serializedString = serialString;
044            status = StatusCode.valueOf(
045                jni.SetConfigs(
046                    deviceIdentifier.getNetwork(),
047                    deviceIdentifier.getDeviceHash(),
048                    timeoutSeconds,
049                    futureProofConfigs,
050                    overrideIfDuplicate));
051        } finally {
052            _lck.unlock();
053        }
054
055        if (!status.isOK()) {
056            String location = this.deviceIdentifier.toString() + " Apply Config";
057            ErrorReportingJNI.reportStatusCode(status.value, location);
058        }
059        return status;
060    }
061
062    protected StatusCode getConfigsPrivate(StringBuilder serialString, double timeoutSeconds) {
063        StatusCode status;
064        _lck.lock();
065        /* make sure we always unlock */
066        try {
067            status = StatusCode.valueOf(
068                jni.GetConfigs(
069                    deviceIdentifier.getNetwork(),
070                    deviceIdentifier.getDeviceHash(),
071                    timeoutSeconds));
072            serialString.append(jni.serializedString);
073        } finally {
074            _lck.unlock();
075        }
076
077        if (!status.isOK()) {
078            String location = this.deviceIdentifier.toString() + " Refresh Config";
079            ErrorReportingJNI.reportStatusCode(status.value, location);
080        }
081        return status;
082    }
083}