Menu

Loading Configuration Data

David McGinnis Kyle

Saving and Loading Your Apps' Configuration

Both apps and IO interfaces harness the persistence framework in order to load information from the configuration file. For a general overview of the configuration file, see the second section of this page. Each app or IO interface which uses this framework needs to do the following two things:

  • Add a ParseableElement attribute to the top of your VoiceActivatedApp or IIOInterface class.

  • Inherit from the IParseable interface (IIOInterface and VoiceActivatedApp already do this).

ParseableElement attribute

The ParseableElement attribute identifies a class which can be created by the configuration file. The attribute takes two parameters, the name of the class which should be persisted, and whether it is an app or an IO interface.

IParseable Interface

The IParseable interface contains all of the information needed in order to load your class from configuration. Note that VoiceActivatedApp and IIOInterface already inherits from IParseable, so you just need to inherit from one of these for your components. If you are not inheriting from one of these types, you need to manually inherit from IParseable. IParseable contains the following members:

  • Properties contains the information about the attributes that should be persisted in this tag. These properties should be primitive values that can be easily serialized and deserialized. The property itself is an IEnumerable of PersistentProperty instances. PersistentProperty contains the name of the property, and a getter and setter for the property. The values coming in and returned should be in Strings, so you'll need to serialize any values you want to persist.

  • Children is an IEnumerable of other IParseables which are persisted as children of this component in the configuration file. These components may not be other apps or IO interfaces.

  • Initialize() is called once the component is first created from persistence, and all children and properties have been filled in. After this function has been called, Initialize() will be called on all of your children in your Children property.

  • AddChild(IParseable) is called during parsing, when a component is parsed from the file which is a child of this component. This function should be overridden in order to add the IParseable instance to whatever member backs the Children property. For apps, this has already been done by BaseSAMIApp, so you should only override this function if there is any extra logic you need when children are added.

Example

Below is an example of a parseable component being created. Note that this isn't the whole of the LightSwitchController code, but represents the relevant parts.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[ParseableElement("LightSwitch", ParseableElementType.IOInterface)]
internal class LightSwitchController : XBeeInterface, ILightSwitchController
{
private List<Switch> _switches;
private String _room;
private byte _xbeeId;

public IEnumerable<PersistentProperty> Properties
{
    get 
    {
        yield return new PersistentProperty("Room", () => _room, r => _room = r);
        yield return new PersistentProperty("XBeeId", () => _xbeeId.ToString(), id => _xbeeId = Byte.Parse(id));
    }
}

public IEnumerable<IParseable> Children
{
    get 
    {
        return _switches;
    }
}

public LightSwitchController()
    : base()
{
    _switches = new List<Switch>();
}

public void Initialize()
{
}

public void AddChild(IParseable component)
{
    if(component is Switch)
    {
        _switches.Add(component as Switch);
    }
}

}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For more on app development, go to this page.
For more on IO interface development, go to this page.


Related

Documentation: App Overview
Documentation: Declare the App to the Framework
Documentation: Home
Documentation: IO Interface Overview
Documentation: Installing and Using SAMI

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.