12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- var option = {
- //mqtt客户端的id,这里面应该还可以加上其他参数,具体看官方文档
- clientId: 'mqttjs_' + (Math.random() * 1000000).toString(),
- timeout: 5000,
- useSSL: false
- }
- var alarmMqttClient = mqtt.connect("ws://localhost:8384/mqtt", option);
- alarmMqttClient.on('connect', function () {
- console.log("alarm bar connected to mqtt server successfully");
- alarmMqttClient.subscribe('/sys/+/+/thing/event/OnAlarm/+', function(err) {
- if (err) {
- console.log("subscribe OnAlarm event failed, error info:"+err);
- }
- });
- });
- alarmMqttClient.on('message', function (topic, message) {
- var reg = RegExp(/OnAlarm/);
- if (topic.toString().match(reg)) {
- ProcessAlarmInformations(JSON.parse(message.toString()));
- }
- });
- function ProcessAlarmInformations(message) {
- if (message == null || message.Alarms == null || !Array.isArray(message.Alarms)) {
- console.log("Invalid alarm message received");
- return;
- }
- message.Alarms.forEach(function(alarm) {
- var temp = {
- "Key": alarm.TankNumber+alarm.Type, "Description": alarm.Description, "OccurTime": alarm.CreatedTimeStamp, "Module": "ATG", "Severity": alarm.Priority, "Action": null
- };
- AddAlarmInfomation(JSON.stringify(temp));
- });
- }
- //message is a json string
- function AddAlarmInfomation(message) {
- $.ajax({
- url: "/AlarmBar/AddInformation",
- contentType: 'application/json',
- data: message,
- type: 'post',
- success: function (res) {
- console.log("Alarm information added successfully");
- },
- error: function (msg) {
- console.log(msg);
- }
- });
- }
- //the key should be the same as it used when added into alarm bar
- function RemoveAlarmInfomationByKey(key) {
- $.ajax({
- url: "/AlarmBar/RemoveInformation?key="+key,
- contentType: 'application/json',
- //data: message,
- type: 'post',
- success: function (res) {
- console.log("Alarm information removed successfully, the key is:"+key);
- },
- error: function (msg) {
- console.log(msg);
- }
- });
- }
|