Browse Source

增加dataV数据维护界面

样式分支
fyf 2 years ago
parent
commit
79403b96f8
4 changed files with 471 additions and 0 deletions
  1. +189
    -0
      BPASmartClient.ViewModel/DataVViewModel.cs
  2. +153
    -0
      BPASmartClient/Control/DataVView.xaml
  3. +122
    -0
      BPASmartClient/Control/DataVView.xaml.cs
  4. +7
    -0
      BPASmartClient/MainWindow.xaml

+ 189
- 0
BPASmartClient.ViewModel/DataVViewModel.cs View File

@@ -0,0 +1,189 @@

using BPASmartClient.Helper;
using BPASmartClient.IoT;
using DataVAPI.Tool.IOT;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPASmartClient.ViewModel
{
public class DataVViewModel : ObservableObject
{
#region 单一变量
private volatile static DataVViewModel _Instance;
public static DataVViewModel GetInstance() => _Instance ?? (_Instance = new DataVViewModel());
private DataVViewModel()
{
deviceTable = new ObservableCollection<DeviceTable>();
device = new DeviceTable();
deviceTableSelectedItem = new DeviceTable();
ApiURL = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString();
Refresh();
}
#endregion

#region 公有变量
/// <summary>
/// 设备表
/// </summary>
public ObservableCollection<DeviceTable> deviceTable { get; set; }
/// <summary>
/// 选中行
/// </summary>
private DeviceTable _deviceTableSelectedItem;

public DeviceTable deviceTableSelectedItem
{
get { return _deviceTableSelectedItem; }
set
{
if (_deviceTableSelectedItem == value)
return;
_deviceTableSelectedItem = value;
OnPropertyChanged("deviceTableSelectedItem");
}
}
/// <summary>
/// 当前设备
/// </summary>
private DeviceTable _device;

public DeviceTable device
{
get { return _device; }
set
{
if (_device == value)
return;
_device = value;
OnPropertyChanged("device");
}
}
/// <summary>
/// API 地址
/// </summary>
public string ApiURL { get; set; }
#endregion

#region 公有函数


/// <summary>
/// 刷新
/// </summary>
public void Refresh()
{
deviceTable.Clear();
Inquire()?.OrderBy(o => int.Parse(o.ClientId)).ToList().ForEach(d =>
{
if (int.Parse(d.ClientId) > 0)
{
deviceTable.Add(d);
}
});
}

/// <summary>
/// 根据客户端iD查询
/// </summary>
/// <param name="clientId"></param>
public List<DeviceTable> Inquire(string clientId = "")
{
try
{
string url = ApiURL + "/api/Device/Query";
if (!string.IsNullOrEmpty(clientId)) url = ApiURL + "/api/Device/Query?clientId=" + clientId;
string json = HttpRequestHelper.HttpGetRequest(url);
JsonMsg<List<DeviceTable>> jsonMsg = Tools.JsonToObjectTools<JsonMsg<List<DeviceTable>>>(json);
return jsonMsg.obj.data;
}
catch (Exception ex)
{
return null;
}

}

/// <summary>
/// 修改
/// </summary>
/// <returns></returns>
public bool Update()
{
try
{
string url = ApiURL + "/api/Device/Modify";
string json = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(device));
Refresh();
return true;
}
catch (Exception ex)
{
return false;
}

}

/// <summary>
/// 增加
/// </summary>
/// <returns></returns>
public bool Add()
{
try
{
string url = ApiURL + "/api/Device/Create";
string json = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(device));
Refresh();
return true;
}
catch (Exception ex)
{
return false;
}

}

public bool Add(DeviceTable deviceTable)
{
try
{
string url = ApiURL + "/api/Device/Create";
string json = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(deviceTable));
Refresh();
return true;
}
catch (Exception ex)
{
return false;
}

}

/// <summary>
/// 删除
/// </summary>
/// <returns></returns>
public bool Delete()
{
try
{
string url = ApiURL + "/api/Device/DeleteDate?id=" + device.Id;
string json = HttpRequestHelper.HttpGetRequest(url);
Refresh();
return true;
}
catch (Exception ex)
{
return false;
}

}
#endregion
}
}

+ 153
- 0
BPASmartClient/Control/DataVView.xaml View File

