You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@edgent.apache.org by Susan Cline <ho...@pacbell.net> on 2017/03/28 16:40:12 UTC

Re: Trying register an application via the registerJar command

Hi,

I have not gotten back to this since Dale posted this a long time ago, but I’d like to get this working.  Has anyone gotten the registerJar command to work
successfully?  If so, it would be great if someone could respond with their code that works.

Thanks!

Susan


> On Sep 26, 2016, at 8:24 AM, Dale LaBossiere <dm...@gmail.com> wrote:
> 
> I think I understand the confusion / issue now...
> 
> Your app created the IotpDevice (with its config) when it started up, as a param to the IotProvider.  You only create one.
> 
> I’m think your registered-jar’s embedded TopologyBuilder builder’s accept<Topology,JsonObject> needs to call IotDevicePubSub.addIotDevice(topology) to get a “virtual" IotDevice (linked to the original IotpDevice you created) for the topology it’s building.  This is alluded to in the IotProvider class javadoc.  (the IotProvider.registerTopology() impl does that automatically for you when using the non-registerJar based flow)
> 
> e.g., something along the lines of:
>    class MyRegisterJarBasedBuilder implements TopologyBuilder {  // registered as Java service provider in the jar’s manifest
> 	...
>        BiConsumer<Topology,JsonObject> getBuilder {	   
>            return (topology,config) -> MyApp.myIotDeviceBasedBuilder( IotDevicePubSub.addIotDevice(topology), config) )
>        }
>    }
> 
>    class MyApp {
>      ...
>      static void myIotDeviceBasedBuilder(IotDevice iotDevice, JsonObject config) {
> 	Topology t = iotDevice.getTopology();
> 	  … build your topology on “t” using iotDevice as needed
>      }
>    }
> 
> — Dale
> 
>> On Sep 19, 2016, at 10:30 PM, Susan Cline <ho...@pacbell.net> wrote:
>> 
>> Many thanks, Dale.
>> 
>> Still trying to figure this out …
>> 
>> Cheers,
>> 
>> Susan
>>> On Sep 19, 2016, at 8:30 AM, Dale LaBossiere <dm...@gmail.com> wrote:
>>> 
>>> FYI I just created https://issues.apache.org/jira/browse/EDGENT-250 <https://issues.apache.org/jira/browse/EDGENT-250> to update IotProvider doc related to registerJar().  You should consider adding commentary there as part of your experiences — and/or consider tacking that jira and maybe even enhancing the doc further in this space :-)
>>> 
>>> — Dale
>>> 
>>> 
>>>> On Sep 19, 2016, at 11:07 AM, Dale LaBossiere <dm...@gmail.com> wrote:
>>>> 
>>>> The TopologyBuilder.getBuilder() impl is bogus.  Looks like it’s copied from TestApplication().  All it does is return a builder (BiConsumer) that creates a topology consisting of a TStream<String> containing one tuple with the app’s name and prints that stream.
>>>> 
>>>> Your getBuilder() impl needs to return a BiConsumer that builds your app’s topology.  Looks like that’s what your accept() is trying to do.  Seems like you want something like (though I’d rename that accept() to something like “buildTopology()” for clarity).
>>>> 
>>>>    public BiConsumer<Topology, JsonObject> getBuilder() {
>>>>    		return (t,c) -> accept(t,c);
>>>>    }   
>>>> 
>>>> Fix that and then see what happens regarding handling of the submit request.  You might also consider adding a println to your getBuilder() and accept() to verify they’re getting called.
>>>> 
>>>> — Dale
>>>> 
>>>>> On Sep 16, 2016, at 5:44 PM, Susan Cline <ho...@pacbell.net> wrote:
>>>>> 
>>>>> Thanks Dale.  It did not work however, the results are the same after removing the 3 lines before provider.start().
>>>>> When I submit the SpeedJarApp I can see this in the pi terminal (this is the full output from when I started the registerJarExample application to when I submitted the command):
>>>>> 
>>>>> pi@raspberrypi:~/devConf $ java -jar registerJarExample.jar speed_sensor_config1.txt
>>>>> Sep 16, 2016 9:39:08 PM org.apache.edgent.runtime.appservice.AppService registerTopology
>>>>> INFO: Register application name: edgentIotDevicePubSub
>>>>> Sep 16, 2016 9:39:08 PM org.apache.edgent.runtime.appservice.AppService registerTopology
>>>>> INFO: Register application name: edgentIotCommandsToControl
>>>>> Sep 16, 2016 9:39:08 PM org.apache.edgent.runtime.appservice.AppService registerTopology
>>>>> INFO: Register application name: edgentJobMonitorApp
>>>>> false
>>>>> Sep 16, 2016 9:39:09 PM pi.RegisterJarExample main
>>>>> INFO: Here is some info
>>>>> Sep 16, 2016 9:39:09 PM com.ibm.iotf.client.AbstractClient createClient
>>>>> INFO: pool-1-thread-22-edgentIotDevicePubSub: Org ID    = ni6dcf
>>>>>     Client ID    = d:ni6dcf:SpeedSensor:Pi3_SpeedSensor_1
>>>>> Sep 16, 2016 9:39:09 PM com.ibm.iotf.client.AbstractClient connect
>>>>> INFO: pool-1-thread-22-edgentIotDevicePubSub: Connecting client d:ni6dcf:SpeedSensor:Pi3_SpeedSensor_1 to ssl://ni6dcf.messaging.internetofthings.ibmcloud.com:8883 (attempt #1)...
>>>>> Sep 16, 2016 9:39:10 PM com.ibm.iotf.client.AbstractClient connect
>>>>> INFO: pool-1-thread-22-edgentIotDevicePubSub: Successfully connected to the IBM Watson IoT Platform
>>>>> Sep 16, 2016 9:39:49 PM org.apache.edgent.runtime.appservice.AppService registerJar
>>>>> INFO: Register jar: file:///home/pi/devConf/pi.sensors.jar
>>>>> Sep 16, 2016 9:39:49 PM org.apache.edgent.runtime.appservice.AppService registerJar
>>>>> INFO: about to call registerTopology
>>>>> Sep 16, 2016 9:39:49 PM org.apache.edgent.runtime.appservice.AppService registerTopology
>>>>> INFO: Register application name: SpeedJarApp
>>>>> SpeedJarApp
>>>>> Sep 16, 2016 9:40:11 PM org.apache.edgent.runtime.etiao.Executable$1 accept
>>>>> INFO: No more active user tasks
>>>>> 
>>>>> Thanks,
>>>>> 
>>>>> Susan
>>>>> 
>>>>> 
>>>>> 
>>>>>> On Sep 16, 2016, at 10:32 AM, Dale LaBossiere <dm...@gmail.com> wrote:
>>>>>> 
>>>>>> I haven’t digested the complete email message yet, but try removing those three lines before provider.start() and try again.
>>>>>> IotProvider already create/registers those services (described in its javadoc).  I guess I wouldn’t be surprised if these extra additions might much things up.
>>>>>> 
>>>>>> — Dale
>>>>>> 
>>>>>>> On Sep 16, 2016, at 1:17 PM, Susan Cline <ho...@pacbell.net> wrote:
>>>>>>> 
>>>>>>> I’ve gotten a step further, but I am still having problems.
>>>>>>> ...
>>>>>>> 
>>>>>>> public class RegisterJarExample {
>>>>>>> 		IotProvider provider = new IotProvider(topology -> new IotpDevice(topology, configFile));
>>>>>>> 		/*
>>>>>>> 		 * Not sure if I need the next few lines or not ...
>>>>>>> 		 */
>>>>>>> 		JsonControlService control = new JsonControlService();
>>>>>>> 		provider.getServices().addService(ControlService.class, control); 
>>>>>>> 		ApplicationService appService = AppService.createAndRegister(provider, provider);
>>>>>>> 		provider.start();
>>>>>>> 		
>>>>>>> 		logger.info("Here is some info");
>>>>>>> 	}
>>>>>>> 	
>>>>>>> }
>>>>>> 
>>>>> 
>>>> 
>>> 
>> 
> 


