package dongfang.mkt.main;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import dongfang.mkt.configuration.ConfigSet;
import dongfang.mkt.frames.UniversalReadParamSetRequestFrame;
import dongfang.mkt.frames.UniversalReadParamSetResponseFrame;
import dongfang.mkt.frames.UniversalWriteParamSetRequestFrame;
import dongfang.mkt.frames.UniversalWriteParamSetResponseFrame;
import dongfang.mkt.io.MKCommPort;
import dongfang.mkt.io.RXTXSerialPort;
import dongfang.mkt.serial.FrameQueue;
public class UniversalConfigurator
{
private static void configure
(UniversalWriteParamSetRequestFrame frame,
int parameterSetNumber
) throws IOException {
MKCommPort port =
new RXTXSerialPort
();
port.
init("COM10");
FrameQueue q =
new FrameQueue
(port
);
frame.
setConfigurationSetNumber(parameterSetNumber
);
q.
sendRequest(frame
);
UniversalWriteParamSetResponseFrame r =
(UniversalWriteParamSetResponseFrame
) q
.
getResponseFor(frame,
5000);
if (r ==
null) {
System.
err.
println("ERROR. Timeout waiting for response.");
} else if (!r.
wasAccepted()) {
System.
err
.
println("ERROR. Parameter set not accepted. Check version against MK firmware EEPROM configuration version and checkparameter set number");
} else {
System.
out.
println("Saved parameter set #"
+ r.
getParameterSetNumber() +
".");
}
q.
kill();
}
private static void writeConfiguration
(String s_parameterSetNumber,
String fileName
) throws IOException {
System.
out.
println("Writing parameter set #" + s_parameterSetNumber +
" from file: " + fileName
);
int parameterSetNumber =
Integer.
parseInt(s_parameterSetNumber
);
InputStream inputStream =
new FileInputStream(fileName
);
UniversalWriteParamSetRequestFrame frame = parseXMLParameterSet
(inputStream
);
configure
(frame, parameterSetNumber
);
}
private static ConfigSet readConfiguration
(int parameterSetNumber
)
throws IOException {
MKCommPort port =
new RXTXSerialPort
();
port.
init(null);
FrameQueue q =
new FrameQueue
(port
);
ConfigSet cs =
null;
UniversalReadParamSetRequestFrame frame =
new UniversalReadParamSetRequestFrame
();
frame.
setConfigurationSetNumber(parameterSetNumber
);
q.
sendRequest(frame
);
UniversalReadParamSetResponseFrame r =
(UniversalReadParamSetResponseFrame
) q
.
getResponseFor(frame,
3000);
if (r ==
null) {
System.
err.
println("ERROR. Timeout waiting for response.");
} else {
int paramSetVersion = r.
getConfigurationVersion();
int[] data = r.
getData();
cs = ConfigSet.
parseXMLConfigSet(paramSetVersion
);
cs.
setData(data
);
// System.out.println(cs.toXML());
}
q.
kill();
return cs
;
}
private static void readConfiguration
(String s_parameterSetNumber,
String fileName
) throws IOException {
int parameterSetNumber =
Integer.
parseInt(s_parameterSetNumber
);
ConfigSet cs = readConfiguration
(parameterSetNumber
);
if (cs
!=
null) {
FileWriter fw =
new FileWriter(fileName
);
fw.
write(cs.
toXML());
fw.
close();
}
}
private static int substituteVariables
(String s_value
) {
int numberOfHighestVar =
255;
int numberOfLowestVar = numberOfHighestVar
- ConfigSet.
NUMBER_OF_VARIABLES;
for (int i =
0; i
< ConfigSet.
NUMBER_OF_VARIABLES; i++
) {
String varName =
"var" + i
;
if (varName.
equals(s_value
)) {
System.
out.
println("Substing: " + s_value +
"-->"
+
(numberOfLowestVar + i
));
return numberOfLowestVar + i
;
}
}
return -
1;
}
private static UniversalWriteParamSetRequestFrame parseXMLParameterSet
(
InputStream input
) throws IOException {
DocumentBuilderFactory saxfac =
DocumentBuilderFactory.
newInstance();
saxfac.
setValidating(false);
try {
DocumentBuilder bldr = saxfac.
newDocumentBuilder();
Document doc = bldr.
parse(input
);
XPath xpath =
XPathFactory.
newInstance().
newXPath();
String s_eepromVersion = xpath.
evaluate(
"/parameterset/@eepromVersion", doc
);
String ss_length = xpath.
evaluate("/parameterset/@length", doc
);
int s_length =
0;
try {
s_length =
Integer.
parseInt(ss_length
);
} catch (NumberFormatException ex
) {
// ignore.
}
int eepromVersion = -
1;
try {
eepromVersion =
Integer.
parseInt(s_eepromVersion
);
} catch (NumberFormatException ex
) {
System.
err
.
println("The parameterset element must have an 'eepromVersion' attribute with a numerical value (eg.<parameterset eepromVersion='74'>)");
System.
exit(-
1);
}
// if (s_length != null)
// try {
// length = Integer.parseInt(s_length);
// } catch (NumberFormatException ex) {
// System.err.println("The length attribute of the parameterset must have a numerical value");
// System.exit(-1);
// }
NodeList parameterNodes =
(NodeList) xpath.
evaluate(
"/parameterset/parameter", doc,
XPathConstants.
NODESET);
int[] parameters =
new int[parameterNodes.
getLength()];
if (s_length
>=
0 && s_length
!= parameterNodes.
getLength()) {
System.
err.
println("The number of parameters ("+parameterNodes.
getLength()+
") does not match the number in the length attribute of the parameterset element ("+s_length+
").");
// System.exit(-1);
}
for (int i =
0; i
< parameterNodes.
getLength(); i++
) {
Element e =
(Element) parameterNodes.
item(i
);
int value =
0;
if (e.
hasAttribute("value")) {
String s_value = e.
getAttribute("value");
value = substituteVariables
(s_value
);
if (value
< 0)
try {
value =
Integer.
parseInt(s_value
);
} catch (NumberFormatException ex
) {
throw new NumberFormatException(
"Value is not a number and not a variable name.");
}
} else {
NodeList bitNodes =
(NodeList) xpath.
evaluate("bit", e,
XPathConstants.
NODESET);
for (int j =
0; j
< 8; j++
) {
Element bitElement =
(Element) bitNodes.
item(j
);
if (bitElement
!=
null) {
String s_bitValue = bitElement
.
getAttribute("value");
if ("1".
equals(s_bitValue
))
value |=
(1 << j
);
else if (!"0".
equals(s_bitValue
))
throw new NumberFormatException(
"Bit value was not 0 or 1.");
}
}
}
parameters
[i
] = value
;
}
input.
close();
return new UniversalWriteParamSetRequestFrame
(eepromVersion,
parameters
);
} catch (ParserConfigurationException ex
) {
// Should never happen.
throw new RuntimeException(ex
);
} catch (SAXException ex
) {
System.
err.
println(ex
);
System.
err
.
println("There is something screwed with your XML document. It is not well-formed and won't parse.");
throw new RuntimeException("Parse error.");
} catch (XPathExpressionException ex
) {
// Should never happen.
throw new RuntimeException(ex
);
}
}
public static void main
(String[] args
) throws IOException {
if (args.
length !=
3 ||
(!"r".
equals(args
[0]) && !"w".
equals(args
[0]))) {
System.
err
.
println("Usage: UniversalConfigurator r [parameter set number to read from] [filename to write to]");
System.
err
.
println("Usage: UniversalConfigurator w [parameter set number to write to] [filename to read from]");
System.
exit(-
1);
}
if ("r".
equals(args
[0])) {
readConfiguration
(args
[1], args
[2]);
} else {
writeConfiguration
(args
[1], args
[2]);
}
System.
exit(0);
}
}