Audio alerts inside your visualization on LogicMachine
Example: Audio alerts inside your visualization on LogicMachine
Task
If you want audio alerts inside your browser on statuses for KNX or other objects, check / fill in the parameters in the head of the script.
Make sure the LogicMachine has Internet connection with correct IP parameters in network settings. The script will run once and will disable itself automatically. You can play around with the parameters like Stop_on_value_0 where you can set to stop the current audio file when 0 is received to specific grp address.
Resident script
- -- * Audio Alarm Notification Module Version 1.4 Created by Erwin van der Zwart * --
- -- * This script perform automaticly all needed actions to support audio alerts * --
- -- After running script once refresh browser and write value to object for audio --
- -- ******************************** SET PARAMETERS ****************************** --
-
- -- Group address to trigger the alarm notification
- Audio_Trigger_Group_Address = '1/1/1' -- !! MUST BE A BIT OBJECT !!
-
- -- Audio on value 0 and value 1 or on value 1 only
- Audio_on_value_1_only = true -- Set to true for audio on value 1 only
-
- -- Stop audio on value 0 (used only when Audio_on_value_1_only = true)
- Stop_on_value_0 = true -- Set to false to play complete audio file when receiving value 0
-
- -- Repeat audio until value is 0 (used only when Audio_on_value_1_only = true and Stop_on_value_0 = true)
- Repeat_on_value_1 = true -- Set to true for audio loop until object value is false
-
- -- Check object value on load of visu to start direct based on object value
- Start_On_Load = true -- Set to true for audio on load based on object value
-
- -- Set interval (in seconds) for audio repeat playing file until mute is pressed (used only when Audio_on_value_1_only = true and Stop_on_value_0 = true)
- Audio_Play_Interval_Time = 10 -- Adjust to length of playing file
-
- -- URL to use for downloading a audiofile to the controller on value 0 (only used / needed when line 8 is set to false)
- URL_Audio_File_0 = 'http://www.freesfx.co.uk/rx2/mp3s/10/11532_1406234695.mp3'
-
- -- URL to use for downloading a audiofile to the controller on value 1
- URL_Audio_File_1 = 'http://www.freesfx.co.uk/rx2/mp3s/1/814_1244852507.mp3'
-
- -- !IMPORTANT! internet must be available for the controller to download a file
- -- check DNS and default gateway or copy your own audio files to the img / backgrounds
- -- folder and name it 'alarm_0.xxx' (format like line 13) and 'alarm_1.xxx' (format like line 13)
-
- -- Downloaded audio file formaton on value 0 (only used / needed when line 8 is set to false)
- Audio_File_Format_0 = 'mp3'
-
- -- Downloaded audio file formaton on value 1
- Audio_File_Format_1 = 'mp3'
-
- -- Create automaticly iframe with logout.html on startpage
- Create_Automatic = true -- Set to false to disable auto create and run script once
-
- -- Delete HTML alarm function from controller
- Delete_HTML = false -- Set to true to delete this function and run script once
-
- -- ******************************** END PARAMETERS ****************************** --
- -- ********************* DON'T CHANGE ANYTHING UNDER THIS LINE ****************** --
-
- -- Set interval (in seconds) for audio repeat playing file until mute is pressed to milliseconds
- Audio_Play_Interval_Time = Audio_Play_Interval_Time * 1000
- if Audio_Play_Interval_Time < 1000 then
- Audio_Play_Interval_Time = 1000
- end
-
- -- Create HTML content
- page = [[
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>homeLYnk Alarm Notifications</title>
- <style type="text/css">
- body {
- background-color: transparent;
- }
- </style>
- </head>
- <body>
- <script type="text/javascript">
- // ***** Make link to parent from iframe *****
- var p = window.parent, root, addr;
- var ip = location.host;
- var URL = "http://" + ip + "/scada/resources/img/";
-
- if (p && p.objectStore) {
- ]]
- if Audio_on_value_1_only == false then
- page = page .. [[
- // Create snd for playing audio
- var snd_0 = new Audio(URL + "alarm_0.]] .. Audio_File_Format_0 .. [[");
- var snd_1 = new Audio(URL + "alarm_1.]] .. Audio_File_Format_1 .. [[");
- ]]
- else
- page = page .. [[
- // Create snd for playing audio
- var snd_1 = new Audio(URL + "alarm_1.]] .. Audio_File_Format_1 .. [[");
- // Create alertaudio for loop playing audio
- var alertaudio = ""
-
- function playalert_loop() {
- if (snd_1.paused || snd_1.ended || snd_1.currentTime == 0 ) {
- snd_1.play();
- }
- }
-
- function playalert() {
- // Create loop
- if(alertaudio==""){
- alertaudio = window.setInterval("playalert_loop()",]] .. Audio_Play_Interval_Time .. [[);
- }
- // Start loop direct
- playalert_loop();
- }
-
- function stopalert() {
- if(alertaudio!=""){
- window.clearInterval(alertaudio)
- alertaudio=""
- }
- if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
- snd_1.pause();
- snd_1.currentTime = 0;
- }
- }
- ]]
- end
- page = page .. [[
- /* ********** DON'T CHANGE ANYTHING ABOVE THIS LINE ******* */
- /* ********** HTML FILE FOR HOMELYNK AUDIO MESSAGES ******* */
- /* ************** CREATED BY ERWIN VAN DER ZWART ********** */
- /* ******************************************************** */
- /* TO ADD COPY BLOCK(S) AND CHANGE (2x) addr0 to addr(x) */
- /* ******************************************************** */
-
- /* START BLOCK */
- /* ]] .. Audio_Trigger_Group_Address .. [[ = ALARM TRIGGER ADDRES */
- addr0 = p.Scada.encodeGroupAddress(']] .. Audio_Trigger_Group_Address .. [[');
- p.objectStore.addListener(addr0, function(obj, type) {
-
- /* to avoid play on opening page */
- if (type == 'init') {]]
- if Start_On_Load == false then
- page = page .. [[
- return;
- ]]
- end
- page = page .. [[
- }
- ]]
- if Audio_on_value_1_only == false then
- page = page .. [[
-
- if ( obj.value == 0 ) {
- if (!snd_0.paused || !snd_0.ended || snd_0.currentTime > 0 ) {
- snd_0.pause();
- snd_0.currentTime = 0;
- }
- if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
- snd_1.pause();
- snd_1.currentTime = 0;
- }
- snd_0.play();
- }
-
- if ( obj.value == 1 ) {
- if (!snd_0.paused || !snd_0.ended || snd_0.currentTime > 0 ) {
- snd_0.pause();
- snd_0.currentTime = 0;
- }
- if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
- snd_1.pause();
- snd_1.currentTime = 0;
- }
- snd_1.play();
- }
- ]]
- else
- if Stop_on_value_0 == true then
- if Repeat_on_value_1 == true then
- page = page .. [[
-
- if ( obj.value == 0 ) {
- stopalert();
- }
-
- if ( obj.value == 1 ) {
- playalert();
- }
- ]]
- else
- page = page .. [[
-
- if ( obj.value == 0 ) {
- if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
- snd_1.pause();
- snd_1.currentTime = 0;
- }
- }
-
- if ( obj.value == 1 ) {
- if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
- snd_1.pause();
- snd_1.currentTime = 0;
- }
- snd_1.play();
- }
- ]]
- end
- else
- page = page .. [[
-
- if ( obj.value == 0 ) {
-
- }
-
- if ( obj.value == 1 ) {
- if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
- snd_1.pause();
- snd_1.currentTime = 0;
- }
- snd_1.play();
- }
- ]]
- end
- end
- page = page .. [[
- });
- /* END OF ]] .. Audio_Trigger_Group_Address .. [[ */
-
- /* ******************************************************** */
-
- /* ******************************************************** */
- /* TO ADD COPY BLOCK(S) AND CHANGE (2x) addr0 to addr(x) */
- /* ******************************************************** */
-
- // ADD HERE EXTRA BLOCKS IF NEEDED
-
- /* ******************************************************** */
-
- /* ********** DON'T CHANGE ANYTHING BELOW THIS LINE ******* */
-
- }
- </script>
- </div>
- </body>
- </html>
- ]]
-
- -- Write HTML file to controller
- io.writefile("/www/scada/resources/img/alarm.html", page)
-
- -- use this as frame URL -> /scada/resources/img/alarm.html
-
- -- Check if html must be automaticly created inside iframe with alarm.html on startpage
- if Create_Automatic == true then
-
- -- Get default startpage
- query = 'SELECT id, usermode_param FROM visfloors'
- for _, floor in ipairs(db:getall(query)) do
- if floor.usermode_param == "D" then
- default_startpage = floor.id
- end
- end
-
- -- Check if default page excists else exit script
- if default_startpage == nil then
- alert("Default page does not excists, exit HTML creation")
- --Exit script
- return
- end
-
- -- Check if object already exist
- object_exists = false
- query = 'SELECT floor, type, name, params FROM visobjects'
- for _, visobject in ipairs(db:getall(query)) do
- if visobject.floor == default_startpage and (visobject.type == 9 or visobject.type == "9") and visobject.name == "alarm" then
- object_exists = true
- end
- end
-
- -- Create if object doesn't exist
- if object_exists == false then
- db:insert('visobjects', {floor = default_startpage, type = 9, params = '{"source":"url","url":"/scada/resources/img/alarm.html","width":50,"height":50}', locx = 0 , locy = 0, name = "alarm", notouch = 1, nobg = 1,})
- end
-
- if Audio_on_value_1_only == false then
- -- Download alarm file on value 0 to controler local
- os.execute('wget -q -U Mozilla -O /www/scada/resources/img/alarm_0.' .. Audio_File_Format_0 .. ' ' .. URL_Audio_File_0)
- -- Download alarm file on value 1 to controler local
- os.execute('wget -q -U Mozilla -O /www/scada/resources/img/alarm_1.' .. Audio_File_Format_1 .. ' ' .. URL_Audio_File_1)
- else
- -- Download alarm file on value 1 to controler local
- os.execute('wget -q -U Mozilla -O /www/scada/resources/img/alarm_1.' .. Audio_File_Format_1 .. ' ' .. URL_Audio_File_1)
- end
- end
-
- log ("Audio alerts are added")
-
- -- Check if HTML logout function must be deleted from the controller
- if Delete_HTML == true then
-
- -- Get default startpage
- query = 'SELECT id, usermode_param FROM visfloors'
- for _, floor in ipairs(db:getall(query)) do
- if floor.usermode_param == "D" then
- default_startpage = floor.id
- end
- end
-
- -- Check if default page excists else exit script
- if default_startpage == nil then
- alert("Default page does not excists, exit HTML deletion")
- --Exit script
- return
- end
-
- -- Select all entrys from DB inside table 'visobjects'
- query = 'SELECT id, floor, type, name, params FROM visobjects'
- for _, visobject in ipairs(db:getall(query)) do
- if visobject.floor == default_startpage and (visobject.type == 9 or visobject.type == "9") and visobject.name == "alarm" then
- current = visobject.id
- db:delete('visobjects', { id = current })
- end
- end
-
- --Delete HTML file from HL dir
- os.remove("/www/scada/resources/img/alarm.html")
-
- if Audio_on_value_1_only == false then
- --Delete Audio file on value 0 from HL dir
- os.remove("/www/scada/resources/img/alarm_0." .. Audio_File_Format_0)
-
- --Delete Audio file on value 1from HL dir
- os.remove("/www/scada/resources/img/alarm_1." .. Audio_File_Format_1)
- else
- --Delete Audio file on value 1from HL dir
- os.remove("/www/scada/resources/img/alarm_1." .. Audio_File_Format_1)
- end
- log ("Audio alerts are deleted")
- end
-
- -- Disable script when done automaticly
- script.disable(_SCRIPTNAME)
Created by Erwin van der Zwart