All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.highmobility.autoapi.Trunk Maven / Gradle / Ivy

/*
 * The MIT License
 * 
 * Copyright (c) 2014- High-Mobility GmbH (https://high-mobility.com)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.highmobility.autoapi;

import com.highmobility.autoapi.property.Property;
import com.highmobility.autoapi.value.LockState;
import com.highmobility.autoapi.value.Position;
import com.highmobility.value.Bytes;
import javax.annotation.Nullable;

/**
 * The Trunk capability
 */
public class Trunk {
    public static final int IDENTIFIER = Identifier.TRUNK;

    public static final byte PROPERTY_LOCK = 0x01;
    public static final byte PROPERTY_POSITION = 0x02;

    /**
     * Get Trunk property availability information.
     */
    public static class GetStateAvailability extends GetAvailabilityCommand {
        /**
         * Get every Trunk property availability
         */
        public GetStateAvailability() {
            super(IDENTIFIER);
        }
    
        /**
         * Get specific Trunk property availabilities
         * 
         * @param propertyIdentifiers The property identifierBytes
         */
        public GetStateAvailability(Bytes propertyIdentifiers) {
            super(IDENTIFIER, propertyIdentifiers);
        }
    
        /**
         * Get specific Trunk property availabilities
         * 
         * @param propertyIdentifiers The property identifierBytes
         */
        public GetStateAvailability(byte... propertyIdentifiers) {
            super(IDENTIFIER, new Bytes(propertyIdentifiers));
        }
    
        GetStateAvailability(byte[] bytes, @SuppressWarnings("unused") boolean fromRaw) throws CommandParseException {
            super(bytes);
        }
    }

    /**
     * Get Trunk properties
     */
    public static class GetState extends GetCommand {
        /**
         * Get all Trunk properties
         */
        public GetState() {
            super(State.class, IDENTIFIER);
        }
    
        /**
         * Get specific Trunk properties
         * 
         * @param propertyIdentifiers The property identifiers
         */
        public GetState(Bytes propertyIdentifiers) {
            super(State.class, IDENTIFIER, propertyIdentifiers);
        }
    
        /**
         * Get specific Trunk properties
         * 
         * @param propertyIdentifiers The property identifiers
         */
        public GetState(byte... propertyIdentifiers) {
            super(State.class, IDENTIFIER, new Bytes(propertyIdentifiers));
        }
    
        GetState(byte[] bytes, @SuppressWarnings("unused") boolean fromRaw) throws CommandParseException {
            super(State.class, bytes);
        }
    }
    
    /**
     * Get specific Trunk properties
     * 
     * @deprecated use {@link GetState#GetState(byte...)} instead
     */
    @Deprecated
    public static class GetProperties extends GetCommand {
        /**
         * @param propertyIdentifiers The property identifiers
         */
        public GetProperties(Bytes propertyIdentifiers) {
            super(State.class, IDENTIFIER, propertyIdentifiers);
        }
    
        /**
         * @param propertyIdentifiers The property identifiers
         */
        public GetProperties(byte... propertyIdentifiers) {
            super(State.class, IDENTIFIER, new Bytes(propertyIdentifiers));
        }
    
        GetProperties(byte[] bytes, @SuppressWarnings("unused") boolean fromRaw) throws CommandParseException {
            super(State.class, bytes);
        }
    }

    /**
     * Control trunk
     */
    public static class ControlTrunk extends SetCommand {
        Property lock = new Property<>(LockState.class, PROPERTY_LOCK);
        Property position = new Property<>(Position.class, PROPERTY_POSITION);
    
        /**
         * @return The lock
         */
        public Property getLock() {
            return lock;
        }
        
        /**
         * @return The position
         */
        public Property getPosition() {
            return position;
        }
        
        /**
         * Control trunk
         * 
         * @param lock The lock
         * @param position The position
         */
        public ControlTrunk(@Nullable LockState lock, @Nullable Position position) {
            super(IDENTIFIER);
        
            addProperty(this.lock.update(lock));
            addProperty(this.position.update(position));
            if (this.lock.getValue() == null && this.position.getValue() == null) throw new IllegalArgumentException();
            createBytes();
        }
    
        ControlTrunk(byte[] bytes) throws CommandParseException, NoPropertiesException {
            super(bytes);
            while (propertyIterator.hasNext()) {
                propertyIterator.parseNext(p -> {
                    switch (p.getPropertyIdentifier()) {
                        case PROPERTY_LOCK: return lock.update(p);
                        case PROPERTY_POSITION: return position.update(p);
                    }
                    return null;
                });
            }
            if (this.lock.getValue() == null && this.position.getValue() == null) throw new NoPropertiesException();
        }
    }

    /**
     * The trunk state
     */
    public static class State extends SetCommand {
        Property lock = new Property<>(LockState.class, PROPERTY_LOCK);
        Property position = new Property<>(Position.class, PROPERTY_POSITION);
    
        /**
         * @return The lock
         */
        public Property getLock() {
            return lock;
        }
    
        /**
         * @return The position
         */
        public Property getPosition() {
            return position;
        }
    
        State(byte[] bytes) throws CommandParseException {
            super(bytes);
            while (propertyIterator.hasNext()) {
                propertyIterator.parseNext(p -> {
                    switch (p.getPropertyIdentifier()) {
                        case PROPERTY_LOCK: return lock.update(p);
                        case PROPERTY_POSITION: return position.update(p);
                    }
    
                    return null;
                });
            }
        }
    
        private State(Builder builder) {
            super(builder);
    
            lock = builder.lock;
            position = builder.position;
        }
    
        public static final class Builder extends SetCommand.Builder {
            private Property lock;
            private Property position;
    
            public Builder() {
                super(IDENTIFIER);
            }
    
            public State build() {
                return new State(this);
            }
    
            /**
             * @param lock The lock
             * @return The builder
             */
            public Builder setLock(Property lock) {
                this.lock = lock.setIdentifier(PROPERTY_LOCK);
                addProperty(this.lock);
                return this;
            }
            
            /**
             * @param position The position
             * @return The builder
             */
            public Builder setPosition(Property position) {
                this.position = position.setIdentifier(PROPERTY_POSITION);
                addProperty(this.position);
                return this;
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy