111 lines
3.9 KiB
C#
111 lines
3.9 KiB
C#
|
|
using Laservall.Solidworks.Model;
|
||
|
|
using Laservall.Solidworks.Server.Dto;
|
||
|
|
using Newtonsoft.Json;
|
||
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Net;
|
||
|
|
using System.Text;
|
||
|
|
using System.Threading;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
|
||
|
|
namespace Laservall.Solidworks.Server.Handlers
|
||
|
|
{
|
||
|
|
internal sealed class BomDataHandler
|
||
|
|
{
|
||
|
|
private readonly IBomDataProvider _dataProvider;
|
||
|
|
private BomLoadResult _lastLoadResult;
|
||
|
|
|
||
|
|
public BomDataHandler(IBomDataProvider dataProvider)
|
||
|
|
{
|
||
|
|
_dataProvider = dataProvider;
|
||
|
|
}
|
||
|
|
|
||
|
|
public BomLoadResult LastLoadResult => _lastLoadResult;
|
||
|
|
|
||
|
|
public async Task HandleGetData(HttpListenerContext context, CancellationToken ct)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var result = await _dataProvider.LoadBomAsync(null, ct);
|
||
|
|
_lastLoadResult = result;
|
||
|
|
|
||
|
|
var response = new BomStreamResponse
|
||
|
|
{
|
||
|
|
Items = result.Items.Select(ToDto).ToList(),
|
||
|
|
DynamicKeys = result.DynamicKeys,
|
||
|
|
Settings = ToSettingsDto(result.Settings)
|
||
|
|
};
|
||
|
|
|
||
|
|
string json = JsonConvert.SerializeObject(response);
|
||
|
|
byte[] bytes = Encoding.UTF8.GetBytes(json);
|
||
|
|
|
||
|
|
context.Response.StatusCode = 200;
|
||
|
|
context.Response.ContentType = "application/json; charset=utf-8";
|
||
|
|
context.Response.ContentLength64 = bytes.Length;
|
||
|
|
await context.Response.OutputStream.WriteAsync(bytes, 0, bytes.Length, ct);
|
||
|
|
context.Response.Close();
|
||
|
|
}
|
||
|
|
catch (OperationCanceledException)
|
||
|
|
{
|
||
|
|
try { context.Response.Close(); } catch { }
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
try
|
||
|
|
{
|
||
|
|
var errorResponse = new { error = ex.Message };
|
||
|
|
string errorJson = JsonConvert.SerializeObject(errorResponse);
|
||
|
|
byte[] errorBytes = Encoding.UTF8.GetBytes(errorJson);
|
||
|
|
|
||
|
|
context.Response.StatusCode = 500;
|
||
|
|
context.Response.ContentType = "application/json; charset=utf-8";
|
||
|
|
context.Response.ContentLength64 = errorBytes.Length;
|
||
|
|
await context.Response.OutputStream.WriteAsync(errorBytes, 0, errorBytes.Length, ct);
|
||
|
|
context.Response.Close();
|
||
|
|
}
|
||
|
|
catch { }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private static BomItemDto ToDto(BomItemModel item)
|
||
|
|
{
|
||
|
|
return new BomItemDto
|
||
|
|
{
|
||
|
|
Level = item.Level,
|
||
|
|
LevelDisplay = item.LevelDisplay,
|
||
|
|
DrawingNo = item.DrawingNo,
|
||
|
|
ConfigName = item.ConfigName,
|
||
|
|
PartName = item.PartName,
|
||
|
|
MaterialProp = item.MaterialProp,
|
||
|
|
Material = item.Material,
|
||
|
|
Classification = item.Classification,
|
||
|
|
Quantity = item.Quantity,
|
||
|
|
IsAssembly = item.IsAssembly,
|
||
|
|
IsOutSourcing = item.IsOutSourcing,
|
||
|
|
DocPath = item.DocPath,
|
||
|
|
Props = item.Props ?? new Dictionary<string, string>(),
|
||
|
|
HasChildren = item.HasChildren,
|
||
|
|
NodeId = item.NodeId,
|
||
|
|
ParentNodeId = item.ParentNodeId
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
private static SettingsDto ToSettingsDto(BomSettings settings)
|
||
|
|
{
|
||
|
|
return new SettingsDto
|
||
|
|
{
|
||
|
|
Columns = settings.Columns.Select(c => new ColumnConfigDto
|
||
|
|
{
|
||
|
|
Name = c.Name,
|
||
|
|
IsVisible = c.IsVisible,
|
||
|
|
IsExport = c.IsExport,
|
||
|
|
IsFixed = c.IsFixed,
|
||
|
|
IsUserAdded = c.IsUserAdded
|
||
|
|
}).ToList(),
|
||
|
|
RemovedKeys = settings.RemovedKeys?.ToList() ?? new List<string>()
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|