001package jmri.jmrix.easydcc; 002 003import org.slf4j.Logger; 004import org.slf4j.LoggerFactory; 005 006/** 007 * Carries the reply to an EasyDccMessage. 008 * 009 * @author Bob Jacobsen Copyright (C) 2001, 2004 010 */ 011public class EasyDccReply extends jmri.jmrix.AbstractMRReply { 012 013 // create a new one 014 public EasyDccReply() { 015 super(); 016 } 017 018 public EasyDccReply(String s) { 019 super(s); 020 } 021 022 public EasyDccReply(EasyDccReply l) { 023 super(l); 024 } 025 026 @Override 027 protected int skipPrefix(int index) { 028 // start at index, passing any whitespace & control characters at the start of the buffer 029 while (index < getNumDataElements() - 1 030 && ((char) getElement(index) <= ' ')) { 031 index++; 032 } 033 return index; 034 } 035 036 /** 037 * Extracts Read-CV returned value from a message. 038 * Expects a message of the format "CVnnnvv" where vv is 039 * the hexadecimal value or "Vnvv" where vv is the hexadecimal value. 040 * 041 * @return -1 if message can't be parsed 042 */ 043 @Override 044 public int value() { 045 int index = 0; 046 if ((char) getElement(index) == 'C') { 047 // integer value of 6th, 7th digits in hex 048 index = 5; // 5th position is index 5 049 } else if ((char) getElement(index) == 'V') { 050 // integer value of 3rd, 4th digits in hex 051 index = 2; // 2nd position is index 2 052 } else { 053 log.warn("Did not find recognizable format: {}", this.toString()); 054 } 055 String s1 = "" + (char) getElement(index); 056 String s2 = "" + (char) getElement(index + 1); 057 int val = -1; 058 try { 059 int sum = Integer.valueOf(s2, 16).intValue(); 060 sum += 16 * Integer.valueOf(s1, 16).intValue(); 061 val = sum; // don't do this assign until now in case the conversion throws 062 } catch (RuntimeException e) { 063 log.error("Unable to get number from reply: \"{}{}\" index: {} message: \"{}\"", s1, s2, index, toString()); 064 } 065 return val; 066 } 067 068 private final static Logger log = LoggerFactory.getLogger(EasyDccReply.class); 069 070}