@@ -0,0 +1,153 @@
<UserControl x:Class="BPASmartClient.Control.DataVView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:BPASmartClient.Control"
mc:Ignorable="d"
d:DesignHeight="550" d:DesignWidth="800">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/BPASmartClient.CustomResource;component/Themes/GenricStyle.xaml" />
<ResourceDictionary Source="/BPASmartClient.CustomResource;component/Themes/MyStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Margin="10">
<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Width="80" FontSize="12">API查询地址:</TextBlock>
<TextBox x:Name="apiurl" Margin="10,0,0,0" Width="200" Text="{Binding ApiURL}"/>
<Button Tag="SetUrl" Margin="10,0,0,0" Click="Button_Click" Style="{DynamicResource CommonBtn_返回}" Width="45">设置</Button>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Width="80" FontSize="12">客户端ID:</TextBlock>
<TextBox x:Name="chen" Margin="10,0,0,0" Width="200" >1</TextBox>
<Button Tag="Inquire" Margin="10,0,0,0" Click="Button_Click" Style="{DynamicResource CommonBtn_返回}" Width="45">查询</Button>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Width="80" HorizontalAlignment="Right" FontSize="12">客户端ID:</TextBlock>
<TextBox x:Name="clientId" Margin="10,0,0,0" Width="200" Text="{Binding device.ClientId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Width="80" FontSize="12">devicename:</TextBlock>
<TextBox x:Name="devicename" Margin="10,0,0,0" Width="200" Text="{Binding device.devicename, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Width="80" FontSize="12">productkey:</TextBlock>
<TextBox x:Name="productkey" Margin="10,0,0,0" Width="200" Text="{Binding device.productkey, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Width="80" FontSize="12">devicesecret:</TextBlock>
<TextBox x:Name="devicesecret" Margin="10,0,0,0" Width="200" Text="{Binding device.devicesecret, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Width="80" FontSize="12">设备类型:</TextBlock>
<TextBox x:Name="devtype" Margin="10,0,0,0" Width="200" Text="{Binding device.devtype, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Width="80" FontSize="12">经度:</TextBlock>
<TextBox x:Name="jd" Margin="10,0,0,0" Width="200" Text="{Binding device.jd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Width="80" FontSize="12">纬度:</TextBlock>
<TextBox x:Name="wd" Margin="10,0,0,0" Width="200" Text="{Binding device.wd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Width="80" FontSize="12">备注:</TextBlock>
<TextBox x:Name="remark" Margin="10,0,0,0" Width="200" Text="{Binding device.remark, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,10,0,0" >
<Button Margin="80,0,0,0" Tag="Add" Click="Button_Click" Style="{DynamicResource CommonBtn_返回}" Width="45">增加</Button>
<Button Margin="20,0,0,0" Tag="Update" Click="Button_Click" Style="{DynamicResource CommonBtn_返回}" Width="45">修改</Button>
<Button Margin="20,0,0,0" Tag="Delete" Click="Button_Click" Style="{DynamicResource CommonBtn_返回}" Width="45">删除</Button>

</StackPanel>

<StackPanel Orientation="Horizontal" Margin="0,20,0,0" >
<Button Margin="80,0,0,0" Tag="Save" Click="Button_Click" Style="{DynamicResource CommonBtn_返回}" Width="73">保存text</Button>
<Button Margin="20,0,0,0" Tag="Insert" Click="Button_Click" Style="{DynamicResource CommonBtn_返回}" Width="73" Cursor="Hand">导入text</Button>
</StackPanel>

</StackPanel>

<Grid Grid.Column="1" Margin="10">
<DataGrid HorizontalScrollBarVisibility="Visible" Margin="10" SelectedItem="{Binding deviceTableSelectedItem, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding deviceTable, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" MouseDoubleClick="DataGrid_MouseDoubleClick">
<DataGrid.Columns>
<DataGridTemplateColumn Header="客户端ID" Width="2*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding ClientId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="12" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="云名称" Width="5*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding devicename, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="12" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="云key" Width="6*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding productkey, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="12" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="云secret" Width="12*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding devicesecret, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="12" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="类型" Width="4*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding devtype, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="12" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="经度" Width="4.4*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding jd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="12" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="纬度" Width="4.4*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding wd, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="12" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="备注" Width="5.4*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock HorizontalAlignment="Center" Text="{Binding remark, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="12" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

</Grid>
</Grid>
</UserControl>

+ 122
- 0
BPASmartClient/Control/DataVView.xaml.cs View File

