ln.ethercat/ln.ethercat.service/api/v1/ControllerApiController.cs

99 lines
3.7 KiB
C#

using System;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Mail;
using System.Reflection;
using System.Timers;
using ln.ethercat.controller;
using ln.ethercat.controller.drives;
using ln.http;
using ln.http.api;
using ln.http.api.attributes;
using ln.http.websocket;
using ln.json;
using ln.json.mapping;
using ln.logging;
using ln.type;
namespace ln.ethercat.service.api.v1
{
public class ControllerApiController : WebApiController
{
Controller Controller;
public ControllerApiController(Controller controller)
{
Controller = controller;
}
[GET("/sockets/controller")]
public HttpResponse GetControllerSocket(){
JSONEventWebSocketResponse webSocket = new JSONEventWebSocketResponse(Controller);
for (int drive = 0; drive < Controller.ECMaster.DriveControllers.Length; drive++)
{
DriveController driveController = Controller.ECMaster.DriveControllers[drive];
webSocket.AddTarget(String.Format("drives/{0}", drive), driveController, false);
}
return webSocket;
}
// [GET("/sockets/controller/drives/:drive")]
// public HttpResponse GetDriveControllerSocket(int drive)
// {
// DriveController driveController = Controller.DriveControllers[drive];
// Timer timer = new Timer(250);
// JSONWebSocketResponse websocket = new JSONWebSocketResponse();
// websocket.OnWebSocketStateChanged += (Socket, newstate) => {
// if (newstate == WebSocketState.CLOSED)
// {
// timer.Stop();
// timer.Dispose();
// }
// };
// websocket.OnWebSocketReceivedText += (s,text) => {
// JSONObject message = (JSONObject)JSONParser.Parse(text);
// switch (message["event"].ToNative().ToString())
// {
// case "action":
// Logging.Log(LogLevel.DEBUG, "DriveControllerSocket: action: {0}", message["value"].ToNative().ToString());
// driveController.GetType().GetMethod(message["value"].ToNative().ToString()).Invoke(driveController, new object[0]{});
// break;
// case "set":
// JSONObject jsonSet = (message["value"] as JSONObject);
// foreach (string key in jsonSet.Keys)
// {
// PropertyInfo propertyInfo = driveController.GetType().GetProperty(key);
// propertyInfo.SetValue(driveController, Cast.To(jsonSet[key].ToNative(), propertyInfo.PropertyType));
// }
// break;
// }
// };
// timer.Elapsed += (s,e) => {
// try{
// JSONObject driveControllerState = new JSONObject()
// .Add("id", drive)
// .Add("DriveState", driveController.DriveState.ToString())
// .Add("OEMDriveState", driveController.OEMDriveState)
// .Add("DriveMode", driveController.DriveMode)
// .Add("OEMDriveMode", driveController.OEMDriveMode)
// ;
// websocket.Send(driveControllerState);
// } catch (Exception ex)
// {
// Logging.Log(ex);
// }
// };
// timer.Start();
// return websocket;
// }
}
}