124 lines
4.6 KiB
C#
124 lines
4.6 KiB
C#
|
|
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
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()
|
|
{
|
|
Timer timer = new Timer(250);
|
|
JSONWebSocketResponse websocket = new JSONWebSocketResponse();
|
|
websocket.OnWebSocketStateChanged += (Socket, newstate) => {
|
|
if (newstate == WebSocketState.CLOSED)
|
|
{
|
|
timer.Stop();
|
|
timer.Dispose();
|
|
}
|
|
};
|
|
|
|
timer.Elapsed += (s,e) => {
|
|
try{
|
|
JSONObject controllerState = new JSONObject()
|
|
.Add("DriveControllers", new JSONArray().Add(Controller.DriveControllers.Select((dc=> new JSONObject()
|
|
.Add("slave", dc.Slave)
|
|
.Add("DriveController", dc.GetType().Name)
|
|
.Add("DriveState", dc.DriveState.ToString())
|
|
))))
|
|
.Add("DrivesState", Controller.DrivesState.ToString())
|
|
.Add("IsRunning", Controller.IsRunning)
|
|
;
|
|
websocket.Send(controllerState);
|
|
} catch (Exception ex)
|
|
{
|
|
Logging.Log(ex);
|
|
}
|
|
};
|
|
|
|
timer.Start();
|
|
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;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
} |