Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
VS Code format all files
Browse files Browse the repository at this point in the history
Closes #183
  • Loading branch information
durera committed Dec 23, 2019
1 parent b03129f commit 00b3038
Show file tree
Hide file tree
Showing 59 changed files with 1,030 additions and 1,081 deletions.
40 changes: 21 additions & 19 deletions samples/oshi/src/main/java/com/ibm/wiotp/samples/oshi/OshiApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,42 @@
import com.ibm.wiotp.sdk.app.messages.Event;
import com.ibm.wiotp.sdk.codecs.JsonCodec;


public class OshiApp implements Runnable {

private volatile boolean quit = false;
protected ApplicationClient client;

public OshiApp() throws Exception {
ApplicationConfig config = ApplicationConfig.generateFromEnv();
this.client = new ApplicationClient(config);
}

public void quit() {
this.quit = true;
}

public void run() {
try {
client.connect();
client.registerEventCallback(new MyEventCallback());
client.registerCodec(new JsonCodec());

// Create subscriptions for json-sigar events only because we only have a json codec registered

// Create subscriptions for json-sigar events only because we only have a json
// codec registered
client.subscribeToDeviceEvents("+", "+", "+", "json-sigar");

while (!quit) {
Thread.sleep(1000);
}
System.out.println("Closing connection to the IBM Watson IoT Platform");

// Once told to stop, cleanly disconnect from WIoTP
client.disconnect();
} catch (InterruptedException | KeyManagementException | NoSuchAlgorithmException | MqttException e) {
e.printStackTrace();
}
}

private class MyEventCallback implements EventCallback<OshiData> {
@Override
public void processEvent(Event<OshiData> evt) {
Expand All @@ -66,21 +66,22 @@ public Class<OshiData> getMessageClass() {
public static void main(String[] args) throws Exception {
createShutDownHook();
// Load custom logging properties file
try {
FileInputStream fis = new FileInputStream("logging.properties");
try {
FileInputStream fis = new FileInputStream("logging.properties");
LogManager.getLogManager().readConfiguration(fis);
} catch (SecurityException | IOException e) {
}
// Start the application thread

// Start the application thread
OshiApp a = new OshiApp();

Thread t1 = new Thread(a);
t1.start();

System.out.println(" * Organization: " + a.client.getConfig().getOrgId());
System.out.println(" * Organization: " + a.client.getConfig().getOrgId());
// TODO: Add this to the interface
// System.out.println("Connected successfully - Your App ID is " + a.client.getConfig().getAppId());
// System.out.println("Connected successfully - Your App ID is " +
// a.client.getConfig().getAppId());
System.out.println("");
System.out.println("(Press <enter> to disconnect)");

Expand All @@ -89,13 +90,14 @@ public static void main(String[] args) throws Exception {
try {
sc.nextLine();
sc.close();
} catch(Exception e) {}

} catch (Exception e) {
}

System.out.println("Closing connection to the IBM Watson IoT Platform");
// Let the App thread know it can terminate
a.quit();
}

private static void createShutDownHook() {
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
Expand Down
100 changes: 50 additions & 50 deletions samples/oshi/src/main/java/com/ibm/wiotp/samples/oshi/OshiData.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,57 @@

import com.ibm.wiotp.sdk.MessageInterface;

public class OshiData implements MessageInterface<OshiData>{
public class OshiData implements MessageInterface<OshiData> {
private static final DecimalFormat twoDForm = new DecimalFormat("#.##");
private String name;
private double mem;
private double cpu;
private DateTime timestamp;
public OshiData() {
this(null, -1, -1, null);
}
public OshiData(String name, double mem, double cpu) {
this(name, mem, cpu, null);
}

public OshiData(String name, double mem, double cpu, DateTime timestamp) {
this.name = name;
setMem(mem);
setCpu(cpu);
this.timestamp = timestamp;
}

public String getName() {
return name;
}
public double getMem() {
return mem;
}

public double getCpu() {
return cpu;
}
public void setName(String name) {
this.name = name;
}
public void setMem(double mem) {
this.mem = Double.valueOf(twoDForm.format(mem));
}

public void setCpu(double cpu) {
this.cpu = Double.valueOf(twoDForm.format(cpu * 100));
}
public String toString() {
return this.name + ":" + "/" + this.mem + "/" + this.cpu;
}

private String name;
private double mem;
private double cpu;
private DateTime timestamp;

public OshiData() {
this(null, -1, -1, null);
}

public OshiData(String name, double mem, double cpu) {
this(name, mem, cpu, null);
}

public OshiData(String name, double mem, double cpu, DateTime timestamp) {
this.name = name;
setMem(mem);
setCpu(cpu);
this.timestamp = timestamp;
}

public String getName() {
return name;
}

public double getMem() {
return mem;
}

public double getCpu() {
return cpu;
}

public void setName(String name) {
this.name = name;
}

public void setMem(double mem) {
this.mem = Double.valueOf(twoDForm.format(mem));
}

public void setCpu(double cpu) {
this.cpu = Double.valueOf(twoDForm.format(cpu * 100));
}

public String toString() {
return this.name + ":" + "/" + this.mem + "/" + this.cpu;
}

@Override
public OshiData getData() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,69 @@
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;


public class OshiDevice {

private static final Logger LOG = LoggerFactory.getLogger(OshiDevice.class);

private static DeviceClient client;
private static SystemInfo si = new SystemInfo();
private static JsonObject createOshiData() {
HardwareAbstractionLayer hal = si.getHardware();
CentralProcessor processor = hal.getProcessor();
double loadAverage = processor.getSystemCpuLoad() * 100;
GlobalMemory memory = hal.getMemory();
long availableMemory = memory.getAvailable();
long totalMemory = memory.getTotal();
long usedMemory = totalMemory - availableMemory;
double memoryUtilization = (usedMemory / (double) totalMemory) * 100;
JsonObject json = new JsonObject();
json.addProperty("memory", memoryUtilization);
json.addProperty("cpu", loadAverage);
return json;
}

private static JsonObject createOshiData() {
HardwareAbstractionLayer hal = si.getHardware();
CentralProcessor processor = hal.getProcessor();
double loadAverage = processor.getSystemCpuLoad() * 100;

GlobalMemory memory = hal.getMemory();
long availableMemory = memory.getAvailable();
long totalMemory = memory.getTotal();
long usedMemory = totalMemory - availableMemory;
double memoryUtilization = (usedMemory / (double) totalMemory) * 100;

JsonObject json = new JsonObject();
json.addProperty("memory", memoryUtilization);
json.addProperty("cpu", loadAverage);
return json;
}

public static void main(String[] args) throws Exception {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
LOG.info("Closing connection to IBM Watson IoT Platform ...");
// Once told to stop, cleanly disconnect from the service
client.disconnect();
// Allow 3 seconds for disconnect to complete
try {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
LOG.info("Closing connection to IBM Watson IoT Platform ...");
// Once told to stop, cleanly disconnect from the service
client.disconnect();
// Allow 3 seconds for disconnect to complete
try {
Thread.sleep(3000);
} catch (InterruptedException e) {}
}
});

} catch (InterruptedException e) {
}
}
});

DeviceConfig config = null;
if (args.length > 0 && args[0].equals("--quickstart")) {
SystemInfo si = new SystemInfo();
String macAddress = si.getHardware().getNetworkIFs()[0].getMacaddr().replace(":", "");
DeviceConfigIdentity identity = new DeviceConfigIdentity("quickstart", "iotsigar", macAddress);
DeviceConfigOptions options = new DeviceConfigOptions();
options.mqtt.port = 1883;

config = new DeviceConfig(identity, null, options);
}
else {
} else {
config = DeviceConfig.generateFromEnv();
}

client = new DeviceClient(config);
client.registerCodec(new JsonCodec());


LOG.info("IBM Watson IoT Platform OSHI Device Client");
LOG.info("https://github.com/ibm-watson-iot/iot-java/tree/master/samples/oshi");
LOG.info("");
if (args.length > 0 && args[0].equals("--quickstart")) {
LOG.info("Welcome to IBM Watson IoT Platform Quickstart, view a vizualization of live data from this device at the URL below:");
LOG.info("https://quickstart.internetofthings.ibmcloud.com/#/device/" + config.identity.deviceId + "/sensor/");
LOG.info(
"Welcome to IBM Watson IoT Platform Quickstart, view a vizualization of live data from this device at the URL below:");
LOG.info("https://quickstart.internetofthings.ibmcloud.com/#/device/" + config.identity.deviceId
+ "/sensor/");
LOG.info("");
}
LOG.info("(Press <Ctrl+C> to quit)");
Expand All @@ -89,7 +89,7 @@ public void run() {
client.publishEvent("oshi", data, 0);
Thread.sleep(5000);
}

}

}
Loading

0 comments on commit 00b3038

Please sign in to comment.