|
|
@@ -4,7 +4,7 @@ import SM4 from '@/utils/sm4'
|
|
|
|
|
|
const willTopic = 'web/offline'
|
|
|
|
|
|
-const sm4 = new SM4({
|
|
|
+export const sm4 = new SM4({
|
|
|
// encrypt/decypt main key; cannot be omitted
|
|
|
// key: 'fELIMxLsdoSgRZnX',
|
|
|
key: [102, 69, 76, 73, 77, 120, 76, 115, 100, 111, 83, 103, 82, 90, 110, 88],
|
|
|
@@ -20,24 +20,6 @@ const host = `${GATEWAY_WS}${process.env.VUE_APP_MQTT_PROXY}`
|
|
|
const username = process.env.VUE_APP_MQTT_USER_NAME
|
|
|
const password = process.env.VUE_APP_MQTT_PASSWORD
|
|
|
|
|
|
-const options = {
|
|
|
- username,
|
|
|
- password,
|
|
|
- clientId: `mqttjs_${Math.random().toString(16).slice(2)}`,
|
|
|
- keepalive: 30,
|
|
|
- protocolId: 'MQTT',
|
|
|
- protocolVersion: 4,
|
|
|
- clean: true,
|
|
|
- reconnectPeriod: 1000,
|
|
|
- connectTimeout: 30 * 1000,
|
|
|
- will: {
|
|
|
- topic: willTopic,
|
|
|
- payload: 'Connection Closed abnormally..!',
|
|
|
- qos: 0,
|
|
|
- retain: false
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
const whiteList = [
|
|
|
{
|
|
|
type: 'topic',
|
|
|
@@ -63,6 +45,18 @@ function isWhite (item, topic, message) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+export function createClient (options) {
|
|
|
+ return mqtt.connect(host, {
|
|
|
+ protocolId: 'MQTT',
|
|
|
+ protocolVersion: 4,
|
|
|
+ clean: true,
|
|
|
+ keepalive: 30,
|
|
|
+ reconnectPeriod: 1000,
|
|
|
+ connectTimeout: 30 * 1000,
|
|
|
+ ...options
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
const cbs = []
|
|
|
let mqttClient
|
|
|
|
|
|
@@ -72,7 +66,17 @@ function changeState (state) {
|
|
|
|
|
|
function start () {
|
|
|
console.log('Connecting mqtt client...')
|
|
|
- const client = mqtt.connect(host, options)
|
|
|
+ const client = createClient({
|
|
|
+ username,
|
|
|
+ password,
|
|
|
+ clientId: `mqttjs_${Math.random().toString(16).slice(2)}`,
|
|
|
+ will: {
|
|
|
+ topic: willTopic,
|
|
|
+ payload: 'Connection Closed abnormally..!',
|
|
|
+ qos: 0,
|
|
|
+ retain: false
|
|
|
+ }
|
|
|
+ })
|
|
|
|
|
|
client.on('error', err => {
|
|
|
console.log('MQTT connection error: ', err)
|
|
|
@@ -110,30 +114,38 @@ function start () {
|
|
|
mqttClient = null
|
|
|
})
|
|
|
|
|
|
- client.on('message', (topic, message) => {
|
|
|
+ client.on('message', (topic, payload) => {
|
|
|
console.log('MQTT topic', topic)
|
|
|
if (cbs.length) {
|
|
|
- try {
|
|
|
- const s = utf8ArrayBufferToString(message)
|
|
|
- if (s) {
|
|
|
- message = decodeMessage(topic, message, s)
|
|
|
- } else {
|
|
|
- console.log('null')
|
|
|
- message = s
|
|
|
- }
|
|
|
-
|
|
|
- console.log(message)
|
|
|
-
|
|
|
- cbs.forEach(cb => cb(topic, message))
|
|
|
- } catch (e) {
|
|
|
- console.warn(e)
|
|
|
- }
|
|
|
+ const message = decodePayload(topic, payload)
|
|
|
+ cbs.forEach(cb => cb(topic, message))
|
|
|
}
|
|
|
})
|
|
|
|
|
|
return client
|
|
|
}
|
|
|
|
|
|
+export function decodePayload (topic, payload) {
|
|
|
+ try {
|
|
|
+ const s = utf8ArrayBufferToString(payload)
|
|
|
+ if (s) {
|
|
|
+ payload = decodeMessage(topic, payload, s)
|
|
|
+ } else {
|
|
|
+ console.log('null')
|
|
|
+ payload = s
|
|
|
+ }
|
|
|
+ console.log(payload)
|
|
|
+ } catch (e) {
|
|
|
+ console.warn(e)
|
|
|
+ }
|
|
|
+ return payload
|
|
|
+}
|
|
|
+
|
|
|
+const decoder = new TextDecoder('utf-8')
|
|
|
+export function utf8ArrayBufferToString (buffer) {
|
|
|
+ return decoder.decode(buffer)
|
|
|
+}
|
|
|
+
|
|
|
function decodeMessage (topic, arrayBuffer, message) {
|
|
|
if (whiteList.some(item => isWhite(item, topic, message))) {
|
|
|
console.log('white list')
|
|
|
@@ -148,11 +160,6 @@ function decodeMessage (topic, arrayBuffer, message) {
|
|
|
return message
|
|
|
}
|
|
|
|
|
|
-const decoder = new TextDecoder('utf-8')
|
|
|
-function utf8ArrayBufferToString (buffer) {
|
|
|
- return decoder.decode(buffer)
|
|
|
-}
|
|
|
-
|
|
|
function inst () {
|
|
|
if (!mqttClient) {
|
|
|
mqttClient = {
|