Search in sources :

Example 1 with ModbusDeviceNature

use of io.openems.impl.protocol.modbus.ModbusDeviceNature in project openems by OpenEMS.

the class ChannelExport method main.

public static void main(String[] args) throws OpenemsException {
    String openemsPath = "C:\\Users\\matthias.rossmann\\Dev\\git\\openems-neu\\edge\\src";
    Collection<ThingDoc> deviceNatures;
    HashMap<Path, FileWriter> files = new HashMap<>();
    try {
        deviceNatures = ClassRepository.getInstance().getAvailableDeviceNatures();
        FileWriter devices = new FileWriter(Paths.get(openemsPath, "\\io\\openems\\impl\\device\\Readme.md").toFile());
        devices.write("# List of implemented Devices.\r\n\r\n");
        for (ThingDoc thingDoc : deviceNatures) {
            try {
                System.out.println(thingDoc.getClazz().getName());
                if (thingDoc.getClazz().equals(AsymmetricSymmetricCombinationEssNature.class) || thingDoc.getClazz().equals(EssClusterNature.class) || thingDoc.getClazz().isInterface() || Modifier.isAbstract(thingDoc.getClazz().getModifiers())) {
                    continue;
                }
                Path p = Paths.get(openemsPath, thingDoc.getClazz().getName().replaceAll("[^\\.]*$", "").replace(".", "/"), "Readme.md");
                FileWriter fw;
                if (files.containsKey(p)) {
                    fw = files.get(p);
                } else {
                    fw = new FileWriter(p.toFile());
                    files.put(p, fw);
                    fw.write("");
                }
                fw.append("# " + thingDoc.getTitle() + "\r\n" + thingDoc.getText() + "\r\n\r\nFollowing Values are implemented:\r\n\r\n" + "|ChannelName|Unit|\r\n" + "|---|---|\r\n");
                devices.append("* [" + thingDoc.getTitle() + "](" + Paths.get(thingDoc.getClazz().getName().replaceAll("io.openems.impl.device.", "").replaceAll("[^\\.]*$", "").replace(".", "/"), "Readme.md").toString().replace("\\", "/") + ")\r\n");
                Thing thing = thingDoc.getClazz().getConstructor(String.class, Device.class).newInstance("", null);
                if (thing instanceof ModbusDeviceNature) {
                    ((ModbusDeviceNature) thing).init();
                }
                List<ChannelDoc> channelDocs = new LinkedList<>(thingDoc.getChannelDocs());
                Collections.sort(channelDocs, new Comparator<ChannelDoc>() {

                    @Override
                    public int compare(ChannelDoc arg0, ChannelDoc arg1) {
                        return arg0.getName().compareTo(arg1.getName());
                    }
                });
                for (ChannelDoc channelDoc : channelDocs) {
                    Member member = channelDoc.getMember();
                    try {
                        List<Channel> channels = new ArrayList<>();
                        if (member instanceof Method) {
                            if (((Method) member).getReturnType().isArray()) {
                                Channel[] ch = (Channel[]) ((Method) member).invoke(thing);
                                for (Channel c : ch) {
                                    channels.add(c);
                                }
                            } else {
                                // It's a Method with ReturnType Channel
                                channels.add((Channel) ((Method) member).invoke(thing));
                            }
                        } else if (member instanceof Field) {
                            // It's a Field with Type Channel
                            channels.add((Channel) ((Field) member).get(thing));
                        } else {
                            continue;
                        }
                        if (channels.isEmpty()) {
                            System.out.println("Channel is returning null! Thing [" + thing.id() + "], Member [" + member.getName() + "]");
                            continue;
                        }
                        for (Channel channel : channels) {
                            if (channel != null) {
                                StringBuilder unit = new StringBuilder();
                                if (channel instanceof ReadChannel) {
                                    ReadChannel rchannel = ((ReadChannel) channel);
                                    unit.append(rchannel.unitOptional());
                                    rchannel.getLabels().forEach((key, value) -> {
                                        unit.append(key + ": " + value + "<br/>");
                                    });
                                }
                                fw.append("|" + channel.id() + "|" + unit + "|\r\n");
                            }
                        }
                    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                        System.out.println("Unable to add Channel. Member [" + member.getName() + "]");
                    }
                }
            } catch (NoSuchMethodException e) {
            }
        }
        for (FileWriter fw : files.values()) {
            fw.close();
        }
        devices.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) ReadChannel(io.openems.api.channel.ReadChannel) Field(java.lang.reflect.Field) EssClusterNature(io.openems.impl.device.system.esscluster.EssClusterNature) Member(java.lang.reflect.Member) Thing(io.openems.api.thing.Thing) ThingDoc(io.openems.api.doc.ThingDoc) Path(java.nio.file.Path) Device(io.openems.api.device.Device) ReadChannel(io.openems.api.channel.ReadChannel) Channel(io.openems.api.channel.Channel) Method(java.lang.reflect.Method) ChannelDoc(io.openems.api.doc.ChannelDoc) LinkedList(java.util.LinkedList) InvocationTargetException(java.lang.reflect.InvocationTargetException) OpenemsException(io.openems.common.exceptions.OpenemsException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AsymmetricSymmetricCombinationEssNature(io.openems.impl.device.system.asymmetricsymmetriccombinationess.AsymmetricSymmetricCombinationEssNature) ModbusDeviceNature(io.openems.impl.protocol.modbus.ModbusDeviceNature)

Aggregations

Channel (io.openems.api.channel.Channel)1 ReadChannel (io.openems.api.channel.ReadChannel)1 Device (io.openems.api.device.Device)1 ChannelDoc (io.openems.api.doc.ChannelDoc)1 ThingDoc (io.openems.api.doc.ThingDoc)1 Thing (io.openems.api.thing.Thing)1 OpenemsException (io.openems.common.exceptions.OpenemsException)1 AsymmetricSymmetricCombinationEssNature (io.openems.impl.device.system.asymmetricsymmetriccombinationess.AsymmetricSymmetricCombinationEssNature)1 EssClusterNature (io.openems.impl.device.system.esscluster.EssClusterNature)1 ModbusDeviceNature (io.openems.impl.protocol.modbus.ModbusDeviceNature)1 FileWriter (java.io.FileWriter)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Member (java.lang.reflect.Member)1 Method (java.lang.reflect.Method)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1