package com.thingworx.sdk.simple; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import com.thingworx.communications.client.ClientConfigurator; import com.thingworx.communications.client.ConnectedThingClient; import com.thingworx.relationships.RelationshipTypes.ThingworxEntityTypes; import com.thingworx.types.collections.ValueCollection; import com.thingworx.types.primitives.IntegerPrimitive; import com.thingworx.types.primitives.StringPrimitive; public class FileTransferExample { public static void main(String[] args) { // Create a client config ClientConfigurator config = new ClientConfigurator(); // Basic configuration. See SimpleClient.java for additional info. config.setUri("ws://localhost:8080/Thingworx/WS"); config.setAppKey("cb184b6d-bc3d-4a4c-8b72-c765fa2416c8"); config.ignoreSSLErrors(true); try { ConnectedThingClient client = new ConnectedThingClient(config); client.start(); if (client.waitForConnection(30000)){ while(!client.isShutdown()) { Thread.sleep(10000);//change it according the interval you want to upload the changed log. // Step 1. Compare new and old log file and return back the line number from which line the new log is noted //A ValueCollection is used to specify a service's parameters ValueCollection params = new ValueCollection(); FileCompare fc = new FileCompare(); File file1 = new File("I:/opt/thingworx/tw_staging/in/hello-edge.txt");// the new log files. File file2 = new File("I:/opt/thingworx/tw_staging/in/hello-thingworx.txt");// the old log on thingworx server; here is a copy locally. int lineNumber = 0; if(file1.exists()&&file2.exists()) lineNumber = fc.compareFile(file1, file2); //Step 2. Overwrite the log file(same to ThingWorx server log) to hello-thingworx.txt with the latest one locally; it is used for next time comparing. FileCompare.copyFile("I:/opt/thingworx/tw_staging/in/hello-edge.txt", "I:/opt/thingworx/tw_staging/in/hello-thingworx.txt"); // Step 3. Write the incremented log file content to a StringBuffer; note down the offset StringBuffer sb = new StringBuffer(); BufferedReader br2 = null; InputStream is2 = null; int currentLineNumber = 1;//read from lineNumber line of the log int offset = 0; try { is2 = new FileInputStream(file2); br2 = new BufferedReader(new InputStreamReader(is2, "UTF-8")); for (String line = br2.readLine(); line != null; line = br2.readLine()) { if(currentLineNumber 0){ if(lineNumber > 1) sb.append("\n"); sb.append(line); } } if (br2 != null) { br2.close(); } if (is2 != null) { is2.close(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if (br2 != null) { br2.close(); if (is2 != null) { is2.close(); } } } String deltaLogContent = sb.toString(); params.put("path", new StringPrimitive("/hello-thingworx.txt")); params.put("data", new StringPrimitive(deltaLogContent)); params.put("offset", new IntegerPrimitive(offset)); // Use the SystemRepository Thing to write incremented log file into the existing log file on the Platform. // This service's result type is NOTHING, so we can ignore the response. client.invokeService(ThingworxEntityTypes.Things, "SystemRepository", "WriteToTextFile", params, 5000); } } } catch(Exception e) { e.printStackTrace(); } } }