Basic commandline invocation working
This commit is contained in:
parent
e2d9878e8e
commit
eb07e42ab0
@ -1,11 +1,18 @@
|
|||||||
package name.nathanmcrae.numbersstation;
|
package name.nathanmcrae.numbersstation;
|
||||||
|
|
||||||
|
import com.jcraft.jsch.JSchException;
|
||||||
|
import com.jcraft.jsch.SftpException;
|
||||||
import com.leakyabstractions.result.api.Result;
|
import com.leakyabstractions.result.api.Result;
|
||||||
import com.leakyabstractions.result.core.Results;
|
import com.leakyabstractions.result.core.Results;
|
||||||
|
import com.tearsofaunicorn.wordpress.api.model.Post;
|
||||||
|
import com.tearsofaunicorn.wordpress.api.WordpressClient;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.logging.FileHandler;
|
import java.util.logging.FileHandler;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
@ -133,12 +140,14 @@ public class Main extends Application {
|
|||||||
public static void runStation(String stationName) {
|
public static void runStation(String stationName) {
|
||||||
if (stationName == null || stationName == "") {
|
if (stationName == null || stationName == "") {
|
||||||
logger.log(Level.SEVERE, "Station name must be provided and not empty");
|
logger.log(Level.SEVERE, "Station name must be provided and not empty");
|
||||||
|
// TODO: Notification
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<MainSettings, Exception> result = MainSettings.load();
|
Result<MainSettings, Exception> result = MainSettings.load();
|
||||||
if (!result.hasSuccess()) {
|
if (!result.hasSuccess()) {
|
||||||
logger.log(Level.SEVERE, "Unable to load settings");
|
logger.log(Level.SEVERE, "Unable to load settings");
|
||||||
|
// TODO: Notification
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
MainSettings settings = result.getSuccess().get();
|
MainSettings settings = result.getSuccess().get();
|
||||||
@ -150,11 +159,57 @@ public class Main extends Application {
|
|||||||
|
|
||||||
if (loadedStation == null) {
|
if (loadedStation == null) {
|
||||||
logger.log(Level.SEVERE, "Unable to find station " + stationName);
|
logger.log(Level.SEVERE, "Unable to find station " + stationName);
|
||||||
|
// TODO: Notification
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Loaded station " + stationName);
|
logger.info("Loaded station " + stationName);
|
||||||
|
|
||||||
|
Path stationDirPath = loadedStation.stationPath();
|
||||||
|
|
||||||
|
try {
|
||||||
|
boolean generateMessage = false;
|
||||||
|
if (!Files.exists(stationDirPath)) {
|
||||||
|
Files.createDirectories(stationDirPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
Path nextMessagePath = stationDirPath.resolve("next-message.txt");
|
||||||
|
String messageText;
|
||||||
|
if (!Files.exists(nextMessagePath)) {
|
||||||
|
messageText = loadedStation.generateMessage();
|
||||||
|
} else {
|
||||||
|
messageText = new String(Files.readAllBytes(nextMessagePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send message using appropriate method
|
||||||
|
switch (loadedStation.getMessageMethod()) {
|
||||||
|
case StationSettings.MessageMethod.SFTP:
|
||||||
|
//loadedStation.uploadNextMessageToSFTP();
|
||||||
|
break;
|
||||||
|
case StationSettings.MessageMethod.WORDPRESS:
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
String dateStr = dateFormat.format(new Date());
|
||||||
|
System.setProperty("wordpress.username", loadedStation.getUsername());
|
||||||
|
System.setProperty("wordpress.password", loadedStation.getPassword());
|
||||||
|
System.setProperty("wordpress.url", loadedStation.getAddress());
|
||||||
|
Post post = new Post(dateStr, messageText);
|
||||||
|
WordpressClient client = new WordpressClient();
|
||||||
|
String newPostId = client.newPost(post);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
logger.log(Level.SEVERE, "Message method " + loadedStation.getMessageMethod() + " not supported");
|
||||||
|
// TODO: Notification
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
String newMessageText = loadedStation.generateMessage();
|
||||||
|
Files.write(nextMessagePath, newMessageText.getBytes(StandardCharsets.UTF_8));
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.log(Level.SEVERE, "Exception while posting message to station " + stationName, e);
|
||||||
|
// TODO: Notification
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,9 +3,11 @@ package name.nathanmcrae.numbersstation;
|
|||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.security.SecureRandom;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalTime;
|
import java.time.LocalTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class StationSettings {
|
public class StationSettings {
|
||||||
private String address;
|
private String address;
|
||||||
@ -74,6 +76,20 @@ public class StationSettings {
|
|||||||
return directoryPath.resolve(stationDirName);
|
return directoryPath.resolve(stationDirName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String generateMessage() {
|
||||||
|
SecureRandom random = new SecureRandom();
|
||||||
|
StringBuilder messageBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
for (int i = 0; i < messageLength; i++) {
|
||||||
|
if (i > 0 && i % digitsPerGroup == 0) {
|
||||||
|
messageBuilder.append(" ");
|
||||||
|
}
|
||||||
|
messageBuilder.append(random.nextInt(10));
|
||||||
|
}
|
||||||
|
|
||||||
|
return messageBuilder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user