Đọc dữ liệu từ MQTT bằng JavaScript

Step 1:

Download file JS từ CDN sau:
mqttws31.js

Step 2: demo.js để test thử như sau:

var websocket=”m15.cloudmqtt.com”;
var port=36113;
var user=”hlexxxxx”;
var pass=”wOWkJOtyyyy”;

clientID = “clientID-” + parseInt(Math.random() * 100);

// Create a client instance
client = new Paho.MQTT.Client(websocket, port,clientID);

// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

var options = {
useSSL: true,
userName: user,
password: pass,
onSuccess:onConnect,
onFailure:doFail
}

// connect the client
client.connect(options);

// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log(“onConnect”);
client.subscribe(“light”);
message = new Paho.MQTT.Message(“Hello CloudMQTT”);
message.destinationName = “light”;
client.send(message);
}

function doFail(e){
console.log(e);
}

// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log(“onConnectionLost:”+responseObject.errorMessage);
}
}

// called when a message arrives
function onMessageArrived(message) {
console.log(“onMessageArrived:”+message.payloadString);
}

Step 3: chèn js vào phần head của trang html

<head>

http://mqttws31.js
http://demo.js
</head>

Step 4: chạy trang html và mở Console ra để test

Select random questions from db

Bài dưới gợi ý một số cách lấy random câu hỏi từ csdl

Cú pháp với cơ sở dữ liệu MySQL:

SELECT column FROM table
ORDER BY RAND()
LIMIT 1

Cú pháp với cơ sở dữ liệu PostgreSQL:

SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

Cú pháp với cơ sở dữ liệu Microsoft SQL Server:

SELECT TOP 1 column FROM table
ORDER BY NEWID()

Cú pháp với cơ sở dữ liệu IBM DB2

SELECT column, RAND() as IDX 
FROM table 
ORDER BY IDX FETCH FIRST 1 ROWS ONLY

Cú pháp với cơ sở dữ liệu Oracle:

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

LastIndexOf char in Excel

LastIndexOf char in Excel

Ví dụ:

Cho chuỗi https://example.com/archive/January

Yêu cầu lấy chuỗi con cuối cùng sau dấu /

=RIGHT(A2,LEN(A2)-FIND(“@”,SUBSTITUTE(A2,”/”,”@”,LEN(A2)-LEN(SUBSTITUTE(A2,”/”,””))),1))

Phân tích mã

  • SUBSTITUTE(A2,”/”,“”) – This part of the formula replaces the forward slash with an empty string. So for example, In case you want to find the occurrence of any string other than the forward slash, use that here.
  • LEN(A2)-LEN(SUBSTITUTE(A2,”/”,“”)) – This part would tell you how many forward slashes are there in the string. It simply subtracts the length of the string without the forward slash from the length of the string with forward-slashes.
  • SUBSTITUTE(A2,”/”,”@”,LEN(A2)-LEN(SUBSTITUTE(A2,”/”,””))) – This part of the formula would replace the last forward slash with @. The idea is to make that character unique. You can use any character you want. Just make sure it’s unique and doesn’t appear in the string already.
  • FIND(“@”,SUBSTITUTE(A2,”/”,”@”,LEN(A2)-LEN(SUBSTITUTE(A2,”/”,””))),1) – This part of the formula would give you the position of the last forward slash.
  • LEN(A2)-FIND(“@”,SUBSTITUTE(A2,”/”,”@”,LEN(A2)-LEN(SUBSTITUTE(A2,”/”,””))),1) – This part of the formula would tell us how many characters are there after the last forward slash.
  • =RIGHT(A2,LEN(A2)-FIND(“@”,SUBSTITUTE(A2,”/”,”@”,LEN(A2)-LEN(SUBSTITUTE(A2,”/”,””))),1)) – Now this would simply give us the string after the last forward slash.

 

Nguồn: https://trumpexcel.com/find-characters-last-position/