From 880f786a52e396bc01e94f0cfc978e8a84881e2e Mon Sep 17 00:00:00 2001
From: Eason010212 <1371033826@qq.com>
Date: Wed, 27 Aug 2025 17:25:00 +0800
Subject: [PATCH] add_blocks_for_timer_and_fix_bugs
---
blockly/iot/IOT_Blocks.js | 45 +++++++++++++++++++++++++++++++++++++++
ejs/projects.ejs | 20 +++++++++++++++++
js/MixIO.js | 23 +++++++++++++++-----
js/projects.js | 3 ++-
js/widgets.js | 14 ++++++++----
5 files changed, 95 insertions(+), 10 deletions(-)
diff --git a/blockly/iot/IOT_Blocks.js b/blockly/iot/IOT_Blocks.js
index ff3bf9b..c38bcb4 100644
--- a/blockly/iot/IOT_Blocks.js
+++ b/blockly/iot/IOT_Blocks.js
@@ -574,6 +574,35 @@ Blockly.Blocks['lineChart_recieved'] = {
}
};
+Blockly.Blocks['timer_triggered'] = {
+ init: function() {
+ this.setColour(lineChart_HUE);
+ this.appendDummyInput().appendField(Blockly.MIXLY_MICROBIT_JS_CURRENT);
+ this.appendValueInput("name").setCheck("String");
+ this.appendDummyInput().appendField(Blockly.TIMER_TRIGGERED);
+ this.appendDummyInput().appendField(Blockly.Msg.PROCEDURES_CALL_BEFORE_PARAMS+"value");
+ this.setInputsInline(true);
+ this.appendStatementInput('DO0').appendField(Blockly.Msg.CONTROLS_REPEAT_INPUT_DO);
+ this.setPreviousStatement(true);
+ this.setNextStatement(true);
+ this.setTooltip("");
+ },
+ getVars:function(){
+ return ["time","value"];
+ }
+};
+
+Blockly.Blocks['get_trigger_times'] = {
+ init: function() {
+ this.setColour(lineChart_HUE);
+ this.appendDummyInput().appendField(Blockly.MIXLY_MICROBIT_PY_STORAGE_GET);
+ this.appendValueInput("name").setCheck("String");
+ this.appendDummyInput().appendField(Blockly.GET_TRIGGER_TIMES);
+ this.setOutput(true, null);
+ this.setTooltip("");
+}
+};
+
Blockly.Blocks['lineChart_send'] = {
init: function() {
this.appendDummyInput().appendField(Blockly.TO);
@@ -2086,6 +2115,19 @@ Blockly.JavaScript.lineChart_recieved=function(block) {
return code;
};
+Blockly.JavaScript.timer_triggered=function(block) {
+ var name = Blockly.JavaScript.valueToCode(this, 'name', Blockly.JavaScript.ORDER_ATOMIC);
+ var code="MixIO.getInstance("+name+",MixIO.typeTags.TIMER)\n"+".bind(MixIO.eventTags.TIMER_TRIGGERED, function(event,value){\n"
+ +Blockly.JavaScript.statementToCode(block, "DO0" )+"\n"+"})\n"
+ return code;
+};
+
+Blockly.JavaScript.get_trigger_times=function(block) {
+ var name = Blockly.JavaScript.valueToCode(this, 'name', Blockly.JavaScript.ORDER_ATOMIC);
+ var code="MixIO.getInstance("+name+",MixIO.typeTags.TIMER).getTriggerTimes()"
+ return [code, Blockly.JavaScript.ORDER_ATOMIC];
+};
+
Blockly.JavaScript.lineChart_send=function(block) {
var name = Blockly.JavaScript.valueToCode(this, 'name', Blockly.JavaScript.ORDER_ATOMIC);
var message = Blockly.JavaScript.valueToCode(this, 'message', Blockly.JavaScript.ORDER_ATOMIC);
@@ -3067,6 +3109,9 @@ Blockly.LINECHART_N_MESSAGE="折线图表的至多前";
Blockly.LINECHART_MESSAGE="条消息";
Blockly.LINECHART_LATEST_MESSAGE="折线图表的最新一条消息";
+Blockly.TIMER_TRIGGERED="定时触发器触发时";
+Blockly.GET_TRIGGER_TIMES="定时触发器的触发次数"
+
Blockly.BARCHART_RECIEVED="柱状图收到消息时";
Blockly.BARCHART_SEND_MESSAGE="柱状图发送消息";
Blockly.CLEAR_BARCHART="柱状图表清空";
diff --git a/ejs/projects.ejs b/ejs/projects.ejs
index 27e69ce..0cb5be5 100644
--- a/ejs/projects.ejs
+++ b/ejs/projects.ejs
@@ -654,8 +654,28 @@
+
+
+
+ name
+
+
+
+
+ message
+
+
+
+
+
+
+ name
+
+
+
+
diff --git a/js/MixIO.js b/js/MixIO.js
index 7afe6dd..06b0292 100644
--- a/js/MixIO.js
+++ b/js/MixIO.js
@@ -79,13 +79,14 @@ var MixIO = {
DATA_TABLE: 10,
DASHBOARD: 11,
DATA_MAP: 12,
- WEATHER: 13
+ WEATHER: 13,
+ TIMER: 14
},
oldTags: ["input_button", "input_slider", "input_keyboard", "input_controller", "input_rgb", "output_bulb", "output_text",
- "output_chart", "output_bar", "table", "output_dashboard", "output_map", "input_weather"
+ "output_chart", "output_bar", "table", "output_dashboard", "output_map", "input_weather", "timer"
],
- zhcnTags: ["按键/开关", "滑杆", "文本输入", "摇杆手柄", "RGB色盘", "指示灯", "文本显示屏", "折线图表", "柱状图表", "数据表格", "仪表盘", "数据地图", "实时气象仪"],
+ zhcnTags: ["按键/开关", "滑杆", "文本输入", "摇杆手柄", "RGB色盘", "指示灯", "文本显示屏", "折线图表", "柱状图表", "数据表格", "仪表盘", "数据地图", "实时气象仪", "定时触发器"],
/*合法的MixIO事件种类*/
eventTags: {
@@ -121,7 +122,9 @@ var MixIO = {
DATA_MAP_CHANGED: "1211", //数据地图收到消息
WEATHER_SYNCED: "1311", //气象仪更新数据
- WEATHER_SENT: "1312" //气象仪发送数据
+ WEATHER_SENT: "1312", //气象仪发送数据
+
+ TIMER_TRIGGERED: "1411"
},
@@ -159,8 +162,9 @@ var MixIO = {
DATA_MAP_CLEAR: "1222", //清空数据地图数据
WEATHER_SYNC: "1321", //更新气象仪数据
- WEATHER_SEND: "1322" //发送气象仪数据
+ WEATHER_SEND: "1322", //发送气象仪数据
+ TRIGGER_TIMER: "1411"
},
publish: function(topic, message) {
// if message undefined
@@ -328,6 +332,15 @@ var MixIO = {
instance.getData = function(type) {
return sepMsgs[type]
}
+ } else if (type === MixIO.typeTags.TIMER){
+ instance.getTriggerTimes = function(){
+ try{
+ return parseInt(instance.attr('user-times'))
+ }
+ catch{
+ return 0
+ }
+ }
}
return instance
},
diff --git a/js/projects.js b/js/projects.js
index ac5c329..4dd4fab 100644
--- a/js/projects.js
+++ b/js/projects.js
@@ -1,5 +1,6 @@
var globalVer = "MixIO ver 1.11.24"
var isChanged = false;
+var initCodeMirror = false;
var globalBLE = {}
function copy(obj) {
return JSON.parse(JSON.stringify(obj))
@@ -1917,7 +1918,7 @@ function view_project(projectName, projectType) {
window.addEventListener("resize", function() {
chart.resize();
});
- if (projectType == LOGIC_MODE && isCode) {
+ if (projectType == LOGIC_MODE && isCode && !initCodeMirror) {
init_codemirror()
}
})
diff --git a/js/widgets.js b/js/widgets.js
index 42d0a67..2c99f31 100644
--- a/js/widgets.js
+++ b/js/widgets.js
@@ -2403,6 +2403,7 @@ function add_magic(user_title, user_topic, user_content, user_style, title_style
async function add_timer(user_title, user_topic, user_content, user_style, title_style, isObserve) {
if(MixIO.editor == undefined)
{
+ initCodeMirror = true
await init_codemirror()
}
var isAlive = true
@@ -2430,19 +2431,23 @@ async function add_timer(user_title, user_topic, user_content, user_style, title
var itemdiv = add_block(1, 1, contents, attrs)
MixIO.triggers[title.text()] = function() {
var localTime = 0
+ title.parent().parent().attr('user-times', localTime)
MixIO.setInterval(function() {
if (triggerTimes == 0 || localTime < triggerTimes) {
itemdiv.addClass("triggered")
setTimeout(function() {
itemdiv.removeClass("triggered")
}, 150)
+ // Aug 2025
+ var sendMessage = triggerMessage
if(triggerMessage=="$CURR_TIME$")
- MixIO.publish(triggerTopic, (new Date().getHours() > 10 ? new Date().getHours()+"" : ("0" + new Date().getHours())) +":"+ (new Date().getMinutes() > 10 ? new Date().getMinutes()+"" : ("0" + new Date().getMinutes())) +":"+ (new Date().getSeconds() > 10 ? new Date().getSeconds()+"" : ("0" + new Date().getSeconds())))
+ sendMessage = (new Date().getHours() > 10 ? new Date().getHours()+"" : ("0" + new Date().getHours())) +":"+ (new Date().getMinutes() > 10 ? new Date().getMinutes()+"" : ("0" + new Date().getMinutes())) +":"+ (new Date().getSeconds() > 10 ? new Date().getSeconds()+"" : ("0" + new Date().getSeconds()))
else if(triggerMessage=="$RAN_NUM$")
- MixIO.publish(triggerTopic, Math.round(Math.random()*99+1))
- else
- MixIO.publish(triggerTopic, triggerMessage)
+ sendMessage = Math.round(Math.random()*99+1)
+ MixIO.publish(triggerTopic, sendMessage)
localTime = localTime + 1
+ title.parent().parent().attr('user-times', localTime)
+ itemdiv.trigger(MixIO.eventTags.TIMER_TRIGGERED, sendMessage)
}
}, triggerInterval)
}
@@ -2623,6 +2628,7 @@ async function add_timer(user_title, user_topic, user_content, user_style, title
async function add_trigger(user_title, user_topic, user_content, user_style, title_style, isObserve) {
if(MixIO.editor == undefined)
{
+ initCodeMirror = true
await init_codemirror()
}
var isAlive = true