小数典应用:农场环境数据采集监控

场端采集传感器数据上传:

using SmallDataDict;

using System;

using System.CodeDom;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace MothineManger

{

public partial class frmMM : Form

{

Dictionary<string,float> Datas = new Dictionary<string,float>();

public frmMM()

{

InitializeComponent();

}

private void frmMM_Load(object sender, EventArgs e)

{

if(SmallDataDict.SmallData.Init(new AppInfo("JingSanNongCang","V30","jsznc20260426ok")))

//本场注册的编号与SEC密码

{

AddLog("初始化小数典成功!");

ss = new SmallData("JingSanNongCang");

}

else

{

AddLog("初始化小数典失败!");

}

Init();

laOwer.Text = ss.Owner;

}

private void Init()

{

Datas = new Dictionary<string, float> {

{"CangId",1001f },

{"Time_Year",2026},{"Time_Month",4},{"Time_Day",26},

{"Time_Hour",14},{"Time_Minute",16},{"Time_Second",01},

{"TempOut",28f },{"TempIn",25f },

{"ShuiBiao",1578f },{"DianBiao",5784f },

{"KongRpm",20f },{"PowerJing",1f },

{"PowerBeng",0f },{"GongShuiJi",2.5f }

};

}

Random f=new Random(DateTime.Now.Millisecond);

SmallDataDict.SmallData ss = null;

Stopwatch t= new Stopwatch();

private void timer1_Tick(object sender, EventArgs e)

{

timer1.Enabled = false;

t.Restart();

string[] keys = Datas.Keys.ToArray();

foreach (string key in keys)

{

if (key == "CangId")

continue;

if(key.StartsWith("Time_"))

{

if (key.EndsWith("Year"))

{ Datas[key] = DateTime.Now.Year; continue; }

if (key.EndsWith("Month"))

{ Datas[key] = DateTime.Now.Month; continue; }

if (key.EndsWith("Day"))

{ Datas[key] = DateTime.Now.Day; continue; }

if (key.EndsWith("Hour"))

{ Datas[key] = DateTime.Now.Hour; continue; }

if (key.EndsWith("Minute"))

{ Datas[key] = DateTime.Now.Minute; continue; }

if (key.EndsWith("Second"))

{ Datas[key] = DateTime.Now.Second; continue; }

}

else

{

if(!key.Contains("Power"))

{

Datas[key] *= (f.Next(80, 120) / 100f);

continue;

}

else

{

Datas[key] = 1f - (f.Next(0, 10) % 2 == 0 ? Datas[key] : 0);

continue;

}

}

}

string data = Newtonsoft.Json.JsonConvert.SerializeObject(Datas);

if (ss != null)

{

if (!ss.SetData("Device" + Datas["CangId"].ToString(), data, out string error))

{

AddLog(error + ",耗时:" + t.ElapsedMilliseconds.ToString() + "ms");

}

else

{

AddLog("上传成功," + "耗时:" + t.ElapsedMilliseconds.ToString() + "ms");

}

}

else

{

AddLog("小数典初始化失败,无法上传数据!");

}

t.Stop();

UpdateUI();

timer1.Enabled = true;

}

private void UpdateUI()

{

this.BeginInvoke(new Action(() =>

{

txtBeng.Text = Datas["PowerBeng"].ToString();

txtEb.Text = Datas["DianBiao"].ToString();

txtJing.Text = Datas["PowerJing"].ToString();

txtKong.Text = Datas["KongRpm"].ToString();

txtLiu.Text = Datas["GongShuiJi"].ToString();

txtTempIn.Text = Datas["TempIn"].ToString();

txtTempOut.Text = Datas["TempOut"].ToString();

txtWb.Text = Datas["ShuiBiao"].ToString();

}));

}

void AddLog(string log)

{

lstLog.BeginInvoke(new Action(() =>

{

lstLog.Items.Add(DateTime.Now.ToString("HH:mm:ss") + ":" + log);

lstLog.SelectedIndex=lstLog.Items.Count-1;

}));

}

private void button1_Click(object sender, EventArgs e)

{

timer1.Enabled = !timer1.Enabled;

button1.Text = timer1.Enabled ? "停止" : "启动";

}

private void lstLog_MouseDoubleClick(object sender, MouseEventArgs e)

{

MessageBox.Show(lstLog.Text);

}

}

}

总控室读取数据展示:

using Microsoft.VisualBasic.Devices;

using SmallDataDict;

namespace DeviceMangerWindows

