尊重原创 勿抄袭
勿私放其他平台
前 言:
create table wiscada.PLCData (id bigint not null
auto_increment,RecordTime datetime,TestFloat_0 real,TestFloat_1
real,TestFloat_2 real,TestFloat_3 real,TestFloat_4 real,primary key(id))
ENGINE = innodb default charset=utf8mb4;
var CurTime //定义当前时间变量
var strSQL //定义变量
//定义全局变量存储标签值,有多少向数据库写的标签就需要定义多少这种全局变量
var TestFloat_0 = context.get('TestFloat_0')
var TestFloat_1 = context.get('TestFloat_1')
var TestFloat_2 = context.get('TestFloat_2')
var TestFloat_3 = context.get('TestFloat_3')
var TestFloat_4 = context.get('TestFloat_4')
//判断当前接受的消息是否是记录触发消息
if(msg.payload == "RecordStart")
{
msg.payload = null; //如果是记录触发消息,则清除该消息,不返回任何结果
RecordTag() //调用记录子函数
return msg; //函数节点输出
}
else //如果接收的消息不是记录触发消息,为正常的数据,将消息传递给全局变量
{
TestFloat_0 = msg.payload["TsetFloat[0]"]; //获取上一节点读取到的标签值
TestFloat_1 = msg.payload["TsetFloat[1]"]; //获取上一节点读取到的标签值
TestFloat_2 = msg.payload["TsetFloat[2]"]; //获取上一节点读取到的标签值
TestFloat_3 = msg.payload["TsetFloat[3]"]; //获取上一节点读取到的标签值
TestFloat_4 = msg.payload["TsetFloat[4]"]; //获取上一节点读取到的标签值
context.set('TestFloat_0',TestFloat_0) //将获取的消息传递给全局变量
context.set('TestFloat_1',TestFloat_1)
context.set('TestFloat_2',TestFloat_2)
context.set('TestFloat_3',TestFloat_3)
context.set('TestFloat_4',TestFloat_4)
}
//定义数据记录子函数
function RecordTag()
{
CurTime = getCurrentDate(2) //取精确到秒的日期时间
strSQL="insert into PLCData (RecordTime,TestFloat_0,TestFloat_1,TestFloat_2,TestFloat_3,TestFloat_4) values ( '" + CurTime + "'," + TestFloat_0 + "," + TestFloat_1 + "," + TestFloat_2 + "," + TestFloat_3 + "," + TestFloat_4 + ")" //举例SQL语句
msg.payload = strSQL //将SQL语句返回给节点输出函数
}
function getCurrentDate(format) //获取当前日期时间函数
{
var now = new Date();
var year = now.getFullYear(); //得到年份
var month = now.getMonth();//得到月份
var date = now.getDate();//得到日期
var day = now.getDay();//得到周几
var hour = now.getHours();//得到小时
var minu = now.getMinutes();//得到分钟
var sec = now.getSeconds();//得到秒
month = month + 1;
if (month < 10) month = "0" + month;
if (date < 10) date = "0" + date;
if (hour < 10) hour = "0" + hour;
if (minu < 10) minu = "0" + minu;
if (sec < 10) sec = "0" + sec;
var time = "";
//精确到天
if(format==1){
time = year + "-" + month + "-" + date;
}
//精确到分
else if(format==2){
time = year + "-" + month + "-" + date+ " " + hour + ":" + minu + ":" + sec;
}
return time;
}
SELECT CONCAT(TRUNCATE(SUM(data_length)/1024/1024,2),'MB') AS data_size,
CONCAT(TRUNCATE(SUM(max_data_length)/1024/1024,2),'MB') AS max_data_size,
CONCAT(TRUNCATE(SUM(data_free)/1024/1024,2),'MB') AS data_free,
CONCAT(TRUNCATE(SUM(index_length)/1024/1024,2),'MB') AS index_size
FROM information_schema.tables WHERE TABLE_SCHEMA = 'wiscada'
SELECT CONCAT(TRUNCATE(SUM(data_length)/1024/1024,2),'MB') AS data_size,
CONCAT(TRUNCATE(SUM(max_data_length)/1024/1024,2),'MB') AS max_data_size,
CONCAT(TRUNCATE(SUM(data_free)/1024/1024,2),'MB') AS data_free,
CONCAT(TRUNCATE(SUM(index_length)/1024/1024,2),'MB') AS index_size
FROM information_schema.tables WHERE TABLE_NAME = 'plcdata'
未完待续
李大拿家的王小拿
2022年2月
往期精选
人气推荐
more+
1.Smart200&V90伺服系统:已更新15课
2.贝加莱直播课程:已更新16课
3.力控科技产品培训:已更新5课
6.专家课堂:已更新12课
原文始发于微信公众号(剑指工控):让PLC数据飞到数据库中