Alert nofitication API Client
Preparation
Obtaining the client library
- /bidal/alert-api-client/blob/develop/build/AlertApiClientModule.js
Obtain the above source and load on the HTML side.
<script src='/path/to/AlertApiClientModule.js'></script>
When loaded as above, it will be possible to use the class named AlertApiClient.
Obtaining an API key
To use the Alert notification API, it is necessary to apply for and obtain an API key and secret key.
Example of Execution
A sample call is shown below.
function onAlertResponse(message) {
let json = JSON.parse(message);
console.log(json);
}
function onAlertError(message) {
let json = JSON.parse(message);
console.log(json);
}
let features = [];
let coodinates = [
[
[135.19050, 34.69430],
[135.19050, 34.68681],
[135.20224, 34.68681],
[135.20224, 34.69430],
[135.19050, 34.69430]
]
];
let feature = { type: "Feature", geometry: { type: "Polygon", coodinates: coodinates }, properties: { comment: "foobar", theme: "rm_xrain", threshold: 1, interval: 60 } };
features.push(feature);
let host = location.host;
let client_options = {
onalertmessage: (jsonrpc_response) => onAlertResponse(jsonrpc_response),
onerrormessage: (jsonrpc_response) => onAlertError(jsonrpc_response),
timeout: 5e3,
maxAttempts: 10,
onopen: (event) => console.log('Connected!', event),
onmessage: (messageevent) => console.log('Received:', messageevent),
onreconnect: (event) => console.log('Reconnecting...', event),
onmaximum: (closeevent) => console.log('Stop Attempting!', closeevent),
onclose: (closeevent) => console.log('Closed!', closeevent),
onerror: (event) => console.log('Error:', event),
apikey: "alertapikey_0123456",
secret: "alertapisecret_0123456",
apipath: "/api/v1/alertapi/socket.io/"
};
var client = new AlertApiClient(host, client_options);
client.setAlert(1, features, 3, "2016-09-04 17:00:00+09", "2019-01-10 12:00:00+09");
client.alterAlert(1, features, 3, "2016-09-04 17:00:00+09", "2019-01-10 12:00:00+09");
Usage Instructions
AlertApiClient class
function onAlertResponse(message) {
let json = JSON.parse(message);
console.log(json);
}
...
let client_options = {
onalertmessage: (jsonrpc_response) => onAlertResponse(jsonrpc_response),
onerrormessage: (jsonrpc_response) => onAlertError(jsonrpc_response),
....
};
var client = new AlertApiClient(host, client_options);
client.setAlert(1, features, 3, "2016-09-04 17:00:00+09", "2019-01-10 12:00:00+09");
When the AlertApiClient object is created and setAlert is executed, the Alert notification API transmits an alert notification message to the browser based on the conditions. At this time, the defined onAlertResponse is executed as a callback function.
In the above example, a json object is displayed in the browser console each time an alert notification is issued.
For details of alert notification messages passed to jsonrpc_response ("message" in the main section of the callback function), see notifyAlert.
Constructor
Parameter | Variable Name during Sample Execution | Details |
---|---|---|
1st Parameter | host | Host name that calls the AlertAPI |
2nd Parameter | client_options | Object summarizing functions, etc. called when Alert notification API has transmitted an alert notification message to the browser |
The object of the 2nd parameter should be made up of functions and a group of text strings using the following keys.
As the function specified with onalertmessage is called when an alert is received, the operations when an alert is received are implemented in this function.
Implement other callback functions as necessary.
Key | Details |
---|---|
onalertmessage | Specifies the callback function called in (notifyAlert) when an alert notification message transmitted by the Alert notification API is received. In the example of execution, when the callback function onAlertResponse is called, a JSON-RPC text string is passed as the message of the parameter. |
onerrormessage | Specifies the callback function called when an error message sent to the browser by the Alert notification API is received. In the example of execution, when the callback function onAlertError is called, a JSON-RPC text string is passed as jsonrpc_response. |
timeout | Specifies the timeout length in seconds. In the example of execution (5e3), this is set to 5000 seconds. The default value is 1000 seconds. |
maxAttempts | Specifies the number of retries when a connection error occurs. The default is infinity (no limit). |
onopen | Specifies the callback function called when communication is started. The default is null. Complies with onopen in the WebSocket properties. |
onmessage | Specifies the callback function called when a message is received from Alert notification API. The default is null. Complies with onmessage in the WebSocket properties. |
onmaximum | Specifies the callback function called during reconnection. The default is null. Complies with onclose in the WebSocket properties. |
onclose | Specifies the callback function called when a connection is closed. The default is null. Complies with onclose in the WebSocket properties. |
onerror | Specifies the callback function called when a connection error occurs. The default is null. Complies with onerror in the WebSocket properties. |
apikey | Specifies the API key that was obtained. |
secret | Specifies the secret key that was obtained. |
apipath | Specifies the text string of /api/v1/alertapi/socket.io/. |
setAlert
This is the method that specifies the alert acquisition conditions for the API when starting use of the Alert notification API.
Explanations on the following are provided based on the example of execution.
client.setAlert(1, features, 3, "2016-09-04 17:00:00+09", "2019-01-10 12:00:00+09");
Parameter | Details | ||
---|---|---|---|
1st Parameter | The ID in jsonrpc. Specify an arbitrary number. | ||
2nd Parameter | The array of the feature based on GeoJSON specifications. Specify the range for which you wish to obtain alerts with this feature. | ||
Required parameter in the properties of each feature | |||
theme | Specify the monitored theme name. | ||
threshold | Lower limit of risk values. Specify 0 when acquiring all risk values. | ||
interval | Specify the check interval (seconds). | ||
3rd Parameter | Specify the in-simulation replay speed (times). | ||
4th Parameter | This is the in-simulation time and date to start monitoring. If omitted, the current date and time will be used. Specify the date and time in the format "YYYY-MM-DD HH-MI-SS+09". | ||
5th Parameter | This is the actual world time when notification is started. If omitted, the current date and time will be used. Specify the date and time in the format "YYYY-MM-DD HH-MI-SS+09". |
alterAlert
This method changes the alert acquisition conditions for the API during use of the Alert notification API.
Explanations on the following are provided based on the example of execution.
client.alterAlert(1, features, 3, "2016-09-04 17:00:00+09", "2019-01-10 12:00:00+09");
While setAlert is the same for each parameter, if a theme that is not used with setAlert is specified, an error will be produced.
Theme
See the manual for information and the "theme" of properties.
Example of onalertmessage
See the notifyAlert manual for the details passed as parameters of the callback function specified with onalertmessage (jsonrpc_response and message in the main section of the callback function in the usage example).