Building Controls Virtual Test Bed
BACnetObjectType.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 
77 package bacnet.util;
78 import java.io.File;
79 import java.io.FileNotFoundException;
80 import java.io.IOException;
81 import java.util.HashMap;
82 import java.util.ArrayList;
83 import java.util.Iterator;
84 import javax.xml.parsers.ParserConfigurationException;
85 import javax.xml.parsers.SAXParser;
86 import javax.xml.parsers.SAXParserFactory;
87 import org.xml.sax.SAXException;
88 import org.xml.sax.Attributes;
89 import org.xml.sax.helpers.DefaultHandler;
90 import bacnet.util.*;
91 
98 public class BACnetObjectType
99 {
113  public BACnetObjectType(final String name,
114  final String instance,
115  final ArrayList<BACnetPropertyValue> mapPI,
116  final HashMap mapOTname,
117  final HashMap mapOTinst,
118  final HashMap piOrder,
119  final HashMap otOrder)
120  {
121  bot_name = name;
122  bot_instance = instance;
123  bot_mapPI = mapPI;
124  bot_mapOTname = mapOTname;
125  bot_mapOTinst = mapOTinst;
126  bot_piOrder = piOrder;
127  bot_otOrder = otOrder;
128 
129  }
130 
135  }
136 
141  public String getBOTName(){
142  return bot_name;
143  }
144 
149  public String getBOTInst()
150  {
151  return bot_instance;
152  }
153 
158  public ArrayList<BACnetPropertyValue> getBOTPI()
159  {
160  return bot_mapPI;
161  }
162 
167  public HashMap getBACnetObjectTypeNameMap()
168  {
169  return bot_mapOTname;
170  }
171 
177  {
178  return bot_mapOTinst;
179  }
180 
185  {
186  return bot_piOrder;
187  }
188 
192  public HashMap getBACnetObjectTypeOrder()
193  {
194  return bot_otOrder;
195  }
196 
206  public ArrayList<BACnetObjectType> parseObjectType(final String s)
207  throws ParserConfigurationException, SAXException, IOException
208  {
209  final ArrayList<BACnetObjectType> bot_arr = new ArrayList<BACnetObjectType>();
210  try{
211  ArrayList<String> dev_arr = readDeviceInst(s);
212  for(int i=0; i<dev_arr.size(); i++)
213  {
214  String dev_inst = dev_arr.get(i);
216  bot_arr.add(bot);
217  HashMap mapOTname = bot.getBACnetObjectTypeNameMap();
218  HashMap mapOTinst = bot.getBACnetObjectTypeInstanceMap();
219  Iterator iterator = mapOTname.keySet().iterator();
220  while( iterator. hasNext() ){
221  String obj_id = iterator.next().toString();
222  String obj_name = mapOTname.get(obj_id).toString();
223  String obj_inst = mapOTinst.get(obj_id).toString();
225  (dev_inst, obj_inst, obj_name,s);
226  bot_arr.add(bot1);
227  }
228  }
229  }
230  catch(SAXException e)
231  {
232  String em = "SAXException when parsing file '" + s + "'.";
233  throw new SAXException(em + LS + e.getMessage());
234  }
235  catch(ParserConfigurationException e)
236  {
237  String em = "SAXParser error in parsing file '" + s + "'.";
238  throw new ParserConfigurationException(em + LS + e.getMessage());
239  }
240  catch(IOException e)
241  {
242  String em = "I/O errors occur while parsing file '" + s + "'.";
243  throw new IOException(em + LS + e.getMessage());
244  }
245 
246  return bot_arr;
247  }
248 
257  public ArrayList<String> readDeviceInst(final String s) throws FileNotFoundException,
258  ParserConfigurationException, SAXException, IOException
259  {
260  final ArrayList<String> dev_arr = new ArrayList<String>();
261  File f = new File(s);
262  if(f.exists()!=true)
263  throw new FileNotFoundException("Configuration file '" + s + "' could not be found.");
264  try{
265  SAXParserFactory factory = SAXParserFactory.newInstance();
266  SAXParser saxParser = factory.newSAXParser();
267  DefaultHandler handler = new DefaultHandler(){
268  String obj_inst = new String();
269  public void startElement
270  (String uri, String localName, String qName, Attributes attributes)
271  throws SAXException {
272  if (qName.equals("Object") && attributes.getValue("Type").equals("Device"))
273  {
274  obj_inst = attributes.getValue("Instance");
275  dev_arr.add(obj_inst);
276  }
277  }
278  };
279  saxParser.parse(s, handler);
280  }
281  catch(SAXException e)
282  {
283  String em = "SAXException when parsing file '" + s + "'.";
284  throw new SAXException(em + e.getMessage());
285  }
286  catch(ParserConfigurationException e)
287  {
288  String em = "SAXParser error in parsing file '" + s + "'.";
289  throw new ParserConfigurationException(em + e.getMessage());
290  }
291  catch(IOException e)
292  {
293  String em = "I/O error occurs when parsing configuration file '" + s + "'.";
294  throw new IOException(em + e.getMessage());
295  }
296  return dev_arr;
297  }
298 
308  public BACnetObjectType readBacnetDeviceObjectType(final String dev_inst, final String s)
309  throws FileNotFoundException, ParserConfigurationException, SAXException, IOException
310  {
311  File f = new File(s);
312  if(f.exists()!=true)
313  throw new FileNotFoundException("Configuration file '" + s + "' could not be found");
314  BACnetObjectType bot = null;
315  try{
316  SAXParserFactory factory = SAXParserFactory.newInstance();
317  SAXParser saxParser = factory.newSAXParser();
318  final ArrayList<BACnetPropertyValue> mapPI = new ArrayList<BACnetPropertyValue>();
319  final HashMap<String, String> mapOTname = new HashMap<String,String>();
320  final HashMap<String, String> mapOTinst = new HashMap<String,String>();
321  final HashMap<String, String> piOrder = new HashMap<String,String>();
322  final HashMap<String, String> otOrder = new HashMap<String,String>();
323  DefaultHandler handler = new DefaultHandler(){
324  boolean dev = false;
325  boolean nonobj = false;
326  int piID =0;
327  int otID =0;
328  public void startElement
329  (String uri, String localName, String qName, Attributes attributes)
330  throws SAXException
331  {
332  if (qName.equals("Object")
333  && attributes.getValue("Type").equals("Device")
334  && attributes.getValue("Instance").equals(dev_inst))
335  {
336  dev = true;
337  nonobj = true;
338  }
339  if(qName.equals("PropertyIdentifier") && dev==true && nonobj==true)
340  {
341  String[] piArray= new String[4];
342  if(attributes.getValue("PropertyValue") != null)
343  piArray[0] = attributes.getValue("PropertyValue");
344  else
345  piArray[0] = "null";
346  if(attributes.getValue("ApplicationTag") != null)
347  piArray[1] = attributes.getValue("ApplicationTag").toUpperCase();
348  else
349  piArray[1] = "null";
350  if(attributes.getValue("Priority") != null)
351  piArray[2] = attributes.getValue("Priority");
352  else
353  piArray[2] = "null";
354  if(attributes.getValue("Index") != null)
355  piArray[3] = attributes.getValue("Index");
356  else
357  piArray[3] = "null";
358  BACnetPropertyValue bpv =
359  new BACnetPropertyValue(attributes.getValue("Name").toUpperCase(),piArray);
360  mapPI.add(bpv);
361  piID = piID +1;
362  piOrder.put(attributes.getValue("Name").toUpperCase(),
363  Integer.toString(piID));
364  }
365 
366  if(qName.equals("Object") && !attributes.getValue("Type").equals("Device")
367  && dev==true)
368  {
369  mapOTname.put(Integer.toString(otID),
370  convertObjectName(attributes.getValue("Type")));
371  mapOTinst.put(Integer.toString(otID),attributes.getValue("Instance"));
372  otID = otID +1;
373  otOrder.put(attributes.getValue("Instance"),Integer.toString(otID));
374  nonobj=false;
375  }
376  if (qName.equals("Object")
377  && attributes.getValue("Type").equals("Device")
378  && !attributes.getValue("Instance").equals(dev_inst))
379  {
380  dev = false;
381  }
382  }
383  };
384  saxParser.parse(s, handler);
385  bot = new BACnetObjectType("DeviceObjectType", dev_inst,
386  mapPI, mapOTname, mapOTinst, piOrder, otOrder);
387  }
388 
389  catch(SAXException e)
390  {
391  String em = "SAXException when parsing file '" + s + "'.";
392  throw new SAXException(em + e.getMessage());
393  }
394  catch(ParserConfigurationException e)
395  {
396  String em = "ParserConfigurationException when parsing file '" + s + "'.";
397  throw new ParserConfigurationException(em + e.getMessage());
398  }
399  catch(IOException e)
400  {
401  String em = "I/O error occurs when parsing configuraiton file '" + s + "'.";
402  throw new IOException(em + e.getMessage());
403  }
404  return bot;
405 
406  }
407 
418  (final String dev_inst, final String obj_inst, final String obj_name, final String s)
419  throws FileNotFoundException, ParserConfigurationException, SAXException, IOException
420  {
421  File f = new File(s);
422  if(f.exists()!=true)
423  throw new FileNotFoundException("Configuration file '" + s + "' could not be found.");
424  BACnetObjectType bot = null;
425  try{
426  SAXParserFactory factory = SAXParserFactory.newInstance();
427  SAXParser saxParser = factory.newSAXParser();
428 
429  final ArrayList<BACnetPropertyValue> mapPI = new ArrayList<BACnetPropertyValue>();
430  final HashMap<String, String> mapOTname = new HashMap<String, String>();
431  final HashMap<String, String> mapOTinst = new HashMap<String, String>();
432  final HashMap<String, String> piOrder = new HashMap<String, String>();
433  final HashMap<String, String> otOrder = new HashMap<String, String>();
434  DefaultHandler handler = new DefaultHandler(){
435  boolean dev = false;
436  boolean obj = false;
437  int piID = 0;
438  int otID = 0;
439  public void startElement
440  (String uri, String localName, String qName, Attributes attributes)
441  throws SAXException
442  {
443  if (qName.equals("Object") &&
444  attributes.getValue("Type").equals("Device") &&
445  attributes.getValue("Instance").equals(dev_inst))
446  {
447  dev = true;
448  }
449  if(dev==true &&
450  qName.equals("Object") &&
451  convertObjectName(attributes.getValue("Type")).equals(obj_name) &&
452  attributes.getValue("Instance").equals(obj_inst))
453  {
454  obj = true;
455  mapOTname.put(Integer.toString(otID),obj_name);
456  mapOTinst.put(obj_inst,dev_inst);
457  otID = otID +1;
458  otOrder.put(obj_inst,Integer.toString(otID));
459  }
460  if(qName.equals("Object") &&
461  attributes.getValue("Type").equals("Device") &&
462  !attributes.getValue("Instance").equals(dev_inst))
463  {
464  dev = false;
465  }
466  if(dev==true && qName.equals("Object")
467  && !attributes.getValue("Instance").equals(obj_inst))
468  {
469  obj = false;
470  }
471  if(dev==true && qName.equals("Object")
472  && !convertObjectName(attributes.getValue("Type")).equals(obj_name))
473  {
474  obj = false;
475  }
476  if(obj==true && dev == true && qName.equals("PropertyIdentifier"))
477  {
478  String[] piArray= new String[4];
479  piArray[0] = "null"; //give a default value to PropertyValue
480  if(attributes.getValue("ApplicationTag") != null)
481  piArray[1] =
482  attributes.getValue("ApplicationTag").toUpperCase();
483  else
484  piArray[1] = "null";
485  /* Set priority default value to 15*/
486  if(attributes.getValue("Priority") != null)
487  piArray[2] = attributes.getValue("Priority");
488  else
489  piArray[2] = "15";
490  /* Set index default value to -1*/
491  if(attributes.getValue("Index") != null)
492  piArray[3] = attributes.getValue("Index");
493  else
494  piArray[3] = "-1";
496  (attributes.getValue("Name").toUpperCase(),piArray);
497  mapPI.add(bpv);
498  piID = piID + 1;
499  piOrder.put(attributes.getValue("Name").toUpperCase(),
500  Integer.toString(piID));
501  }
502  }
503 
504  };
505 
506  saxParser.parse(s, handler);
507  bot = new BACnetObjectType
508  (obj_name, obj_inst, mapPI, mapOTname, mapOTinst, piOrder, otOrder);
509  }
510  catch(SAXException e)
511  {
512  String em = "SAXException when parsing file '" + s + "'.";
513  throw new SAXException(em + e.getMessage());
514  }
515  catch(ParserConfigurationException e)
516  {
517  String em = "ParserConfigurationException when parsing file '" + s + "'.";
518  throw new ParserConfigurationException(em + e.getMessage());
519  }
520  catch(IOException e)
521  {
522  String em = "I/O error occurs when parsing configuration file '" + s + "'.";
523  throw new IOException(em + e.getMessage());
524  }
525  return bot;
526  }
527 
532  public String convertObjectName(String orStr)
533  {
534  return orStr.replace(" ","").concat("ObjectType");
535  }
536 
538  protected String bot_name;
539 
541  protected String bot_instance;
542 
544  protected ArrayList<BACnetPropertyValue> bot_mapPI;
545 
547  protected HashMap bot_mapOTname;
548 
551  protected HashMap bot_mapOTinst;
552 
554  protected HashMap bot_piOrder;
555 
557  protected HashMap bot_otOrder;
558 
560  private final static String LS = System.getProperty("line.separator");
561 }
BACnetObjectType()
Empty constructor.
for i
Definition: compile.m:69
This class reads the XML configuration file and stores the data in the BACnetObjectType data type...
BACnetObjectType readBacnetDeviceObjectType(final String dev_inst, final String s)
Read Bancet DeviceObjectType from xml configuration file.
String getBOTInst()
Get the instance of the bacnet object type.
HashMap getBACnetObjectTypeNameMap()
Get the map of the object type.
HashMap bot_otOrder
Hashmap containing the instance and index pair of object type.
ArrayList< BACnetPropertyValue > getBOTPI()
Get the map of the property identifier.
String bot_name
The BACnetObjectType name.
String convertObjectName(String orStr)
Convert object name to the same convention as in BacnetDeviceManager.
HashMap bot_piOrder
Hashmap containing the name and index pair of property identifier.
String bot_instance
The BACnetObjectType instance.
HashMap bot_mapOTname
Hashmap containing the index and name of the objecty type pair.
BACnetObjectType readBacnetNonDeviceObjectType(final String dev_inst, final String obj_inst, final String obj_name, final String s)
Read NonDevice BACnetObjectType from xml configuration file.
String getBOTName()
Get the bacnet object type name.
HashMap getBACnetObjectTypeInstanceMap()
Get the map of the object type.
HashMap getBACnetObjectTypePropertyIdentifierOrder()
Get the map of the property identifier index.
This class stores the BACnet property name and property value array pair.
ArrayList< BACnetPropertyValue > bot_mapPI
ArrayList with elements of BACnetPropertyValue.
BACnetObjectType(final String name, final String instance, final ArrayList< BACnetPropertyValue > mapPI, final HashMap mapOTname, final HashMap mapOTinst, final HashMap piOrder, final HashMap otOrder)
Construct an instance of BACnet object.
static final String LS
System-dependent line separator.
HashMap getBACnetObjectTypeOrder()
Get the map of the bacnet object type index.
HashMap bot_mapOTinst
Hashmap containing the index and object instance number pair for BACnet Device Object and device inst...
ArrayList< String > readDeviceInst(final String s)
Read ArrayList String from xml configuration file.
ArrayList< BACnetObjectType > parseObjectType(final String s)
Read bacnet object types from the xml configuration.