feat(boards): 物联网 分类下添加 TinyWebDB 分类
This commit is contained in:
@@ -778,4 +778,123 @@ export const ollama_empty_history = {
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
const TINY_WEB_DB = 0;
|
||||
|
||||
export const iot_tiny_web_db_init = {
|
||||
init: function () {
|
||||
this.setColour(TINY_WEB_DB);
|
||||
this.appendDummyInput()
|
||||
.appendField(`${Blockly.Msg.MIXLY_SETUP} TinyWebDB`);
|
||||
this.appendValueInput('ADDR')
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.MIXLY_EMQX_SERVER);
|
||||
this.appendValueInput('USERNAME')
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.MIXLY_EMQX_USERNAME);
|
||||
this.appendValueInput('PASSWORD')
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.MIXLY_EMQX_PASSWORD);
|
||||
this.setInputsInline(false);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const iot_tiny_web_db_init_with_mqtt = {
|
||||
init: function () {
|
||||
this.setColour(TINY_WEB_DB);
|
||||
this.appendDummyInput()
|
||||
.appendField(`${Blockly.Msg.MIXLY_SETUP} TinyWebDB`);
|
||||
this.appendValueInput('MQTT')
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(`MixIO ${Blockly.Msg.MIXLY_Client}`);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const iot_tiny_web_db_update = {
|
||||
init: function () {
|
||||
this.setColour(TINY_WEB_DB);
|
||||
this.appendDummyInput()
|
||||
.appendField('TinyWebDB');
|
||||
this.appendValueInput('TAG')
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.MIXLY_TINY_WEB_DB_UPDATE_VARIABLE);
|
||||
this.appendValueInput('VALUE')
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.DICTS_ADD_VALUE);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const iot_tiny_web_db_get = {
|
||||
init: function () {
|
||||
this.setColour(TINY_WEB_DB);
|
||||
this.appendDummyInput()
|
||||
.appendField('TinyWebDB');
|
||||
this.appendValueInput('TAG')
|
||||
.appendField(Blockly.Msg.MIXLY_TINY_WEB_DB_GET_VARIABLE);
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.DICTS_ADD_VALUE);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const iot_tiny_web_db_count = {
|
||||
init: function () {
|
||||
this.setColour(TINY_WEB_DB);
|
||||
this.appendDummyInput()
|
||||
.appendField('TinyWebDB');
|
||||
this.appendDummyInput()
|
||||
.appendField(Blockly.Msg.MIXLY_TINY_WEB_DB_GET_VARIABLES_NUM);
|
||||
this.setInputsInline(true);
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const iot_tiny_web_db_search = {
|
||||
init: function () {
|
||||
this.setColour(TINY_WEB_DB);
|
||||
this.appendDummyInput()
|
||||
.appendField('TinyWebDB')
|
||||
.appendField(Blockly.Msg.MIXLY_TINY_WEB_DB_SEARCH_VARIABLES);
|
||||
this.appendValueInput('NO')
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.MIXLY_TINY_WEB_DB_START_NUMBER);
|
||||
this.appendValueInput('COUNT')
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.MIXLY_TINY_WEB_DB_VARIABLE_NUMBER);
|
||||
this.appendValueInput('TAG')
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(Blockly.Msg.MIXLY_TINY_WEB_DB_SEARCH_VARS);
|
||||
this.appendDummyInput()
|
||||
.setAlign(Blockly.inputs.Align.RIGHT)
|
||||
.appendField(new Blockly.FieldDropdown([
|
||||
[Blockly.Msg.MIXLY_TINY_WEB_DB_GET_VARIABLES_ALL, 'both'],
|
||||
[Blockly.Msg.MIXLY_TINY_WEB_DB_GET_VARIABLES_TAG, 'tag'],
|
||||
[Blockly.Msg.MIXLY_TINY_WEB_DB_GET_VARIABLES_VALUE, 'value']
|
||||
]), 'DTYPE');
|
||||
this.setInputsInline(false);
|
||||
this.setOutput(true);
|
||||
}
|
||||
};
|
||||
|
||||
export const iot_tiny_web_db_delete = {
|
||||
init: function () {
|
||||
this.setColour(TINY_WEB_DB);
|
||||
this.appendDummyInput()
|
||||
.appendField('TinyWebDB');
|
||||
this.appendValueInput('TAG')
|
||||
.appendField(Blockly.Msg.MIXLY_TINY_WEB_DB_DELETE_VARIABLE);
|
||||
this.setInputsInline(true);
|
||||
this.setPreviousStatement(true);
|
||||
this.setNextStatement(true);
|
||||
}
|
||||
};
|
||||
@@ -310,4 +310,52 @@ export const use_ollama_llm_to_chat_return = function (_, generator) {
|
||||
export const ollama_empty_history = function () {
|
||||
var code = 'llm.empty_history()\n';
|
||||
return code;
|
||||
}
|
||||
|
||||
export const iot_tiny_web_db_init = function (_,generator) {
|
||||
generator.definitions_['import_tiny_webdb'] = "import tiny_webdb";
|
||||
const addr = generator.valueToCode(this, 'ADDR', generator.ORDER_ATOMIC);
|
||||
const username = generator.valueToCode(this, 'USERNAME', generator.ORDER_ATOMIC);
|
||||
const password = generator.valueToCode(this, 'PASSWORD', generator.ORDER_ATOMIC);
|
||||
const code = `webdb = tiny_webdb.TinyWebDB(${addr}, ${username}, ${password})\n`;
|
||||
return code;
|
||||
}
|
||||
|
||||
export const iot_tiny_web_db_init_with_mqtt = function (_,generator) {
|
||||
const mqtt = generator.valueToCode(this, 'MQTT', generator.ORDER_ATOMIC);
|
||||
const code = `webdb = tiny_webdb.TinyWebDB(${mqtt})\n`;
|
||||
return code;
|
||||
}
|
||||
|
||||
export const iot_tiny_web_db_update = function (_,generator) {
|
||||
const tag = generator.valueToCode(this, 'TAG', generator.ORDER_ATOMIC);
|
||||
const value = generator.valueToCode(this, 'VALUE', generator.ORDER_ATOMIC);
|
||||
const code = `webdb.update(${tag}, ${value})\n`;
|
||||
return code;
|
||||
}
|
||||
|
||||
export const iot_tiny_web_db_get = function (_,generator) {
|
||||
const tag = generator.valueToCode(this, 'TAG', generator.ORDER_ATOMIC);
|
||||
const code = `webdb.get(${tag})`;
|
||||
return [code, generator.ORDER_ATOMIC];
|
||||
}
|
||||
|
||||
export const iot_tiny_web_db_count = function (_,generator) {
|
||||
const code = 'webdb.count()';
|
||||
return [code, generator.ORDER_ATOMIC];
|
||||
}
|
||||
|
||||
export const iot_tiny_web_db_search = function (_,generator) {
|
||||
const no = generator.valueToCode(this, 'NO', generator.ORDER_ATOMIC);
|
||||
const count = generator.valueToCode(this, 'COUNT', generator.ORDER_ATOMIC);
|
||||
const tag = generator.valueToCode(this, 'TAG', generator.ORDER_ATOMIC);
|
||||
const dtype = this.getFieldValue('DTYPE');
|
||||
const code = `webdb.search(${no}, ${count}, ${tag}, '${dtype}')`;
|
||||
return [code, generator.ORDER_ATOMIC];
|
||||
}
|
||||
|
||||
export const iot_tiny_web_db_delete = function (_,generator) {
|
||||
const tag = generator.valueToCode(this, 'TAG', generator.ORDER_ATOMIC);
|
||||
const code = `webdb.delete(${tag})\n`;
|
||||
return code;
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import time
|
||||
import usocket as socket
|
||||
import urequests as requests
|
||||
import ustruct as struct
|
||||
from machine import unique_id
|
||||
from ubinascii import hexlify
|
||||
@@ -41,8 +40,9 @@ def image_base64(path="mixly.jpg"):
|
||||
return b'data:image/jpg;base64,' + b64encode(path)
|
||||
|
||||
def ntp(url='mixio.mixly.cn'):
|
||||
import urequests
|
||||
try:
|
||||
results=eval(requests.get('http://{}/time.php'.format(url)).text)
|
||||
results=eval(urequests.get('http://{}/time.php'.format(url)).text)
|
||||
except Exception as e:
|
||||
raise RuntimeError("API request failed or WiFi is not connected",e)
|
||||
return results
|
||||
@@ -72,6 +72,7 @@ class MQTTClient:
|
||||
port = 8883 if ssl else 1883
|
||||
self.client_id = client_id
|
||||
self.sock = None
|
||||
self.server = server
|
||||
self.addr = socket.getaddrinfo(server, port)[0][-1]
|
||||
self.ssl = ssl
|
||||
self.ssl_params = ssl_params
|
||||
@@ -86,7 +87,6 @@ class MQTTClient:
|
||||
self.lw_retain = False
|
||||
self._on_message_filtered = MQTTMatcher()
|
||||
self._star_time = time.ticks_ms()
|
||||
self._tiny_web_db = TinyWebDB("", username, password)
|
||||
|
||||
def _send_str(self, s):
|
||||
self.sock.write(struct.pack("!H", str_len(s)))
|
||||
@@ -343,85 +343,5 @@ class MQTTClient:
|
||||
self.ping()
|
||||
return self.wait_msg()
|
||||
|
||||
def tiny_web_db_update(self, url, key, value):
|
||||
self._tiny_web_db.set_url(url)
|
||||
return self._tiny_web_db.update(key, value)
|
||||
|
||||
def tiny_web_db_get(self, url, key):
|
||||
self._tiny_web_db.set_url(url)
|
||||
return self._tiny_web_db.get(key)
|
||||
|
||||
def tiny_web_db_count(self, url):
|
||||
self._tiny_web_db.set_url(url)
|
||||
return self._tiny_web_db.count()
|
||||
|
||||
def tiny_web_db_search(self, url, no=1, count=1, tag='', dtype='both'):
|
||||
self._tiny_web_db.set_url(url)
|
||||
return self._tiny_web_db.search(no, count, tag, dtype)
|
||||
|
||||
def tiny_web_db_delete(self, url, key):
|
||||
self._tiny_web_db.set_url(url)
|
||||
return self._tiny_web_db.delete(key)
|
||||
|
||||
class TinyWebDB:
|
||||
def __init__(self, url, username, password):
|
||||
self._api_url = ""
|
||||
self._username = username
|
||||
self._password = password
|
||||
self.set_url(url)
|
||||
|
||||
def update(self, key, value):
|
||||
key = str(key)
|
||||
value = str(value)
|
||||
result = self._request("update", "tag={}&value={}".format(key, value))
|
||||
if result["status"] == "error":
|
||||
raise RuntimeError(result["message"])
|
||||
|
||||
def get(self, key):
|
||||
key = str(key)
|
||||
result = self._request("get", "tag={}".format(key))
|
||||
if result["status"] == "error":
|
||||
raise RuntimeError(result["message"])
|
||||
return result["value"]
|
||||
|
||||
def count(self):
|
||||
result = self._request("count")
|
||||
if result["status"] == "error":
|
||||
raise RuntimeError(result["message"])
|
||||
return int(result["count"])
|
||||
|
||||
def search(self, no=1, count=1, tag='', dtype='both'):
|
||||
no = str(no)
|
||||
count = str(count)
|
||||
result = self._request("search", "no={}&count={}&tag={}&type={}".format(no, count, tag, dtype))
|
||||
if result["status"] == "error":
|
||||
raise RuntimeError(result["message"])
|
||||
return result["data"]
|
||||
|
||||
def delete(self, key):
|
||||
key = str(key)
|
||||
result = self._request("delete", "tag={}".format(key))
|
||||
if result["status"] == "error":
|
||||
raise RuntimeError(result["message"])
|
||||
|
||||
def set_url(self, url):
|
||||
if url[-1] != '/':
|
||||
url += '/'
|
||||
self._api_url = url
|
||||
|
||||
def _request(self, op, param=""):
|
||||
data = "user={}&secret={}&action={}".format(self._username, self._password, op)
|
||||
if param:
|
||||
data += '&' + param
|
||||
try:
|
||||
headers = {
|
||||
"Content-Type": "application/x-www-form-urlencoded"
|
||||
}
|
||||
response = requests.post(self._api_url, data=data, headers=headers)
|
||||
result = {}
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
response.close()
|
||||
return result
|
||||
except Exception as e:
|
||||
raise RuntimeError("API request failed or WiFi is not connected", e)
|
||||
def get_server_info(self):
|
||||
return (self.server, self.username, self.password)
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import urequests as requests
|
||||
|
||||
|
||||
class TinyWebDB:
|
||||
def __init__(self, url, username, password):
|
||||
self._api_url = ""
|
||||
self._username = username
|
||||
self._password = password
|
||||
self.set_url(url)
|
||||
|
||||
def __init__(self, mqtt_client):
|
||||
self._api_url = ""
|
||||
url, username, password = mqtt_client.get_server_info()
|
||||
self.set_url('https://{}:443'.format(url))
|
||||
self._username = username
|
||||
self._password = password
|
||||
|
||||
def update(self, key, value):
|
||||
key = str(key)
|
||||
value = str(value)
|
||||
result = self._request("update", "tag={}&value={}".format(key, value))
|
||||
if result["status"] == "error":
|
||||
raise RuntimeError(result["message"])
|
||||
|
||||
def get(self, key):
|
||||
key = str(key)
|
||||
result = self._request("get", "tag={}".format(key))
|
||||
if result["status"] == "error":
|
||||
raise RuntimeError(result["message"])
|
||||
return result["value"]
|
||||
|
||||
def count(self):
|
||||
result = self._request("count")
|
||||
if result["status"] == "error":
|
||||
raise RuntimeError(result["message"])
|
||||
return int(result["count"])
|
||||
|
||||
def search(self, no=1, count=1, tag='', dtype='both'):
|
||||
no = str(no)
|
||||
count = str(count)
|
||||
result = self._request("search", "no={}&count={}&tag={}&type={}".format(no, count, tag, dtype))
|
||||
if result["status"] == "error":
|
||||
raise RuntimeError(result["message"])
|
||||
return result["data"]
|
||||
|
||||
def delete(self, key):
|
||||
key = str(key)
|
||||
result = self._request("delete", "tag={}".format(key))
|
||||
if result["status"] == "error":
|
||||
raise RuntimeError(result["message"])
|
||||
|
||||
def set_url(self, url):
|
||||
if url[-1] != '/':
|
||||
url += '/'
|
||||
self._api_url = url
|
||||
|
||||
def _request(self, op, param=""):
|
||||
data = "user={}&secret={}&action={}".format(self._username, self._password, op)
|
||||
if param:
|
||||
data += '&' + param
|
||||
try:
|
||||
headers = {
|
||||
"Content-Type": "application/x-www-form-urlencoded"
|
||||
}
|
||||
response = requests.post(self._api_url, data=data, headers=headers)
|
||||
result = {}
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
response.close()
|
||||
return result
|
||||
except Exception as e:
|
||||
raise RuntimeError("API request failed or WiFi is not connected", e)
|
||||
Reference in New Issue
Block a user