001package jmri.jmrit.logixng.actions; 002 003import com.fasterxml.jackson.core.JsonProcessingException; 004import com.fasterxml.jackson.databind.JsonNode; 005import com.fasterxml.jackson.databind.ObjectMapper; 006 007import java.beans.*; 008import java.util.*; 009 010import jmri.*; 011import jmri.jmrit.logixng.*; 012import jmri.jmrit.logixng.util.parser.*; 013import jmri.util.TypeConversionUtil; 014 015/** 016 * This action decodes a Json string to a JsonNode. 017 * 018 * @author Daniel Bergqvist Copyright 2024 019 */ 020public class JsonDecode extends AbstractDigitalAction 021 implements PropertyChangeListener { 022 023 private String _jsonLocalVariable; 024 private String _resultLocalVariable; 025 026 027 public JsonDecode(String sys, String user) 028 throws BadUserNameException, BadSystemNameException { 029 super(sys, user); 030 } 031 032 @Override 033 public Base getDeepCopy(Map<String, String> systemNames, Map<String, String> userNames) throws ParserException { 034 DigitalActionManager manager = InstanceManager.getDefault(DigitalActionManager.class); 035 String sysName = systemNames.get(getSystemName()); 036 String userName = userNames.get(getSystemName()); 037 if (sysName == null) sysName = manager.getAutoSystemName(); 038 JsonDecode copy = new JsonDecode(sysName, userName); 039 copy.setComment(getComment()); 040 copy.setJsonLocalVariable(_jsonLocalVariable); 041 copy.setResultLocalVariable(_resultLocalVariable); 042 return manager.registerAction(copy); 043 } 044 045 public void setJsonLocalVariable(String variableName) { 046 assertListenersAreNotRegistered(log, "setJsonLocalVariable"); // No I18N 047 _jsonLocalVariable = variableName; 048 } 049 050 public String getJsonLocalVariable() { 051 return _jsonLocalVariable; 052 } 053 054 public void setResultLocalVariable(String variableName) { 055 assertListenersAreNotRegistered(log, "setResultLocalVariable"); // No I18N 056 _resultLocalVariable = variableName; 057 } 058 059 public String getResultLocalVariable() { 060 return _resultLocalVariable; 061 } 062 063 /** {@inheritDoc} */ 064 @Override 065 public Category getCategory() { 066 return Category.OTHER; 067 } 068 069 /** {@inheritDoc} */ 070 @Override 071 public void execute() throws JmriException { 072 if (_jsonLocalVariable == null) return; 073 if (_resultLocalVariable == null) return; 074 075 final ConditionalNG conditionalNG = getConditionalNG(); 076 077 SymbolTable symbolTable = conditionalNG.getSymbolTable(); 078 079 String json = TypeConversionUtil.convertToString( 080 symbolTable.getValue(_jsonLocalVariable), false); 081 082 ObjectMapper om = new ObjectMapper(); 083 try { 084 JsonNode jsonNode = om.readTree(json); 085 symbolTable.setValue(_resultLocalVariable, jsonNode); 086 } catch (JsonProcessingException ex) { 087 throw new JmriException(ex); 088 } 089 } 090 091 @Override 092 public FemaleSocket getChild(int index) throws IllegalArgumentException, UnsupportedOperationException { 093 throw new UnsupportedOperationException("Not supported."); 094 } 095 096 @Override 097 public int getChildCount() { 098 return 0; 099 } 100 101 @Override 102 public String getShortDescription(Locale locale) { 103 return Bundle.getMessage(locale, "JsonDecode_Short"); 104 } 105 106 @Override 107 public String getLongDescription(Locale locale) { 108 return Bundle.getMessage(locale, "JsonDecode_Long", _jsonLocalVariable, _resultLocalVariable); 109 } 110 111 /** {@inheritDoc} */ 112 @Override 113 public void setup() { 114 // Do nothing 115 } 116 117 /** {@inheritDoc} */ 118 @Override 119 public void registerListenersForThisClass() { 120 if (!_listenersAreRegistered) { 121 _listenersAreRegistered = true; 122 } 123 } 124 125 /** {@inheritDoc} */ 126 @Override 127 public void unregisterListenersForThisClass() { 128 if (_listenersAreRegistered) { 129 _listenersAreRegistered = false; 130 } 131 } 132 133 /** {@inheritDoc} */ 134 @Override 135 public void propertyChange(PropertyChangeEvent evt) { 136 getConditionalNG().execute(); 137 } 138 139 /** {@inheritDoc} */ 140 @Override 141 public void disposeMe() { 142 } 143 144 /*.* {@inheritDoc} *./ 145 @Override 146 public void getUsageDetail(int level, NamedBean bean, List<NamedBeanUsageReport> report, NamedBean cdl) { 147 log.debug("getUsageReport :: JsonDecode: bean = {}, report = {}", cdl, report); 148 } 149*/ 150 private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(JsonDecode.class); 151 152}