{

public partial class frmWidnows : Form

{

public frmWidnows()

{

InitializeComponent();

}

SmallDataDict.SmallData ss = null;

private string CangId = "1001";

private void Form1_Load(object sender, EventArgs e)

{

Init();

this.Text = "小数典应用:总控室数据展示系统 - " + CangId;

}

private void Init()

{

if (SmallDataDict.SmallData.Init(new AppInfo("JingSanNongCang", "V30", "jsznc20260426ok")))

//本场注册的编号与SEC密码

{

AddLog("初始化小数典成功!");

ss = new SmallData("JingSanNongCang");

laName.Text = ss.Owner;

}

else

{

AddLog("初始化小数典失败!");

}

}

void AddLog(string log)

{

lstLog.BeginInvoke(new Action(() =>

{

lstLog.Items.Add(DateTime.Now.ToString("HH:mm:ss") + ":" + log);

lstLog.SelectedIndex = lstLog.Items.Count - 1;

}));

}

private void timer1_Tick(object sender, EventArgs e)

{

timer1.Enabled = false;

if (ss != null)

{

if (ss.GetData("Device" + "1001", out string value, out string error))

{

Dictionary<string, float>? datas = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, float>>(value);

if (datas != null && datas.Count > 0)

{

//有内容

foreach (string key in datas.Keys.ToArray())

{

if (key != "CangId" && !key.StartsWith("Time"))

{

if (panel1.Controls.ContainsKey("uc" + key))

{

(panel1.Controls["uc" + key] as UC_Device).DeviceValue = datas[key];

}

else

{

AddLog(key + "设备没有控件!");

}

}

}

}

AddLog("数据刷新");

}

}

timer1.Enabled = true;

}

private void button1_Click(object sender, EventArgs e)

{

timer1.Enabled = !timer1.Enabled;

button1.Text = timer1.Enabled ? "停止" : "开始";

}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

timer1.Enabled = false;

ss?.Dispose();

SmallData.Clear();

}

}

}

仿真传感器控件

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.Windows.Forms;

namespace DeviceMangerWindows

{

public partial class UC_Device : UserControl

{

private string _name = "";

public string DeviceName

{

get

{

return _name;

}

set

{

if (_name != value)

{

_name = value;

laName.Text = _name;

}

}

}

private string _type = "";

private Image? bg = null;

public string DeviceType

{

get

{

return _type;

}

set

{

if (_type != value)

{

_type = value;

bg= Properties.Resources.ResourceManager.GetObject(_type) as Image;

Refresh();

}

}

}

private float _value { get; set; } = 0;

public float DeviceValue

{

get

{

return _value;

}

set

{

_value = value;

picImage.Refresh();

}

}

public UC_Device()

{

InitializeComponent();

}

private void picImage_Paint(object sender, PaintEventArgs e)

{

if (bg != null)

{

e.Graphics.DrawImage(bg, e.ClipRectangle, new Rectangle(Point.Empty, bg.Size),GraphicsUnit.Pixel);

}

Rectangle textrect = new Rectangle(e.ClipRectangle.X+30,

(e.ClipRectangle.Y + (e.ClipRectangle.Height - 30) / 2),

e.ClipRectangle.Width-60,30);

e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(160,255,255,255)), textrect);

e.Graphics.DrawString(_value.ToString("0.00"), this.Font, new SolidBrush(this.ForeColor),textrect,

new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center });

}

}

}

小数典库,只要用同一样的appinfo,无论在任何地方,只要有网的地方,使用它就会访问到原来存的数据内容。单数据内容长度2KB,一个appinfo帐户能存1000项数据。

点击链接加入群聊【小数典】:https://qm.qq.com/q/uSmCtLJhHG

相关推荐
许长安19 小时前
C++ 原子变量与内存序:从std::atomic到release/acquire
开发语言·数据结构·c++·经验分享·笔记
代码中介商21 小时前
C++ STL 容器完全指南(二):vector 深入与 stringstream 实战
开发语言·c++
郝学胜-神的一滴1 天前
Qt 入门 01-01:从零基础到商业级客户端实战
开发语言·c++·qt·程序人生·软件构建
测试员周周1 天前
【Appium 系列】第06节-页面对象实现 — LoginPage 实战
开发语言·前端·人工智能·python·功能测试·appium·测试用例
摇滚侠1 天前
@Autowired 和 @Resource 的区别
java·开发语言
vortex51 天前
PowerShell 的命令补全方案: PSReadLine + PSCompletions + argc + Carapace
windows·powershell
Wy_编程1 天前
go语言中的结构体
开发语言·后端·golang
SeaTunnel1 天前
(八)收官篇 | 数据平台最后一公里:数据集成开发设计与上线治理实战
java·大数据·开发语言·白鲸开源
Ujimatsu1 天前
虚拟机安装Ubuntu 26.04.x服务器版(命令行版)(2026.5)
linux·windows·ubuntu
大卡片1 天前
C++的基础知识点
开发语言·c++