11 Replies Latest reply on Oct 31, 2016 4:17 PM by liweich RSS
    cjackson Explorer

    Run a local service from SDK

    I am new to Thingworx and trying to understand how the services work through an SDK (.net for what it's worth).

    Basically I want to know how to run a local service (local being relative to the platform) from my client code.  I see how we create a remote service, pick it up at the platform and can test it out at the platform which executes the code on the client.  How do I do the opposite where I reach up to the platform, execute a service and get the result back down to the client?

      • Re: Run a local service from SDK
        Ravi Upadhyay Communicator

        Hi @Chris

         

        I have achiveved the similar functionality through Java SDK.

         

        We have created a service on Things which when invoked from Java SDK running on Remote machine can create another thing from a specific template.

         

        Please see the steps and code below:

         

        Step 1 Create a new Template named as "TemplateForCreatingThingsRemotely" from Remote Template.

         

        Step 2  Add Service with Name "CreateThing"  with one input which would be name of  new Thing which will be created

         

        Example Script :

         

        var params = {

          name: NameOfThing /* STRING */,

          description: me.description /* STRING */,

          thingTemplateName: me.thingTemplate /* THINGTEMPLATENAME */,

          tags: me.tags /* TAGS */

        };

          // no return

        Resources["EntityServices"].CreateThing(params);

         

        Step 3 , Create a Thing Extending this newly created Template.

        Step 4 Test the Service .

         

        Now you need to call the service of first Thing which you have created from "TemplateForCreatingThingsRemotely"from Edge or Java SDK.

         

         

         

         

         

        Please do let me know if you find this useful or if  you need further clarification or help on this

          • Re: Run a local service from SDK
            cjackson Explorer

            Hello Ravi,

            Thanks for replying, let me specify more.  I think the part I'm not clear on is after step 4, you call the service from the SDK.  It's not clear from the SDK guide or Function calls in the VirtualThing class as to how to actually run a service which resides on the platform from code.  It seems I can only run remote services that I define in my client code.  Hope that makes sense.

            -Chris

          • Re: Run a local service from SDK
            Ravi Upadhyay Communicator

            Hi Chris,

            There are numerous ways to achieve this.

            Firstly , Once a Virtual Object from Java SDK is binded with Remote Thing on Thingworx server , all the properties can be pushed from Virtual object to Remote Thing on Thingworx , and you can put an alert against any such properties to call a service once certain specified condition is met. I mean you can call service depending on value of any property.

             

            Secondly , You can invoke a service on Thing from Client application.

             

            I am sending you a small documentation wherein an example code is given for service invoked from from Java SDK .

            (Please excuse the formatting , as I created just for single user , ie you. )

              • Re: Run a local service from SDK
                cjackson Explorer

                Thanks Ravi!  I was looking for a call in the VirtualThing class, but as your code points out there is an InvokeService method available from the BaseClient class which is what I actually was looking for.  Thanks for your help!

                 

                -Chris

                • Re: Run a local service from SDK
                  liweich Newbie

                  Hi Ravi!

                   

                  This is Liwei. Hope all is well!

                   

                  I'm currently working in Java SDK too and I'm very interesting in the code documentation of Calling service from EMS, if it is not too much trouble, may I ask for this documentation?

                   

                  Thank you very much!

                   

                  Best

                   

                  Liwei

                      • Re: Run a local service from SDK
                        liweich Newbie

                        Hi Sushant,

                         

                        Thank you so much for such quick reply!

                         

                        Actually I'm more looking for guide of using Java SDK, especially how to call a Thing service from java app(Java sdk guide didn't provide much info about this part), it is my bad that i misused  "EMS".

                         

                        Still thank you for your help!

                         

                        Best

                         

                        Liwei

                          • Re: Run a local service from SDK
                            supandey Collaborator

                            No problem - thanks for clarifying though -  just curious from the Java SDK I remembered there's a topic concerning invoking the service with a sample code, was this not helpful?

                             

                            //

                            // Invoking a service on a thing

                            ///////////////////////////////////////////////////////////////

                            //A ValueCollection is used to specify a service's parameters

                            ValueCollection params = new ValueCollection();

                            params.put("path", new StringPrimitive("/simple.txt"));

                            params.put("data", new StringPrimitive("Here is the content of the file."));

                            params.put("overwrite", new BooleanPrimitive(true));

                             

                            // Use the SystemRepository thing to create a text file on the Platform.

                            // This service's result type is NOTHING, so we can ignore the response.

                            client.invokeService(ThingworxEntityTypes.Things, "SystemRepository",

                            "CreateTextFile", params, 5000);

                             

                            // If a service does have a result, it is returned within an InfoTable.

                            params.clear(); // Clear the params used in the previous service invocation.

                            params.put("path", new StringPrimitive("/simple.txt"));

                             

                            // This service queries the SystemRepository for information about the file

                            // that was just created.

                            result = client.invokeService(ThingworxEntityTypes.Things, "SystemRepository",

                            "GetFileInfo", params, 5000);

                             

                            // The rows of an InfoTable are ValueCollections.

                            ValueCollection row = result.getFirstRow();