Java Callback Methods API
Contents
Java Callback Methods
Callback methods are called by the HSYCO events management system.
By implementing these methods, you can associate your custom Java code to each event.
In some cases, the return values of these methods can influence the behavior of HSYCO.
Most of the methods, propagate exceptions to the top, that is to HSYCO internal code. In this case, HSYCO generates an error message in the log.
System Functions
DaylightEvent
DaylightEvent(day)
Called at sunrise and sunset, according to the latitude and longitude values set in hsyco.ini, and the optional SunriseOffsetMinutes and SunsetOffsetMinutes parameters.
Parameters:
- day: boolean - true at sunrise, false at sunset.
getUserVersion
static String getUserVersion()
This method returns a string, used by the HSYCO run-time to generate the user code version information log line at startup.
It is recommended to return a meaningful string with an appropriate identification of your user.java code and its version.
Returns:
- String - user code version information.
haActiveEvent
haActiveEvent(active)
Triggered by the change of state of an HSYCO server in a master/slave high availability configuration.
Parameters:
- active: boolean - true if the server is active, false if not active.
PowerEvent
PowerEvent(power)
Triggered by a change of the current power load level, as set using the powerSet() Java API or the POWER action.
Thanks to this method it is possible to alter the power value shown in the Web interface, for example to aggregate power readings acquired from other sensors.
Parameters:
- power: numeric - the power level, in Watts.
Returns:
- numeric - if PowerEvent() returns -1 or doesn't return a value, HSYCO status is updated with the detected power value, the value returned by PowerEvent() is otherwise used.
The JavaScript PowerEvent function is always called, but its return value is used only when the Java PowerEvent callback is not defined or returned -1.
programTimerEvent
static void programTimerEvent(String name)
Called when a program timer is activated.
Program timers are set using programTimerSet(), programTimerReset(), programTimerRepeat(), or using the corresponding actions in EVENTS.
Parameters:
- name: String - program timer name.
SchedulerEvent
SchedulerEvent(groupname, schedulename)
This callback blocking function allows to call user methods at configurable intervals.
You define schedules using a group name and a schedule name.
Schedules under the same group run in the same thread and are executed sequentially, based on their interval in milliseconds. Schedules in different groups run in parallel.
Parameters:
- groupname: string - the scheduler’s group name
- schedulename: string - the scheduler’s name.
StartupEvent
static void StartupEvent()
Called only once when HSYCO starts, after the initialization and HTTP server start-up, but before the execution of the different field interface threads. It is not executed in a thread, it is therefore a blocking method, that must complete its execution before all other HSYCO services can be started.
SunPositionEvent
SunPositionEvent(azimuth, elevation)
Called when the Sun changes its height with respect to the horizon or its angle from true north.
Parameters:
- azimuth: numeric - the current Sun angle from true north, in decimal degrees
- elevation: numeric - the current Sun elevation in decimal degrees from the horizon. Elevation is negative at night.
TimeEvent
static void TimeEvent(long time)
Called every 60 seconds.
HSYCO tries to synchronize the execution at the beginning of the minute, executing this method at seconds 0 of every minute.
However, the execution is not guaranteed. In case of heavy load HSYCO might not be able to execute the call with accurate timing. In extreme cases, it could occasionally skip some calls.
Parameters:
- time: long - the current time in milliseconds.
varEvent
varEvent(name, value)
Triggered by the change of value of a program variable.
Parameters:
- name: string - the variable name. Names are case-insensitive
- value: string - the value to be assigned to the program variable.
Cameras
CameraCommandEvent
static int cameraCommandEvent(String function, String action, String camera)
Triggered by a camera control input from the Web interface.
It can be used to execute arbitrary actions when the user clicks on the active areas of the camera view, or to replace some or all of the standard PTZ commands with custom actions.
Parameters:
- function: String - see the table below
- action: String - see the table below
- camera: String - the camera name.
Returns:
- int - if CameraCommandEvent() returns -1 or doesn't return a value, the standard PTZ command associated to the PTZ driver is skipped, otherwise it is executed normally.
|
CameraMotionEvent
static void CameraMotionEvent(String eventName, long remoteTime)
Called when HSYCO starts recording video from a camera.
Recording starts when a camera notifies a recording request with the recording HTTP API call (the zone=id portion is optional):
http://192.168.0.50/x/camerarec?camera=name&zone=id
Recording can also be triggered by the CameraRecTrigger() Java method or JavaScript function, or the CAMERAREC or CAMERARECFULL actions in EVENTS.
Parameters:
- eventName: String - the event identification string, either the camera name only or name:id if recording is triggered by a camerarec HTTP call with the optional zone=id parameter
- remoteTime: long - the current time in milliseconds.
CameraViewEvent
static void CameraViewEvent(String camera, boolean active)
CameraViewEvent() is called on changes of the live view status of cameras.
When at least one web client is showing the live feed from a camera, the camera is marked as active.
When no live feed request is received for a several consecutive seconds, the camera is marked as not active.
Parameters:
- camera: String - the camera name
- active: boolean - true if active, false if not.
DMX
DmxEvent
static void DmxEvent(int channel, int state)
Called after changes to DMX-512 channels.
Parameters:
- channel: int - DMX channel address, from 1 to 512 which might be preceded by the DMX gateway number, starting from 0
- state: int - the new channel value, from 0 to 255.
DmxFilter
static int DmxFilter(int channel, int state, boolean reverse)
This method allows to filter the value of the status of each channel on the DMX-512 buses, sending to the gateway different values from those set in HSYCO.
This way, it is possible to apply chromatic corrections, or any on the fly translation of channels values.
This is a blocking method and is called when sending commands to the DMX gateway.
It is also called after reading current channels values from the gateway. In this case the conversion function should be symmetric and return a complementary value.
Parameters:
- channel: int - the DMX channel address, from 1 to 512 which might be preceded by the DMX gateway number, starting from 0
- state: int - the unfiltered value of the channel, from 0 to 255
- reverse: boolean - false if called when writing to the gateway; true if called when reading from the gateway.
Returns:
- int - the channel value that will be sent to the DMX gateway.
The JavaScript DmxFilter function is called only when the Java DmxFilter callback is not defined or returned the original value.
DmxStartupEvent
static void DmxStartupEvent(int serverIndex)
Called when starting the monitor threads for each DMX bus, once per bus at the start of the execution of HSYCO, and also after every restart of the monitor thread, for example after communication errors.
It is safe, inside this method, to execute command methods for the same DMX gateway.
This is a blocking method. The DMX driver will start after this function returns.
Parameters:
- serverIndex: int - number of the DMX bus, starting from 0.
Infrared Control
IREvent
static void IREvent(boolean received, int irtransid, String event)
This method is called when an IRTrans receives or sends an IR command from its local IR database.
IRTrans can only receive and decode infrared commands that have been previously stored in its internal FLASH memory database.
Parameters:
- received: boolean - true if the command has been received by the IRTrans, false if sent
- irtransid: int - IRTrans number, starting from 0 for the first device, based on the listing order of the IRTrans parameter in hsyco.ini
- event: String - the string of the command received or issued, formatted as: "remote_name,command".
I/O Servers
IOEvent
static void IOEvent(String id, String value)
Triggered by the value change of any data point of an I/O server.
Parameters:
- id: String - the server id and the name of the data point. According to the type of server, the format changes but it generally appears as server name, as declared the ioServers parameter in hsyco.ini, followed by a specific name representing the data point within the system
- value: String - the new value. For binary inputs or outputs, the returned values are “0” or “1”.
IOStartupEvent
static void IOStartupEvent(int serverIndex)
Triggered by the start of the communication thread of each I/O server - once for each server at the beginning of the execution of HSYCO, and also after every reboot of the communication thread, for example after communication errors.
This is a blocking method. The I/O server driver will start after this function returns.
Parameters:
- serverIndex: int - the I/O server number, starting from 0, based on the listing order of the ioServers parameter in hsyco.ini.
Modbus
ModbusEvent
static byte[] ModbusEvent(String name, InetAddress addr, int unitid, byte[] pdu)
Called by the Modbus Server I/O Server on every Modbus request received from a Modbus client.
The returned byte array is used as the Modbus response PDU (function code and data).
Parameters:
- name: String - the name of the Modbus Server I/O server
- addr: InetAddress - IP address of the Modbus client
- unitid: int - the unit id set in the received Modbus request
- pdu: byte[] - the request PDU data, in hexadecimal string format.
Returns:
- byte[] - the response PDU data.
Network Services
LocationEvent
static void LocationEvent(long mac, InetAddress ip, int zoneId)
Called, if the location service is active, when a variation in the association of a client to the Wi-Fi Access Points is detected.
Parameters:
- mac: long - the MAC address of the client
- ip: InetAddress - the IP address of the client, if available, or null (the IP address could be null if the associated device is not connected to the HSYCO Web interface)
- zoneId: int - in case of association, it is the Access Point sequence number, according to the order defined in the LocationBases parameter in hsyco.ini; -1 if the client was de-associated from the Wi-Fi network.
PBX
PBXCallEvent
static boolean PBXCallEvent(String host, String caller, String called)
Called when a call notification is sent to HSYCO by the PBX system.
If the method returns true, the request is recorded on the log file with the [OK] status, with [ERROR] otherwise.
Parameters:
- host: String - the IP address of the PBX server
- caller: String - the caller number
- called: String - the called number.
Returns:
- boolean - return true to generate a log entry with [OK] status, false for [ERROR] status.
Squeezebox
SlimPowerEvent
static void SlimPowerEvent(int index, int power)
Called when a Squeezebox player is turned on or off.
Parameters:
- index: int - player number, starting from 0 for the first player, based on the listing order of the slimPlayers parameter in hsyco.ini
- power: int - 0: OFF, 1: ON.
SlimStatusEvent
static void SlimStatusEvent(int index, int status)
Called when a Squeezebox player changes its playback mode.
Parameters:
- index: int - player number, starting from 0 for the first player, based on the listing order of the slimPlayers parameter in hsyco.ini
- status: int - 0: OFF, 1: ON, 2: PAUSE, 3: PLAY, -1: UNKNOWN.
SlimVolumeEvent
static void SlimVolumeEvent(int index, int volume)
Called when a Squeezebox player changes its volume level.
Parameters:
- index: int - player number, starting from 0 for the first player, based on the listing order of the slimPlayers parameter in hsyco.ini
- volume: int - an integer number representing the volume change (not the absolute audio level) in a scale from 0 to 100; positive numbers correspond to a volume increase, negative numbers to a decrease.
Timers and Schedulers
UserTimerEvent
static boolean UserTimerEvent(String name, boolean active)
Called when a user timer or scheduler’s rule should be activated, and before executing the associated action in EVENTS.
It is also executed at the timer or scheduler’s rule deactivation.
This method must return a true value in order to make the timer or rule actually active and the associated actions executed.
Whenever the method returns false, the timer will not be activated, and the method will be called again once a minute for the scheduled duration of the timer.
In the same way, if the method returns false when the timer or rule is deactivated, the timer or rule will not be deactivated, extending the timer activation beyond the scheduled duration.
This is a blocking method.
Parameters:
- name: String - timer name or scheduler’s rule name
- active: boolean - true when activating and false when deactivating the timer or scheduler’s rule.
Returns:
- boolean - true to confirm the timer event, false to block the event.
The JavaScript UserTimerEvent function is alway called, but its return value is used only when the Java UserTimerEvent callback is not defined or returned true.
User Interface
pageEvent
pageEvent(address, session, userid, project, page)
Called when a Web client initially loads a project and when navigating between pages.
There is no guarantee that the event would be fired the exact moment the page is loaded.
Network delays or errors could cause delayed triggering of this event.
Parameters:
- address: string - the client’s IP address
- session: string - session id string that uniquely identifies the client session
- userid: string - the user id
- project: string - project’s id
- page: string - the page id of the current page.
uiClearEvent
uiClearEvent(session)
Called, following an explicit call of uiClear() or after the client session inactivity timeout expires, if the client session becomes active again (i.e. the client starts handshaking again with the server with the same session id).
Parameters:
- session: string - session id string that uniquely identifies the client session
userCommand
static String userCommand(String name, String param)
Called by user clicks on buttons created in the Web interface with the (user), (usermini), (usermicro), (userrgb) or (userimage) objects, by a USER action in EVENTS, or by the user() Java method and JavaScript function.
This method can optionally return a string. Any returned string causes the log of an [OK] status. If you want to prevent logging, for example to avoid sensitive information to be written in the log file, you should return the string “!”.
If you want to force the Web interface to show a specific page after the button is pressed (it would be like pressing a (link) object), return a string starting with “page:” followed by the page name; in this case, userCommand() will be called again when that popup or page is closed, with "/close" appended to param.
Parameters:
- name: String - the name field of the (user) object
- param: String - the param of the (user) object.
Returns:
- String - log or go to page, see text above. If this function doesn't return a value, an [ERROR] status is logged in the log file, while any other string causes the log of an [OK] status.
userCommand with session information
static String userCommand(String session, String userid, String name, String param)
Same as userCommand(String name, String param), but with additional session and user id information passed as parameters. You should only use one of the two userCommand() callback methods in user.java. If you declare both, only this four parameters version will be called.
Parameters:
- session: string - a session id string that uniquely identifies the client session
- userid: string - the user id
- name: String - the name field of the (user) object
- param: String - the param of the (user) object.
Returns:
- String - log or go to page, see text above. If this function doesn't return a value, an [ERROR] status is logged in the log file, while any other string causes the log of an [OK] status.
userSubmit
static String userSubmit(String session, String uid, String name, HashMap<String, String> fields)
This callback is similar to userCommand(String session, String uid, String name, String param) and should be used only when the four parameters version of userCommand() is also defined in user.java.
When userSubmit() is defined, it will be called when a (submit!id), (submitmini!id) or (submitmicro!id) button is pressed, while userCommand() will continue to be called on (user) buttons.
userSubmit() provides a more convenient access to the values of multiple input fields in a form.
This function can optionally return a string. Any returned string causes the log of an [OK] status. If you want to prevent logging, for example to avoid sensitive information to be written in the log file, you should return the string “!”.
If you want to force the Web interface to show a specific page after the button is pressed (it would be like pressing a (link) object), return a string starting with “page:” followed by the page name; in this case, userCommand() will be called when that popup or page is closed, with "/close" appended to param.
Parameters:
- session: String - a session id string that uniquely identifies the client session
- userid: String - the user id
- name: String - the id of the (submit!id) object
- fields: HashMap<String, String> - the hash map of all input fields in the container where the (submit) object is located.
Returns:
- String - log or go to page, see text above. If this function doesn't return a value, an [ERROR] status is logged in the log file, while any other string causes the log of an [OK] status.
WebRootRequestEvent
WebRootRequestEvent(addr, secure, useragent)
This method is called when the Web server receives a root URL request. It can return a string to redirect the browser to a valid URL, or null to prevent redirection.
Parameters:
- addr: string - the HTTP client’s address
- secure: boolean - true if this is an HTTPS request
- useragent: string - the Web browser’s user agent information.
Returns:
- string - optional return string containing a valid absolute or relative URL
Examples:
The following code redirects any root HTTP request received by the HSYCO server to the manager page.
function WebRootRequestEvent(addr, secure, useragent) : {
return "hsycoserver/manager";
}