146 lines
5.3 KiB
C#
146 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using ln.ethercat.controller;
|
|
using ln.ethercat.controller.drives;
|
|
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 ControllerWebSocket : WebSocketResponse
|
|
{
|
|
static List<ControllerWebSocket> webSockets = new List<ControllerWebSocket>();
|
|
static bool sendingUpdates;
|
|
public static void SendUpdates(Controller controller)
|
|
{
|
|
lock (webSockets)
|
|
{
|
|
if (sendingUpdates)
|
|
return;
|
|
sendingUpdates = true;
|
|
}
|
|
|
|
try
|
|
{
|
|
JSONArray jsonDriveControllers = new JSONArray();
|
|
|
|
foreach (DriveController driveController in controller.ECMaster.DriveControllers)
|
|
{
|
|
JSONObject jsonDriveController =
|
|
new JSONObject()
|
|
.Add("DriveState", driveController.DriveState.ToString())
|
|
.Add("OEMState", driveController.OEMDriveState)
|
|
.Add("DriveMode", driveController.DriveMode.ToString())
|
|
.Add("TargetValue", driveController.TargetValue)
|
|
.Add("ActualSpeed", driveController.ActualSpeed)
|
|
.Add("ActualTorque", driveController.ActualTorque)
|
|
.Add("ActualLoad", driveController.ActualLoad)
|
|
;
|
|
|
|
jsonDriveControllers.Add(jsonDriveController);
|
|
}
|
|
|
|
JSONObject jsonController =
|
|
new JSONObject()
|
|
.Add("State", controller.ControllerState.ToString())
|
|
.Add("Drives", jsonDriveControllers)
|
|
.Add("CycleCounter", controller.CycleCounter)
|
|
.Add("DisableRemoteWatchdog", controller.DisableRemoteWatchdog)
|
|
.Add("IgnoreRemoteInterface", controller.IgnoreRemoteInterface)
|
|
;
|
|
|
|
|
|
JSONObject jsonMessage =
|
|
new JSONObject()
|
|
.Add("event", "update")
|
|
.Add("value", jsonController)
|
|
;
|
|
|
|
string jsonMessageText = jsonMessage.ToString();
|
|
|
|
ControllerWebSocket[] currentSockets;
|
|
lock (webSockets)
|
|
currentSockets = webSockets.ToArray();
|
|
|
|
foreach (ControllerWebSocket webSocket in currentSockets)
|
|
{
|
|
if (webSocket.Controller.Equals(controller) && (webSocket.State == WebSocketState.OPEN))
|
|
webSocket.Send(jsonMessageText);
|
|
}
|
|
} catch (Exception e)
|
|
{
|
|
Logging.Log(e);
|
|
} finally
|
|
{
|
|
sendingUpdates = false;
|
|
}
|
|
}
|
|
|
|
Controller Controller;
|
|
|
|
public ControllerWebSocket(Controller controller)
|
|
{
|
|
Controller = controller;
|
|
OnWebSocketStateChanged += (WebSocketResponse webSocket, WebSocketState newState) => {
|
|
switch (newState)
|
|
{
|
|
case WebSocketState.OPEN:
|
|
lock (webSockets)
|
|
webSockets.Add(this);
|
|
break;
|
|
default:
|
|
lock (webSockets)
|
|
webSockets.Remove(this);
|
|
break;
|
|
}
|
|
};
|
|
}
|
|
|
|
|
|
public override void Received(string textMessage)
|
|
{
|
|
object target;
|
|
|
|
base.Received(textMessage);
|
|
|
|
JSONValue jsonTextValue = JSONParser.Parse(textMessage);
|
|
if (jsonTextValue is JSONObject jsonMessage)
|
|
{
|
|
string eventName = jsonMessage["event"].ToNative().ToString();
|
|
JSONObject jsonValue = jsonMessage["value"] as JSONObject;
|
|
|
|
switch (eventName)
|
|
{
|
|
case "set":
|
|
string propName = jsonValue["name"].ToNative().ToString();
|
|
object propValue = jsonValue["value"].ToNative();
|
|
target = Controller;
|
|
if (jsonValue.ContainsKey("drive"))
|
|
target = Controller.ECMaster.DriveControllers[(Int64)jsonValue["drive"].ToNative()];
|
|
|
|
PropertyInfo propertyInfo = target.GetType().GetProperty(propName);
|
|
propertyInfo.SetValue(target, Cast.To(propValue, propertyInfo.PropertyType));
|
|
break;
|
|
case "action":
|
|
string methodName = jsonValue["method"].ToNative().ToString();
|
|
JSONMapper.DefaultMapper.Deserialize(jsonValue["arguments"], out object[] arguments);
|
|
target = Controller;
|
|
if (jsonValue.ContainsKey("drive"))
|
|
target = Controller.ECMaster.DriveControllers[(Int64)jsonValue["drive"].ToNative()];
|
|
|
|
MethodInfo methodInfo = target.GetType().GetMethod(methodName);
|
|
methodInfo.Invoke(target,new object[0]);
|
|
break;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
} |