public override async Task ReceiveAsync(WebSocket socket, WebSocketReceiveResult result, byte[] buffer) { var socketid = WebSocketObjectHolder.GetId(socket); var message = $"{socketid} said: {Encoding.UTF8.GetString(buffer, 0, result.Count)}"; await SendMessageToAllAsync(message, AppConst.NON_BOM_UTF8_ENCORDING); }
public ForceSensorHandlerSingle(WebSocketObjectHolder webSocketObjectHolder, Settings settings) : base(webSocketObjectHolder) { this.settings = settings; if (isEthernet) { forceSensor = new ForceSensorEthernetManager(); } else { forceSensor = new ForceSensorSerialManager { PortName = settings.SensorCom }; } forceSensor.SensorDataUpdated += ForceSensor_SensorDataUpdated; try { forceSensor.Connect(); } catch (System.Exception e) { Console.Error.WriteLine(e.ToString()); } }
public override async Task OnDisconnected(WebSocket socket) { var socketId = WebSocketObjectHolder.GetId(socket); await base.OnDisconnected(socket); await SendMessageToAllAsync($"{socketId} disconnected", AppConst.NON_BOM_UTF8_ENCORDING); }
public override async Task ReceiveAsync(WebSocket socket, WebSocketReceiveResult result, byte[] buffer) { var socketid = WebSocketObjectHolder.GetId(socket); var message = AppConst.NON_BOM_UTF8_ENCORDING.GetString(buffer, 0, result.Count); await Task.CompletedTask; // Todo: コマンド受信処理 }
public override async Task OnConnected(WebSocket socket) { await base.OnConnected(socket); var socketid = WebSocketObjectHolder.GetId(socket); Console.WriteLine($"socket created: {socketid}"); await Task.CompletedTask; //await SendMessageToAllAsync($"{socketid} is now connected", AppConst.NON_BOM_UTF8_ENCORDING); }
public override async Task OnDisconnected(WebSocket client) { var socketId = WebSocketObjectHolder.GetId(client); await base.OnDisconnected(client); Console.WriteLine($"socket closed: {socketId}"); await Task.CompletedTask; //await SendMessageToAllAsync($"{socketId} disconnected", AppConst.NON_BOM_UTF8_ENCORDING); }
public ForceSensorHandlerDouble(WebSocketObjectHolder webSocketObjectHolder) : base(webSocketObjectHolder) { forceSensor = new ForceSensorEthernetManager(); forceSensor.SensorDataUpdated += ForceSensor_SensorDataUpdated; try { forceSensor.Connect(); } catch (System.Exception e) { Console.Error.WriteLine(e.ToString()); } }
public RobotInfoHandler(WebSocketObjectHolder webSocketObjectHolder, Settings settings) : base(webSocketObjectHolder) { this.settings = settings; this.robotInfo = new RobotInfo { Tcp = new Position { X = 0, Y = 0, Z = 0, Role = 0, Pitch = 0, Yaw = 0 } }; var flex = new flexData { version = "1", dataExchange = new type_dataExchange { dataRequest = new type_dataExchange_dataRequest[] { new type_dataExchange_dataRequest { data = new type_dataExchange_dataRequest_data[] { new type_dataExchange_dataRequest_data { unit = "1", group = type_DataGroup.Generic, id = "SYSTEM!", subid = "810", count = "6", push = "-1", threshold = 0.01, priority = "5" } } } } } }; this.sendMessage = createMessage(flex); }
public override async Task ReceiveAsync(WebSocket socket, WebSocketReceiveResult result, byte[] buffer) { var socketid = WebSocketObjectHolder.GetId(socket); var message = AppConst.NON_BOM_UTF8_ENCORDING.GetString(buffer, 0, result.Count); if (message.Length > 0) { try { var request = JsonConvert.DeserializeObject <WebSocketRequest>(message); Console.WriteLine(request.Cmd); Console.WriteLine(request.Args); switch (request.Cmd) { case (WebSocketCommands.SENSOR_RESET): sensorCount = 0; break; case (WebSocketCommands.LOG_START): LogStart(); break; case (WebSocketCommands.LOG_STOP): LogStopAndSave(request.Args.ToString()); break; default: break; } } catch (Exception e) { Console.WriteLine(e.ToString()); //await socket.SendAsync(AppConst.NON_BOM_UTF8_ENCORDING.GetBytes(e.ToString()), WebSocketMessageType.Text, true, CancellationToken.None); } } await Task.CompletedTask; // Todo: コマンド受信処理 }
public SensorInfoHandler(WebSocketObjectHolder webSocketObjectHolder, Settings settings) : base(webSocketObjectHolder) { this.settings = settings; if (isEthernet) { forceSensor = new ForceSensorEthernetManager(); } else { forceSensor = new ForceSensorSerialManager { PortName = settings.SensorCom }; } forceSensor.SensorDataUpdated += ForceSensor_SensorDataUpdated; cfdLogger = new CFD_Logger(); cfdLogger.RobotInfoUpdated += CfdLogger_RobotInfoUpdated; }
public override async Task OnDisconnected(WebSocket socket) { var socketId = WebSocketObjectHolder.GetId(socket); await base.OnDisconnected(socket); Console.WriteLine($"socket closed: {socketId}"); if (WebSocketObjectHolder.Count <= 0) { StopLoggingTimer(); StopSendTimer(); Thread.Sleep(100); this.forceSensor.DisConnect(); this.cfdLogger.DisConnect(); } await Task.CompletedTask; }
// WebSocket 関連イベント public override async Task OnConnected(WebSocket socket) { await base.OnConnected(socket); if (!cfdLogger.IsLogging) { cfdLogger.Connect(settings.CfdIP, settings.CfdPort); } if (!forceSensor.IsConnecting) { forceSensor.Connect(); } StartSendTimer(); var socketid = WebSocketObjectHolder.GetId(socket); Console.WriteLine($"socket created: {socketid}"); await Task.CompletedTask; }
public ForceSensorTestHandlerDouble(WebSocketObjectHolder webSocketObjectHolder) : base(webSocketObjectHolder) { DateTime s = DateTime.Now; this.testLoop = Task.Run(async() => { this.isStop = false; var random = new Random(); var seed = 2.0; while (!this.isStop) { var randomFArray = Enumerable.Range(0, 6).Select(i => Convert.ToSingle(seed * random.NextDouble() - seed / 2.0)).ToArray(); var data = new ForceSensorData { Fx = randomFArray[0], Fy = randomFArray[1], Fz = randomFArray[2], Mx = randomFArray[3], My = randomFArray[4], Mz = randomFArray[5], }; var res = new ForceSensorResponceDouble { Sensor1 = data, Sensor2 = data, TimeStamp = Convert.ToUInt32((DateTime.Now - s).TotalMilliseconds), }; var resStr = JsonConvert.SerializeObject(res); await SendMessageToAllAsync(resStr, AppConst.NON_BOM_UTF8_ENCORDING); System.Threading.Thread.Sleep(10); } }); }
public RobotInfoTestHandler(WebSocketObjectHolder webSocketObjectHolder) : base(webSocketObjectHolder) { DateTime s = DateTime.Now; this.testLoop = Task.Run(async() => { this.isStop = false; var random = new Random(); var seed = 2.0; while (!this.isStop) { var randomFArray = Enumerable.Range(0, 6).Select(i => Convert.ToSingle(seed * random.NextDouble() - seed / 2.0)).ToArray(); var data = new Position { X = randomFArray[0], Y = randomFArray[1], Z = randomFArray[2], Role = randomFArray[3], Pitch = randomFArray[4], Yaw = randomFArray[5], }; var res = new RobotInfo { Tcp = data, }; var resStr = JsonConvert.SerializeObject(res); await SendMessageToAllAsync(resStr, AppConst.NON_BOM_UTF8_ENCORDING); System.Threading.Thread.Sleep(10); } }); }
public ChatMessageHandler(WebSocketObjectHolder webSocketObjectHolder) : base(webSocketObjectHolder) { }
public override async Task OnConnected(WebSocket client) { await base.OnConnected(client); var socketid = WebSocketObjectHolder.GetId(client); Console.WriteLine($"socket created: {socketid}"); if (this.watchLoop == null) { this.watchLoop = Task.Run(async() => { Connect(); isStop = false; while (!isStop) { this.socket.Send(this.sendMessage); int receiveLength = socket.Receive(this.resBuffer); flexData resData = null; using (var ms = new System.IO.MemoryStream()) { try { var length = ParseHeader(this.resBuffer, 0); System.Diagnostics.Debug.WriteLine(length); // ms.Write(res, 8, receiveLength - 8); ms.Write(this.resBuffer, 8, length); ms.Position = 0; //Console.WriteLine(Encoding.UTF8.GetString(ms.GetBuffer())); resData = (flexData)serializer.Deserialize(ms); if (resData.notifications != null) { resData.notifications.ToList().ForEach(n => { Console.Error.WriteLine($"{n.code}: {n.message}"); }); Thread.Sleep(1000); continue; } var updateData = resData.dataExchange.dataUpdate[0].data[0].r; await infoSemaphore.WaitAsync(); try { this.robotInfo.Tcp.X = (float)updateData[0]; this.robotInfo.Tcp.Y = (float)updateData[1]; this.robotInfo.Tcp.Z = (float)updateData[2]; this.robotInfo.Tcp.Role = (float)updateData[3]; this.robotInfo.Tcp.Pitch = (float)updateData[4]; this.robotInfo.Tcp.Yaw = (float)updateData[5]; } finally { infoSemaphore.Release(); } Thread.Sleep(5); } catch (System.Exception err) { Console.WriteLine(err.ToString()); Thread.Sleep(5000); throw; } finally { ms.Dispose(); } } } }); } Thread.Sleep(100); if (sendLoop == null) { this.sendLoop = Task.Run(async() => { isStop = false; string str = null; while (!isStop) { await infoSemaphore.WaitAsync(); try { str = JsonConvert.SerializeObject(this.robotInfo); } finally { infoSemaphore.Release(); } //Console.WriteLine(str); await SendMessageToAllAsync(str, AppConst.NON_BOM_UTF8_ENCORDING); Thread.Sleep(10); } }); } await Task.CompletedTask; }
public WebSocketHandler(WebSocketObjectHolder webSocketObjectHolder) { WebSocketObjectHolder = webSocketObjectHolder; }