Skip to content

Commit

Permalink
Merge pull request #2 from TeleIO/camera-appveyor
Browse files Browse the repository at this point in the history
Camera appveyor
  • Loading branch information
dsolmann authored Sep 30, 2018
2 parents 9446c88 + 6684443 commit f81e8d9
Show file tree
Hide file tree
Showing 13 changed files with 1,154 additions and 10 deletions.
9 changes: 9 additions & 0 deletions Parts/ThirdPartyParts/rpm-camera-support.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This patch file adds support for RasterPropMonitor cameras

@PART[JSIPrimitiveExternalCamera]
{
MODULE
{
name = RasterPropMonitorCamera
}
}
28 changes: 18 additions & 10 deletions Telemachus/Telemachus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Telemachus</RootNamespace>
<AssemblyName>TeleIO</AssemblyName>
<AssemblyName>Telemachus</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
Expand Down Expand Up @@ -61,8 +61,16 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="src\CameraSnapshots\CameraCapture.cs" />
<Compile Include="src\CameraSnapshots\IVACameraCapture.cs" />
<Compile Include="src\CameraSnapshots\CurrentFlightCameraCapture.cs" />
<Compile Include="src\CameraSnapshots\RasterPropMonitorCamera.cs" />
<Compile Include="src\CameraSnapshots\RasterPropMonitorCameraCapture.cs" />
<Compile Include="src\CameraSnapshots\CameraCaptureManager.cs" />
<Compile Include="src\CameraSnapshots\SnapshotRenderer.cs" />
<Compile Include="src\DataLinkFormatters.cs" />
<Compile Include="src\DataLinkHandlers.cs" />
<Compile Include="src\CameraResponsibility.cs" />
<Compile Include="src\DataLinkResponsibility.cs" />
<Compile Include="src\ElseResponsibility.cs" />
<Compile Include="src\IOPageResponsibility.cs" />
Expand All @@ -88,18 +96,18 @@
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>rd /s /q "$(ProjectDir)..\publish\GameData"
rd /s /q "$(ProjectDir)..\ksp-telemachus-dev\GameData\TeleIO"
rd /s /q "$(ProjectDir)..\ksp-telemachus-dev\GameData\Telemachus"

xcopy "$(TargetDir)Servers.dll" "$(ProjectDir)..\publish\GameData\TeleIO\Plugins\" /e /y /i /r
xcopy "$(TargetDir)TeleIO.dll" "$(ProjectDir)..\publish\GameData\TeleIO\Plugins\" /e /y /i /r
xcopy "$(TargetDir)websocket-sharp.dll" "$(ProjectDir)..\publish\GameData\TeleIO\Plugins\" /e /y /i /r
xcopy "$(TargetDir)Servers.dll" "$(ProjectDir)..\publish\GameData\Telemachus\Plugins\" /e /y /i /r
xcopy "$(TargetDir)Telemachus.dll" "$(ProjectDir)..\publish\GameData\Telemachus\Plugins\" /e /y /i /r
xcopy "$(TargetDir)websocket-sharp.dll" "$(ProjectDir)..\publish\GameData\Telemachus\Plugins\" /e /y /i /r

xcopy "$(ProjectDir)..\Parts\*" "$(ProjectDir)..\publish\GameData\TeleIO\Parts\" /e /y /i /r
xcopy "$(ProjectDir)..\WebPages\WebPages\src\*" "$(ProjectDir)..\publish\GameData\TeleIO\Plugins\PluginData\Telemachus\" /e /y /i /r
xcopy "$(ProjectDir)..\licences\*" "$(ProjectDir)..\publish\GameData\TeleIO\" /e /y /i /r
copy "$(ProjectDir)..\readme.md" "$(ProjectDir)..\publish\GameData\TeleIO\"
xcopy "$(ProjectDir)..\Parts\*" "$(ProjectDir)..\publish\GameData\Telemachus\Parts\" /e /y /i /r
xcopy "$(ProjectDir)..\WebPages\WebPages\src\*" "$(ProjectDir)..\publish\GameData\Telemachus\Plugins\PluginData\Telemachus\" /e /y /i /r
xcopy "$(ProjectDir)..\licences\*" "$(ProjectDir)..\publish\GameData\Telemachus\" /e /y /i /r
copy "$(ProjectDir)..\readme.md" "$(ProjectDir)..\publish\GameData\Telemachus\"

xcopy "$(ProjectDir)..\WebPages\WebPagesTest\src\*" "$(ProjectDir)..\ksp-telemachus-dev\GameData\TeleIO\Plugins\PluginData\Telemachus\test" /e /y /i /r
xcopy "$(ProjectDir)..\WebPages\WebPagesTest\src\*" "$(ProjectDir)..\ksp-telemachus-dev\GameData\Telemachus\Plugins\PluginData\Telemachus\test" /e /y /i /r
xcopy "$(ProjectDir)..\publish\GameData\*" "$(ProjectDir)..\ksp-telemachus-dev\GameData\" /e /y /i /r
</PostBuildEvent>
</PropertyGroup>
Expand Down
177 changes: 177 additions & 0 deletions Telemachus/src/CameraResponsibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
//Author: Richard Bunt
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading;
using System.Reflection;
using WebSocketSharp.Net;
using WebSocketSharp;
using UnityEngine;
using System.Collections;
using Telemachus.CameraSnapshots;
using System.Text.RegularExpressions;

