Hi, 

I’m trying to come up with fairly complete example that allows me to do the 
following:

Send a registerJar command to a raspberry pi to register an Edgent application
Send a ‘submit’ command to start the Edgent application
Close/stop the Edgent application

Here is what I have done so far and have not been successful:

1) “manually” start an application on a raspberry pi that creates an 
IotProvider, and uses the AppService to creates and register this provider:

public class RunningApp {

        public static void main(String[] args) throws Exception {
                //DirectProvider provider = new DirectProvider();
                File configFile = new File("./device_config.txt");
                IotProvider provider = new IotProvider(topology -> new 
IotfDevice(topology, configFile));
                JsonControlService control = new JsonControlService();
                provider.getServices().addService(ControlService.class, 
control);
                AppService.createAndRegister(provider, provider);
                
                provider.start();
        }

}
2) Put a jar file called SensorApp.jar on the pi in the same directory where 
the ‘RunningApp’ is running.

3) Send a registerJar command that looks like the following:
command
:
"edgentControl"
date
:
1473800605520
payload
:
{args: ["/home/pi/slcQuarks/edgent/SensorApp.jar", ""], op: "registerJar", 
alias: "edgent",…}
alias
:
"edgent"
args
:
["/home/pi/slcQuarks/edgent/SensorApp.jar", ""]
0
:
"/home/pi/slcQuarks/edgent/SensorApp.jar"
1
:
""
op
:
"registerJar"
type
:
“appService"


I get no errors coming from the RunningApp console, and just see this:

pi@raspberrypi:~/slcQuarks/edgent $ java -jar runningApp.jar
Sep 13, 2016 8:43:23 PM com.ibm.iotf.client.AbstractClient createClient
INFO: pool-1-thread-22-edgentIotDevicePubSub: Org ID    = ni6dcf
         Client ID    = d:ni6dcf:raspberryPi3:Pi-SLC-Oakland
Sep 13, 2016 8:43:23 PM com.ibm.iotf.client.AbstractClient connect
INFO: pool-1-thread-22-edgentIotDevicePubSub: Connecting client 
d:ni6dcf:raspberryPi3:Pi-SLC-Oakland to 
ssl://ni6dcf.messaging.internetofthings.ibmcloud.com:8883 (attempt #1)...
Sep 13, 2016 8:43:25 PM com.ibm.iotf.client.AbstractClient connect
INFO: pool-1-thread-22-edgentIotDevicePubSub: Successfully connected to the IBM 
Watson IoT Platform


Does anyone know what I am doing wrong?

I looked at the IotAppServiceTest and it appears to me that my jar file to be 
loaded is correct.
Here is the code for the SensorApp:

import java.util.Date;
import java.util.concurrent.TimeUnit;
import com.google.gson.JsonObject;
import com.pi4j.system.SystemInfo;
import org.apache.edgent.connectors.iot.QoS;
import org.apache.edgent.connectors.iot.IotDevice;
import org.apache.edgent.function.BiConsumer;
import org.apache.edgent.topology.TStream;
import org.apache.edgent.topology.Topology;
import org.apache.edgent.topology.services.TopologyBuilder;

public class SensorApplications {


   private static abstract class SensorApp implements TopologyBuilder {
           
        @Override
        public BiConsumer<Topology, JsonObject> getBuilder() {
            return (t,c) -> t.strings(getName()).print();
        }     
    }
    
    public static class CpuSensorApp extends SensorApp {
        
        @Override
        public String getName() {
            return "CpuSensorApp";
        }
        
        public void accept(IotDevice device, JsonObject config) {
            TStream<Date> readingTime = device.topology().poll(() -> new 
Date(), 1, TimeUnit.SECONDS);

                TStream<JsonObject> cpuReading = readingTime.map(rt ->
                        {
                                JsonObject cpuInfo = new JsonObject();
                                cpuInfo.addProperty("ts", 
System.currentTimeMillis());
        
                                try {
                                        cpuInfo.addProperty("cpuTemperature", 
SystemInfo.getCpuTemperature());
                                        cpuInfo.addProperty("cpuVoltage", 
SystemInfo.getCpuVoltage());
                                } catch(Exception e) {
                                        throw new RuntimeException(e);
                                }
                    return cpuInfo;
                        });
                cpuReading.print();
                device.events(cpuReading,  "cpuReading", QoS.FIRE_AND_FORGET);  
        
        }
  
    }
    
    public static class MemorySensorApp extends SensorApp {
        @Override
        public String getName() {
            return "MemorySensorApp";
        }
    }
}


Thanks,

Susan

Reply via email to