using AutomaticApp.Base;
using AutomaticApp.Common;
using AutomaticApp.Controls;
using AutomaticApp.DataAccess;
using AutomaticApp.Model;
using AutomaticApp.Models;
using AutomaticApp.TestModels;
using AutomaticApp.Views;
using Communication;
using MySql.Data.MySqlClient;
using Org.BouncyCastle.Asn1.Mozilla;
using Org.BouncyCastle.Bcpg.OpenPgp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Ubiety.Dns.Core.Common;
using NotifyBase = AutomaticApp.Common.NotifyBase;
namespace AutomaticApp.ViewModels
{
///
/// 主页面数据
///
public class MainViewModel : NotifyBase
{
string sql = "";
#region 初始化DataContext
public static BuoystationModel buoystation1Model = new BuoystationModel();
public static BuoystationModel buoystation2Model = new BuoystationModel();
public static ConboxStationModel conboxStation1Model = new ConboxStationModel();
public static ConboxStationModel conboxStation2Model = new ConboxStationModel();
public static ControlPageViewModel CPV1 = new ControlPageViewModel();
public static ControlPageViewModel CPV2 = new ControlPageViewModel();
static public Switch_State SS1 = new Switch_State();
static public Switch_State SS2 = new Switch_State();
static public State_FeedBack SFB1 = new State_FeedBack();
static public State_FeedBack SFB2 = new State_FeedBack();
static public ParaSet PS1 = new ParaSet();
static public ParaSet PS2 = new ParaSet();
static public EarthQuakeParaSet EQPS1 = new EarthQuakeParaSet();
static public EarthQuakeParaSet EQPS2 = new EarthQuakeParaSet();
static public EarthquakeSensorModel EQSM1 = new EarthquakeSensorModel();
static public EarthquakeSensorModel EQSM2 = new EarthquakeSensorModel();
static public FTPModel ftp1 { get; set; } = new FTPModel() { IP = tools.GetAppSetting("PLC_IP1"), Port = Convert.ToInt32(tools.GetAppSetting("PLC_FTP_Port1")) };
static public FTPModel ftp2 { get; set; } = new FTPModel() { IP = tools.GetAppSetting("PLC_IP2"), Port = Convert.ToInt32(tools.GetAppSetting("PLC_FTP_Port2")) };
#endregion
private Switch_State ss;
public Switch_State SS
{
get { return ss; }
set { ss = value; this.DoNotify(); }
}
private ParaSet ps;
public ParaSet PS
{
get { return ps; }
set { ps = value; this.DoNotify(); }
}
private EarthQuakeParaSet eqps;
public EarthQuakeParaSet EQPS
{
get { return eqps; }
set { eqps = value; }
}
private EarthquakeSensorModel eqsm;
public EarthquakeSensorModel EQSM
{
get { return eqsm; }
set { eqsm = value; }
}
///
/// 刷新下控制域的DataContext
///
public void FlashControlPageDataContext()
{
if (station_id == 1)
{
SS = SS1;
PS = PS1;
EQPS = EQPS1;
EQSM = EQSM1;
}
else if (station_id == 2)
{
SS = SS2;
PS = PS2;
EQPS = EQPS2;
EQSM = EQSM2;
}
}
private FrameworkElement _pageContent;
public FrameworkElement PageContent
{
get { return _pageContent; }
set { _pageContent = value; this.DoNotify(); }
}
public int station_id { get; set; } = 1;
//int station_id = 1; //设置当前显示的站点号 1为1号站点 2为2号站点
public CommandBase NavChangedCommand { get; set; }
string Title_Name = ""; //设置当前显示的标题 1 浮标 2 接驳盒 3 系统控制 4 日志记录
public CommandBase ComDoConnectCommand { get; set; } //串口连接/断开操作
public CommandBase Socket1DoConnectCommand { get; set; } //客户端连接/断开操作
public CommandBase Socket2DoConnectCommand { get; set; } //客户端连接/断开操作
public CommandBase CommunicationChangedCommand { get; set; }//切换控制器的通信路径
#region 开关
public CommandBase Switch { set; get; }
public CommandBase ParaSetSwitch { set; get; } //系统参数设置按钮
#endregion
public ComModel comModel { get; set; } = new ComModel();
public ClientModel clientModel1 { get; set; } = new ClientModel();
public ClientModel clientModel2 { get; set; } = new ClientModel();
private NR_MsgModel nR_Msg = new NR_MsgModel();
private BD_MsgModel BD_Msg = new BD_MsgModel();
private string bDCountDown = "";
public string BDCountDown
{
get { return bDCountDown; }
set { bDCountDown = value; this.DoNotify(); }
}
private bool gridIsEnabled = true;
public bool GridIsEnabled
{
get { return gridIsEnabled; }
set { gridIsEnabled = value; this.DoNotify(); }
}
///
/// 是否处于发送状态
///
static bool isSending = false;
private Visibility vis = Visibility.Hidden;
///
/// 按钮是否可见
///
public Visibility visibility
{
get { return vis; }
set { vis = value; this.DoNotify(); }
}
///
/// 待发送的数据队列
///
public List sendMessageLists { get; set; } = new List();
public void AddsendMessageLists(string MessageContent, byte[] Message, int CommunicateChoose)
{
if (CommunicateChoose == 1)
{
if (sendMessageLists.Count < 5)
{
sendMessageLists.Add(new SendMessageList()
{
ID = sendMessageLists.Count + 1,
StationID = station_id,
MessageContent = MessageContent,
Message = Message,
SendState = "未发送"
});
}
else
{
EM.ControlMessage = "当前北斗发送数据队列已满,请稍后再试!!!";
return;
}
}
else if (CommunicateChoose == 2)
{
if (station_id == 1)
{
clientModel1.SendData(Message);
}
else if (station_id == 2)
{
clientModel2.SendData(Message);
}
}
}
public async Task BDCountDownCommand()
{
isSending = true;
bool isSend = true;
GridIsEnabled = false;
visibility = Visibility.Visible;//显示详情按钮
while (sendMessageLists.Count != 0)
{
isSend = comModel.SendMessage(sendMessageLists[0].Message);
sendMessageLists[0].SendState = "已发送";
int CountDown = 0;
if (isSend)
{
while (CountDown < 65)
{
for (int i = 0; i < sendMessageLists.Count; i++)
{
sendMessageLists[i].RemindTime = ((65 * i - CountDown) < 0) ? 0 : (65 * i - CountDown);
}
this.BDCountDown = string.Format("北斗短报文倒计时{0}秒,剩余{1}条数据未发送", (65 * sendMessageLists.Count - CountDown).ToString(), (sendMessageLists.Count - 1).ToString());
await Task.Delay(1000);
CountDown++;
}
}
//移除报文中的第一条
sendMessageLists.RemoveAt(0);
CountDown = 0;
}
//for (int i = 0; i < sendMessageLists.Count; i++)
//{
// isSend = comModel.SendMessage(sendMessageLists[0].Message);
// int CountDown = 0;
// if (isSend)
// {
// while (CountDown < 65)
// {
// this.BDCountDown = string.Format("北斗短报文倒计时{0}秒,剩余{1}条数据未发送", (65 * (sendMessageLists.Count - i) - CountDown).ToString(), (sendMessageLists.Count - i - 1).ToString());
// await Task.Delay(1000);
// CountDown++;
// }
// }
// //移除报文中的第一条
// sendMessageLists.RemoveAt(0);
// CountDown = 0;
//}
visibility = Visibility.Hidden;//隐藏详情按钮
GridIsEnabled = true;
sendMessageLists.Clear();
this.BDCountDown = "";
isSending = false;
return isSend;
}
public static ErrorMessageModel EM { get; set; } = new ErrorMessageModel();
public static SerialInfo SerialInfo { get; set; } = new SerialInfo() //初始化串口连接信息
{
PortName = tools.GetAppSetting("PortName"),
BaudRate = Convert.ToInt32(tools.GetAppSetting("BaudRate")),
DataBit = 8,
Parity = Parity.None,
StopBits = StopBits.One
};
public static SocketInfo SocketInfo1 { get; set; } = new SocketInfo() //PLC1连接信息
{
IP = tools.GetAppSetting("PLC_IP1"),
Port = Convert.ToInt32(tools.GetAppSetting("PLC_Service_Port1"))
};
public static SocketInfo SocketInfo2 { get; set; } = new SocketInfo() //PLC2连接信息
{
IP = tools.GetAppSetting("PLC_IP2"),
Port = Convert.ToInt32(tools.GetAppSetting("PLC_Service_Port2"))
};
public MainViewModel()
{
this.NavChangedCommand = new CommandBase();
this.NavChangedCommand.DoExcute = new Action