Re: Trying register an application via the registerJar command

Posted by Susan Cline <ho...@pacbell.net>.
Your table looks good Dale, many thanks!

Susan

> On Mar 31, 2017, at 10:23 AM, Dale LaBossiere <dm...@gmail.com> wrote:
> 
>> On Mar 30, 2017, at 8:53 PM, Susan Cline <home4slc@pacbell.net <ma...@pacbell.net>> wrote:
>> 
>> Yay!  I got it to work.
> 
> Woo hoo!  Happy Friday indeed!
> 
> BTW, I just improved doc on registerJar and IotProvider.  Take look and see if you agree / that’s enough.  I had previously improved the IotProvider package-info.java and it’s info on JSON for cmds, now including registerJar.
> 
> All post 1.1.0 I’m afraid.
> 
> https://issues.apache.org/jira/browse/EDGENT-405 <https://issues.apache.org/jira/browse/EDGENT-405>
> https://issues.apache.org/jira/browse/EDGENT-406 <https://issues.apache.org/jira/browse/EDGENT-406>
> 
> <PastedGraphic-1.png>
> 
> — Dale


Re: Trying register an application via the registerJar command

Posted by Dale LaBossiere <dm...@gmail.com>.
> On Mar 30, 2017, at 8:53 PM, Susan Cline <ho...@pacbell.net> wrote:
> 
> Yay!  I got it to work.

