2020-07-02 22:02:21 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2020 Photon Vision.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-27 14:58:03 -07:00
|
|
|
package org.photonvision.server;
|
|
|
|
|
|
2020-08-14 12:39:21 -07:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2020-06-27 14:58:03 -07:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2020-08-14 12:39:21 -07:00
|
|
|
import edu.wpi.first.wpilibj.geometry.Rotation2d;
|
2020-06-27 14:58:03 -07:00
|
|
|
import io.javalin.http.Context;
|
2020-08-14 12:39:21 -07:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
|
import org.photonvision.common.configuration.ConfigManager;
|
|
|
|
|
import org.photonvision.common.configuration.NetworkConfig;
|
|
|
|
|
import org.photonvision.common.dataflow.networktables.NetworkTablesManager;
|
|
|
|
|
import org.photonvision.common.hardware.HardwareManager;
|
2020-09-04 18:18:44 -07:00
|
|
|
import org.photonvision.common.hardware.Platform;
|
2020-08-14 12:39:21 -07:00
|
|
|
import org.photonvision.common.logging.LogGroup;
|
|
|
|
|
import org.photonvision.common.logging.Logger;
|
|
|
|
|
import org.photonvision.common.networking.NetworkManager;
|
2020-09-10 19:20:16 -07:00
|
|
|
import org.photonvision.common.util.ShellExec;
|
2020-08-14 12:39:21 -07:00
|
|
|
import org.photonvision.vision.processes.VisionModuleManager;
|
2020-08-26 10:03:56 -07:00
|
|
|
import org.photonvision.vision.target.TargetModel;
|
2020-06-27 14:58:03 -07:00
|
|
|
|
|
|
|
|
public class RequestHandler {
|
2020-08-14 12:39:21 -07:00
|
|
|
private static final Logger logger = new Logger(RequestHandler.class, LogGroup.WebServer);
|
2020-06-27 14:58:03 -07:00
|
|
|
|
2020-06-28 04:40:43 -04:00
|
|
|
private static final ObjectMapper kObjectMapper = new ObjectMapper();
|
|
|
|
|
|
2020-08-14 12:39:21 -07:00
|
|
|
public static void onSettingUpload(Context ctx) {
|
|
|
|
|
var file = ctx.uploadedFile("zipData");
|
|
|
|
|
if (file != null) {
|
|
|
|
|
var tempZipPath =
|
|
|
|
|
new File(Path.of(System.getProperty("java.io.tmpdir"), file.getFilename()).toString());
|
|
|
|
|
tempZipPath.getParentFile().mkdirs();
|
|
|
|
|
try {
|
|
|
|
|
FileUtils.copyInputStreamToFile(file.getContent(), tempZipPath);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.error("Exception uploading settings file!");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
ConfigManager.saveUploadedSettingsZip(tempZipPath);
|
2020-09-04 18:18:44 -07:00
|
|
|
ctx.status(200);
|
|
|
|
|
logger.info("Settings uploaded, going down for restart.");
|
|
|
|
|
|
2020-09-10 19:20:16 -07:00
|
|
|
restartProgram(ctx);
|
2020-08-14 12:39:21 -07:00
|
|
|
} else {
|
|
|
|
|
logger.error("Couldn't read uploaded settings ZIP! Ignoring.");
|
2020-09-04 18:18:44 -07:00
|
|
|
ctx.status(500);
|
2020-08-14 12:39:21 -07:00
|
|
|
}
|
2020-06-28 04:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:39:21 -07:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public static void onGeneralSettings(Context context) throws JsonProcessingException {
|
|
|
|
|
Map<String, Object> map =
|
|
|
|
|
(Map<String, Object>) kObjectMapper.readValue(context.body(), Map.class);
|
|
|
|
|
var networking =
|
|
|
|
|
(Map<String, Object>)
|
|
|
|
|
map.get("networkSettings"); // teamNumber (int), supported (bool), connectionType (int),
|
|
|
|
|
// staticIp (str), netmask (str), gateway (str), hostname (str)
|
|
|
|
|
var lighting =
|
|
|
|
|
(Map<String, Object>) map.get("lighting"); // supported (true/false), brightness (int)
|
|
|
|
|
// TODO do stuff with lighting
|
|
|
|
|
|
|
|
|
|
var networkConfig = NetworkConfig.fromHashMap(networking);
|
|
|
|
|
ConfigManager.getInstance().setNetworkSettings(networkConfig);
|
2020-08-17 13:25:28 -07:00
|
|
|
ConfigManager.getInstance().requestSave();
|
2020-08-14 12:39:21 -07:00
|
|
|
NetworkManager.getInstance().reinitialize();
|
2020-08-19 17:08:24 -07:00
|
|
|
NetworkTablesManager.getInstance().setConfig(networkConfig);
|
2020-06-28 04:40:43 -04:00
|
|
|
|
2020-08-14 12:39:21 -07:00
|
|
|
context.status(200);
|
2020-06-28 04:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:39:21 -07:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public static void onCameraSettingsSave(Context context) {
|
|
|
|
|
try {
|
|
|
|
|
var settingsAndIndex = kObjectMapper.readValue(context.body(), Map.class);
|
|
|
|
|
logger.info("Got cam setting json from frontend!\n" + settingsAndIndex.toString());
|
|
|
|
|
var settings = (HashMap<String, Object>) settingsAndIndex.get("settings");
|
|
|
|
|
int index = (Integer) settingsAndIndex.get("index");
|
|
|
|
|
|
|
|
|
|
// The only settings we actually care about are FOV and pitch
|
|
|
|
|
var fov = Double.parseDouble(settings.get("fov").toString());
|
|
|
|
|
var pitch =
|
|
|
|
|
Rotation2d.fromDegrees(Double.parseDouble(settings.get("tiltDegrees").toString()));
|
|
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
|
String.format(
|
|
|
|
|
"Setting camera %s's fov to %s w/pitch %s", index, fov, pitch.getDegrees()));
|
|
|
|
|
var module = VisionModuleManager.getInstance().getModule(index);
|
|
|
|
|
module.setFovAndPitch(fov, pitch);
|
|
|
|
|
module.saveModule();
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
logger.error("Got invalid camera setting JSON from frontend!");
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
2020-06-28 04:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:39:21 -07:00
|
|
|
public static void onSettingsDownload(Context ctx) {
|
|
|
|
|
logger.info("exporting settings to download...");
|
|
|
|
|
try {
|
|
|
|
|
var zip = ConfigManager.getInstance().getSettingsFolderAsZip();
|
|
|
|
|
var stream = new FileInputStream(zip);
|
|
|
|
|
logger.info("Uploading settings with size " + stream.available());
|
|
|
|
|
ctx.result(stream);
|
|
|
|
|
ctx.contentType("application/zip");
|
|
|
|
|
ctx.header("Content-Disposition: attachment; filename=\"photonvision-settings-export.zip\"");
|
|
|
|
|
ctx.status(200);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
ctx.status(501);
|
|
|
|
|
logger.error("Got bad recode from zip to byte");
|
|
|
|
|
}
|
2020-06-28 04:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:39:21 -07:00
|
|
|
public static void onCalibrationEnd(Context ctx) {
|
|
|
|
|
var index = Integer.parseInt(ctx.body());
|
|
|
|
|
var calData = VisionModuleManager.getInstance().getModule(index).endCalibration();
|
|
|
|
|
if (calData == null) {
|
|
|
|
|
ctx.status(500);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.result(String.valueOf(calData.standardDeviation));
|
|
|
|
|
ctx.status(200);
|
2020-06-28 04:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:39:21 -07:00
|
|
|
public static void restartDevice(Context ctx) {
|
|
|
|
|
ctx.status(HardwareManager.getInstance().restartDevice() ? 200 : 500);
|
2020-06-28 04:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:39:21 -07:00
|
|
|
/**
|
|
|
|
|
* Note that this doesn't actually restart the program itself -- instead, it relies on systemd or
|
|
|
|
|
* an equivalent.
|
|
|
|
|
*/
|
|
|
|
|
public static void restartProgram(Context ctx) {
|
|
|
|
|
ctx.status(200);
|
2020-09-10 19:20:16 -07:00
|
|
|
|
|
|
|
|
if (Platform.isRaspberryPi()) {
|
|
|
|
|
try {
|
|
|
|
|
new ShellExec().executeBashCommand("systemctl restart photonvision");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.error("Could not restart device!", e);
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
2020-06-28 04:40:43 -04:00
|
|
|
}
|
2020-08-26 10:03:56 -07:00
|
|
|
|
|
|
|
|
public static void uploadPnpModel(Context ctx) {
|
|
|
|
|
UITargetData data;
|
|
|
|
|
try {
|
|
|
|
|
data = kObjectMapper.readValue(ctx.body(), UITargetData.class);
|
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
ctx.status(500);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VisionModuleManager.getInstance().getModule(data.index).setTargetModel(data.targetModel);
|
|
|
|
|
ctx.status(200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class UITargetData {
|
|
|
|
|
public int index;
|
|
|
|
|
public TargetModel targetModel;
|
|
|
|
|
}
|
2020-06-27 14:58:03 -07:00
|
|
|
}
|