Over a million developers have joined DZone.

Connect Anything with Mayhem

·
Mayhem is a scripting tool or application formerly developed by Microsoft, which has now been open sourced and donated to the Outercurve foundation. Mayhem is a program that helps you to connect anything which is an event to another thing which is the reaction.

Now, what makes Mayhem so cool is that you can simply select an event and connect it to a reaction and turn it on, without any code and there you are! An Event can be an email alert, when someone posts on your Facebook wall, stock or weather alert, an input from your mobile phone, a speech recognition command and anything you could dream of. Whereas a Reaction is what happens when an event occurs, a reaction can be a phone call, it might be running a program, scheduling an appointment with a doctor, putting on your tv set, starting your web camera and the list goes on and on as you could imagine. You are responsible for connecting an event to a reaction and then turning it on. The beauty of it is that you dont have to be a programmer or have a computer science degree to do that.

You can download the mayhem application from Codeplex and install it, then you are good to go. After the installation, if you run the application, you should see an interface like this.

Mayhem Application UI

You can click on Choose Event to select any event you want, there are some basic events that you can choose from as you can see below, if you click on Choose Reaction, it will equally bring out a dialog box containing the list of Reactions.

   Mayhem event list         Mayhem reaction list

You can now select any event and associate a reaction to it. If I want to open Safari any time I pronounce the word "Browser", I will simply select Speech Recognition from the Event List and type browser into the Listen for Phrase textbox and click ok and then associate a Reaction to it, by selecting Run Program from the Reaction List, it will then bring a dialog box prompting me to navigate to where the program to be run is, in which I will select Safari application file from Safari folder in Program file folder. And the last thing to do is to put it on and that is it. Anytime I pronounced the word browser my system will open up Safari.

The coolest thing about Mayhem is that you can extend it, you as a programmer or developer can write your own reaction or event and deploy it as a module into your Mayhem installation, and if your module is good enough and you want others to use it, you can submit it to the Outercurve foundation for review and acceptance.

Would it not be nice if I want to go out and simply tell my computer system to hibernate, without me touching it? I would demonstrate the power of Mayhem by writing a Reaction that will hibernate the system, so that anytime I say "hibernate", my computer will hibernate.

Before you can starting writing modules for Mayhem, you need to setup visual studio, if you have not installed Nuget, you need to do that, this article here will show you how to install Nuget.  After Nuget installation has been done, you need to install Mayhem packages and this can be done by  Selecting  Library Package Manager from Tool menu in visual studio and then select Package Manager Setting, in the dialog box that will be displayed, click on Package Source, now type Mayhem Package in the name textbox and type http://makemayhem.com/nuget  in the source textbox and click ok.

Create a new C# class library project and give it the name Mayhem.Hibernator or any name you like. In the project's solution explorer, right click References and select Manage Nuget Package. This will bring a new dialog box, click the Online tab and select Mayhem Packages, this will bring a number of Mayhem packages, select and install MayhemCore.

Add a new class to the class library project and name it Hibernator. You need to add reference to MayhemCore so as to be able to access the Mayhem base classes and other functionalities. Since the module will be a reaction, the class must extend ReactionBase class so that the Perform method can be overriden. The class must be decorated with the DataContract attribute which is found in System.Runtime.Serialization namespace and MayhemModule attribute contained in MayhemCore. The MayhemModule attribute has two parameters; the name and description of the reaction.

        
using System.Runtime.Serialization;
using MayhemCore;
using System;

namespace Mayhem.Hibernator
{
    [DataContract]
    [MayhemModule("Hibernator", "this reaction hibernates the system")]
    public class Hibernator : ReactionBase
    {
        public override void Perform() 
        {
            System.Diagnostics.Process.Start("shutdown", "-h");
        }

    }
}
        

The code that will be executed when the reaction is called will be placed in the Perform method, after the code has been written, you can now build the class library project. The output of the class library project which is a dll file can now be copied and placed in "C:\Program Files\Outercurve\Mayhem\DefaultModules.1.0.0\lib\net40" depending on where your Mayhem installation is. Restart Mayhem application and click Choose Reaction, you will see the that Hibernator has been added to the Reaction list.


                                  Mayhem reaction list

You can now connect any Event of your choice to this Reaction, in this case I associate a Speech Recognition Event with the Reaction so that anytime I say hibernate, the system will hibernate itself.

This is a simple demonstration of Mayhem, you can use Mayhem to do virtually anything you can dream of !!!
Topics:

Published at DZone with permission of Ayobami Adewole, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}