Building Controls Virtual Test Bed
BACnetWriter.java
Go to the documentation of this file.
1 // Write to a BACnet device
2 
3 /*
4 ********************************************************************
5 Copyright Notice
6 ----------------
7 
8 Building Controls Virtual Test Bed (BCVTB) Copyright (c) 2008-2009, The
9 Regents of the University of California, through Lawrence Berkeley
10 National Laboratory (subject to receipt of any required approvals from
11 the U.S. Dept. of Energy). All rights reserved.
12 
13 If you have questions about your rights to use or distribute this
14 software, please contact Berkeley Lab's Technology Transfer Department
15 at TTD@lbl.gov
16 
17 NOTICE. This software was developed under partial funding from the U.S.
18 Department of Energy. As such, the U.S. Government has been granted for
19 itself and others acting on its behalf a paid-up, nonexclusive,
20 irrevocable, worldwide license in the Software to reproduce, prepare
21 derivative works, and perform publicly and display publicly. Beginning
22 five (5) years after the date permission to assert copyright is obtained
23 from the U.S. Department of Energy, and subject to any subsequent five
24 (5) year renewals, the U.S. Government is granted for itself and others
25 acting on its behalf a paid-up, nonexclusive, irrevocable, worldwide
26 license in the Software to reproduce, prepare derivative works,
27 distribute copies to the public, perform publicly and display publicly,
28 and to permit others to do so.
29 
30 
31 Modified BSD License agreement
32 ------------------------------
33 
34 Building Controls Virtual Test Bed (BCVTB) Copyright (c) 2008-2009, The
35 Regents of the University of California, through Lawrence Berkeley
36 National Laboratory (subject to receipt of any required approvals from
37 the U.S. Dept. of Energy). All rights reserved.
38 
39 Redistribution and use in source and binary forms, with or without
40 modification, are permitted provided that the following conditions are met:
41 
42  1. Redistributions of source code must retain the above copyright
43  notice, this list of conditions and the following disclaimer.
44  2. Redistributions in binary form must reproduce the above copyright
45  notice, this list of conditions and the following disclaimer in
46  the documentation and/or other materials provided with the
47  distribution.
48  3. Neither the name of the University of California, Lawrence
49  Berkeley National Laboratory, U.S. Dept. of Energy nor the names
50  of its contributors may be used to endorse or promote products
51  derived from this software without specific prior written permission.
52 
53 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
54 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
56 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
57 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.%
64 
65 You are under no obligation whatsoever to provide any bug fixes,
66 patches, or upgrades to the features, functionality or performance of
67 the source code ("Enhancements") to anyone; however, if you choose to
68 make your Enhancements available either publicly, or directly to
69 Lawrence Berkeley National Laboratory, without imposing a separate
70 written license agreement for such Enhancements, then you hereby grant
71 the following license: a non-exclusive, royalty-free perpetual license
72 to install, use, modify, prepare derivative works, incorporate into
73 other computer software, distribute, and sublicense such enhancements or
74 derivative works thereof, in binary and source code form.
75 
76 ********************************************************************
77 */
78 
79 
80 package bacnet.actor;
81 
86 import bacnet.util.BACnet;
87 
88 import ptolemy.kernel.CompositeEntity;
89 import ptolemy.kernel.util.IllegalActionException;
90 import ptolemy.kernel.util.NameDuplicationException;
91 import ptolemy.kernel.util.InternalErrorException;
92 import ptolemy.kernel.util.Settable;
93 import ptolemy.actor.TypedIOPort;
94 import ptolemy.data.type.ArrayType;
95 import ptolemy.data.ArrayToken;
96 import ptolemy.data.type.BaseType;
97 import ptolemy.data.Token;
98 import ptolemy.data.IntToken;
99 import ptolemy.data.StringToken;
100 import ptolemy.data.ArrayToken;
101 import ptolemy.data.BooleanToken;
102 import ptolemy.data.expr.Parameter;
103 import java.io.File;
104 import java.util.ArrayList;
105 import java.util.HashMap;
106 import java.io.IOException;
107 import org.xml.sax.SAXException;
108 import javax.xml.parsers.ParserConfigurationException;
109 import javax.naming.OperationNotSupportedException;
110 import org.xml.sax.SAXParseException;
112 //BacnetWriter
113 
124 public class BACnetWriter extends BACnet {
125 
134  public BACnetWriter(CompositeEntity container, String name)
135  throws IllegalActionException,NameDuplicationException {
136  super(container, name);
137  /*Use input port to receive Ptolemy input tokens
138  */
139  input = new TypedIOPort(this, "input", true, false);
140  input.setMultiport(true);
141 
142  /*Use input token consumption rate to check whether there are tokens
143  * in the input port, used in prefire()
144  */
145  input_tokenConsumptionRate = new Parameter(input, "tokenConsumptionRate");
146  input_tokenConsumptionRate.setExpression("1");
147  input_tokenConsumptionRate.setVisibility(Settable.NOT_EDITABLE);
148  input_tokenConsumptionRate.setTypeEquals(BaseType.INT);
149  input_tokenConsumptionRate.setPersistent(false);
150 
151 
152  }
153 
156 
157 
164  public boolean prefire() throws IllegalActionException {
165  Token rateToken = input_tokenConsumptionRate.getToken();
166  int required = ((IntToken) rateToken).intValue();
167 
168  // Derived classes may convert the input port to a multiport.
169 
170  for (int i = 0; i < input.getWidth(); i++) {
171  if (!input.hasToken(i, required)) {
172  if (_debugging) {
173  _debug("Called prefire(), "
174  + " input tokenConsumptionRate = " + required
175  + ", input.hasToken(" + i + ", " + required
176  + ") is false, prefire() returning false");
177  }
178 
179  return false;
180  }
181  }
182  return super.prefire();
183  }
184 
193  public void checkNumberOfInput() throws IllegalActionException
194  {
195  final int size = input.getWidth();
196  final int num = prop_arr.size();
197  if(size != num){
198  String em = "The number of input '" + size + "' is ";
199  if(size > num)
200  em += "bigger ";
201  else
202  em += "less ";
203  em += "than the number"
204  + LS + "of properties found in the configuration file '" + num +"'.";
205  throw new IllegalActionException(em);
206  }
207  }
208 
215  public Token[] consumeToken() throws IllegalActionException
216  {
217  final int n = input.getWidth();
218  Token inp[] = new Token[n];
219  for(int i=0;i<n;i++)
220  inp[i]= input.get(i);
221  return inp;
222  }
223 
229  public ArrayList<BACnetCompleteProperty> addPropertyValue(Token[] inp) throws IllegalActionException
230  {
231  ArrayList<BACnetCompleteProperty> proparr = new ArrayList<BACnetCompleteProperty>();
232  for (int i = 0; i < prop_arr.size(); i++) {
233  BACnetCompleteProperty bcp = prop_arr.get(i);
234  int dev_inst_str = bcp.getDeviceInstance();
235  String obj_type = bcp.getObjectType();
236  int obj_inst = bcp.getObjectInstance();
237  String pro_name = bcp.getPropertyName();
238  String pro_value = inp[i].toString();
239  String apptag = bcp.getApplicationTag();
240  String priority = bcp.getPropertyPriority();
241  String index = bcp.getPropertyIndex();
242  BACnetCompleteProperty bcp_new = new BACnetCompleteProperty(dev_inst_str,
243  obj_type,obj_inst,pro_name,pro_value,
244  apptag,priority,index);
245  proparr.add(bcp_new);
246  }
247  return proparr;
248  }
249 
259  public void initialize() throws IllegalActionException
260  {
261  super.initialize();
262 
263  /* Check if application tag is part of the standard,
264  if priority and index are integers
265  */
267 
268  /* Check if number of tokens from Ptolemy is equal to
269  the number of properties read from the configuration file
270  */
272  }
273 
281  public void fire() throws IllegalActionException {
282  super.fire();
283  /*get input tokens from Ptolemy
284  */
285  Token[] inp = consumeToken();
286  /*add these input tokens to BACnetCompleteProperty
287  * as read from configuration file
288  */
290  /*Prepare processes to be executed
291  */
292  proc_arr = bacDevMan.getProcesses(prop_add_arr);
293  /*Prepare the tokens to be sent to outlet port
294  */
296  /*Send tokens to outlet port
297  */
298  sendToken();
299 
300  /*Remove token in last time step*/
301  removeToken();
302  }
303 
308  protected void setSchemaFileName(){
310  bcvtbhome + FS + "lib" + FS + "ptII" + FS + "bacnet" + FS + "BACnetWriter.xsd";
311  }
312 
315 
316  /* The input port. This base class imposes no type constraints except
317  * that the type of the input cannot be greater than the type of the
318  * output.
319  */
320  public TypedIOPort input;
321 
322  /* The rate parameter for the input port.
323  */
324  public Parameter input_tokenConsumptionRate;
325 
329  public static ArrayList<BACnetCompleteProperty> prop_add_arr;
330 }
String schemaFileName
Schema file path.
Definition: BACnet.java:897
for i
Definition: compile.m:69
static ArrayList< BACnetCompleteProperty > prop_add_arr
BACnetCompleteProperty after adding the value from Ptolemy.
This actor writes to BACnet devices.
This class reads the XML configuration file and stores the data in the BACnetObjectType data type...
void removeToken()
Remove tokens in previous time step.
Definition: BACnet.java:841
This is an abstract base class for actors that generates an output stream.
Definition: BACnet.java:117
static final String FS
File seperator.
Definition: BACnet.java:957
void fire()
At each timestep write properties to BACnet devices.
This object stores information of BACnet objects.
This class is used to create objects that have the information from the xml configuration file...
This class stores the BACnet property name and property value array pair.
Token[] consumeToken()
consume token, store the token in an array it is called in fire()
This file compiles the BSD socket interface for Simulink It is called by ant On it requires the Microsoft compiler since matlab requires the size of output arrays to be fixed If more elements are required
Definition: compile.m:5
static final String LS
Line Seperator.
Definition: BACnet.java:960
void sendToken()
Sends the error signal to the errorSignal port, the error message to the errorMessage port...
Definition: BACnet.java:804
void prepareTokensToSend()
Get tokens to send for the next timestep.
Definition: BACnet.java:784
void validateAttributes()
Validate application tag, priority and index.
Definition: BACnet.java:542
void setSchemaFileName()
Sets the schema file name.
boolean prefire()
Return true if the number of available tokens on the input port is at least the declared consumption ...
ArrayList< ProcessBuilder > proc_arr
Arraylist containing processes to be executed in console.
Definition: BACnet.java:920
ArrayList< BACnetCompleteProperty > addPropertyValue(Token[] inp)
Replace the property value in BACnetCompleteProperty with tokens from Ptolemy.
static final String bcvtbhome
String that points to root directory of the BCVTB.
Definition: BACnet.java:963
void initialize()
Initialize variables and check for errors.
BACnetWriter(CompositeEntity container, String name)
Construct an actor with the given container and name.
void checkNumberOfInput()
Check if the number of input tokens of the actor is equal to the number of properties read from the c...