Android Open Source - Android-NetPowerctrl-Shared Device Port






From Project

Back to project page Android-NetPowerctrl-Shared.

License

The source code is released under:

Copyright (c) 2014, David Gr?ff All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *...

If you think the Android project Android-NetPowerctrl-Shared listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package oly.netpowerctrl.device_base.device;
/*w w w .  jav a 2 s.  c  om*/
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.JsonReader;
import android.util.JsonWriter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import oly.netpowerctrl.device_base.data.JSONHelper;
import oly.netpowerctrl.device_base.executables.Executable;
import oly.netpowerctrl.device_base.executables.ExecutableReachability;
import oly.netpowerctrl.device_base.executables.ExecutableType;

/**
 * Base class for actions (IO, Outlet, ...)
 */
public final class DevicePort implements Comparable, Executable {
    // Some value constants
    public static final int OFF = 0;
    // Values
    public int min_value = OFF;
    public static final int ON = 1;
    public int max_value = ON;
    public static final int TOGGLE = -1;
    public static final int INVALID = -2;
    // The device this port belongs to.
    public final Device device;
    public int current_value = 0;
    // Executable groups
    public List<UUID> groups = new ArrayList<>();
    // unique identity among device ports on this device
    public int id = 0;
    // Used to disable control in list until ack from device has been received.
    public long last_command_timecode = 0;
    // Ports may be hidden by the user. We still need the DevicePort objects for those ports (for alarms, scenes etc)
    private boolean hidden = false;
    // unique identity among all device ports: Generated by device_unique_id + id
    private String uuid;
    // Type of this executable
    private ExecutableType ui_type;
    private String title = "";
    private boolean valid = true;

    private DevicePort(@NonNull Device di, ExecutableType ui_type) {
        device = di;
        this.ui_type = ui_type;
    }

    public static DevicePort create(@NonNull Device di, ExecutableType ui_type, int id) {
        DevicePort devicePort = new DevicePort(di, ui_type);
        devicePort.id = id;
        devicePort.uuid = di.getUniqueDeviceID() + "-" + String.valueOf(id);
        return devicePort;
    }

    public static DevicePort createWithTitle(@NonNull Device di, ExecutableType ui_type, int id, String title) {
        DevicePort devicePort = new DevicePort(di, ui_type);
        devicePort.id = id;
        devicePort.uuid = di.getUniqueDeviceID() + "-" + String.valueOf(id);
        devicePort.title = title;
        return devicePort;
    }

    public static DevicePort fromJSON(JsonReader reader, @NonNull Device di)
            throws IOException, ClassNotFoundException {
        reader.beginObject();
        DevicePort devicePort = new DevicePort(di, ExecutableType.TypeUnknown);
        devicePort.last_command_timecode = di.getUpdatedTime();

        while (reader.hasNext()) {
            String name = reader.nextName();

            assert name != null;

            switch (name) {
                case "Type":
                    int t = reader.nextInt();
                    if (t > ExecutableType.values().length)
                        throw new ClassNotFoundException();
                    devicePort.ui_type = ExecutableType.values()[t];
                    break;
                case "Description":
                    devicePort.title = reader.nextString();
                    break;
                case "Value":
                    devicePort.current_value = reader.nextInt();
                    break;
                case "max_value":
                    devicePort.max_value = reader.nextInt();
                    break;
                case "min_value":
                    devicePort.min_value = reader.nextInt();
                    break;
                case "Disabled":
                case "hidden":
                    devicePort.hidden = reader.nextBoolean();
                    break;
                case "id":
                    devicePort.id = reader.nextInt();
                    break;
                case "groups":
                    devicePort.groups.clear();
                    reader.beginArray();
                    while (reader.hasNext()) {
                        devicePort.groups.add(UUID.fromString(reader.nextString()));
                    }
                    reader.endArray();
                    break;
                default:
                    reader.skipValue();
                    break;
            }
        }

        reader.endObject();

        devicePort.uuid = di.getUniqueDeviceID() + "-" + String.valueOf(devicePort.id);
        return devicePort;
    }

    /**
     * Return the json representation of this scene
     *
     * @return JSON String
     */
    @Override
    public String toString() {
        try {
            JSONHelper h = new JSONHelper();
            toJSON(h.createWriter(), true);
            return h.getString();
        } catch (IOException ignored) {
            return null;
        }
    }

    @Override
    public List<UUID> getGroups() {
        return groups;
    }

    @Override
    public String getUid() {
        return uuid;
    }

    public boolean isEnabled() {
        return last_command_timecode <= device.getUpdatedTime();
    }

