78 import ptolemy.actor.TypedAtomicActor;
79 import ptolemy.actor.TypedIOPort;
80 import ptolemy.actor.lib.SequenceActor;
81 import ptolemy.data.IntToken;
82 import ptolemy.data.Token;
83 import ptolemy.data.ArrayToken;
84 import ptolemy.data.expr.Parameter;
85 import ptolemy.data.type.BaseType;
86 import ptolemy.kernel.CompositeEntity;
87 import ptolemy.kernel.util.IllegalActionException;
88 import ptolemy.kernel.util.NameDuplicationException;
89 import ptolemy.data.type.ArrayType;
90 import ptolemy.data.BooleanToken;
91 import ptolemy.data.StringToken;
92 import ptolemy.data.expr.FileParameter;
93 import ptolemy.kernel.util.Workspace;
94 import ptolemy.kernel.util.Attribute;
96 import java.io.FileNotFoundException;
97 import java.io.IOException;
98 import java.io.BufferedReader;
99 import java.io.InputStreamReader;
100 import java.util.ArrayList;
101 import java.util.Arrays;
102 import org.xml.sax.SAXException;
103 import javax.xml.validation.Schema;
104 import javax.xml.parsers.ParserConfigurationException;
117 public abstract class BACnet extends TypedAtomicActor implements SequenceActor {
128 public BACnet(CompositeEntity container, String name)
129 throws NameDuplicationException, IllegalActionException {
130 super(container, name);
134 errorSignal =
new TypedIOPort(
this,
"errorSignal",
false,
true);
135 errorSignal.setTypeEquals(BaseType.INT);
138 errorMessage =
new TypedIOPort(
this,
"errorMessage",
false,
true);
139 errorMessage.setTypeEquals(BaseType.STRING);
143 consoleArr =
new TypedIOPort(
this,
"consoleOutput",
false,
true);
144 consoleArr.setTypeEquals(
new ArrayType(BaseType.STRING));
148 new Parameter(configurationFile,
"allowFiles", BooleanToken.TRUE);
149 new Parameter(configurationFile,
"allowDirectories", BooleanToken.FALSE);
152 continueWhenError =
new Parameter(
this,
"continueWhenError",
new BooleanToken(
true));
153 continueWhenError.setTypeEquals(BaseType.BOOLEAN);
164 public Object
clone(Workspace workspace)
throws CloneNotSupportedException {
168 newObject.errorMessage.setTypeEquals(BaseType.STRING);
169 newObject.errorSignal.setTypeEquals(BaseType.INT);
170 newObject.consoleArr.setTypeEquals(
new ArrayType(BaseType.STRING));
171 newObject.configurationFile = (FileParameter) newObject
172 .getAttribute(
"configurationFile");
173 newObject.continueWhenError = (Parameter) newObject
174 .getAttribute(
"continueWhenError");
184 throws IllegalActionException {
188 super.attributeChanged(attribute);
231 checkOperatingSystem();
238 checkBACnetStackCompilation();
251 nDev = devins_arr.size();
269 nPro = prop_arr.size();
291 final File confile = configurationFile.asFile();
293 if(!confile.exists())
294 throw new IllegalActionException(
"The configuration file '"
295 + confile.getPath() +
296 "' cannot be found.");
298 catch(NullPointerException e)
301 if (e.getMessage() == null)
302 em =
"Failed to read configuration file in BACnetReader or BACnetWriter." +
LS +
303 "Check parameters of these actors.";
306 throw new IllegalActionException(em);
327 if(!schemaFile.exists())
328 throw new IllegalActionException(
"The schema file '" +
schemaFileName
329 +
"' cannot be found.");
338 void checkOperatingSystem() throws IllegalActionException
340 if(
osName.startsWith(
"Mac"))
341 throw new IllegalActionException(
"This program is only working on Windows and Linux."
342 +
LS +
"The operating system '" +
osName +
"' is not supported.");
352 void checkBACnetStackCompilation() throws IllegalActionException
354 String globalwi = bacDevMan.getGlobalwiBinaryPath();
355 String bacrp = bacDevMan.getReadPropertyBinaryPath();
356 String bacwp = bacDevMan.getWritePropertyBinaryPath();
357 File fglobalwi =
new File(globalwi);
358 File fbacrp =
new File(bacrp);
359 File fbacwp =
new File(bacwp);
360 if(!fglobalwi.exists() || !fbacrp.exists() || !fbacwp.exists())
361 throw new IllegalActionException
362 (
"The BACnet-stack library has not been compiled."
363 +
LS +
"Please compile it.");
376 throws IllegalActionException
378 ArrayList<BACnetObjectType> confdata =
new ArrayList<BACnetObjectType>();
382 catch(SAXException e)
384 throw new IllegalActionException(e.getMessage());
386 catch(ParserConfigurationException e)
388 throw new IllegalActionException(e.getMessage());
392 throw new IllegalActionException(e.getMessage());
406 IllegalActionException
408 ArrayList<Integer> devint =
new ArrayList<Integer>();
410 devint = bacDevMan.readDeviceArray(
bot_arr);
412 catch(NumberFormatException e)
414 throw new IllegalActionException(e.getMessage());
429 ArrayList<Integer> devintcon =
new ArrayList<Integer>();
430 devintcon = bacDevMan.findDevice();
446 for(
int i=0;
i<devins_arr.size();
i++)
448 int dev = devins_arr.get(
i);
449 boolean foundDevice =
false;
450 for(
int j=0; j<devcon_arr.size(); j++)
457 String em =
"The specified device with instance number "
458 + Integer.toString(dev) +
" cannot be found in the network,"
459 +
LS +
" please check your configuration file and check the network connection.";
460 throw new IllegalActionException(em);
477 for(
int i=0;
i<devins_arr.size();
i++)
479 int devinst = devins_arr.get(
i);
480 ArrayList<BACnetObject> objlist =
new ArrayList<BACnetObject>();
484 objlist = bacDevMan.readObjectList(
bot_arr,devinst);
486 catch(NumberFormatException e)
488 throw new IllegalActionException(e.getMessage());
491 for(
int j=0;j<objlist.size();j++)
496 bacDevMan.validateObjectType(bacobj.getObjectType());
498 catch(IllegalArgumentException e)
500 throw new IllegalActionException(e.getMessage());
517 for(
int i=0;
i<bot_arr.size();
i++)
521 ArrayList<BACnetPropertyValue> mapPI = bot.getBOTPI();
523 for(
int j=0; j<mapPI.size();j++)
526 String piname = bpv.getName();
527 bacDevMan.validatePropertyName(piname);
544 for(
int i=0;
i<bot_arr.size();
i++)
548 ArrayList<BACnetPropertyValue> mapPI = bot.getBOTPI();
550 for(
int j=0; j<mapPI.size();j++)
553 String[] propval = bpv.getValue();
554 String apptag = propval[1];
556 bacDevMan.validateApplicationTag(apptag);
573 throws IllegalActionException
575 ArrayList<BACnetCompleteProperty> proparr =
new ArrayList<BACnetCompleteProperty>();
576 for(
int i=0;
i<devins_arr.size();
i++)
578 int devins = devins_arr.get(
i);
579 ArrayList<BACnetCompleteProperty> devproparr =
580 new ArrayList<BACnetCompleteProperty>();
583 devproparr = bacDevMan.readDevicePropertyList(
bot_arr,devins);
585 catch(NumberFormatException e)
587 throw new IllegalActionException(e.getMessage());
590 for(
int j=0; j<devproparr.size();j++)
596 proparr.trimToSize();
605 Object[] propval_arr_obj =
new String[
nPro];
606 String[] propval_arr_str = (String[])
propval_arr.toArray(propval_arr_obj);
607 String[] propval_arr_temp_str =
new String[propval_arr_obj.length];
608 System.arraycopy(propval_arr_str,0,propval_arr_temp_str,0,
nPro);
609 propval_arr_temp =
new ArrayList<String>(Arrays.asList(propval_arr_temp_str));
611 Object[] consout_arr_obj =
new String[
nPro];
612 String[] consout_arr_str = (String[])
consout_arr.toArray(consout_arr_obj);
613 String[] consout_arr_temp_str =
new String[consout_arr_obj.length];
614 System.arraycopy(consout_arr_str,0,consout_arr_temp_str,0,
nPro);
615 consout_arr_temp =
new ArrayList<String>(Arrays.asList(consout_arr_temp_str));
640 throws IllegalActionException
645 String infoline =
"";
649 for(
int i=0;
i<process_arr.size();
i++){
651 p = process_arr.get(
i).redirectErrorStream(
true).start();
655 String em =
"Error while executing processes."
656 +
LS + e.getMessage();
660 throw new IllegalActionException(em);
662 catch(NullPointerException e)
664 String em =
"Some of the command arguments are null."
665 +
LS + e.getMessage();
669 throw new IllegalActionException(em);
671 catch(IndexOutOfBoundsException e)
673 String em =
"The proecss list is empty." +
678 throw new IllegalActionException(em);
680 catch(SecurityException e)
682 String em =
"Creation of the process is not allowed."
683 +
LS + e.getMessage();
687 throw new IllegalActionException(em);
689 BufferedReader br =
new BufferedReader(
new InputStreamReader
690 (p.getInputStream()));
692 while((line=br.readLine())!=null)
694 allline = allline + line;
695 if(!line.startsWith(
"Error:") &&
696 !line.contains(
"BACnet Abort") && !line.contains(
"BACnet Reject")
697 && !line.contains(
"BACnet Error") && !line.contains(
"Null")
698 && !line.contains(
"TSM Error") && !line.contains(
"TSM Timeout")
699 && !line.contains(
"Abort") && !line.contains(
"APDU Timeout")
700 && !line.contains(
"Unknown Property")){
702 infoline = infoline + line;
706 String em =
"The property with index " + (
i+1) +
707 " in the configuration file cannot be "
708 +
"read." +
LS +
"Please check whether this property exists." +
LS
709 +
"The response from BACnet is '" + line +
"'." +
LS;
713 throw new IllegalActionException(em);
717 propval_arr.add(propval_arr_temp.get(
i));
724 String em =
"Error while reading response." +
729 throw new IllegalActionException(em);
734 catch(InterruptedException e)
736 String em =
"The process of running bacnet stack" +
LS
737 +
"command has been interrupted" +
LS +
742 throw new IllegalActionException(em);
744 consout_arr.add(allline);
746 propval_arr.add(infoline);
748 int exit = p.exitValue();
751 String em =
"BACnet-stack process quit abnormally.";
755 throw new IllegalActionException(em);
758 exitval_arr.add(exit);
760 consout_arr.trimToSize();
761 propval_arr.trimToSize();
762 exitval_arr.trimToSize();
770 Token[] valArray =
new Token[data_str.size()];
771 for(
int i=0;
i<data_str.size();
i++)
772 valArray[
i] =
new StringToken( data_str.get(
i) );
806 errorSignal.send(0,
new IntToken(
_errsig));
807 errorMessage.send(0,
new StringToken(
_errmsg));
808 consoleArr.send(0,
new ArrayToken(BaseType.STRING,
consout_tok));
823 catch(FileNotFoundException e)
825 throw new IllegalActionException(e.getMessage());
829 throw new IllegalActionException(e.getMessage());
831 catch (SAXException e) {
832 throw new IllegalActionException(e.getMessage());
916 protected ArrayList<BACnetCompleteProperty>
prop_arr;
957 public final static String
FS = System.getProperty(
"file.separator");
960 public final static String
LS = System.getProperty(
"line.separator");
963 public final static String
bcvtbhome = System.getenv(
"BCVTB_HOME");
966 public final static String
osName= System.getProperty(
"os.name");
void checkConnection()
Check if the connection is lost.
String schemaFileName
Schema file path.
void checkSchemaFileExists()
Check if schema file exists.
void validateDevice()
validate device specified in configuration file.
boolean _continueWhenError
Parameter to control whether to contine if there was an error message.
ArrayList< Integer > readDeviceArray()
Read the device array from the configuration file.
BACnetDeviceManager bacDevMan
The BACnet device manager.
ArrayList< Integer > devins_arr
ArrayList containing device instance number read from configuration file.
boolean throwExceptions()
Returns true if an exception should be thrown, or false if only an error message should be sent to th...
void validatePropertyName()
Validate property name.
Object clone(Workspace workspace)
Clone the actor into the specified workspace.
ArrayList< String > consout_arr
ArrayList containing strings of response from device for both reading and writing process...
void validateObjectType()
Validate Object Type.
This class reads the XML configuration file and stores the data in the BACnetObjectType data type...
int _errsig
signal to indicate whether there is an error, 0 means right, 1 means wrong
String configurationFilePath
String of configuration file path.
ArrayList< BACnetObjectType > readXMLConfigurationFile()
Read the xml configuration file, store the elements to BACnetObjectType ArrayList.
int _firecount
parameter indicate how many times executeProcess function has been run
void removeToken()
Remove tokens in previous time step.
int nDev
Number of BACnet devices specified in the configuration file.
void validateSyntax()
Validate xml configuration file syntax.
This is an abstract base class for actors that generates an output stream.
void checkConfFileExists()
Check if configuration file exists.
static final String FS
File seperator.
ArrayList< String > propval_arr
ArrayList containing strings of prperty values read from device.
void setConfigurationFilePath()
Set configuration file path.
BACnet(CompositeEntity container, String name)
Construct an actor with the given container and name.
Parameter errorMessage_tokenProductionRate
The rate parameter for the errorMessage port.
This object stores information of BACnet objects.
This class is used to create objects that have the information from the xml configuration file...
Token[] consout_tok
Token array of console outputs.
ArrayList< String > consout_arr_temp
ArrayList temporarily holding console outputs read from previous time step.
void executeProcess(ArrayList< ProcessBuilder > process_arr)
Execute Processes, store results to consoleoutput_arr and exitvalue-arr.
void initialize()
Initialization section, initialize variiables and check possible errors.
This class stores the BACnet property name and property value array pair.
static final String osName
Name of the operating system.
void attributeChanged(Attribute attribute)
Override the base class to determine which argument is being used.
FileParameter configurationFile
Parameter to specify the path for configuraton file.
This is the driver class in BACnet.
ArrayList< BACnetCompleteProperty > prop_arr
Arraylist containing BACnetCompleteProperty read from configuration file.
TypedIOPort errorMessage
The errorMessage port.
ArrayList< Integer > devcon_arr
ArrayList containing device instance number that are found in the network.
Parameter continueWhenError
Parameter to control whether to contine if there was an error message.
TypedIOPort consoleArr
The port that send response from console.
static final String LS
Line Seperator.
void sendToken()
Sends the error signal to the errorSignal port, the error message to the errorMessage port...
Token[] transformArrayList(ArrayList< String > data_str)
Transform String Arraylist to Token Arrays.
void prepareTokensToSend()
Get tokens to send for the next timestep.
void validateAttributes()
Validate application tag, priority and index.
ArrayList< ProcessBuilder > proc_arr
Arraylist containing processes to be executed in console.
void storeDataInLastTimeStep()
Store response from last time step.
ArrayList< String > propval_arr_temp
ArrayList temporarily holding property values read from previous time step.
ArrayList< BACnetObjectType > bot_arr
ArrayList containing elements read from configuration file.
TypedIOPort errorSignal
The port that outputs the error signal.
String _errmsg
detailed error message
int nPro
Number of BACnet properties specified in the configuration file.
static final String bcvtbhome
String that points to root directory of the BCVTB.
Token[] propval_tok
Token array of property values.
Parameter errorMessage_tokenInitProduction
The rate parameter for the errorMessage port that declares the initial production.
ArrayList< Integer > exitval_arr
ArrayList containing strings process exit values for each process.
ArrayList< BACnetCompleteProperty > readDevicePropertyArray()
Read all properties listed in the configuration file.
abstract void setSchemaFileName()
Abstract class that sets the schema file name.