sound - balatis - 25.03.2020
Hi
i try to use below skript for play a sound when i have an alarm , but does not work in alerts i have this message "default page does not excists exit HTMLcreation" no erros ,no logs...
thanks in advance
- -- * 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://https://www.computerhope.com/jargon/m/example.mp3'
-
- -- URL to use for downloading a audiofile to the controller on value 1
- URL_Audio_File_1 = 'https://www.computerhope.com/jargon/m/example.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)
RE: sound - admin - 25.03.2020
Make sure that you have a plan in Vis. structure with "Usermode visualization" parameter set to "Show, make default"
RE: sound - balatis - 26.03.2020
(25.03.2020, 13:15)admin Wrote: Make sure that you have a plan in Vis. structure with "Usermode visualization" parameter set to "Show, make default" Thanks Admin!
RE: sound - Domoticatorino - 21.08.2020
Hi Balatis,
file sounds till you make the acknowledge?
I have to create an alarm system for a reception desk. I would like to use an android touch.
What do you suggest? Thanks.
RE: sound - Erwin van der Zwart - 23.08.2020
Hi,
This is a very old script and was used before we had custom JS.
Due the implemantation of custom JS we had better options to implement things..
Use this custom JS instead of the old script above:
Code: // Closing current script section to add elements
</script>
<audio id="SE_Audio" style="display: none;"></audio>
<script type="text/javascript"> // Starting new script section after adding elements
$(function(){
// Table with parameters address, audio URL, repeat, interval in ms
var AudioTable = [
{ address:"32/0/0", url:"/user/beep.mp3", repeat:true, interval: 2000, intervalHandle: 0},
{ address:"32/0/1", url:"/user/beep1.mp3", repeat:true, interval: 2000, intervalHandle: 0},
{ address:"32/0/2", url:"/user/beep.mp3", repeat:false, interval: 2000, intervalHandle: 0},
{ address:"32/0/3", url:"/user/beep1.mp3", repeat:false, interval: 2000, intervalHandle: 0},
];
var ctx = new(window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.oAudioContext || window.msAudioContext), request = new XMLHttpRequest();
var snd = document.getElementById("SE_Audio");
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
//console.log(userAgent)
if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
return 'iOS';
} else if ( userAgent.match(/Android/i)) {
return 'Android';
} else if ( userAgent.match(/iKonWebTouch/i)) {
return 'U.Motion';
} else {
return 'PC';
}
}
var OS_Version = getMobileOperatingSystem();
// Fix iOS Audio Context
(function() {
var fixAudioContext = function (e) {
if (ctx) {
// Create empty buffer
var buffer = ctx.createBuffer(1, 1, 22050);
var source = ctx.createBufferSource();
source.buffer = buffer;
// Connect to output
source.connect(ctx.destination);
// Play sound
if (source.start) {
source.start(0);
} else if (source.play) {
source.play(0);
} else if (source.noteOn) {
source.noteOn(0);
}
}
// Check if document is loaded in iframe
if (window.frameElement){
// Remove event listeners from parent
var thisparent = window.parent;
thisparent.document.removeEventListener('touchstart', fixAudioContext);
thisparent.document.removeEventListener('touchend', fixAudioContext);
}
// Remove events
document.removeEventListener('touchstart', fixAudioContext);
document.removeEventListener('touchend', fixAudioContext);
};
// Check if document is loaded in iframe
if (window.frameElement){
// Add event listeners to parent
var thisparent = window.parent;
// Event listener for iOS 6-8 (was previous touchstart event)
thisparent.document.addEventListener('touchstart', fixAudioContext);
// Event listener for iOS 9+ (is now touchend event)
thisparent.document.addEventListener('touchend', fixAudioContext);
}
// Event listener for iOS 6-8 (was previous touchstart event)
document.addEventListener('touchstart', fixAudioContext);
// Event listener for iOS 9+ (is now touchend event)
document.addEventListener('touchend', fixAudioContext);
})();
// Function to play audio by URL
function Play_Audio(url){
//console.log(OS_Version)
if (OS_Version === 'Android' || OS_Version === 'U.Motion' ){
//alert(OS_Version);
snd.src = url;
snd.pause();
sndcurrentTime = 0;
//snd.load();
snd.play();
} else {
request.open("GET", url, true);
request.responseType = "arraybuffer";
request.onload = function(){
ctx.decodeAudioData(request.response, onDecoded);
}
function onDecoded(buffer){
var bufferSource = ctx.createBufferSource();
bufferSource.buffer = buffer;
bufferSource.connect(ctx.destination);
bufferSource.start();
}
request.send();
}
};
// Function to create event listeners
function CreateListeners(groupaddr, url, repeat, interval, item){
// Make event listener to object
if (typeof grp != 'undefined') {
grp.listen(groupaddr, function(object, state) {
var value = object.value;
if (state == 'value') {
if (value == 1) {
if (repeat == true){
if (AudioTable[item].intervalHandle === null) {
Play_Audio(url);
AudioTable[item].intervalHandle = setInterval(function() {
Play_Audio(url);
}, interval);
}
} else {
Play_Audio(url);
}
} else {
if (AudioTable[item].intervalHandle !== null) {
clearInterval(AudioTable[item].intervalHandle);
AudioTable[item].intervalHandle = null;
}
}
}
}, true);
}
}
// Start creating listeners
for (var i in AudioTable) {
CreateListeners(AudioTable[i].address, AudioTable[i].url, AudioTable[i].repeat, AudioTable[i].interval, i);
}
});
BR,
Erwin
RE: sound - Domoticatorino - 23.08.2020
Thank you Erwin.
|