Building Controls Virtual Test Bed
ADInterfaceMCCDeviceManager.java
Go to the documentation of this file.
1 /*
2 ********************************************************************
3 Copyright Notice
4 ----------------
5 
6 Building Controls Virtual Test Bed (BCVTB) Copyright (c) 2008-2009, The
7 Regents of the University of California, through Lawrence Berkeley
8 National Laboratory (subject to receipt of any required approvals from
9 the U.S. Dept. of Energy). All rights reserved.
10 
11 If you have questions about your rights to use or distribute this
12 software, please contact Berkeley Lab's Technology Transfer Department
13 at TTD@lbl.gov
14 
15 NOTICE. This software was developed under partial funding from the U.S.
16 Department of Energy. As such, the U.S. Government has been granted for
17 itself and others acting on its behalf a paid-up, nonexclusive,
18 irrevocable, worldwide license in the Software to reproduce, prepare
19 derivative works, and perform publicly and display publicly. Beginning
20 five (5) years after the date permission to assert copyright is obtained
21 from the U.S. Department of Energy, and subject to any subsequent five
22 (5) year renewals, the U.S. Government is granted for itself and others
23 acting on its behalf a paid-up, nonexclusive, irrevocable, worldwide
24 license in the Software to reproduce, prepare derivative works,
25 distribute copies to the public, perform publicly and display publicly,
26 and to permit others to do so.
27 
28 
29 Modified BSD License agreement
30 ------------------------------
31 
32 Building Controls Virtual Test Bed (BCVTB) Copyright (c) 2008-2009, The
33 Regents of the University of California, through Lawrence Berkeley
34 National Laboratory (subject to receipt of any required approvals from
35 the U.S. Dept. of Energy). All rights reserved.
36 
37 Redistribution and use in source and binary forms, with or without
38 modification, are permitted provided that the following conditions are met:
39 
40  1. Redistributions of source code must retain the above copyright
41  notice, this list of conditions and the following disclaimer.
42  2. Redistributions in binary form must reproduce the above copyright
43  notice, this list of conditions and the following disclaimer in
44  the documentation and/or other materials provided with the
45  distribution.
46  3. Neither the name of the University of California, Lawrence
47  Berkeley National Laboratory, U.S. Dept. of Energy nor the names
48  of its contributors may be used to endorse or promote products
49  derived from this software without specific prior written permission.
50 
51 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
52 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
54 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
55 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
56 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
58 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
59 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
60 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 
63 You are under no obligation whatsoever to provide any bug fixes,
64 patches, or upgrades to the features, functionality or performance of
65 the source code ("Enhancements") to anyone; however, if you choose to
66 make your Enhancements available either publicly, or directly to
67 Lawrence Berkeley National Laboratory, without imposing a separate
68 written license agreement for such Enhancements, then you hereby grant
69 the following license: a non-exclusive, royalty-free perpetual license
70 to install, use, modify, prepare derivative works, incorporate into
71 other computer software, distribute, and sublicense such enhancements or
72 derivative works thereof, in binary and source code form.
73 
74 ********************************************************************
75 */
76 package adInterfaceMCC.util;
77 
78 import java.io.FileNotFoundException;
79 import java.io.IOException;
80 import java.util.ArrayList;
81 import java.util.HashSet;
82 import java.util.Iterator;
83 import java.util.List;
84 import java.util.Set;
85 import java.io.File;
86 import org.xml.sax.SAXParseException;
87 import javax.xml.parsers.ParserConfigurationException;
88 import javax.xml.validation.Schema;
89 import javax.xml.validation.SchemaFactory;
90 import javax.xml.validation.Validator;
91 import javax.xml.transform.sax.SAXSource;
92 import javax.xml.XMLConstants;
93 import org.xml.sax.SAXException;
94 import org.xml.sax.InputSource;
95 
111 {
112 
114  private final static String bcvtbhome = System.getenv("BCVTB_HOME");
115 
117  private final static String LS = System.getProperty("line.separator");
118 
120  private final static String FS = System.getProperty("file.separator");
121 
123  private final static String osName= System.getProperty("os.name");
124 
126  private final static String cmd = "java";
127 
129  private final static String cmdOptions = "-jar";
130 
136  WRITE
137  }
138 
147  {
148  String s = new String();
149  s = bcvtbhome + FS + "lib" + FS + "adInterfaceMCC-stack" + FS
150  + "bin-windows" + FS + "adInterfaceReader.jar";
151  return s;
152  }
153 
163  {
164  String s = new String();
165  s = bcvtbhome + FS + "lib" + FS + "adInterfaceMCC-stack" + FS
166  + "bin-windows" + FS + "adInterfaceWriter.jar";
167  return s;
168  }
179  public ArrayList<ADInterfaceMCCObjectType> parseADInterfaceMCCObjectType(final String s) throws
180  ParserConfigurationException, SAXException, IOException
181  {
182  ArrayList<ADInterfaceMCCObjectType> ad_arr = new ArrayList<ADInterfaceMCCObjectType>();
184  try{
185  ad_arr = ad.readADInterfaceMCCObjects(s);
186  }
187  catch (SAXException e) {
188  String em = "Parser error occurs while parsing xml configuration file '"
189  + s + "'.";
190  throw new SAXException(em + LS + e.getMessage());
191  }
192  catch(ParserConfigurationException e)
193  {
194  String em = "SAX Parser configuration error in parsing configuration file '"
195  + s + "'.";
196  throw new ParserConfigurationException(em + LS + e.getMessage());
197  }
198  catch(IOException e)
199  {
200  final String em = "IOException when parsing configuration file '" + s + "'." +
201  LS + e.getMessage();
202  throw new IOException(em);
203  }
204  return ad_arr;
205  }
212  public static ArrayList<Integer> saveDuplicates(ArrayList<Integer> arlList) {
213  // this functions save all duplicated in an Array.
214 
215  Set<Integer> set = new HashSet<Integer>();
216  List<Integer> newList = new ArrayList<Integer>();
217  List<Integer> newListDuplicates = new ArrayList<Integer>();
218  for (Iterator<Integer> iter = arlList.iterator(); iter.hasNext();) {
219  Integer element = iter.next();
220  if (set.add(element))
221  newList.add(element);
222  else {
223  newListDuplicates.add(element);
224  }
225  }
226  arlList.clear();
227  arlList.addAll(newListDuplicates);
228  return arlList;
229  }
230 
236  public ArrayList<ProcessBuilder> getProcesses(ArrayList<ADInterfaceMCCObjectType> prop_arr)
237  {
238  // All elements in prop_arr are either for the writer or for the reader.
239  // Determine if we are called by the reader or the writer
240 
241  ArrayList<ProcessBuilder> pro = new ArrayList<ProcessBuilder>();
242  for(int i=0; i<prop_arr.size(); i++)
243 
244  {
245  final String pro_app = prop_arr.get(i).getApplicationTag();
246  final boolean isReader = pro_app.equals("READ");
247  final String binPat = isReader ?
248 
249  // Path of binary
251 
252  ADInterfaceMCCObjectType bcp = prop_arr.get(i);
253  String boardNumber = bcp.getBoardNumber();
254  String channelNumber = bcp.getChannelNumber();
255  String channelGain = bcp.getChannelGain();
256  String channelValue = bcp.getChannelValue();
257  String channelOptions = bcp.getChannelOptions();
258 
259  if(isReader){
260  ProcessBuilder procBui = new ProcessBuilder
261  (cmd, cmdOptions, binPat, boardNumber,channelNumber,
262  channelGain, channelOptions);
263  pro.add(procBui);
264  }
265  else {
266  ProcessBuilder procBui = new ProcessBuilder
267  (cmd, cmdOptions, binPat, boardNumber,channelNumber,
268  channelGain, channelValue, channelOptions);
269  pro.add(procBui);
270  }
271  }
272  return pro;
273  }
274 
280  static protected String toString(String arg)
281  throws IllegalArgumentException{
282  ArrayList<String> s = new ArrayList<String>();
283  int nLen = 50;
284  int num;
285  if(arg.equals("ADInterfaceMCCApplicationTag"))
286  num = 1;
287  else{
288  num = -1;
289  throw new IllegalArgumentException("Program error. Argument '" + arg + "' is not known.");
290  }
291  switch (num)
292  {
293  case 1:
295  s.add(p.toString());
296  }
297  break;
298  }
299  // Assemble string that will be reported in the error message
300  String r ="";
301  int strLen = 0;
302  for(int i=0; i<s.size(); i++){
303  final String ele = s.get(i);
304  strLen += ele.length() + 2;
305  if (strLen > nLen){
306  r += LS;
307  strLen = 0;
308  }
309  r += ele;
310  if (i < s.size()-1)
311  r += ", ";
312  }
313  return r;
314  }
315 
323  public void validateApplicationTag(final String s) throws IllegalArgumentException
324  {
325  try{
326  ADInterfaceMCCApplicationTag.valueOf(s);
327  }
328  catch(IllegalArgumentException e)
329  {
330  String em = "'" + s +
331  "' is not allowed as the value for 'ApplicationTag.'" +
332  LS + "Possible values are:" + LS + toString("ADInterfaceMCCApplicationTag") + ".";
333  throw new IllegalArgumentException(em + LS + e.getMessage());
334  }
335  }
336 
348  public void validateXMLFile(final String xmlfile, final Schema schema)
349  throws SAXException, FileNotFoundException,IOException
350  {
351  try{
352  Validator validator = schema.newValidator(); //construct a validator
353  SAXSource source = new SAXSource(
354  new InputSource(new java.io.FileInputStream(xmlfile)));
355  validator.validate(source);
356  }
357  catch(SAXParseException e)
358  {
359  String em = xmlfile + ": Line " + e.getLineNumber() + ", column "
360  + e.getColumnNumber() + ": " + e.getMessage();
361  throw new SAXException(em);
362  }
363  catch(FileNotFoundException e)
364  {
365  String em = "XML file configuration file '" + xmlfile + "' cannot be found.";
366  throw new FileNotFoundException(em + LS + e.getMessage());
367  }
368  catch(IOException e)
369  {
370  String em = "Error while validating XML configuration file '" + xmlfile +".";
371  throw new IOException(em + LS + e.getMessage());
372  }
373  }
374 
383  public Schema loadSchema(final String name)throws SAXException
384  {
385  Schema schema = null;
386  try {
387  String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
388  SchemaFactory factory = SchemaFactory.newInstance(language);
389  schema = factory.newSchema(new File(name));
390  }
391  catch (SAXException e) {
392  throw new SAXException("Error while loading schema: " + name + LS +
393  e.getMessage());
394  }
395  return schema;
396  }
397 }
This class reads the XML configuration file and stores the data in the ADInterfaceMCCObjectType data ...
for i
Definition: compile.m:69
function r
static final String cmdOptions
Options of the command to run the process.
static final String FS
System-dependent file separator.
String getWritePropertyBinaryPath()
Returns the ADWriter.jar binary file path.
void validateApplicationTag(final String s)
Application tag validation.
static final String osName
Name of the operating system.
enumeration object, contains all the BACnet application tags specified in ADInterfaceMCC ...
static ArrayList< Integer > saveDuplicates(ArrayList< Integer > arlList)
Removes duplicates from an array list of integers.
static final String cmd
Name of the command to run the process.
ArrayList< ProcessBuilder > getProcesses(ArrayList< ADInterfaceMCCObjectType > prop_arr)
Prepare the process to be executed.
ArrayList< ADInterfaceMCCObjectType > parseADInterfaceMCCObjectType(final String s)
Parse the configuration file and generate an ArrayList that contains the xml content as specified in ...
const int * strLen
the length of string parsed to this function
Definition: util/utilXml.h:159
static final String bcvtbhome
String that points to root directory of the BCVTB.
This is the driver class in ADInterfaceMCC.
static final String LS
System-dependent line separator.
static String toString(String arg)
Gets a string representation of the enumeration.
void validateXMLFile(final String xmlfile, final Schema schema)
XMLfile validation.
int source
flag for function /c getepvariables 0=EnergyPlus, 1=Ptolemy
Definition: util/utilXml.h:158
String getReadPropertyBinaryPath()
Returns the ADReader.jar binary file path.