cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Set avatar for ThingTemplate in custom extension?

sclifford
Newbie

Set avatar for ThingTemplate in custom extension?

Hi,

I want to set the avatar for a ThingTemplate, and all implementing Things, in my custom extension. There's not much on this in the Extension Development Guide (v4).

Currently, I'm attempting to use SetAvatar in the initializeThing() method of my extensions ThingTemplate.

@Override

public void initializeThing() throws Exception {

     super.initializeThing();

    InputStream stream = this.getClass().getClassLoader().getResourceAsStream(resourceName);

   if (stream == null) {

       throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");

   }

    byte[] content = Base64.encode(IOUtils.toByteArray(stream));

   this.setAvatar(content);

}

resourceName is the name of a PNG image located in the root of my extension project directory.

Is their annotation or aspect I should be setting?

Thanks,

Shane

Tags (2)
14 REPLIES 14

Re: Set avatar for ThingTemplate in custom extension?

Shane,

What happens when the code you cited above is exercised?

Meghan

Re: Set avatar for ThingTemplate in custom extension?

Meghan,

Thanks for getting back to me.
Nothing happens; no exception is thrown and the code after (which retrieves values from the configuration table) runs as expected. However, the avatar image is not set for the ThingTemplate or initialised Thing in the Composer.
I will try open up a debug port on my local Tomcat instance and step through the code from my Eclipse IDE.
Shane

Re: Set avatar for ThingTemplate in custom extension?

I don't know if that's the case, but sometimes you can have Cache Problems with ThingTemplate's Avatar, did you tried to refresh Composer browser tab with shift key pressed?

Re: Set avatar for ThingTemplate in custom extension?

Thanks for the suggestion. Unfortunately, shift+refresh didn't solve it.

Re: Set avatar for ThingTemplate in custom extension?

Shane,

The code I used was this:

@Override

  public void initializeThing() throws Exception {

  super.initializeThing();

  Path path = Paths.get("D:\\Users\\mhollenbach\\Pictures\\werd_2014.gif");

  byte[] content = Files.readAllBytes(path);

  this.SetAvatar(content);

  }

This is basically what you have, I'm just setting it from an image on my file system. This works when you create a Thing from that Template, but on the Template itself I'm seeing this (a white circle where the picture should be):

avatar_whitecircle.PNG

On the Thing I created I am seeing the image:

avatar_picture.PNG

Maybe try obtaining your image differently and that will work. I'll keep investigating what's going on with the ThingTemplate though.

Meghan

Re: Set avatar for ThingTemplate in custom extension?

Thanks Meghan,

I tried that and specifying the image from a local file path works as you described. But still no avatar for the ThingTemplate.

However, this approach won't work unless the Thingworx instance is installed on the same local system, right? For example, if I import the extension on an instance on a different machine, where the image file doesn't exist.

There are lots of extensions on the Thingworx Marketplace with custom avatars ... are there any source examples available?

Shane

Re: Set avatar for ThingTemplate in custom extension?

The stream variable was 'null' until I modified the following line of code:

InputStream stream = this.getClass().getResourceAsStream(resourceName);

where:

String resourceName = "my-icon.png";

Also both the *.class files for my extension project and the PNG image are located in the Jar file under the same package.

However, as I step through the code in the IDE a NullPointerException is thrown by:

this.SetAvatar(content);

?

Re: Set avatar for ThingTemplate in custom extension?

If you look at your code, Meghan it's using .SetAvatar and Shane it's using .setAvatar, that lower case can be the clue...

Re: Set avatar for ThingTemplate in custom extension?

That was a typo ... my bad. In the actual code it is .SetAvatar

Re: Set avatar for ThingTemplate in custom extension?

Hi Shane,

Just download the extensions which does it, and look onto the code how they do it

Carles.

Re: Set avatar for ThingTemplate in custom extension?

Ok, new code. I switched from nesting it in the java package to placing it in the Entities directory for now.

@Override

  public void initializeThing() throws Exception {

  super.initializeThing();

  URL url = getClass().getResource("Entities/werd_2014.gif");

  File file = new File(url.getPath());

  FileInputStream fileInputStream;

  byte[] content = new byte[(int) file.length()];

       

    try {

        //convert file into array of bytes

    fileInputStream = new FileInputStream(file);

    fileInputStream.read(content);

    fileInputStream.close();

    } catch(Exception e){

          e.printStackTrace();

    }

  this.SetAvatar(content);

  }



​Now, this sets the Thing's Avatar, but on the General Information page of the Thing and ThingTemplate there is still a white circle, but you will see that the actual icon representing the Thing in Composer has been set.

Meghan

Re: Set avatar for ThingTemplate in custom extension?

Thanks Meghan, I think that is the solution if setting the avatar from within the code.

Carles, so I downloaded and extracted an extension. No source code provided, but after decompiling the .class file, I discovered the avatar is not being set in code.

Avatar is set in the metadata.xml file for the extension; by adding an <avatar> tag to the <ThingTemplate> tag. Thing instances inherit the Avatar of their base Template.

<Entities>

    ...

    <ThingTemplates>

        <ThingTemplate dataChangeEventsEnabled="true" description="" iconURL="" name="myExtension" tags="" thingPackage="MyExtensionThing">

            <avatar>

                iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI...

            </avatar>

        </ThingTemplate>

    </ThingTemplates>

</Entities>

Thank you both for your help.

Re: Set avatar for ThingTemplate in custom extension?

Shane,

That is a good alternative solution, but the avatar should still be able to be set in the Java code. I'm speaking with a developer right now about this and will post on this thread once I get this resolved. In the meantime, if that workaround works for you, fantastic !

Meghan

Re: Set avatar for ThingTemplate in custom extension?

Sorry to bump this old thread, but do we have a solution on how to set the Avatar from Java extension SDK. The above code snippets work on absolute paths, but not for relative paths.

Announcements

Check out the upcoming Expert Session: Understanding ThingWorx Navigate Licensing in Community "Customer Events" section.