You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

398 lines
13 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using FileDataUpload.Common;
using FileDataUpload.Entity;
using FileDataUpload.FileOperate;
using FileDataUpload.Mqtt;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FileDataUpload.viewModel
{
public class MainWindowViewModel: ViewModelBase
{
private readonly FileHelper fileHelper = FileHelper.Instance;
private readonly JsonChange jsonChange = JsonChange.Instance;
private readonly MqttClient client = MqttClient.Instance;
private readonly LogHelper logHelper = LogHelper.Instance;
private AppConfig appConfig = AppConfig.Instance;
private ObservableCollection<dynamic> listItems = new ObservableCollection<dynamic>();
[Obsolete]
public MainWindowViewModel()
{
try
{
client.PrintMessageReceivedEvent += PrintMessageToListBox;
ConnectCommand = new RelayCommand(Connect);
DisConnectCommand = new RelayCommand(DisConnect);
SubscriptionCommand = new RelayCommand(Subscription);
UnsubscribeCommand = new RelayCommand(Unsubscribe);
UploadFileDataCommand = new RelayCommand(UploadFileData);
ChooseFileCommand = new RelayCommand(ChooseFile);
PushMessageCommand = new RelayCommand(PushMessage);
borkerIp = "scada.midea.com";
borkerPort = "1883";
PrintMessageToListBox("文件数据上传程序初始化成功");
}catch(Exception ex)
{
PrintMessageToListBox($"文件数据上传程序初始化失败:{ex.Message}");
}
}
#region Command
/// <summary>
/// 连接
/// </summary>
public RelayCommand ConnectCommand { get; set; }
/// <summary>
/// 断开
/// </summary>
public RelayCommand DisConnectCommand { get; set; }
/// <summary>
/// 订阅
/// </summary>
public RelayCommand SubscriptionCommand { get; set; }
/// <summary>
/// 取消订阅
/// </summary>
public RelayCommand UnsubscribeCommand { get; set; }
/// <summary>
/// 选择文件
/// </summary>
public RelayCommand ChooseFileCommand { get; set; }
/// <summary>
/// 上传文件数据
/// </summary>
public RelayCommand UploadFileDataCommand { get; set; }
/// <summary>
/// 推送消息
/// </summary>
public RelayCommand PushMessageCommand { get; set; }
#endregion
private IEnumerable listBoxData;
/// <summary>
/// LisBox数据模板
/// </summary>
public IEnumerable ListBoxData
{
get { return listBoxData; }
set { listBoxData = value; RaisePropertyChanged(() => ListBoxData); }
}
public String borkerIp = String.Empty;
public String borkerPort = String.Empty;
public String clientId = String.Empty;
public String userName = String.Empty;
public String password = String.Empty;
public String topical = String.Empty;
public String messageStr = String.Empty;
public String BorkerIp
{
get { return borkerIp; }
set { borkerIp = value; RaisePropertyChanged(()=> BorkerIp); }
}
public String BorkerPort
{
get { return borkerPort; }
set { borkerPort = value; RaisePropertyChanged(()=> BorkerPort); }
}
public String ClientId
{
get { return clientId; }
set { clientId = value; RaisePropertyChanged(()=> ClientId); }
}
public String UserName
{
get { return userName; }
set { userName = value; RaisePropertyChanged(()=> UserName); }
}
public String Password
{
get { return password; }
set { password = value; RaisePropertyChanged(()=> Password); }
}
public String Topical
{
get { return topical; }
set { topical = value; RaisePropertyChanged(()=> Topical); }
}
public String MessageStr
{
get { return messageStr; }
set { messageStr = value; RaisePropertyChanged(() => MessageStr); }
}
/// <summary>
/// 建立连接
/// </summary>
public void Connect()
{
if(string.IsNullOrEmpty(BorkerIp) || string.IsNullOrEmpty(BorkerPort))
{
PrintMessageToListBox("连接失败Borker IP、Port不允许为空");
return;
}
if (string.IsNullOrEmpty(ClientId) || string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
{
PrintMessageToListBox("连接失败ClientId、UserName、Password不允许为空");
return;
}
client.Connect(BorkerIp, Convert.ToInt32(BorkerPort), ClientId,UserName,Password);
}
/// <summary>
/// 断开连接
/// </summary>
public void DisConnect()
{
client.DisConnect();
}
/// <summary>
/// 订阅主题
/// </summary>
public void Subscription()
{
if (string.IsNullOrEmpty(topical))
{
PrintMessageToListBox("订阅主题不允许为空");
return;
}
client.SubscriptionAsync(topical);
}
/// <summary>
/// 取消订阅
/// </summary>
public void Unsubscribe()
{
if (string.IsNullOrEmpty(topical))
{
PrintMessageToListBox("取消订阅主题不允许为空");
return;
}
client.Unsubscribe(topical);
}
/// <summary>
/// 选择文件
/// </summary>
[System.Obsolete]
public void ChooseFile()
{
string fileName = "";
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true; //该值确定是否可以选择多个文件
openFileDialog.Title = "请选择文件"; //弹窗的标题
openFileDialog.InitialDirectory = "D:\\"; //默认打开的文件夹的位置
openFileDialog.Filter = "MicroSoft Excel文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*"; //筛选文件
openFileDialog.ShowHelp = true; //是否显示“帮助”按钮
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = openFileDialog.FileName;
var info = fileHelper.ReadExcelFile_New(fileName);
//var info = fileHelper.ReadExcelFile(fileName);
if (info != null)
{
MessageStr = jsonChange.ModeToJson(info);
PrintMessageToListBox($"文件读取成功:{MessageStr}");
}
else
{
PrintMessageToListBox($"文件读取失败,请仔细检查文件格式");
}
}
}
[Obsolete]
public void UploadFileData()
{
string fileName = "";
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true; //该值确定是否可以选择多个文件
openFileDialog.Title = "请选择文件"; //弹窗的标题
openFileDialog.InitialDirectory = "D:\\"; //默认打开的文件夹的位置
openFileDialog.Filter = "MicroSoft Excel文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*"; //筛选文件
openFileDialog.ShowHelp = true; //是否显示“帮助”按钮
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fileName = openFileDialog.FileName;
UploadFileData(fileName);
}
}
[Obsolete]
private async void UploadFileData(string path)
{
try
{
int pushFlag = 0;
Dictionary<string, List<DeviceParam>> info = fileHelper.ReadExcelFile_New(path);
if (info != null)
{
borkerIp = "scada.midea.com";
borkerPort = "1883";
foreach (var item in info)
{
switch (item.Key)
{
case "铆压环":
clientId = "EV62Q0000501YASM346";
userName = "EV62Q0000501YASM346|1";
password = "30dc225be0cb418b974b68eb10339b31";
pushFlag = appConfig.pushFlag_RivetingRing + 1;
break;
case "限位器":
clientId = "EV33210110YTJDMC001";
userName = "EV33210110YTJDMC001|1";
password = "cebf88bcc61147c1a42a4749fadf2ed1";
pushFlag = appConfig.pushFlag_Stopper + 1;
break;
case "排气阀片":
clientId = "EV33200210YTJDMC008";
userName = "EV33200210YTJDMC008|1";
password = "396bf2a332f64810a12a0385cae8c5a9";
pushFlag = appConfig.pushFlag_displacingValve + 1;
break;
}
this.Connect();
await Task.Delay(1000 * 5);
var @params = item.Value.OrderBy(x => Convert.ToInt32(x.data.propertyCode.Substring(3, x.data.propertyCode.Length - 3))).ToList();
foreach (var param in @params)
{
param.data.deviceCode = ClientId;
PrintMessageToListBox($"{item.Key}---上传{param.data.propertyCode}数据:{jsonChange.ModeToJson(param)}");
topical = $"/sys/932e4a47e5414e058fdbd067a4942247/device/{clientId}/thing/property/{param.data.propertyCode}/post";
if (param.data.propertyCode == "tag10")
{
param.data.propertyValue = pushFlag;
}
messageStr = jsonChange.ModeToJson(param);
this.PushMessage();
await Task.Delay(1000 * 1);
}
UpdatePushFlag(item.Key);
this.DisConnect();
}
}
else
{
PrintMessageToListBox("文件读取失败,请仔细检查文件格式");
}
}catch(Exception ex)
{
PrintMessageToListBox($"文件解析上传异常:{ex.Message}");
}
}
public void PushMessage()
{
try
{
if (string.IsNullOrEmpty(Topical) || string.IsNullOrEmpty(MessageStr))
{
PrintMessageToListBox("推送消息主题、内容不允许为空");
return;
}
client.Publish(Topical, MessageStr);
}catch(Exception ex)
{
PrintMessageToListBox($"消息推送异常:{ex.Message}");
}
}
/// <summary>
/// listBox绑定日志
/// </summary>
/// <param name="message"></param>
private void PrintMessageToListBox(string message)
{
logHelper.Info(message);
listItems.Add($"{DateTime.Now.ToString("HH:mm:ss")}==>{message}");
ListBoxData = listItems.OrderByDescending(x=>x);
}
/// <summary>
/// 更新推送标志位
/// </summary>
/// <param name="sheetName"></param>
private void UpdatePushFlag(string sheetName)
{
switch (sheetName)
{
case "铆压环":
appConfig.pushFlag_RivetingRing = appConfig.pushFlag_RivetingRing + 1;
break;
case "限位器":
appConfig.pushFlag_Stopper = appConfig.pushFlag_Stopper + 1;
break;
case "排气阀片":
appConfig.pushFlag_displacingValve = appConfig.pushFlag_displacingValve + 1;
break;
}
}
}
}