Woo hoo!  Happy Friday indeed!

BTW, I just improved doc on registerJar and IotProvider.  Take look and see if you agree / that’s enough.  I had previously improved the IotProvider package-info.java and it’s info on JSON for cmds, now including registerJar.

All post 1.1.0 I’m afraid.

https://issues.apache.org/jira/browse/EDGENT-405 <https://issues.apache.org/jira/browse/EDGENT-405>
https://issues.apache.org/jira/browse/EDGENT-406 <https://issues.apache.org/jira/browse/EDGENT-406>



— Dale

Re: Trying register an application via the registerJar command

Posted by Susan Cline <ho...@pacbell.net>.
Yay!  I got it to work.

Here is what I did:

1) On the pi start an application called RegisterJarExample that was made into a runnable jar file.

package pi;

...

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

		String s = null;
		for (String str: args) {
			s = str;
		}
		/* a device configuration file ties a specific device
		 * to a device type and an organization
		 * each device needs an authentication token
		 * devices must be registered at IOT prior to running this application
		 */
		File configFile;
		if (s != null) {
			s = "./" + s;
			configFile = new File(s);
		} else {
			configFile = new File("./device_config.txt");
		}
		
		IotProvider provider = new IotProvider(topology -> new IotfDevice(topology, configFile));
		provider.start();
	}
	
}

java -jar RegisterJar.jar

2) I placed the jar file that will be registered via the Edgent registerJar command on the pi (note this could be some external URL, but for my example I just uploaded it to the pi).
I called the jar file pi.sensors.jar.

To build the jar file, my build file looked like this:

<project name="pi.sensors" default="registerJar">
<target name="registerJar">
    <jar destfile="bin/pi.sensors.jar">
      <service type="org.apache.edgent.topology.services.TopologyBuilder">
        <provider classname="pi.sensors.SpeedSensorTopologyBuilder$SpeedSensor" />
      </service>
     <fileset dir="./bin" includes="**/SpeedSensorTopologyBuilder*"/>
    </jar>
  </target>
</project>

The SpeedSensorTopologyBuilder class looks like this:

public class SpeedSensorTopologyBuilder {
	private static abstract class Sensor implements TopologyBuilder {
       @Override
       public BiConsumer<Topology, JsonObject> getBuilder() {
    	   
       		return (topology,config) -> SpeedSensor.myIotDeviceBasedBuilder(IotDevicePubSub.addIotDevice(topology), config);
       }   
	}
	
	public static class SpeedSensor extends Sensor {
       @Override
       public String getName() {
           return "SpeedJarApp";
       }