    @Override
    public int compareTo(@NonNull Object o) {
        DevicePort other = (DevicePort) o;
        if (other.equals(this))
            return 0;

        return getTitle().compareTo(other.getTitle());
    }

    public boolean equals(DevicePort other) {
        return (other != null) && (id == other.id) && device.equalsByUniqueID(other.device);
    }

    /**
     * @param source_oi The source DevicePort to copy from.
     * @return Return true if values in this object have changed because of source_oi.
     */
    public boolean copyValues(DevicePort source_oi) {
        valid = true;

        // We update the command time code here, too.
        last_command_timecode = source_oi.device.getUpdatedTime();
        boolean hasChanged;
        hasChanged = current_value != source_oi.current_value;
        current_value = source_oi.current_value;
        hasChanged |= max_value != source_oi.max_value;
        max_value = source_oi.max_value;
        hasChanged |= min_value != source_oi.min_value;
        min_value = source_oi.min_value;
        hasChanged |= setTitle(source_oi.getTitle());
        return hasChanged;
    }

    public ExecutableType getType() {
        return ui_type;
    }

    public void toJSON(JsonWriter writer, boolean addDeviceID) throws IOException {
        writer.beginObject();
        writer.name("Type").value(ui_type.ordinal());
        writer.name("Description").value(title);
        writer.name("Value").value(current_value);
        writer.name("max_value").value(max_value);
        writer.name("min_value").value(min_value);
        writer.name("hidden").value(hidden);
        writer.name("id").value(id);
        writer.name("groups").beginArray();
        for (UUID group_uuid : groups)
            writer.value(group_uuid.toString());
        writer.endArray();
        if (addDeviceID)
            writer.name("device_id").value(device.getUniqueDeviceID());
        writer.endObject();
    }

    public String getDescription(Context context) {
        return device.getDeviceName();
    }

    @Override
    public String getTitle() {
        return this.title;
    }

    @Override
    public ExecutableReachability reachableState() {
        return device.reachableState();
    }

    @Override
    public int getCurrentValue() {
        return current_value;
    }

    @Override
    public int getMaximumValue() {
        return max_value;
    }

    @Override
    public int getMinimumValue() {
        return min_value;
    }

    /**
     * Please be aware that if you do not request the description
     * change directly on the device, the change you make here will
     * be overridden on next device update!
     *
     * @param desc The new descriptive name.
     */
    public boolean setTitle(String desc) {
        boolean hasChanged = !desc.equals(title);
        title = desc;
        return hasChanged;
    }

    public void addToGroup(UUID uuid) {
        if (!groups.contains(uuid))
            groups.add(uuid);
    }

    public int getCurrentValueToggled() {
        return current_value > min_value ? min_value : max_value;
    }

    public boolean isValid() {
        return valid;
    }

    /**
     * Make this devicePort invalid. Warning: Do only use this for
     * update loops between device.lock and device.release.
     * Most of the code can not cope with an invalid DevicePort!
     */
    public void setValid(boolean valid) {
        this.valid = valid;
    }

    public boolean isHidden() {
        return hidden;
    }

    public void setHidden(Boolean hidden) {
        this.hidden = hidden;
    }
}




Java Source Code List

oly.netpowerctrl.device_base.ApplicationTest.java
oly.netpowerctrl.device_base.data.JSONHelper.java
oly.netpowerctrl.device_base.data.StorableInterface.java
oly.netpowerctrl.device_base.device.DeviceConnectionAPI.java
oly.netpowerctrl.device_base.device.DeviceConnectionFabric.java
oly.netpowerctrl.device_base.device.DeviceConnectionHTTP.java
oly.netpowerctrl.device_base.device.DeviceConnectionUDP.java
oly.netpowerctrl.device_base.device.DeviceConnection.java
oly.netpowerctrl.device_base.device.DeviceFeatureFabric.java
oly.netpowerctrl.device_base.device.DeviceFeatureInterface.java
oly.netpowerctrl.device_base.device.DeviceFeatureTemperature.java
oly.netpowerctrl.device_base.device.DevicePort.java
oly.netpowerctrl.device_base.device.Device.java
oly.netpowerctrl.device_base.executables.ExecutableReachability.java
oly.netpowerctrl.device_base.executables.ExecutableType.java
oly.netpowerctrl.device_base.executables.Executable.java
oly.netpowerctrl.plugin.App2PluginCommunication.java
oly.netpowerctrl.plugin.App2PluginService.java
oly.netpowerctrl.plugin.DeviceConnectionStateInterface.java
oly.netpowerctrl.plugin.onDeviceStateChange.java
oly.netpowerctrl.plugin.onServiceStateChange.java