Blog topics for tag 'home automation' |
Developer documentation - Rule Items (c# / .Net)
Creating new rule items for lavalamp is fairly straightforward, mostly thanks to .Net's Reflection abilities. The easiest way to demonstrate this is with a couple of examples.
Firstly, a trivial example. Observe the 'ruleitem_not.cs' file in the source tree (server\netGui\RuleEngine\ruleItems\Basic logic\ruleItem-not.cs). This rule item simply outputs a negation of its input. Important bits of the code:
[RuleEngine.ToolboxRule]
[ToolboxRuleCategory("Basic logic")]
Stick these tags on your class to tell the rule system that your class represents a rule item. Change the string in the 'ToolboxRuleCategory' to whatever category your ruleItem would fit - it will be classified as this in the toolbox.
public class ruleItem_not : ruleItemBase
Define your class and inherit from ruleItemBase (adding a reference to the netgui project and a using netGui.RuleEngine.ruleItems if neccesary).
Now we simply override some methods to provide information about the ruleItem:
public override string ruleName() { return "NOT function"; }
Obviously, change this to a brief name of your ruleItem. This will be the name shown in the toolbox.
public override Dictionary getPinInfo()
{
Dictionary pinList = new Dictionary();
pinList.Add("input1", new pin { name = "input1", description = "input to invert", direction = pinDirection.input });
pinList.Add("output1", new pin { name = "output1", description = "input is false", direction = pinDirection.output });
return pinList;
}
Now is where it starts to get interesting! You should override the getPinInfo method, as above, and return a new Dictionary containing strings-and-pins. It's easier to read the code than it is to explain. The only thing to note is that the name of each pin must be unique (obviously, since the returned Dictionary is keyed by it).
public override void evaluate()
{
bool input1 = (bool)pinStates["input1"];
// only set the output if neccesary! constantly setting the output will result in a stack overflow.
if ((bool)pinStates["output1"] != !input1 )
pinStates["output1"] = !input1 ;
}
The main meat of your ruleItem will probably go here. This function gets called at any point that the rule system wants your ruleItem to update its outputs. Note the casting (pinStates contains Objects) and the comment - it is important not to let your outputs 'flap' or to set them to their existing values.
public ruleItem_not() { }
Finally, we add a parameterless constructor, for our toolbox routines to use.
Optionally, you can add some shine with the following:
public override System.Drawing.Image background() { return netGui.Properties.Resources.ruleItem_not; }
Return an Image which will be used as a background to your ruleItem;
public override Size preferredSize() { return new Size( 150,75 ); }
Ask the rule system to display your Item in a custom size |