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

Source code    
  1. -- * Audio Alarm Notification Module Version 1.4 Created by Erwin van der Zwart * --
  2. -- * This script perform automaticly all needed actions to support audio alerts * --
  3. -- After running script once refresh browser and write value to object for audio --
  4. -- ******************************** SET PARAMETERS ****************************** --
  5.  
  6. -- Group address to trigger the alarm notification
  7. Audio_Trigger_Group_Address = '1/1/1' -- !! MUST BE A BIT OBJECT !!
  8.  
  9. -- Audio on value 0 and value 1 or on value 1 only
  10. Audio_on_value_1_only = true -- Set to true for audio on value 1 only
  11.  
  12. -- Stop audio on value 0 (used only when Audio_on_value_1_only = true)
  13. Stop_on_value_0 = true -- Set to false to play complete audio file when receiving value 0
  14.  
  15. -- Repeat audio until value is 0 (used only when Audio_on_value_1_only = true and Stop_on_value_0 = true)
  16. Repeat_on_value_1 = true -- Set to true for audio loop until object value is false
  17.  
  18. -- Check object value on load of visu to start direct based on object value
  19. Start_On_Load = true -- Set to true for audio on load based on object value
  20.  
  21. -- 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)
  22. Audio_Play_Interval_Time = 10 -- Adjust to length of playing file
  23.  
  24. -- URL to use for downloading a audiofile to the controller on value 0 (only used / needed when line 8 is set to false)
  25. URL_Audio_File_0 = 'http://www.freesfx.co.uk/rx2/mp3s/10/11532_1406234695.mp3'
  26.  
  27. -- URL to use for downloading a audiofile to the controller on value 1
  28. URL_Audio_File_1 = 'http://www.freesfx.co.uk/rx2/mp3s/1/814_1244852507.mp3'
  29.  
  30. -- !IMPORTANT! internet must be available for the controller to download a file
  31. -- check DNS and default gateway or copy your own audio files to the img / backgrounds
  32. -- folder and name it 'alarm_0.xxx' (format like line 13) and 'alarm_1.xxx' (format like line 13)
  33.  
  34. -- Downloaded audio file formaton on value 0 (only used / needed when line 8 is set to false)
  35. Audio_File_Format_0 = 'mp3'
  36.  
  37. -- Downloaded audio file formaton on value 1
  38. Audio_File_Format_1 = 'mp3'
  39.  
  40. -- Create automaticly iframe with logout.html on startpage
  41. Create_Automatic = true -- Set to false to disable auto create and run script once
  42.  
  43. -- Delete HTML alarm function from controller
  44. Delete_HTML = false -- Set to true to delete this function and run script once
  45.  
  46. -- ******************************** END PARAMETERS ****************************** --
  47. -- ********************* DON'T CHANGE ANYTHING UNDER THIS LINE ****************** --
  48.  
  49. -- Set interval (in seconds) for audio repeat playing file until mute is pressed to milliseconds
  50. Audio_Play_Interval_Time = Audio_Play_Interval_Time * 1000
  51. if Audio_Play_Interval_Time < 1000 then
  52. Audio_Play_Interval_Time = 1000
  53. end
  54.  
  55. -- Create HTML content
  56. page = [[
  57. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  58. <html xmlns="http://www.w3.org/1999/xhtml">
  59. <head>
  60. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  61. <title>homeLYnk Alarm Notifications</title>
  62. <style type="text/css">
  63. body {
  64. background-color: transparent;
  65. }
  66. </style>
  67. </head>
  68. <body>
  69. <script type="text/javascript">
  70. // ***** Make link to parent from iframe *****
  71. var p = window.parent, root, addr;
  72. var ip = location.host;
  73. var URL = "http://" + ip + "/scada/resources/img/";
  74.  
  75. if (p && p.objectStore) {
  76. ]]
  77. if Audio_on_value_1_only == false then
  78. page = page .. [[
  79. // Create snd for playing audio
  80. var snd_0 = new Audio(URL + "alarm_0.]] .. Audio_File_Format_0 .. [[");
  81. var snd_1 = new Audio(URL + "alarm_1.]] .. Audio_File_Format_1 .. [[");
  82. ]]
  83. else
  84. page = page .. [[
  85. // Create snd for playing audio
  86. var snd_1 = new Audio(URL + "alarm_1.]] .. Audio_File_Format_1 .. [[");
  87. // Create alertaudio for loop playing audio
  88. var alertaudio = ""
  89.  
  90. function playalert_loop() {
  91. if (snd_1.paused || snd_1.ended || snd_1.currentTime == 0 ) {
  92. snd_1.play();
  93. }
  94. }
  95.  
  96. function playalert() {
  97. // Create loop
  98. if(alertaudio==""){
  99. alertaudio = window.setInterval("playalert_loop()",]] .. Audio_Play_Interval_Time .. [[);
  100. }
  101. // Start loop direct
  102. playalert_loop();
  103. }
  104.  
  105. function stopalert() {
  106. if(alertaudio!=""){
  107. window.clearInterval(alertaudio)
  108. alertaudio=""
  109. }
  110. if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
  111. snd_1.pause();
  112. snd_1.currentTime = 0;
  113. }
  114. }
  115. ]]
  116. end
  117. page = page .. [[
  118. /* ********** DON'T CHANGE ANYTHING ABOVE THIS LINE ******* */
  119. /* ********** HTML FILE FOR HOMELYNK AUDIO MESSAGES ******* */
  120. /* ************** CREATED BY ERWIN VAN DER ZWART ********** */
  121. /* ******************************************************** */
  122. /* TO ADD COPY BLOCK(S) AND CHANGE (2x) addr0 to addr(x) */
  123. /* ******************************************************** */
  124.  
  125. /* START BLOCK */
  126. /* ]] .. Audio_Trigger_Group_Address .. [[ = ALARM TRIGGER ADDRES */
  127. addr0 = p.Scada.encodeGroupAddress(']] .. Audio_Trigger_Group_Address .. [[');
  128. p.objectStore.addListener(addr0, function(obj, type) {
  129.  
  130. /* to avoid play on opening page */
  131. if (type == 'init') {]]
  132. if Start_On_Load == false then
  133. page = page .. [[
  134. return;
  135. ]]
  136. end
  137. page = page .. [[
  138. }
  139. ]]
  140. if Audio_on_value_1_only == false then
  141. page = page .. [[
  142.  
  143. if ( obj.value == 0 ) {
  144. if (!snd_0.paused || !snd_0.ended || snd_0.currentTime > 0 ) {
  145. snd_0.pause();
  146. snd_0.currentTime = 0;
  147. }
  148. if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
  149. snd_1.pause();
  150. snd_1.currentTime = 0;
  151. }
  152. snd_0.play();
  153. }
  154.  
  155. if ( obj.value == 1 ) {
  156. if (!snd_0.paused || !snd_0.ended || snd_0.currentTime > 0 ) {
  157. snd_0.pause();
  158. snd_0.currentTime = 0;
  159. }
  160. if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
  161. snd_1.pause();
  162. snd_1.currentTime = 0;
  163. }
  164. snd_1.play();
  165. }
  166. ]]
  167. else
  168. if Stop_on_value_0 == true then
  169. if Repeat_on_value_1 == true then
  170. page = page .. [[
  171.  
  172. if ( obj.value == 0 ) {
  173. stopalert();
  174. }
  175.  
  176. if ( obj.value == 1 ) {
  177. playalert();
  178. }
  179. ]]
  180. else
  181. page = page .. [[
  182.  
  183. if ( obj.value == 0 ) {
  184. if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
  185. snd_1.pause();
  186. snd_1.currentTime = 0;
  187. }
  188. }
  189.  
  190. if ( obj.value == 1 ) {
  191. if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
  192. snd_1.pause();
  193. snd_1.currentTime = 0;
  194. }
  195. snd_1.play();
  196. }
  197. ]]
  198. end
  199. else
  200. page = page .. [[
  201.  
  202. if ( obj.value == 0 ) {
  203.  
  204. }
  205.  
  206. if ( obj.value == 1 ) {
  207. if (!snd_1.paused || !snd_1.ended || snd_1.currentTime > 0 ) {
  208. snd_1.pause();
  209. snd_1.currentTime = 0;
  210. }
  211. snd_1.play();
  212. }
  213. ]]
  214. end
  215. end
  216. page = page .. [[
  217. });
  218. /* END OF ]] .. Audio_Trigger_Group_Address .. [[ */
  219.  
  220. /* ******************************************************** */
  221.  
  222. /* ******************************************************** */
  223. /* TO ADD COPY BLOCK(S) AND CHANGE (2x) addr0 to addr(x) */
  224. /* ******************************************************** */
  225.  
  226. // ADD HERE EXTRA BLOCKS IF NEEDED
  227.  
  228. /* ******************************************************** */
  229.  
  230. /* ********** DON'T CHANGE ANYTHING BELOW THIS LINE ******* */
  231.  
  232. }
  233. </script>
  234. </div>
  235. </body>
  236. </html>
  237. ]]
  238.  
  239. -- Write HTML file to controller
  240. io.writefile("/www/scada/resources/img/alarm.html", page)
  241.  
  242. -- use this as frame URL -> /scada/resources/img/alarm.html
  243.  
  244. -- Check if html must be automaticly created inside iframe with alarm.html on startpage
  245. if Create_Automatic == true then
  246.  
  247. -- Get default startpage
  248. query = 'SELECT id, usermode_param FROM visfloors'
  249. for _, floor in ipairs(db:getall(query)) do
  250. if floor.usermode_param == "D" then
  251. default_startpage = floor.id
  252. end
  253. end
  254.  
  255. -- Check if default page excists else exit script
  256. if default_startpage == nil then
  257. alert("Default page does not excists, exit HTML creation")
  258. --Exit script
  259. return
  260. end
  261.  
  262. -- Check if object already exist
  263. object_exists = false
  264. query = 'SELECT floor, type, name, params FROM visobjects'
  265. for _, visobject in ipairs(db:getall(query)) do
  266. if visobject.floor == default_startpage and (visobject.type == 9 or visobject.type == "9") and visobject.name == "alarm" then
  267. object_exists = true
  268. end
  269. end
  270.  
  271. -- Create if object doesn't exist
  272. if object_exists == false then
  273. 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,})
  274. end
  275.  
  276. if Audio_on_value_1_only == false then
  277. -- Download alarm file on value 0 to controler local
  278. os.execute('wget -q -U Mozilla -O /www/scada/resources/img/alarm_0.' .. Audio_File_Format_0 .. ' ' .. URL_Audio_File_0)
  279. -- Download alarm file on value 1 to controler local
  280. os.execute('wget -q -U Mozilla -O /www/scada/resources/img/alarm_1.' .. Audio_File_Format_1 .. ' ' .. URL_Audio_File_1)
  281. else
  282. -- Download alarm file on value 1 to controler local
  283. os.execute('wget -q -U Mozilla -O /www/scada/resources/img/alarm_1.' .. Audio_File_Format_1 .. ' ' .. URL_Audio_File_1)
  284. end
  285. end
  286.  
  287. log ("Audio alerts are added")
  288.  
  289. -- Check if HTML logout function must be deleted from the controller
  290. if Delete_HTML == true then
  291.  
  292. -- Get default startpage
  293. query = 'SELECT id, usermode_param FROM visfloors'
  294. for _, floor in ipairs(db:getall(query)) do
  295. if floor.usermode_param == "D" then
  296. default_startpage = floor.id
  297. end
  298. end
  299.  
  300. -- Check if default page excists else exit script
  301. if default_startpage == nil then
  302. alert("Default page does not excists, exit HTML deletion")
  303. --Exit script
  304. return
  305. end
  306.  
  307. -- Select all entrys from DB inside table 'visobjects'
  308. query = 'SELECT id, floor, type, name, params FROM visobjects'
  309. for _, visobject in ipairs(db:getall(query)) do
  310. if visobject.floor == default_startpage and (visobject.type == 9 or visobject.type == "9") and visobject.name == "alarm" then
  311. current = visobject.id
  312. db:delete('visobjects', { id = current })
  313. end
  314. end
  315.  
  316. --Delete HTML file from HL dir
  317. os.remove("/www/scada/resources/img/alarm.html")
  318.  
  319. if Audio_on_value_1_only == false then
  320. --Delete Audio file on value 0 from HL dir
  321. os.remove("/www/scada/resources/img/alarm_0." .. Audio_File_Format_0)
  322.  
  323. --Delete Audio file on value 1from HL dir
  324. os.remove("/www/scada/resources/img/alarm_1." .. Audio_File_Format_1)
  325. else
  326. --Delete Audio file on value 1from HL dir
  327. os.remove("/www/scada/resources/img/alarm_1." .. Audio_File_Format_1)
  328. end
  329. log ("Audio alerts are deleted")
  330. end
  331.  
  332. -- Disable script when done automaticly
  333. script.disable(_SCRIPTNAME)

 

Created by Erwin van der Zwart