Thursday, May 19, 2011

SQLTags DataType casting

One interesting feature when programming with SQLTags is that you can store just about any type of object you want within a tag.  Want a tag that stores a List?  Go for it!  Want to store a 3d model of your car?  Why not.

But hold on, maestro.  When Ignition gets a tag's value, it expects it in a certain form, so you actually can't just hand it your photo of Jennifer Aniston.

The Tag interface defines a few basic methods.  The ones we care about are:

public TagValue getValue()
public DataType getDataType()

TagValue (and its simple implementation BasicTagValue) contains this:

public Object getValue()

This method deceptively doesn't really want you to store an Object.  It's looking for that object to be a certain data type.  That data type is defined back up in your Tag object, not in your TagValue object.

In order to make sure your object conforms, a convenience method exists.

DataType.getTypeForClass(object.getClass());

If this method returns null, you know your object doesn't conform to any known data type.

If it does conform, why not create a new tag with that type?

BasicTagValue tagValue = BasicTagValue.fromObject(object);
BasicTag tag = new BasicTag(tagValue, DataType.getTypeForClass(object));

Note: make sure you use the correct DataType for sqltags:

com.inductiveautomation.ignition.common.sqltags.model.types.DataType

But wait! I actually *do* want to store a dancing baby in a tag, you say? Well, all you need to do is subclass BasicTagValue. Of course, you won't be able to hand Ignition's SQLTags browser the groovin picture, but you'll know it's there.

public class AnimationTagValue extends BasicTagValue {
 /** For serialization ** /
 @Deprecated
 public AnimationTagValue() {super();}

 public AnimationTagValue(AnimatedGif animation) {
  super(animation);
 }

 @Override
 public Object getValue() {
  AnimatedGif animation = (AnimatedGif) object;
  return animation.getNumberOfFrames();
 }

 public AnimatedGif getAnimation() {
  return (AnimatedGif) object;
 }
}

And while I suppose it would be possible to write a component for Vision that would display this data, it's really a bad idea. You don't want pictures of cats flying across your network and clogging up the tubes.  At least, not unless you've got mice in there.

Until next time.


Ignition Version: 7.2 (7.2.5)

1 comments:

zahavahzachmann said...

Best Casinos Near Harris, AR, TX - Mapyro
Looking for the nearest casinos to 이천 출장안마 Harris, AR? Mapyro offers information on 서산 출장샵 everything from slot 태백 출장안마 machines to table 여수 출장안마 games. 논산 출장안마

Post a Comment