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

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

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

相关推荐
叶小鸡2 小时前
Java 篇-项目实战-天机学堂(从0到1)-day7
java·开发语言
原来是猿2 小时前
Linux线程同步与互斥(五):线程池的全面实现
linux·服务器·开发语言
eqwaak02 小时前
PyTorch入门:10分钟搭建首个神经网络
开发语言·人工智能·pytorch·python
雪碧聊技术2 小时前
上午题_计算机系统
java·开发语言
纤纡.2 小时前
解锁 Python 实用编程技巧:线程、视觉识别、正则匹配与装饰器实战
开发语言·python·深度学习·opencv
t***5442 小时前
如何在Dev-C++中配置Clang编译器
开发语言·c++
逆境不可逃2 小时前
一篇速通RabbitMQ (从入门到生产实战:核心原理、高级特性与 Spring Boot 集成全解)
开发语言·后端·ruby
yuanpan2 小时前
Python Pandas 库入门:介绍与基本使用教程
开发语言·python·pandas
t***5442 小时前
Dev-C++ 中使用 Clang 调试有哪些常见问题
开发语言·c++