20211010_CZPM_upperpc/垂直剖面动态观测系统/ViewModel/MainViewModel.cs

142 lines
5.7 KiB
C#
Raw Normal View History

2023-07-27 03:01:29 +00:00
using MaterialDesignThemes.Wpf;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using .Common;
using .DataAccess;
namespace .ViewModel
{
public class MainViewModel : NotifyBase
{
private FrameworkElement _mainContent;
LocalDataAccess localDataAccess = new LocalDataAccess();
public FrameworkElement MainContent
{
get { return _mainContent; }
set { _mainContent = value; this.DoNotify(); }
}
public CommandBase NavChangedCommand { get; set; }
public static System.Timers.Timer timer { get; set; }
public MainViewModel()
{
//初始化
this.NavChangedCommand = new CommandBase();
this.NavChangedCommand.DoExcute = new Action<object>(DoNavChanged);
//按钮是否可用
this.NavChangedCommand.DoCanExcute = new Func<object, bool>((o) => true);
//默认打开第一个界面
DoNavChanged("FirstPageView");
//获取GPS信息
Thread t = new Thread(Get_GPS);
t.Start();
t.IsBackground = true;
timer = new System.Timers.Timer(1 * 60 * 1000);
timer.Elapsed += timer_Elapsed;
timer.Start();
}
private void DoNavChanged(object obj)
{
//ClearMemory();
Type type = Type.GetType("垂直剖面动态观测系统.View." + obj.ToString());
ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
this.MainContent = (FrameworkElement)cti.Invoke(null);
}
#region
/// <summary>
///设置线程工作的空间
/// </summary>
/// <param name="process">线程</param>
/// <param name="minSize">最小空间</param>
/// <param name="maxSize">最大空间</param>
/// <returns></returns>
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);
public void ClearMemory()
{
GC.Collect();
GC.SuppressFinalize(this);
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
#endregion
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("清理了一次内存!");
ClearMemory();
}
private void Get_GPS()
{
while (!tools.PingIp("120.55.40.127"))
{
Console.WriteLine("没有通");
Thread.Sleep(10000);
}
var factory = new ConnectionFactory()
{
HostName = "120.55.40.127",
Port = 31552,
UserName = "15162866407_mq",
Password = "Mq1qaz2wsx",
VirtualHost = "/128"
};
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
//channel.QueueDeclare(queue: "bd_data_queue", durable: true,
// exclusive: false,
// autoDelete: false,
// arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
string message = Encoding.UTF8.GetString(body.ToArray());
JObject jo = (JObject)JsonConvert.DeserializeObject(message);
string content = jo["content"].ToString();
System.DateTime Time = DateTime.Parse((int.Parse(content.Substring(10, 2)) + 2000).ToString() + "-" + int.Parse(content.Substring(12, 2)).ToString()
+ "-" + int.Parse(content.Substring(14, 2)).ToString() + " " + int.Parse(content.Substring(16, 2)).ToString() + ":" + int.Parse(content.Substring(18, 2)).ToString()
+ ":" + int.Parse(content.Substring(20, 2)).ToString());
float JD = (float)Convert.ToInt32(content.Substring(22, 2).ToString() + content.Substring(24, 2).ToString() + content.Substring(26, 2).ToString() + content.Substring(28, 2).ToString(), 16) / 10000000;
float WD = (float)Convert.ToInt32(content.Substring(30, 2).ToString() + content.Substring(32, 2).ToString() + content.Substring(34, 2).ToString() + content.Substring(36, 2).ToString(), 16) / 10000000;
localDataAccess.write("insert into gps(datetime,datetime_sensor,jd,wd) values('" + System.DateTime.Now + "','" + Time + "','" + JD + "','" + WD + "');");
};
channel.BasicConsume(queue: "bd_data_queue", autoAck: true, consumer: consumer);
Console.ReadLine();
}
}
}
}
}