using IStation.Model;
using IStation.ZyDto;
using Microsoft.Ajax.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace IStation
{
///
/// 调度方案(众毅)
///
internal class ZyProjectHelper
{
// static string _url_prj = "ws://192.168.201.17:8001";
public static string _url_prj = "ws://192.168.3.152:8001";
///
/// 提交调度项目
///
///
///
///
///
public static async Task SubmitPrj(IStation.CalcModel.AnaPrj currentViewPrj)
{
if (ZyConnectHelper.isDebug)
{
return " is isDebug";
}
if (currentViewPrj == null)
{
return "currentViewPrj is null";
}
IStation.ZyDto.SubmitPrj dto_prj = new ZyDto.SubmitPrj();
if (string.IsNullOrEmpty(currentViewPrj.ID))
{
var time = DateTime.Now;
currentViewPrj.ID = $"{time.Year}{time.Month}{time.Day}{time.Hour}{time.Minute}{time.Second}";
}
dto_prj.schemeID = currentViewPrj.ID;
dto_prj.Day = currentViewPrj.StartTime.ToString("yyyy-MM-dd");
dto_prj.StartTime = currentViewPrj.StartTime.ToString("yyyy-MM-dd HH:mm:ss");
dto_prj.EndTime = currentViewPrj.EndTime.ToString("yyyy-MM-dd HH:mm:ss");
dto_prj.FlowTotalIn = Math.Round(currentViewPrj.SumFlow, 0);
dto_prj.PowerTotal = Math.Round(currentViewPrj.SumPower, 0);
dto_prj.MoneyTotal = Math.Round(currentViewPrj.SumMoney, 0);
dto_prj.BlockTimes = new List();
foreach (var bt in currentViewPrj.BlockTimes)
{
if (bt.OpenPumpIndexs == null || bt.OpenPumpIndexs.Count() == 0)
continue;
ZyDto.SubmitPrjBlockTime dto_item = new ZyDto.SubmitPrjBlockTime();
dto_item.StartTime = bt.StartTime;
dto_item.EndTime = bt.EndTime;
dto_item.OpenPumpCount = bt.OpenPumpIndexs.Count;
dto_item.OpenPumpIndexs = bt.OpenPumpIndexs;// IStation.AnaGlobalParas.Setting.GetOpenPumpIndexArrays(bt.OpenPumpCount);
dto_prj.BlockTimes.Add(dto_item);
}
var ws = new ClientWebSocket();
await ws.ConnectAsync(new Uri(_url_prj), CancellationToken.None);
if (ws.State != WebSocketState.Open)
{
return "WebSocketState is close";
}
var inputJson = JsonHelper.Object2Json(dto_prj);
var inputBytes = Encoding.UTF8.GetBytes(inputJson);
IStation.LogHelper.Info(inputJson);
await ws.SendAsync(new ArraySegment(inputBytes), WebSocketMessageType.Text, true, CancellationToken.None);
try
{
List outputBytes = new List();//全部消息容器
var buffer = new byte[1024 * 4];//缓冲区
var result = await ws.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None); //监听Socket信息
while (!result.CloseStatus.HasValue) //是否关闭
{
if (result.MessageType == WebSocketMessageType.Text)//文本消息
{
outputBytes.AddRange(buffer.Take(result.Count));
if (result.EndOfMessage)//消息是否已接收完全
{
//发送过来的消息
string outputJson = Encoding.UTF8.GetString(outputBytes.ToArray(), 0, outputBytes.Count);
var outPrj = JsonHelper.Json2Object(outputJson);
if (outPrj == null)
return "return is null";
if (outPrj.result == 1)
{
//cbSuccess.Invoke(outPrj.schemeID);
}
else
{
return "outPrj result:" + outPrj.result;
}
}
}
// result = await ws.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None);//继续监听Socket信息
}
}
catch (Exception ex)
{
IStation.LogHelper.WriteError("Error:101", ex);
return "ex:" + ex.Message;
}
finally
{
try
{
//关闭WebSocket
await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "AcknowledgeCloseframe", CancellationToken.None);
}
catch (Exception ex)
{
IStation.LogHelper.WriteError("Error:111", ex);
// return "ex:" + ex.Message;
}
ws.Abort();
ws.Dispose();
}
return null;
}
///
/// 提交调度项目
///
///
///
///
///
public static async Task SubmitPrjAsync(IStation.CalcModel.AnaPrj currentViewPrj)
{
if (ZyConnectHelper.isDebug)
{
return " is isDebug";
}
if (currentViewPrj == null)
{
return "currentViewPrj is null";
}
IStation.ZyDto.SubmitPrj dto_prj = new ZyDto.SubmitPrj();
if (string.IsNullOrEmpty(currentViewPrj.ID))
{
var time = DateTime.Now;
currentViewPrj.ID = $"{time.Year}{time.Month}{time.Day}{time.Hour}{time.Minute}{time.Second}";
}
dto_prj.schemeID = currentViewPrj.ID;
dto_prj.Day = currentViewPrj.StartTime.ToString("yyyy-MM-dd");
dto_prj.StartTime = currentViewPrj.StartTime.ToString("yyyy-MM-dd HH:mm:ss");
dto_prj.EndTime = currentViewPrj.EndTime.ToString("yyyy-MM-dd HH:mm:ss");
dto_prj.FlowTotalIn = Math.Round(currentViewPrj.SumFlow, 0);
dto_prj.PowerTotal = Math.Round(currentViewPrj.SumPower, 0);
dto_prj.MoneyTotal = Math.Round(currentViewPrj.SumMoney, 0);
dto_prj.BlockTimes = new List();
foreach (var bt in currentViewPrj.BlockTimes)
{
if (bt.OpenPumpIndexs == null || bt.OpenPumpIndexs.Count() == 0)
continue;
ZyDto.SubmitPrjBlockTime dto_item = new ZyDto.SubmitPrjBlockTime();
dto_item.StartTime = bt.StartTime;
dto_item.EndTime = bt.EndTime;
dto_item.OpenPumpCount = bt.OpenPumpIndexs.Count;
dto_item.OpenPumpIndexs = bt.OpenPumpIndexs;// IStation.AnaGlobalParas.Setting.GetOpenPumpIndexArrays(bt.OpenPumpCount);
dto_prj.BlockTimes.Add(dto_item);
}
var ws = new ClientWebSocket();
await ws.ConnectAsync(new Uri(_url_prj), CancellationToken.None);
if (ws.State != WebSocketState.Open)
{
return "WebSocketState is close";
}
var inputJson = JsonHelper.Object2Json(dto_prj);
var inputBytes = Encoding.UTF8.GetBytes(inputJson);
IStation.LogHelper.Info(inputJson);
await ws.SendAsync(new ArraySegment(inputBytes), WebSocketMessageType.Text, true, CancellationToken.None);
try
{
List outputBytes = new List();//全部消息容器
var buffer = new byte[1024 * 4];//缓冲区
var result = await ws.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None); //监听Socket信息
while (!result.CloseStatus.HasValue) //是否关闭
{
if (result.MessageType == WebSocketMessageType.Text)//文本消息
{
outputBytes.AddRange(buffer.Take(result.Count));
if (result.EndOfMessage)//消息是否已接收完全
{
//发送过来的消息
string outputJson = Encoding.UTF8.GetString(outputBytes.ToArray(), 0, outputBytes.Count);
var outPrj = JsonHelper.Json2Object(outputJson);
if (outPrj == null)
return "return is null";
if (outPrj.result == 1)
{
//cbSuccess.Invoke(outPrj.schemeID);
}
else
{
return "outPrj result:" + outPrj.result;
}
}
}
// result = await ws.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None);//继续监听Socket信息
}
}
catch (Exception ex)
{
IStation.LogHelper.WriteError("Error:101", ex);
return "ex:" + ex.Message;
}
finally
{
try
{
//关闭WebSocket
await ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "AcknowledgeCloseframe", CancellationToken.None);
}
catch (Exception ex)
{
IStation.LogHelper.WriteError("Error:111", ex);
// return "ex:" + ex.Message;
}
ws.Abort();
ws.Dispose();
}
return null;
}
}
}