001package jmri.jmrix.ipocs.configurexml; 002 003import java.io.IOException; 004 005import org.jdom2.Element; 006import org.slf4j.Logger; 007import org.slf4j.LoggerFactory; 008 009import jmri.configurexml.JmriConfigureXmlException; 010import jmri.jmrix.configurexml.AbstractConnectionConfigXml; 011import jmri.jmrix.ipocs.IpocsConnectionConfig; 012import jmri.jmrix.ipocs.IpocsPortController; 013import jmri.jmrix.ipocs.IpocsSystemConnectionMemo; 014 015/** 016 * 017 * @author Fredrik Elestedt Copyright (C) 2020 018 * @since 4.21.2 019 */ 020public class IpocsConnectionConfigXml extends AbstractConnectionConfigXml { 021 private final static Logger log = LoggerFactory.getLogger(IpocsConnectionConfigXml.class); 022 IpocsPortController portController; 023 024 public IpocsConnectionConfigXml() { 025 super(); 026 } 027 028 @Override 029 public Element store(Object o) { 030 Element e = new Element("connection"); 031 getInstance((IpocsConnectionConfig)o); 032 033 storeCommon(e, portController); 034 e.setAttribute("port", String.valueOf(portController.getPort())); 035 e.setAttribute("class", this.getClass().getName()); 036 037 extendElement(e); 038 return e; 039 } 040 041 @Override 042 public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException { 043 getInstance(); 044 try { 045 short port = (short)shared.getAttribute("port").getIntValue(); 046 portController.setPort(port); 047 } catch (org.jdom2.DataConversionException ex) { 048 log.warn("Could not parse port attribute: {}", shared.getAttribute("port")); 049 } catch (NullPointerException ex) { 050 log.error("No port attribute availableCould not parse port attribute", ex); 051 } 052 loadCommon(shared, perNode, portController); 053 register(); 054 055 if (!portController.getDisabled()) { 056 portController.configure(); 057 try { 058 portController.connect(); 059 } catch (IOException ex) { 060 log.error("Unable to start service: {}", ex.getMessage()); 061 } 062 } 063 return true; 064 } 065 066 @Override 067 protected void getInstance() { 068 portController = new IpocsPortController(new IpocsSystemConnectionMemo()); 069 portController.getSystemConnectionMemo().setPortController(portController); 070 } 071 072 protected void getInstance(IpocsConnectionConfig o) { 073 portController = o.getAdapter(); 074 } 075 076 @Override 077 protected void register() { 078 this.register(new IpocsConnectionConfig(portController)); 079 } 080 081 @Override 082 protected void dispose() { 083 portController.dispose(); 084 } 085}