I'm trying to make a script but I'm not very experienced... can you help me?
I need to play a wav file when one device is triggered.
//-----------------------------------------
using MLS.ZWave.Service.Rules;
using System;
using System.Collections.Generic;
using System.Media;
using System.Text;
using MLS.ZWave.BusinessObjects;
using MLS.HA.DeviceController.Common;
using System.IO;
using MLS.HA.DeviceController.Common.Device;
public class MyScript : ScriptBase, ScriptInterface {
public void runScript() {
var device = triggerMetaData.triggeringDevice.deviceName;
If (device.find("Bagno")) {
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\Windows\Media\Alarm01.wav");
player.Play();
}
If (device.find("Cucina")) {
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\Windows\Media\Alarm02.wav");
player.Play();
}
}
}
//-----------------------------------------
in the logfile there's:
26/07/2015 18:26:17: Script failed to compile with error: ; expected on line 17.
26/07/2015 18:26:17: Script failed to compile with error: ; expected on line 22.
If Triggering Device Play Sound
- 739 Views
- Last Post 16 March 2017
"If" should be in lower case
sorry to dig up an old post... but did this end up working? I'd love to use this script as a door chime. I'm just not sure what this device.find Bango and Cucina are... Are those devices? comments on this script would really help. thanks!
sorry to dig up an old post... but did this end up working? I'd love to use this script as a door chime. I'm just not sure what this device.find Bango and Cucina are... Are those devices? comments on this script would really help. thanks!
Is your plan to play the chime through your computer speakers? If that's all you want, the following script would do the trick - you would create a new .cs file in your axial server\scripts folder, then add it as a post or pre-script to a scene. You could then setup a trigger when the doorbell rings that executes the scene.
using MLS.ZWave.Service.Rules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MLS.HA.DeviceController.Common.Device.ZWave;
public class MyScript : ScriptBase, ScriptInterface {
public void runScript() {
var player = new System.Media.SoundPlayer(@"C:\Windows\Media\Alarm01.wav");
player.Play();
}
}
Yes, "Bagno" and "Cucina" are parts of the Device Name, in my case "Finsestra Bagno" (Windows Bathroom).
sorry to dig up an old post... but did this end up working? I'd love to use this script as a door chime. I'm just not sure what this device.find Bango and Cucina are... Are those devices? comments on this script would really help. thanks!
Is your plan to play the chime through your computer speakers? If that's all you want, the following script would do the trick - you would create a new .cs file in your axial server\scripts folder, then add it as a post or pre-script to a scene. You could then setup a trigger when the doorbell rings that executes the scene.
using MLS.ZWave.Service.Rules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MLS.HA.DeviceController.Common.Device.ZWave;
public class MyScript : ScriptBase, ScriptInterface {
public void runScript() {
var player = new System.Media.SoundPlayer(@"C:\Windows\Media\Alarm01.wav");
player.Play();
}
}
Thank you so much! thats perfect.
So... I made the cs file... I changed the wav file to notify.wav which is something i actually have in that folder... but it doesnt work. i get no sound out of my speakers. No error message in logfile. Any ideas?
Seems straight foward.... Try this code (can append to the method).
using (System.IO.TextWriter writer = System.IO.File.CreateText("C:\\hello.txt"))
{
writer.WriteLine("hello world");
}
Then do your trigger. Check to see if the file hello.txt exists at C:\. If it doesn't then the script isn't being triggered or there's a compile error some how. If the file does exist then its two possiblilities.
1) your sound isn't working
2) the thread is executing and ending before the sound is played. Use .PlaySync(); instead of .Play(). If Axial is kicking off the script and terminating when the thread completes, then the sound would never play as the .Play(); is asynchronous in a new thread and not blocking. PlaySync is syncrhonous and blocks until sound play completes.
-N
hmmm I'm not getting any text file or sound... what did i screw up?
using MLS.ZWave.Service.Rules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MLS.HA.DeviceController.Common.Device.ZWave;
public class DoorChimenotify : ScriptBase, ScriptInterface {
public void runScript() {
var player = new System.Media.SoundPlayer(@"C:\Windows\Media\notify.wav");
player.PlaySync();
}
using (System.IO.TextWriter writer = System.IO.File.CreateText("C:\\hello.txt"))
{
writer.WriteLine("hello world");
}
}
Also note that Axial caches the file to help the system run faster, so you'll have to clear the cache after you make changes. Do this by going offline then back online.
Ok so, I have a txt file but still no sound. is there a volume associated with the Sound.Media class? I cant find any if there is.
Nothing in the log file (so at least thats good)
I'm running windows 7 if that matters...
Is there something else i neede to have installed? all the other scripts work fine
It been like 15 years since i've dont any C language coding, so I'm sorry I seem a bit helpless...
I've tested a few things. It does appear there is a restriction on SYSTEM presumably because services don't load the sound directives required.. Oddly I had tested on my WIn10 machine and it worked but I couldn't get it to work on my Win8 machine (just tested with a scheduled task running as system). I stumbled on this article:
http://bresleveloper.blogspot.co.il/2012/06/c-service-play-sound-with-naudio.html
There's a lot of code there but what you would have to do is to write your own little console app .exe to play a sound, then just run the Process.Start from the Axial script.
I wasn't aware of this limitation until now. Quite interesting.
thanks for the info... oh well
@jonnysax
Alright here you go. I took that example and just made an exe that you can use from the script. I didn't do anything fancy so it only supports .wav files. I've tested this on my Axial server and heard sound from it's speakers. The exe is called SoundPlayer.exe and takes 1 parameter of the full path to the .wav file. I put the source in there in case someone is interested (it's literally the same as the web link).
Unzip to some directory.
The .cs script would be:
public void runScript() {
System.Diagnostics.Process.Start(@"(folderlocation)\soundplayer.exe", @"C:\Windows\Media\notify.wav");
writeFileLog("Ran sound script"); //writes to axial log file
}
Attachments don't seem to work on the forum. Here's a dropbox link.
https://www.dropbox.com/s/xnh9yc006wfko15/player.zip?dl=0
You rock!
but after running, i get this error...
3/13/2017 10:09:12 PM: Error running script DoorChime.cs - System.ComponentModel.Win32Exception
The system cannot find the file specified
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at DoorChime.runScript()
at MLS.ZWave.Service.Rules.ScriptEngine.runScript(Assembly script, RulesDTO rule, List`1 sceneDevices, TriggerMetaData tmd) in Z:\work\mls\MLS.ZWave.Controller\MLS.HA.DeviceController\ScriptEngine.cs:line 168
at MLS.ZWave.Service.Rules.ScriptEngine.runScript(String fileName, RulesDTO rule, Boolean enableLogging, List`1 sceneDevices, TriggerMetaData tmd) in Z:\work\mls\MLS.ZWave.Controller\MLS.HA.DeviceController\ScriptEngine.cs:line 46
and here is my cs file:
using System;
using System.Collections.Generic;
using System.Text;
using MLS.ZWave.Service.Rules;
using MLS.ZWave.BusinessObjects;
public class DoorChime : ScriptBase, ScriptInterface {
public void runScript() {
System.Diagnostics.Process.Start(@"(D:\Alerts\player\soundplayer.exe", @"D:\Alerts\tngdoorbell.wav");
writeFileLog("Ran sound script"); //writes to axial log file
}
}
What the heck did i mess up this time?
Hmm "The system cannot find the file specified" makes it look like a file isn't there.
You have an extra ( in there... Right there by the D:\
System.Diagnostics.Process.Start(@"(D:\Alerts\player\soundplayer.exe"
it works! thanks so much!