using Newtonsoft.Json;
|
using SqlSugar;
|
using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Drawing;
|
using System.Drawing.Drawing2D;
|
using System.Drawing.Imaging;
|
using System.IO;
|
using System.Linq;
|
using System.Runtime.InteropServices;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
using uPLibrary.Networking.M2Mqtt;
|
using uPLibrary.Networking.M2Mqtt.Messages;
|
using WindowsFormsApp1.Common;
|
using WindowsFormsApp1.Controllers;
|
using WindowsFormsApp1.Entity;
|
using WindowsFormsApp1.Helper;
|
using WindowsFormsApp1.Item;
|
|
namespace WindowsFormsApp1
|
{
|
public partial class Form1 : Form
|
{
|
private Metafile metafile1;
|
private Graphics.EnumerateMetafileProc metafileDelegate;
|
private MqttClient client = null;
|
|
public Form1()
|
{
|
InitializeComponent();
|
|
}
|
|
|
string _UserNo { get { return MqttconfigHelp.UserName.Trim(); } }
|
string _PW { get { return MqttconfigHelp.Password.Trim(); } }
|
private bool MetafileCallback(EmfPlusRecordType recordType, int flags, int dataSize, IntPtr data, PlayRecordCallback callbackData)
|
{
|
byte[] dataArray = null;
|
if (data != IntPtr.Zero)
|
{
|
|
dataArray = new byte[dataSize];
|
Marshal.Copy(data, dataArray, 0, dataSize);
|
//GdipComment
|
metafile1.PlayRecord(recordType, flags, dataSize, dataArray);
|
switch (recordType) //记录类型
|
{
|
|
case EmfPlusRecordType.Object: //对象
|
case EmfPlusRecordType.DrawLines: //线
|
case EmfPlusRecordType.SetPageTransform: //设置页变换
|
break;
|
}
|
}
|
Console.WriteLine(recordType.ToString());
|
return true;
|
}
|
|
protected override void OnPaint(PaintEventArgs e)
|
{
|
//PointF destPoint = new PointF(e.ClipRectangle.X, e.ClipRectangle.Y);
|
//e.Graphics.EnumerateMetafile(metafile1, destPoint, metafileDelegate);
|
|
}
|
protected override void OnClosed(EventArgs e)
|
{
|
base.OnClosed(e);
|
//metafile1.Dispose();
|
}
|
string emf { get; set; }
|
/// <summary>
|
/// 导出为 Emf 或 Wmf 文件
|
/// </summary>
|
/// <param name="filePath">文件路径</param>
|
/// <returns>是否成功</returns>
|
private bool Export(string filePath)
|
{
|
|
filePath = @"D:\CompanyCode\DIXWeb\src\WindowsFormsApp1\imgs\imgup.jpg";
|
//filePath = @"C:\Users\Administrator\Desktop\Debug\1.jpg";
|
FileInfo fi = new FileInfo(filePath);
|
emf = Path.GetDirectoryName(filePath) + "\\1.emf";
|
fi.CopyTo(emf,true);
|
try
|
{
|
Bitmap bmp = new Bitmap(2220, 2220);
|
Graphics gs = Graphics.FromImage(bmp);
|
Metafile mf = new Metafile(emf, gs.GetHdc());
|
|
Graphics g = Graphics.FromImage(mf);
|
//string sf = System.IO.Path.GetDirectoryName(filePath) + "1.wmf";
|
//mf.Save(sf, ImageFormat.Wmf);
|
Draw(g);
|
g.Save();
|
|
g.Dispose();
|
mf.Dispose();
|
|
return true;
|
}
|
catch(Exception er)
|
{
|
MessageBox.Show(er.Message);
|
return false;
|
}
|
}
|
/// <summary>
|
/// 绘制图形
|
/// </summary>
|
/// <param name="g">用于绘图的Graphics对象</param>
|
private void Draw(Graphics g)
|
{
|
HatchBrush hb = new HatchBrush(HatchStyle.LightUpwardDiagonal, Color.Black, Color.White);
|
|
g.FillEllipse(Brushes.Gray, 10f, 10f, 200, 200);
|
g.DrawEllipse(new Pen(Color.Black, 1f), 10f, 10f, 200, 200);
|
|
g.FillEllipse(hb, 30f, 95f, 30, 30);
|
g.DrawEllipse(new Pen(Color.Black, 1f), 30f, 95f, 30, 30);
|
|
g.FillEllipse(hb, 160f, 95f, 30, 30);
|
g.DrawEllipse(new Pen(Color.Black, 1f), 160f, 95f, 30, 30);
|
|
g.FillEllipse(hb, 95f, 30f, 30, 30);
|
g.DrawEllipse(new Pen(Color.Black, 1f), 95f, 30f, 30, 30);
|
|
g.FillEllipse(hb, 95f, 160f, 30, 30);
|
g.DrawEllipse(new Pen(Color.Black, 1f), 95f, 160f, 30, 30);
|
|
g.FillEllipse(Brushes.Blue, 60f, 60f, 100, 100);
|
g.DrawEllipse(new Pen(Color.Black, 1f), 60f, 60f, 100, 100);
|
|
g.FillEllipse(Brushes.BlanchedAlmond, 95f, 95f, 30, 30);
|
g.DrawEllipse(new Pen(Color.Black, 1f), 95f, 95f, 30, 30);
|
|
g.DrawRectangle(new Pen(System.Drawing.Brushes.Blue, 0.1f), 6, 6, 208, 208);
|
|
g.DrawLine(new Pen(Color.Black, 0.1f), 110f, 110f, 220f, 25f);
|
g.DrawString("剖面图", new Font("宋体", 9f), Brushes.Green, 220f, 20f);
|
}
|
private void button1_Click(object sender, EventArgs e)
|
{
|
|
string MotifName=txtFilePath.Text.ToString();
|
var db = SqlSugarConnection.SugarClient();
|
List<Toprie_Motif> ToprieMotifList = db.SqlQueryable<Toprie_Motif>("select * from Toprie_Motif").Where(m => m.IsVal == 0).ToList();
|
|
if (ToprieMotifList.Where(m => m.MotifName == MotifName).ToList().Count() == 0)
|
{
|
if (!string.IsNullOrEmpty(MotifName))
|
{
|
Toprie_Motif ToprieMotif = new Toprie_Motif();
|
ToprieMotif.Code = SnowFlakeSingle.Instance.NextId().ToString();
|
ToprieMotif.MotifName = MotifName;
|
ToprieMotif.Address = "";
|
ToprieMotif.Remark = "";
|
ToprieMotif.IsVal = 0;
|
ToprieMotif.CreateCode = "5105986045";
|
ToprieMotif.CreateTime = DateTime.Now;
|
InsertToprieMotif(ToprieMotif);
|
MessageBox.Show("新增成功", "提示");
|
|
}else {
|
MessageBox.Show("请输入订阅主题", "提示");
|
}
|
}
|
else{
|
MessageBox.Show("当前主题已添加,无法重复添加", "提示");
|
}
|
}
|
|
private void button2_Click(object sender, EventArgs e)
|
{
|
|
}
|
|
|
/// <summary>
|
/// 发布消息成功后调用的事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void client_MqttMsgPublished(object sender, MqttMsgPublishedEventArgs e)
|
{
|
MessageBox.Show("发布消息成功。", "信息");
|
}
|
|
/// <summary>
|
/// 订阅主题成功后调用的事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e)
|
{
|
|
|
}
|
|
/// <summary>
|
/// 取消订阅主题成功后调用的事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void client_MqttMsgUnsubscribed(object sender, MqttMsgUnsubscribedEventArgs e)
|
{
|
MessageBox.Show("取消订阅主题成功。", "信息");
|
}
|
|
/// <summary>
|
/// 当接收到订阅主题的消息时调用的事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
|
{
|
SnowFlakeSingle.WorkId =1;
|
|
|
try {
|
this.Invoke(new Action(() =>
|
{
|
txt_SubscribeMessage.Text += DateTime.Now.ToString("yyyy/MM/dd-HH:mm:ss") + " Received topic: " + e.Topic + ", message: " + Encoding.UTF8.GetString(e.Message) + "\n";
|
string Topic = e.Topic;
|
string Message = Encoding.UTF8.GetString(e.Message);
|
bool DupFlag = e.DupFlag;
|
byte QosLevel = e.QosLevel;
|
bool Retain = e.Retain;
|
|
lbMsg.Items.Insert(0, Encoding.UTF8.GetString(e.Message));
|
lbMsg.Items.Insert(0, "订阅主题:"+ Topic+ "********************************************* --" + DateTime.Now.ToString("yyyy/MM/dd-HH:mm:ss"));
|
|
if (Message!= "getParam"){
|
|
ToreieItem ToreieItem = JsonConvert.DeserializeObject<ToreieItem>(Encoding.UTF8.GetString(e.Message));
|
|
List<Toprie> ToprieList = new List<Toprie>();
|
|
if (ToreieItem.sensorDatas != null && ToreieItem.sensorDatas.Count() > 0)
|
{
|
|
foreach (var item in ToreieItem.sensorDatas)
|
{
|
|
Toprie obj = new Toprie();
|
obj.Code = SnowFlakeSingle.Instance.NextId().ToString();
|
obj.motif = Topic;
|
obj.times = ToreieItem.times;
|
obj.flag = item.flag;
|
obj.CreateCode = "5105986045";
|
obj.CreateTime = DateTime.Now;
|
|
switch (item.flag)
|
{
|
case FlagHelp.flag1:
|
case FlagHelp.flag2:
|
case FlagHelp.flag3:
|
case FlagHelp.flag4:
|
case FlagHelp.flag5:
|
case FlagHelp.flag7:
|
obj.val = item.value;
|
break;
|
case FlagHelp.flag6:
|
obj.switcher = item.switcher;
|
break;
|
|
case FlagHelp.flag8:
|
obj.lat = item.lat;
|
obj.lng = item.lng;
|
break;
|
case FlagHelp.flagICCID:
|
obj.str = item.str;
|
break;
|
default:
|
continue;
|
}
|
ToprieList.Add(obj);
|
|
}
|
}
|
|
InsertListToprieList(ToprieList);
|
|
}
|
|
}));
|
|
}
|
catch (Exception ex) {
|
|
string str = ex.ToString();
|
|
}
|
}
|
|
/// <summary>
|
/// 当连接断开时调用的事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void client_ConnectionClosed(object sender, EventArgs e)
|
{
|
MessageBox.Show("连接已断开。", "信息");
|
}
|
|
/// <summary>
|
/// 连接
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btn_Connect_Click(object sender, EventArgs e)
|
{
|
try
|
{
|
string brokerAddress = MqttconfigHelp.BrokerAddress;
|
int brokerPort = MqttconfigHelp.BrokerPort;
|
client = new MqttClient(brokerAddress, brokerPort, false, null, null, MqttSslProtocols.None, null); //实例化mqtt客户端
|
|
string clientId = Guid.NewGuid().ToString("N"); //生成32位全球唯一标识符
|
client.Connect(clientId,"",""); //建立客户端连接
|
|
client.MqttMsgPublished += client_MqttMsgPublished; //发布消息成功后调用
|
client.MqttMsgSubscribed += client_MqttMsgSubscribed; //订阅主题成功后调用
|
client.MqttMsgUnsubscribed += client_MqttMsgUnsubscribed; //取消订阅主题成功后调用
|
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; //当接收到订阅主题的消息时调用
|
client.ConnectionClosed += client_ConnectionClosed; //连接断开后调用
|
MessageBox.Show("连接成功。", "信息");//
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show("连接失败!\n\r" + ex.Message.ToString(), "警告");
|
}
|
}
|
|
/// <summary>
|
/// 断开连接
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btn_DisConnect_Click(object sender, EventArgs e)
|
{
|
client.Disconnect();
|
}
|
|
/// <summary>
|
/// 发布消息
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btn_Publish_Click(object sender, EventArgs e)
|
{
|
//string publishTopic = txt_PublishTopic.Text.ToString(); //发布的主题
|
//string publishMessage = txt_PublishMessage.Text.ToString(); //发布的消息
|
//client.Publish(publishTopic, Encoding.UTF8.GetBytes(publishMessage)); //发布消息
|
}
|
|
/// <summary>
|
/// 订阅消息
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btn_Subscribe_Click(object sender, EventArgs e)
|
{
|
|
try {
|
|
client = new MqttClient(MqttconfigHelp.BrokerAddress,MqttconfigHelp.BrokerPort, false, null, null, MqttSslProtocols.None, null); //实例化mqtt客户端
|
|
string clientId = Guid.NewGuid().ToString("N"); //生成32位全球唯一标识符
|
client.Connect(clientId, MqttconfigHelp.UserName, MqttconfigHelp.Password); //建立客户端连接
|
|
client.MqttMsgPublished += client_MqttMsgPublished; //发布消息成功后调用
|
client.MqttMsgSubscribed += client_MqttMsgSubscribed; //订阅主题成功后调用
|
client.MqttMsgUnsubscribed += client_MqttMsgUnsubscribed; //取消订阅主题成功后调用
|
client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; //当接收到订阅主题的消息时调用
|
client.ConnectionClosed += client_ConnectionClosed;//连接断开后调用
|
|
string[] subscribeTopic = Toprie_MotifList().Split(','); //可以同时订阅多个主题的消息,各主题之间用逗号分隔
|
byte[] qosLevels = new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE };
|
|
foreach (var item in subscribeTopic){
|
string[] MotifNameList ={ item };
|
client.Subscribe(MotifNameList, qosLevels);
|
}
|
//this.Opacity = 0;
|
MessageBox.Show("订阅主题成功。", "信息");
|
}
|
catch (Exception ex){
|
MessageBox.Show(ex.Message.ToString(),"异常");
|
}
|
|
}
|
|
/// <summary>
|
/// 取消订阅消息
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void btn_UnSubscribe_Click(object sender, EventArgs e)
|
{
|
string[] unSubscribeTopic =null;
|
client.Unsubscribe(unSubscribeTopic);
|
}
|
|
/// <summary>
|
/// 窗口关闭事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void Window_Closed(object sender, EventArgs e)
|
{
|
Environment.Exit(0); //强制退出,即使有其他的线程没有结束,因为M2Mqtt调用事件都是在单独的线程中进行的
|
}
|
|
private void txtFilePath_TextChanged(object sender, EventArgs e)
|
{
|
|
|
}
|
|
private void Form1_Load(object sender, EventArgs e)
|
{
|
//this.Opacity = 0;
|
////窗体最小化显示
|
//this.WindowState = FormWindowState.Minimized;
|
////不显示在任务栏中
|
//this.ShowInTaskbar = false;
|
|
}
|
|
|
/// <summary>
|
///
|
/// </summary>
|
public void InsertToprie(Toprie Toprie)
|
{
|
try
|
{
|
var db = SqlSugarConnection.SugarClient();
|
int RowIndex = db.Insertable(Toprie).ExecuteReturnIdentity();
|
}
|
catch (Exception ex)
|
{
|
|
}
|
}
|
|
|
/// <summary>
|
/// 批量添加数据
|
/// </summary>
|
public void InsertListToprieList(List<Toprie> ToprieList)
|
{
|
|
try
|
{
|
|
var db = SqlSugarConnection.SugarClient();
|
int RowIndex = db.Insertable(ToprieList).ExecuteReturnIdentity();
|
}
|
catch (Exception ex)
|
{
|
|
}
|
}
|
|
/// <summary>
|
/// 获取所有订阅主题
|
/// </summary>
|
public string Toprie_MotifList(){
|
|
try {
|
|
#region 初始化
|
|
string str = "";
|
|
#endregion
|
|
#region 获取订阅主题
|
var db = SqlSugarConnection.SugarClient();
|
List<Toprie_Motif> ToprieMotifList = db.SqlQueryable<Toprie_Motif>("select * from Toprie_Motif").Where(m => m.IsVal == 0).ToList();
|
|
#endregion
|
|
#region
|
|
for (int i=0; ToprieMotifList !=null && i < ToprieMotifList.Count();i++){
|
|
if (i == ToprieMotifList.Count() - 1)
|
{
|
str += ToprieMotifList[i].MotifName;
|
}
|
else {
|
str += ToprieMotifList[i].MotifName + ",";
|
}
|
}
|
|
|
#endregion
|
|
return str;
|
|
|
}
|
catch (Exception ex){
|
|
return "";
|
|
}
|
}
|
|
|
|
/// <summary>
|
/// 添加订阅主题
|
/// </summary>
|
public void InsertToprieMotif(Toprie_Motif ToprieMotif)
|
{
|
try
|
{
|
var db = SqlSugarConnection.SugarClient();
|
int RowIndex = db.Insertable(ToprieMotif).ExecuteReturnIdentity();
|
}
|
catch (Exception ex)
|
{
|
|
}
|
}
|
|
private void txt_SubscribeMessage_TextChanged(object sender, EventArgs e)
|
{
|
|
}
|
|
private void txt_BrokerAddress_TextChanged(object sender, EventArgs e)
|
{
|
|
}
|
}
|
}
|