2025-12-09 09:28:39 +08:00

90 lines
2.4 KiB
C#

using MQTT_WinformV1.Service;
using MqttClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace MQTT_WinformV1
{
public partial class FormDataZF : Form
{
private System.Timers.Timer _timer;
private DataUploadService _upLoadService;
public DataTable sendDb = new DataTable();
public FormDataZF()
{
InitializeComponent();
_upLoadService = new DataUploadService();
}
private void FormDataZF_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
button2_Click(null,null);
button1_Click(null,null);
}
private void _timer_Tick(object sender, ElapsedEventArgs e)
{
DataUpload();
}
//数据上传
private async Task DataUpload()
{
int count = await _upLoadService.QueryDataAsync();
AppendLog(count.ToString() + "条数据已上传!");
}
private void AppendLog(string message)
{
try
{
if (InvokeRequired)
{
BeginInvoke(new Action<string>(AppendLog), message);
return;
}
string timeStamped = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}\r\n";
textBoxLog.AppendText(timeStamped);
}
catch (Exception ex)
{
}
}
private void button1_Click(object sender, EventArgs e)
{
int min;
if (!int.TryParse(txtLUploadPL.Text, out min) || min <= 0)
{
min = 30;
}
// 设置定时器(转换为毫秒)
_timer = new System.Timers.Timer(min * 60 * 1000);
_timer.Elapsed += _timer_Tick;
_timer.AutoReset = true; // 是否重复
_timer.Enabled = true; // 开始计时
// 方法:立即执行一次
_timer_Tick(null, null);
}
private async void button2_Click(object sender, EventArgs e)
{
sendDb = await _upLoadService.getSendDB();
bindingSource1.DataSource = sendDb;
}
}
}