usingdatabaseserverbeansinscripting
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| usingdatabaseserverbeansinscripting [2021/07/28 14:34] – created wikiadmin | usingdatabaseserverbeansinscripting [2021/10/11 18:48] (current) – wikiadmin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| =====Scripting for Database Servers (in MIStudio and TransSECS)===== | =====Scripting for Database Servers (in MIStudio and TransSECS)===== | ||
| - | Although there is a lot you can do with just using Database Servers set up in the MIStudio logic and in the Devices of TransSECS and MIStudio, using scripts that reference these beans can be a powerful tool. | + | Although there is a lot you can do with just using Database Servers set up in the MIStudio logic and in the Devices of TransSECS and MIStudio, using scripts that reference these beans can be a powerful tool. Recipe Management is the most common use case for using TransSECS Devices with databases. If the recipes are stored locally (file based recipes) then you might want to consider the automatic file-based recipe handling of TransSECS (by setting transsecs.recipemanager=1 in the ErgoTechConfiguration.properties of your project deployment). |
| These examples assume that you have already set up your database (either local or remote) and have set up the connection in your project using a generic (or specific) DatabaseConnectionManager for your database. In MIStudio you will need to be in LIVE mode (toggle from SIM -> LIVE) to enable the database connection during development. | These examples assume that you have already set up your database (either local or remote) and have set up the connection in your project using a generic (or specific) DatabaseConnectionManager for your database. In MIStudio you will need to be in LIVE mode (toggle from SIM -> LIVE) to enable the database connection during development. | ||
| - | =====Triggering a Database Server===== | + | ====Triggering a Database Server==== |
| - | Database servers can be triggered in a script to run the existing SQL statement. Be sure the SQL statement is set before triggering the bean. Use setSQLStatement() to set the SQL statement, if needed. Also make sure the server has been properly configured to use the DatabaseConnectionManager. | + | Database servers can be triggered in a script to run the existing SQL statement |
| - | If the database bean is writing data to the database, the SQL Statement should include this current data. No output is generated from the bean when it is triggered. | + | If the database bean is writing data to the database, the SQL Statement should include this current data to write to specific columns of the table. This can be either an UPDATE or INSERT statement. No output is generated from the bean when it is triggered. This type of bean can also use a CREATE statement. |
| - | If the database bean is reading data from the database, the output of the bean after triggering will contain a result (either a single value, array, or 2D array, depending on the bean type). | + | If the database bean is reading data from the database, the output of the bean after triggering will contain a result (either a single value, array, or 2D array, depending on the bean type). The SQL statement would typically be a SELECT statement to read this data. |
| + | ====Triggered Historical==== | ||
| + | |||
| + | The TriggeredHistorical server can also be triggered from a script. However, this bean does not use a SQL Statement, instead it uses the current values connected to its input connection. When triggered the current values and the timestamp of the trigger are recorded in a row in the specified database table. The column names used in the table must match the name of the data sources connected to the input. | ||
| + | |||
| + | Instead of using values connected directly into the TriggeredHistorical input, you can trigger the database write using one of two alternate inputs: mapTrigger() or jsonTrigger(). mapTrigger() takes a ValueObject with a " | ||
| + | |||
| + | Using mapTrigger and jsonTrigger in scripting allows you more flexibility in using various data sources and specifying individual column names in the table for this data. For the regular trigger() in TriggeredHistorical, | ||
| + | |||
| + | The script below assumes there is a bean called TriggeredHistorical set up with a valid DatabaseConnectionManager with a table set up (and the CreateTable property set to true). It is triggered using mapTrigger() with a set of data for two columns in the table called value1 and value2. These values are read from two BroadcastServers in the logic called " | ||
| + | |||
| + | {{: | ||
| + | |||
| + | <code JavaScript> | ||
| + | //test writing to triggeredhistorical using mapTrigger() | ||
| + | |||
| + | var LinkedHashMap = Java.type(" | ||
| + | var StringValueObject = Java.type(" | ||
| + | var ValueChangedEvent = Java.type(" | ||
| + | |||
| + | dataMap=new LinkedHashMap(); | ||
| + | |||
| + | //put the data into the map | ||
| + | |||
| + | bcs1 = BCS1-> | ||
| + | bcs2 = BCS2-> | ||
| + | |||
| + | dataMap.put(" | ||
| + | dataMap.put(" | ||
| + | |||
| + | |||
| + | triggerValue = new StringValueObject(" | ||
| + | triggerValue.setProperty(" | ||
| + | |||
| + | //write to database -- this assumes TriggeredHistorical is set up and the database is live | ||
| + | |||
| + | |||
| + | TriggeredHistorical-> | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | Another example of using mapTrigger() (this example uses a TriggeredHistorical in the Devices node of the project): | ||
| + | |||
| + | [[host_connection_status# | ||
| - | ===== | ||
usingdatabaseserverbeansinscripting.1627497279.txt.gz · Last modified: by wikiadmin