       public static void myIotDeviceBasedBuilder(IotDevice iotDevice, JsonObject config) {
	        TStream<Date> readingTime = iotDevice.topology().poll(() -> new Date(), 3, TimeUnit.SECONDS);
	    	
			TStream<JsonObject> speedReading = readingTime.map(rt ->
				{
					JsonObject speedInfo = new JsonObject();
					long curTime = System.currentTimeMillis();
					SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss");
					Date dateObj = new Date(curTime);
					speedInfo.addProperty("time:", sdf.format(dateObj));
		
					try {
						double speed = SystemInfo.getMemoryUsed() * 0.0000000752;
					    Random randomGenerator = new Random();
					    int randomInt = randomGenerator.nextInt(20);
					    
						double randSpeed = speed + randomInt;
						DecimalFormat df = new DecimalFormat("#.##");
						speedInfo.addProperty("Speed", df.format(randSpeed));
					} catch(Exception e) {
						throw new RuntimeException(e);
					}
		            return speedInfo;
				});
			speedReading.print();
			iotDevice.events(speedReading,  "speedReading", QoS.FIRE_AND_FORGET);
       }
	}
}

3. I issued this registerJar command to the pi:

{"args":["file:/home/pi/slcEdgent/pi.sensors.jar",""],"op":"registerJar","alias":"edgent","type":"appService”}

4. I issued the ‘submit’ command:
{"args":["SpeedJarApp",{}],"op":"submit","alias":"edgent","type":"appService”}

In the running application on the pi, I saw this line:
Mar 31, 2017 12:34:58 AM pi.sensors.SpeedSensorTopologyBuilder$SpeedSensor myIotDeviceBasedBuilder


and then these lines:

{"time:":"Mar 31,2017 00:36:49","Speed":"15.12"}
{"time:":"Mar 31,2017 00:36:52","Speed":"23.13”}

5. Finally, I stopped the running application by issuing the ‘CLOSE’ command:

{"args":["CLOSE"],"op":"stateChange","alias":"SpeedJarApp","type":"job”}

The output on the pi stopped.


Thanks a bunch for your help Dale!

Cheers,

Susan

> On Mar 30, 2017, at 7:19 AM, Dale LaBossiere <dm...@gmail.com> wrote:
> 
>> On Mar 28, 2017, at 12:40 PM, Susan Cline <ho...@pacbell.net> wrote:
>> ...
>> I have not gotten back to this since Dale posted this a long time ago, but I’d like to get this working.  
> 
> I’ve reread all of this thread - whew :-).  I think you’re very close.  Your jar is OK. The registerJar cmd worked.  The submit cmd worked (with your getBuilder() that returned a fn that just created a top that printed the app name).
> 
> I’ll stick with the assertion in my last msg of 9/26 11:24.  I think all that's left to fix is:
> 
> - change your TopologyBuilder.getBuilder() impl
>  from
> 	(t, c) -> buildTopology(t, c)
>  to
> 	(t, c) -> buildTopology( IotDevicePubSub.addIotDevice(t), c))
> 	Notice that’s exactly what IotProvider.registerTopology() does - how the IotDevice will get created for and passed to your builder/top
> 
> - change your buildTopology(Topology t, JsonConfig c) to
> 	buildTopology(IotDevice iotDevice, JsonConfig c)  <== just a rename of the accept() in your 9/16/1:17 mail
> 
> I’m going to add a JIRA and improve the doc in IotProvider to make this clear.
> — Dale


Re: Trying register an application via the registerJar command

Posted by Dale LaBossiere <dm...@gmail.com>.
> On Mar 28, 2017, at 12:40 PM, Susan Cline <ho...@pacbell.net> wrote:
> ...
> I have not gotten back to this since Dale posted this a long time ago, but I’d like to get this working.  

I’ve reread all of this thread - whew :-).  I think you’re very close.  Your jar is OK. The registerJar cmd worked.  The submit cmd worked (with your getBuilder() that returned a fn that just created a top that printed the app name).

I’ll stick with the assertion in my last msg of 9/26 11:24.  I think all that's left to fix is:

- change your TopologyBuilder.getBuilder() impl
  from
	(t, c) -> buildTopology(t, c)
  to
	(t, c) -> buildTopology( IotDevicePubSub.addIotDevice(t), c))
	Notice that’s exactly what IotProvider.registerTopology() does - how the IotDevice will get created for and passed to your builder/top

- change your buildTopology(Topology t, JsonConfig c) to
	buildTopology(IotDevice iotDevice, JsonConfig c)  <== just a rename of the accept() in your 9/16/1:17 mail

I’m going to add a JIRA and improve the doc in IotProvider to make this clear.
— Dale