namespace Telemachus
{
public class CameraResponsibility : IHTTPRequestResponder
{
/// The page prefix that this class handles
public const String PAGE_PREFIX = "/telemachus/cameras";
public const String CAMERA_LIST_ENDPOINT = PAGE_PREFIX;
public const String NGROK_ORIGINAL_HOST_HEADER = "X-Original-Host";
protected Regex _cameraNameEndpointRegex;
protected Regex cameraNameEndpointRegex
{
get
{
if (_cameraNameEndpointRegex == null)
{
_cameraNameEndpointRegex = new Regex(Regex.Escape(PAGE_PREFIX) + "\\/(.+)");
}

return _cameraNameEndpointRegex;
}
}

/// The KSP API to use to access variable data
private IKSPAPI kspAPI = null;

private UpLinkDownLinkRate dataRates = null;



#region Initialisation

public CameraResponsibility(IKSPAPI kspAPI, UpLinkDownLinkRate rateTracker)
{
this.kspAPI = kspAPI;
dataRates = rateTracker;
}

public void setCameraCapture()
{
//PluginLogger.debug("START CAMERA CATPURE");

//PluginLogger.debug("CAM CAMPTURE CREATED");
}

#endregion

private static Dictionary<string,object> splitArguments(string argstring)
{
var ret = new Dictionary<string, object>();
if (argstring.StartsWith("?")) argstring = argstring.Substring(1);

foreach (var part in argstring.Split('&'))
{
var subParts = part.Split('=');
if (subParts.Length != 2) continue;
var keyName = UnityEngine.WWW.UnEscapeURL(subParts[0]);
var apiName = UnityEngine.WWW.UnEscapeURL(subParts[1]);
ret[keyName] = apiName;
}
return ret;
}

private static IDictionary<string, object> parseJSONBody(string jsonBody)
{
return (IDictionary<string, object>)SimpleJson.SimpleJson.DeserializeObject(jsonBody);
}

public string cameraURL(HttpListenerRequest request, CameraCapture camera)
{
String hostname = "";
if (request.Headers.Contains(NGROK_ORIGINAL_HOST_HEADER))
{
hostname = request.Headers[NGROK_ORIGINAL_HOST_HEADER];
}
else
{
hostname = request.UserHostName;
}

return request.Url.Scheme + "://" + hostname + PAGE_PREFIX + "/" + UnityEngine.WWW.EscapeURL(camera.cameraManagerName());
}

public bool processCameraManagerIndex(HttpListenerRequest request, HttpListenerResponse response)
{
if (GameObject.Find("CurrentFlightCameraCapture") == null)
{
//PluginLogger.debug("REBUILDING CAMERA CAPTURE");
this.setCameraCapture();
}

var jsonObject = new List<Dictionary<string, object>>();

foreach(KeyValuePair<string, CameraCapture> cameraKVP in CameraCaptureManager.classedInstance.cameras)
{
var jsonData = new Dictionary<string, object>();
jsonData["name"] = cameraKVP.Value.cameraManagerName();
jsonData["type"] = cameraKVP.Value.cameraType();
jsonData["url"] = cameraURL(request, cameraKVP.Value);

jsonObject.Add(jsonData);
}

byte[] jsonBytes = Encoding.UTF8.GetBytes(SimpleJson.SimpleJson.SerializeObject(jsonObject));

response.ContentEncoding = Encoding.UTF8;
response.ContentType = "application/json";
response.WriteContent(jsonBytes);
dataRates.SendDataToClient(jsonBytes.Length);

return true;
}

public bool processCameraImageRequest(string cameraName, HttpListenerRequest request, HttpListenerResponse response)
{
cameraName = cameraName.ToLower();
if (!CameraCaptureManager.classedInstance.cameras.ContainsKey(cameraName))
{
response.StatusCode = 404;
return true;
}

CameraCapture camera = CameraCaptureManager.classedInstance.cameras[cameraName];
//PluginLogger.debug("RENDERING SAVED CAMERA: "+ camera.cameraManagerName());
if (camera.didRender)
{
response.ContentEncoding = Encoding.UTF8;
response.ContentType = "image/jpeg";
response.WriteContent(camera.imageBytes);
dataRates.SendDataToClient(camera.imageBytes.Length);
}
else
{
response.StatusCode = 503;
}

return true;
}

public bool process(HttpListenerRequest request, HttpListenerResponse response)
{
//PluginLogger.debug(request.Url.AbsolutePath.TrimEnd('/'));
//PluginLogger.debug(String.Join(",", CameraCaptureManager.classedInstance.cameras.Keys.ToArray()));
//PluginLogger.debug("FLIGHT CAMERA: " + this.cameraCaptureTest);
if (request.Url.AbsolutePath.TrimEnd('/').ToLower() == CAMERA_LIST_ENDPOINT)
{
// Work out how big this request was
long byteCount = request.RawUrl.Length + request.ContentLength64;
// Don't count headers + request.Headers.AllKeys.Sum(x => x.Length + request.Headers[x].Length + 1);
dataRates.RecieveDataFromClient(Convert.ToInt32(byteCount));

return processCameraManagerIndex(request, response);
} else if (cameraNameEndpointRegex.IsMatch(request.Url.AbsolutePath))
{
Match match = cameraNameEndpointRegex.Match(request.Url.AbsolutePath);
string cameraName = UnityEngine.WWW.UnEscapeURL(match.Groups[1].Value);
//PluginLogger.debug("GET CAMERA: " + cameraName);
return processCameraImageRequest(cameraName, request, response);
}

return false;
}
}
}
Loading

0 comments on commit f81e8d9

Please sign in to comment.