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

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

How to return a value when create a service in extension

yzhang1
1-Visitor

How to return a value when create a service in extension

Hi all,

I am now using Thingworx SDK and eclipse plugin to create thingworx extension.

But I found when I create a service, I don't know how to return value to thingworx.

In pure java environment, I use code below and can get JSON reply:

public class Imagetemp

{

public static void main(String[] args) throws Exception {

          String httpsURL = "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=4d52e94bd14312daee226197d05557697dc58867&version=2016-05-20&url=https://github.com/watson-developer-cloud/doc-tutorial-downloads/raw/master/visual-recognition/fruitbowl.jpg";

           URL myUrl = new URL(httpsURL);

           Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.company.com", 8080));  

            HttpsURLConnection conn = (HttpsURLConnection)myUrl.openConnection(proxy);

            TrustModifier.relaxHostChecking(conn);

            InputStream is = conn.getInputStream();

            InputStreamReader isr = new InputStreamReader(is);

            BufferedReader br = new BufferedReader(isr);

     

        String inputLine;

        while ((inputLine = br.readLine()) != null) {

            System.out.println(inputLine);

        }

      br.close();

     

// Add your code below

}       

}

And in thingworx project, I copy the code above into logger.trace:

@ThingworxBaseTemplateDefinition(name = "GenericThing")

@ThingworxPropertyDefinitions(properties = {

@ThingworxPropertyDefinition(name = "ImageAnal", description = "", category = "", baseType = "STRING", isLocalOnly = false, aspects = {

"isPersistent:true", "isLogged:true", "dataChangeType:VALUE" }) })

public class FinalImage extends Thing {

private static Logger _logger = LogUtilities.getInstance().getApplicationLogger(FinalImage.class);

@ThingworxServiceDefinition(name = "ImageAna", description = "", category = "", isAllowOverride = false, aspects = {

"isAsync:false" })

@ThingworxServiceResult(name = "Result", description = "", baseType = "STRING", aspects = {})

public String ImageAna() throws Exception {

_logger.trace("Entering Service: ImageAna");

        String httpsURL = "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?api_key=4d52e94bd14312daee226197d05557697dc58867&version=2016-05-20&url=https://github.com/watson-developer-cloud/doc-tutorial-downloads/raw/master/visual-recognition/fruitbowl.jpg";

        URL myUrl = new URL(httpsURL);

        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.micron.com", 8080));

    

        HttpsURLConnection conn = (HttpsURLConnection)myUrl.openConnection(proxy);

        TrustModifier.relaxHostChecking(conn);

        InputStream is = conn.getInputStream();

        InputStreamReader isr = new InputStreamReader(is);

        BufferedReader br = new BufferedReader(isr);

        StringBuilder sb = new StringBuilder();

        String Result = "";

        while ((Result = br.readLine()) != null) {

        System.out.println(Result);

        }

_logger.trace("Exiting Service: ImageAna");

return Result;

}

}

But the output is empty.

Can I know how to pass the result "inputline" to Result and get result when I trigger the service. Thanks!

Thanks

3 REPLIES 3
jamesm1
12-Amethyst
(To:yzhang1)

I think this may be a simple bug in the Eclipse Extension code whereby it uses a capital R for Result. See if changing:

@ThingworxServiceResult(name = "Result", description = "", baseType = "STRING", aspects = {})


to


@ThingworxServiceResult(name = "result", description = "", baseType = "STRING", aspects = {})


fixes the issue.

yzhang1
1-Visitor
(To:jamesm1)

Hi James,

Thank you for your reply.

I still cannot get the output.

Yingnan

jamesm1
12-Amethyst
(To:yzhang1)

Can you enable remote debugging to make sure Result is getting set with a non-empty string value? Here is a snippet on how to enable and use remote debugging in Eclipse: https://support.ptc.com/appserver/cs/view/solution.jsp?n=CS219756

Announcements


Top Tags