use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.
the class EventLoopManager method commandEvent.
@Override
public CallbackResult commandEvent(Command command) throws RobotCoreException {
// called on RecvRunnable.run() thread
CallbackResult result = CallbackResult.NOT_HANDLED;
// check if it's in the cache to avoid duplicate executions
for (Command c : commandRecvCache) {
if (c != null && c.equals(command)) {
// no need to continue, just return now
return CallbackResult.HANDLED;
}
}
// cache the command
commandRecvCache[(commandRecvCachePosition++) % commandRecvCache.length] = command;
// start or stop the event loop while it's busy processing a command
try {
synchronized (eventLoopLock) {
result = eventLoop.processCommand(command);
}
} catch (Exception e) {
// we should catch everything, since we don't know what the event loop might throw
RobotLog.ee(TAG, e, "Event loop threw an exception while processing a command");
}
return result;
}
use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.
the class WriteXMLFileHandler method writeToFile.
public void writeToFile(String data, File folder, String filenameWithExt) throws RobotCoreException, IOException {
if (duplicates.size() > 0) {
throw new DuplicateNameException("Duplicate names: " + duplicates);
}
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
File file = new File(folder, filenameWithExt);
FileOutputStream stream = null;
try {
stream = new FileOutputStream(file);
stream.write(data.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
stream.close();
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
}
}
} else {
throw new RobotCoreException("Unable to create directory");
}
}
use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.
the class TelemetryMessage method toByteArray.
@Override
public synchronized byte[] toByteArray() throws RobotCoreException {
// See countMessageBytes(...) for information about data format
timestamp = System.currentTimeMillis();
if (dataStrings.size() > cCountMax) {
throw new RobotCoreException("Cannot have more than %d string data points", cCountMax);
}
if (dataNumbers.size() > cCountMax) {
throw new RobotCoreException("Cannot have more than %d number data points", cCountMax);
}
int payloadSize = countMessageBytes();
int totalSize = RobocolParsable.HEADER_LENGTH + payloadSize;
ByteBuffer buffer = getWriteBuffer(payloadSize);
// timestamp
buffer.putLong(timestamp);
// sorted
buffer.put((byte) (isSorted ? 1 : 0));
// robot state
buffer.put(robotState.asByte());
// tag
if (tag.length() == 0) {
putTagLen(buffer, 0);
} else {
byte[] tagBytes = tag.getBytes(CHARSET);
if (tagBytes.length > cbTagMax) {
throw new RobotCoreException(String.format("Telemetry tag cannot exceed %d bytes [%s]", cbTagMax, tag));
}
putTagLen(buffer, tagBytes.length);
buffer.put(tagBytes);
}
// data strings
putCount(buffer, dataStrings.size());
for (Entry<String, String> entry : dataStrings.entrySet()) {
byte[] key = entry.getKey().getBytes(CHARSET);
byte[] value = entry.getValue().getBytes(CHARSET);
if (key.length > cbKeyMax) {
throw new RobotCoreException("telemetry key '%s' too long: %d bytes; max %d bytes", entry.getKey(), key.length, cbKeyMax);
}
if (value.length > cbValueMax) {
throw new RobotCoreException("telemetry value '%s' too long: %d bytes; max %d bytes", entry.getValue(), value.length, cbValueMax);
}
putKeyLen(buffer, key.length);
buffer.put(key);
putValueLen(buffer, value.length);
buffer.put(value);
}
// data numbers
putCount(buffer, dataNumbers.size());
for (Entry<String, Float> entry : dataNumbers.entrySet()) {
byte[] key = entry.getKey().getBytes(CHARSET);
float val = entry.getValue();
if (key.length > cbKeyMax) {
throw new RobotCoreException("telemetry key '%s' too long: %d bytes; max %d bytes", entry.getKey(), key.length, cbKeyMax);
}
putKeyLen(buffer, key.length);
buffer.put(key);
buffer.putFloat(val);
}
// done
return buffer.array();
}
use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.
the class RobotLog method cancelWriteLogcatToDisk.
/**
* Cancels any logcat writing to disk that might currently be going on
*/
public static synchronized void cancelWriteLogcatToDisk() {
// If we're not logging, then we've got nothing to do
if (loggingThread == null) {
return;
}
Context context = AppUtil.getDefContext();
final String packageName = context.getPackageName();
final String filename = getLogFile(context).getAbsolutePath();
// let last few log messages out before we stop logging
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// just continue
}
// Kill off the logging process. That will let the shell.run() in the logging thread return
try {
RobotLog.v("Killing logcat process.");
RunShellCommand shell = new RunShellCommand();
RunShellCommand.killSpawnedProcess(logcatCommand, packageName, shell);
} catch (RobotCoreException e) {
RobotLog.e("Unable to cancel writing log file to disk: " + e.toString());
return;
}
RobotLog.v("Waiting for the logcat process to die.");
ElapsedTime timeoutTimer = new ElapsedTime();
// wait until the log thread terminates
while (loggingThread != null) {
if (timeoutTimer.milliseconds() > 1000.0) {
loggingThread.interrupt();
}
Thread.yield();
}
}
use of com.qualcomm.robotcore.exception.RobotCoreException in project robotcode by OutoftheBoxFTC.
the class RunShellCommand method killSpawnedProcess.
/**
* Kill any spawn processes matching a given process name
*
* @param processName name of process to kill
* @param packageName name of this package
* @param shell an instance of RunShellCommand
* @throws RobotCoreException if unable to kill process
*/
public static void killSpawnedProcess(String processName, String packageName, RunShellCommand shell) throws RobotCoreException {
try {
int pid = getSpawnedProcessPid(processName, packageName, shell);
while (pid != -1) {
RobotLog.v("Killing PID " + pid);
shell.run(String.format("kill %d", pid));
pid = getSpawnedProcessPid(processName, packageName, shell);
}
} catch (Exception e) {
throw new RobotCoreException(String.format("Failed to kill %s instances started by this app", processName));
}
}
Aggregations