@@ -0,0 +1,122 @@
using BPASmartClient.CustomResource.UserControls;
using BPASmartClient.CustomResource.UserControls.MessageShow;
using BPASmartClient.IoT;
using BPASmartClient.ViewModel;
using DataVAPI.Tool.IOT;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BPASmartClient.Control
{
/// <summary>
/// DataVView.xaml 的交互逻辑
/// </summary>
public partial class DataVView : UserControl
{
public DataVView()
{
InitializeComponent();
this.DataContext = DataVViewModel.GetInstance();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender is Button)
{
bool istrue = false;
Button button = sender as Button;
if (button.Tag != null)
{
switch (button.Tag.ToString())
{
case "Add":
istrue = DataVViewModel.GetInstance().Add();
if (istrue) NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, MainViewModel.GetInstance().window, "提示", $"增加成功!");
else NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, MainViewModel.GetInstance().window, "提示", $"增加失败!");
break;
case "Update":
if (string.IsNullOrEmpty(DataVViewModel.GetInstance().device.Id))
{
NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "提示", $"请先“查询”或者“双击右侧表格行”,修改之后在点击“修改”!");
return;
}
istrue = DataVViewModel.GetInstance().Update();
if (istrue) NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, MainViewModel.GetInstance().window, "提示", $"修改成功!");
else NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, MainViewModel.GetInstance().window, "提示", $"修改失败!");
break;
case "Delete":
if (string.IsNullOrEmpty(DataVViewModel.GetInstance().device.Id))
{
NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "提示", $"请先“查询”或者“双击右侧表格行”,修改之后在点击“删除”!");
return;
}
istrue = DataVViewModel.GetInstance().Delete();
if (istrue) NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, MainViewModel.GetInstance().window, "提示", $"删除成功!");
else NoticeDemoViewModel.OpenMsg(EnumPromptType.Error, MainViewModel.GetInstance().window, "提示", $"删除失败!");
break;
case "SetUrl":
DataVViewModel.GetInstance().ApiURL = apiurl.Text;
DataVViewModel.GetInstance().Refresh();
NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, MainViewModel.GetInstance().window, "提示", $"设置成功!");
//MessageBox.Show("设置成功");
break;
case "Inquire":
DataVViewModel.GetInstance().device = DataVViewModel.GetInstance().Inquire(chen.Text)?.FirstOrDefault();
break;
case "Save":
System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
saveFileDialog.Filter = "txt文件(*.txt)|*.txt";
if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StreamWriter sw = File.CreateText(saveFileDialog.FileName);
sw.Write(Tools.JsonConvertTools(DataVViewModel.GetInstance().deviceTable)); //写入文件中
sw.Flush();//清理缓冲区
sw.Close();//关闭文件
}
NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, MainViewModel.GetInstance().window, "提示", $"导出设备成功,条数: {DataVViewModel.GetInstance().deviceTable.Count}!");
break;
case "Insert":
System.Windows.Forms.OpenFileDialog file = new System.Windows.Forms.OpenFileDialog();//定义新的文件打开位置控件
file.Filter = "txt文件(*.txt)|*.txt";//设置文件后缀的过滤
if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)//如果有选择打开文件
{
DeviceTable device = null;
FileStream fs = new FileStream(file.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8); //选择编码方式
string str = sr.ReadToEnd();
if (!string.IsNullOrEmpty(str))
{
Tools.JsonToObjectTools<List<DeviceTable>>(str)?.ForEach(par =>
{
DataVViewModel.GetInstance().Add(par);
});
NoticeDemoViewModel.OpenMsg(EnumPromptType.Success, MainViewModel.GetInstance().window, "提示", $"插入设备成功,条数: {Tools.JsonToObjectTools<List<DeviceTable>>(str)?.Count}!");
DataVViewModel.GetInstance().Refresh();
}
}

break;
}
}
}
}

private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataVViewModel.GetInstance().device = DataVViewModel.GetInstance().deviceTableSelectedItem;
}
}
}

+ 7
- 0
BPASmartClient/MainWindow.xaml View File

@@ -76,6 +76,12 @@
FontSize="12"
Header="店铺设备配置"
Tag="ShopDeviceConfigView" />
<Separator />
<MenuItem
Click="MenuItem_Click"
FontSize="12"
Header="阿里连接维护"
Tag="DataVView" />
</MenuItem>
<MenuItem Header="状态监视">
<MenuItem
@@ -107,6 +113,7 @@
FontSize="12"
Header="IOT监视"
Tag="LogView" />
<Separator />
</MenuItem>
<MenuItem Header="综合查询">
<MenuItem


Loading…
Cancel
Save