- using DDS.IoT.Com;
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace DDS.IoT.Mqtt
- {
- public class MqttHostCom : ComBase
- {
- public override string Type => "mqttHostCom";
- public override string Name => "Mqtt主机";
- private MqttHostService hostService;
- public override bool Init(int port, int baudRate = 9600, string extendData = null)
- {
- this.Port = port;
- hostService = new MqttHostService();
- hostService.PushId = this.Id;
- hostService.StartAsync(extendData, OnPushData);
- Console.WriteLine("初始化MqttHostCom驱动程序成功!");
- return true;
- }
- public override event PushData OnPushData;
- public override bool Open()
- {
- var b = false;
- try
- {
- b = true;
- IsOpen = true;
- }
- catch (Exception ex)
- {
- string msg = string.Format("MqttHostCom串口打开失败:{0} ", ex.Message);
- Console.WriteLine(msg);
- }
- return b;
- }
- public override bool Close()
- {
- hostService.Dispose();
- hostService = null;
- IsOpen = false;
- OnPushData = null;
- return true;
- }
- public override string Command(string name, string data)
- {
- var outData = string.Empty;
- return outData;
- }
- }
- }
2.MqttClientCom 客户端
- using DDS.IoT.Com;
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace DDS.IoT.Mqtt
- {
- public class MqttClientCom : ComBase
- {
- public override string Type => "mqttClientCom";
- public override string Name => "Mqtt客户端";
- private MqttClientService mqttClientService;
- public override bool Init(int port, int baudRate = 9600, string extendData = null)
- {
- mqttClientService = new MqttClientService();
- mqttClientService.PushId = this.Id;
- this.Port = port;
- mqttClientService.MqttClientStart(extendData,this.OnPushData);
- Console.WriteLine("初始化MqttClientCom驱动程序成功!");
- return true;
- }
- public override event PushData OnPushData;
- public override bool Open()
- {
- var b = false;
- try
- {
- mqttClientService.Open();
- b = true;
- IsOpen = true;
- }
- catch (Exception ex)
- {
- string msg = string.Format("MqttClientCom串口打开失败:{0} ", ex.Message);
- Console.WriteLine(msg);
- }
- return b;
- }
- public override bool Close()
- {
- mqttClientService.Close();
- mqttClientService = null;
- IsOpen = false;
- OnPushData = null;
- return true;
- }
- public override string Command(string name, string data)
- {
- var outData = string.Empty;
- var dataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(data);
- switch (name)
- {
- case "Publish":
- string topic = dataObj.topic;
- string payload = dataObj.data;
- int? level = dataObj.level;
- bool? retain = dataObj.retain;
- if (!level.HasValue)
- {
- level = 1;
- }
- if (!retain.HasValue)
- {
- retain = false;
- }
- outData =mqttClientService.Publish(topic, payload, level.Value, retain.Value).ToString();
- break;
- case "Subscribe":
- topic = dataObj.topic;
- level = dataObj.level;
- if (!level.HasValue)
- {
- level = 0;
- }
- outData = mqttClientService.Subscribe(topic, level.Value).ToString();
- break;
- }
- return outData;
- }
- }
- }
- <!DOCTYPE HTML PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
- <html>
- <head lang="en">
- <title>Mqtt</title>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0">
- <meta name="format-detection" content="telephone=no">
- <!-- Set render engine for 360 browser -->
- <meta name="renderer" content="webkit">
- <!-- No Baidu Siteapp-->
- <!--<meta http-equiv="Cache-Control" content="no-siteapp" />-->
- <META HTTP-EQUIV="pragma" CONTENT="no-cache">
- <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
- <META HTTP-EQUIV="expires" CONTENT="0">
- <link rel="alternate icon" type="image/png" href="favicon.png">
- <link rel="stylesheet" href="/scripts/amazeui/amazeui.min.css" />
- <link rel="stylesheet" href="../css/main.css" />
- <script src="/scripts/jquery-3.3.1.min.js"></script>
- <script src="/scripts/amazeui/amazeui.min.js"></script>
-
- <script src="/scripts/jquery.signalR-2.4.1.min.js"></script>
- <style>
- .list {
- width: 1000px !important;
- }
- .am-form {
- width: 100%;
- background: #fff;
- }
- .refresh-port {
- width: 80px !important;
- height: 30px !important;
- }
- #msg, #msgWrite {
- clear: both;
- }
- .am-u-sm-4 {
- padding: 3px;
- }
- </style>
- <script type="text/javascript">
- var hostid;// 主机id
- var clientid;// 客户端id
-
- function startHost() {
- var args = $('#txtHostArgs').val();
- dds.iot.com.open({
- type: 'mqttHostCom',//mqtt主机
- port: 1,
- baudRate: 1,
- extendData: args,
- //extendData: JSON.stringify({ server: "*", port: 1883 }),
- onReceive: function (res) {
- addMsg('host:' + JSON.stringify(res.data))
- console.log('host', res.data)
- },
- onOpen: function (ar) {
- if (ar.Success) {
- hostid = ar.Data;
- addMsg('连接成功!')
- } else {
- alert(ar.Message)
- }
- }
- })
- }
- function closeHost() {
- dds.iot.com.close(hostid)
- }
- function startClient() {
- var args = $('#txtClientArgs').val();
- dds.iot.com.open({
- type: 'mqttClientCom',//mqtt客户端
- port: 1,
- baudRate: 1,
- extendData: args,
- //extendData: JSON.stringify({ server: "localhost", port: 1883, clientid: "1", username: "", password:""}),
- onReceive: function (res) {
- addMsg('client:' + JSON.stringify(res.data))
- console.log('client',res.data)
- },
- onOpen: function (ar) {
- if (ar.Success) {
- clientid = ar.Data;
- addMsg('连接成功!')
- } else {
- alert(ar.Message)
- }
- }
- })
- }
- function subscribe() {
- var topic = $('#txtTopic').val();
- dds.iot.com.exeCommand({ id: clientid, name: "Subscribe", data: { topic: topic, level: 0 } }, function (ar) {
- if (ar.Success) {
- addMsg('订阅成功!')
- } else {
- addMsg('操作失败:' + ar.Message)
- }
- })
- }
- function publish() {
- var topic = $('#txtTopic').val();
- var contents = $('#txtContents').val();
- dds.iot.com.exeCommand({ id: clientid, name: "Publish", data: { topic: topic, data: contents } }, function (ar) {
- if (ar.Success) {
- addMsg('发布成功!')
- } else {
- addMsg('操作失败:' + ar.Message)
- }
- })
- }
- function closeClient() {
- dds.iot.com.close(clientid)
- }
- var $msg;
- function addMsg(msg) {
- $msg.val($msg.val()+"\n"+msg);
- }
- function clearLog() {
- $msg.val('');
- }
- // 窗口初始化事件(操作窗口大小、标题)
- $(document).bind('dds.window.init', function (e, win) {
- $msg = $("#msg");
- })
- </script>
-
- </head>
-
- <body>
- <div class="fun_bd" style="padding:10px;">
- <form class="am-form">
- <h3>数据读取</h3>
- <fieldset>
- <div class="am-form-group">
- <label for="doc-ipt-email-1" class="am-u-sm-4">主机参数</label>
- <div class="am-u-sm-6">
- <input id="txtHostArgs" type="text" value='{ server: "*", port: 1883 }' />
- </div>
- <button onclick="startHost()" class="am-btn-primary" type="button">启动主机</button>
- <button onclick="closeHost()" class="am-btn-danger" type="button">关闭主机</button>
- </div>
- <div class="am-form-group">
- <label for="doc-ipt-email-1" class="am-u-sm-4">客户端参数</label>
- <div class="am-u-sm-6">
- <input id="txtClientArgs" type="text" value='{ server: "localhost", port: 1883, clientid: "1", username: "", password:""}' />
- </div>
- <button onclick="startClient()" class="am-btn-primary" type="button">启动客户端</button>
- <button onclick="closeClient()" class="am-btn-danger" type="button">关闭客户端</button>
- </div>
- <div class="am-form-group">
- <label for="doc-ipt-email-1" class="am-u-sm-4">主题</label>
- <div class="am-u-sm-6">
- <input id="txtTopic" type="text" value="/dds/iot/mqtt/test" />
- </div>
- <button onclick="subscribe()" class="am-btn-primary" type="button">订阅主题</button>
- </div>
- <div class="am-form-group">
- <label for="doc-ipt-email-1" class="am-u-sm-4">主题内容</label>
- <div class="am-u-sm-6">
- <input id="txtContents" type="text" value="测试测试" />
- </div>
- <button onclick="publish()" class="am-btn-primary" type="button">发布主题</button>
- </div>
-
- <!--<div id="msg"></div>-->
- <textarea id="msg" rows="18"></textarea>
- <div class="am-form-group">
- <button onclick="clearLog()" class="am-btn-default" type="button">清除日志</button>
- </div>
- </fieldset>
- </form>
- </div>
-
- </body>
-
- </html>
基于Chromium内核使用H5快速开发工控系统界面,使用JS API前端人员既可以完成界面展示与硬件控制。系统自带串口、RFID、电子秤等硬件协议支持,并且支持二次定制开发。可以用来开发人机界面(HMI)或数据采集与监督控制系统(SCADA) 。 使用H5或Vue可以本地打包离线应用,也可以在线加载Web网页来控制设备硬件。