Index.razor 5.93 KB
@page "/Basic/Equipment"

@using System.Text.Json;
@using AntDesign.TableModels
@using System.ComponentModel
@using DataAcquisition.Common.Enums
@using DataAcquisition.Common.Utils
@using DataAcquisition.DataAccess
@using DataAcquisition.Models
@using DataAcquisition.Pages.Basic.EquipmentProptery
@using DataAcquisition.Services
@using DataAcquisition.ViewModels.IOT
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<DataContext> dbContextFactory;
@inject DataCacheService dataCacheService;
@inject IotService iotService;
@inject IMessageService _message

<Flex Justify="space-between" Align="center">
    <Flex Justify="flex-start" Align="center" Gap="small">
        <AntDesign.Input DefaultValue="@("mysite")">
            <AddOnBefore>编号</AddOnBefore>
        </AntDesign.Input>
        <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Search">搜索</Button>
        <Button Type="@ButtonType.Default" Icon="@IconType.Outline.Redo">重置</Button>
    </Flex>
    <Flex Justify="flex-end" Align="center" Gap="small">
        <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.CloudUpload">新增</Button>
        <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Delete" Danger>批量删除</Button>
        <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.CloudUpload" OnClick="IotCloudUpload">上传设备数据至IOT</Button>
    </Flex>
</Flex>
<Table @ref="mainTable"
       TItem="Equipment"
       DataSource="@equipments"
       Total="_total"
       @bind-PageIndex="_pageIndex"
       @bind-PageSize="_pageSize"
       Loading="loading"
       OnChange="EquipmentOnChange"
       Size="TableSize.Small"
       RowKey="x=>x.Id">
    <Selection Key="@(context.Id.ToString())" />
    <PropertyColumn Property="c=>c.Id" Hidden />
    <PropertyColumn Title="编号" Property="c=>c.Code" />
    <PropertyColumn Title="设备名称" Property="c=>c.Name" />
    <PropertyColumn Title="设备类型" Property="c=>c.EquipmentType" />
    <PropertyColumn Title="通信配置" Property="c=>c.CommunicationConfig!.Code" />
    <PropertyColumn Title="是否启用" Property="c=>c.Enable">
        <Switch @bind-Value="@context.Enable" Disabled="true"></Switch>
    </PropertyColumn>
    <PropertyColumn Title="区域" Property="c=>c.Area" />
    <PropertyColumn Title="备注" Property="c=>c.Remark" />
    <PropertyColumn Title="创建时间" Property="c=>c.CreateTime" />
    <ActionColumn Title="操作">
        <Space>
            <SpaceItem>
                <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Edit" Size="@ButtonSize.Small">修改</Button>
                <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Delete" Size="@ButtonSize.Small" Danger>删除</Button>
                <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.InfoCircle" Size="@ButtonSize.Small" OnClick="()=>OpenDetail(context.Id)">详细</Button>
            </SpaceItem>
        </Space>
    </ActionColumn>
</Table>

<Modal Title="@modalTitle"
       @bind-Visible="@_visible"
       Maximizable="true"
       Centered="true"
       Resizable="true"
       Draggable="true"
       Width="1200"
       MaxBodyHeight="80vh"
       Footer="null"
       DestroyOnClose="true"
       DefaultMaximized="true">
    <EquipmentPropteryIndex EquipmentId="@equipmentId" />
</Modal>

@code {

    bool loading = true;

    bool loading_upload = false;

    bool _visible = false;

    string modalTitle = string.Empty;

    List<Equipment> equipments = new List<Equipment>();

    SystemLog log = SystemLog.Instance;

    ITable mainTable = null!;

    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;

    int equipmentId = 0;

    public async Task EquipmentOnChange(QueryModel<Equipment> queryModel)
    {
        loading = true;
        using var dbContext = dbContextFactory.CreateDbContext();
        var query = dbContext.Equipments;
        equipments = await query.OrderBy(x => x.Id).Skip(((queryModel.PageIndex - 1) * queryModel.PageSize)).Take(queryModel.PageSize).Include(x => x.CommunicationConfig).AsSplitQuery().AsNoTracking().ToListAsync();
        _total = await query.CountAsync();
        loading = false;
    }

    private void OpenDetail(int id)
    {
        _visible = true;
        equipmentId = id;
        modalTitle = equipments.Find(x => x.Id == id)?.Name ?? string.Empty;
    }

    private async Task IotCloudUpload()
    {
        try
        {
            await _message.Loading(new MessageConfig()
                {
                    Content = "Uploading...",
                    Key = Guid.NewGuid().ToString()
                });
            await Task.Delay(1500);
            var context = dbContextFactory.CreateDbContext();
            var robotTypes = new List<EquipmentTypeConst>
            {
                EquipmentTypeConst.Kuka,
                EquipmentTypeConst.Fanuc,
                EquipmentTypeConst.Efort,
            };
            var data = context.Equipments.Where(x => robotTypes.Contains(x.EquipmentType)).Include(x => x.CommunicationConfig).Select(x => new IotEquipment
                {
                    EquipmentCode = x.Code,
                    EquipmentName = x.Name,
                    EquipmentTypeCode = x.EquipmentType.ToString(),
                    EquipmentTypeName = x.EquipmentType.ToString(),
                    IP = x.CommunicationConfig!.IpAddress,
                    DestinationArea = $"{x.Area}",
                    Remark = x.Remark ?? string.Empty,
                    Type = "I",
                    IsEnable = 1,
                }).AsSplitQuery().AsNoTracking().ToList();
            var result = iotService.SendEquipment(data);
            if (!result.Status)
            {
                await _message.Error($"上传失败,{result.Message}");
            }
            else
            {
                await _message.Success($"上传成功");
            }
        }
        catch (Exception ex)
        {
            await _message.Error($"操作失败,{ex.Message}");
        }
    }
}