1 Reply Latest reply on Jun 1, 2016 9:43 PM by hhsu RSS
    lukass Newbie

    Issues creating a service without annotations

    Hi all,

     

    i tried to create a Service following this example:

    http://support.ptc.com/cs/help/thingworx_hc/thingworx_edge/index.jspx?id=javasdk_topics-c_javasdk_service_adding_definition&action=show

     

    but somehow it does not work for me.

     

    I am able to create Thing and Properties without annotations (Properties are also set correctly on the server), but when i try and copy the sxample code from the Documentation i encounter a problem.

     

    I am able to see the Service in the "Browse Remote Services" section and i can also enter the parameters, but everytime i try to execute the Service i get this error:

     

    Unable to Invoke Service Add on Simple1 : Service Handler For [Add] Does Not Exist On Thing [Simple1]


    i know this error occurs when i try to test a service without saving but i did save it.

     

    I created a simple thing which only sends a random int to my server.

     

    See my Code for the SimpleTing1 here:

     

    package com.tinkerforge;

    import java.util.Random;

    import com.thingworx.communications.client.ConnectedThingClient;

    import com.thingworx.communications.client.things.VirtualThing;

    import com.thingworx.metadata.FieldDefinition;

    import com.thingworx.metadata.PropertyDefinition;

    import com.thingworx.metadata.ServiceDefinition;

    import com.thingworx.metadata.annotations.ThingworxServiceDefinition;

    import com.thingworx.metadata.annotations.ThingworxServiceParameter;

    import com.thingworx.metadata.annotations.ThingworxServiceResult;

    import com.thingworx.metadata.collections.FieldDefinitionCollection;

    import com.thingworx.types.BaseTypes;

    import com.thingworx.types.collections.AspectCollection;

    import com.thingworx.types.constants.Aspects;

    import com.thingworx.types.constants.CommonPropertyNames;

    import com.thingworx.types.constants.DataChangeType;

    import com.thingworx.types.primitives.BooleanPrimitive;

    import com.thingworx.types.primitives.IntegerPrimitive;

    import com.thingworx.types.primitives.NumberPrimitive;

    import com.thingworx.types.primitives.StringPrimitive;

     

     

    public class SimpleThing extends VirtualThing implements Runnable  {

     

     

    private static final long serialVersionUID = 1L;

    public SimpleThing(String name, String description, ConnectedThingClient client) throws Exception {

        super(name, description, client);

     

      //Create the property definition with name, description, and baseType

      PropertyDefinition property1 = new PropertyDefinition(

         "Property1",

         "Description for Property1",

         BaseTypes.NUMBER);

     

     

      //Create an aspect collection to hold all of the different aspects

      AspectCollection aspects = new AspectCollection();

     

     

      //Add the dataChangeType aspect

      aspects.put(Aspects.ASPECT_DATACHANGETYPE,

      new StringPrimitive(DataChangeType.ALWAYS.name()));

     

     

      //Add the dataChangeThreshold aspect

      aspects.put(Aspects.ASPECT_DATACHANGETHRESHOLD, new NumberPrimitive(0.0));

     

     

      //Add the cacheTime aspect

      aspects.put(Aspects.ASPECT_CACHETIME, new IntegerPrimitive(-1));

     

     

      //Add the isPersistent aspect

      aspects.put(Aspects.ASPECT_ISPERSISTENT, new BooleanPrimitive(false));

     

     

      //Add the isReadOnly aspect

      aspects.put(Aspects.ASPECT_ISREADONLY, new BooleanPrimitive(false));

     

     

      //Add the pushType aspect

      aspects.put("pushType", new StringPrimitive(DataChangeType.ALWAYS.name()));

     

     

      //Add the defaultValue aspect

      aspects.put(Aspects.ASPECT_DEFAULTVALUE, new NumberPrimitive(2));

     

     

      //Set the aspects of the property definition

      property1.setAspects(aspects);

     

     

      //Add the property definition to the Virtual Thing

     

      this.defineProperty(property1);

     

     

     

     

      //Put this code in the constructor (or a method called from the constructor)

     

     

      ServiceDefinition serviceDef = new ServiceDefinition(

      "Add",

      "Add two numbers");

      FieldDefinitionCollection parameterFields =

          new FieldDefinitionCollection();

      parameterFields.addFieldDefinition(

      new FieldDefinition(

     

     

      "p1",

      "The first addend of the operation",

      BaseTypes.NUMBER)

      );

      parameterFields.addFieldDefinition(

      new FieldDefinition(

     

     

      "p2",

      "The second addend of the operation",

      BaseTypes.NUMBER)

      );

     

     

      FieldDefinition resultField = new FieldDefinition(

      CommonPropertyNames.PROP_RESULT,

      "The sum of the two parameters",

      BaseTypes.NUMBER);

     

     

      serviceDef.setParameters(parameterFields);

      serviceDef.setResultType(resultField);

      super.defineService(serviceDef);

     

      super.initialize();

     

     

     

       }

     

      public Double Add(Double p1, Double p2) throws Exception {

     

     

           return p1 + p2;

      }

     

     

     

      public void set(){

      try {

      setProperty("Property1",  randInt(10,700));

      System.out.println("Property for Simple Thing sent to Server");

      updateSubscribedProperties(1);

      } catch (Exception e) {

      e.printStackTrace();

      }

      }

      public static int randInt(int min, int max) {

     

     

      Random rand = new Random();

     

     

      int randomNum = rand.nextInt((max - min) + 1) + min;

     

     

      return randomNum;

      }

      @Override

      public void processScanRequest() throws Exception {

      // Be sure to call the base classes scan request

      super.processScanRequest();

      // Execute the code for this simulation every scan

      this.set();

      }

     

      @Override

      public void run() {

      try {

      this.getClient().shutdown();

      } catch (Exception x) {

      }

      }

     

     

     

     

    }

     

    Thanks for your help,

     

    Lukas