(
function
execute() {
'use strict'
;
angular.module(
'angular.mqtt'
, [])
.provider(
'MQTT'
,
function
() {
//mqtt是全局变量
//monit分为三种状态
// connected
// disConnected
// connecting
var
instance, scope = {};
var
inited;
function
initSth() {
if
(inited)
return
;
inited =
true
;
instance.subscribe(scope.TOPIC_MONIT);
instance.on(
'error'
,
function
(err) {
console.error(err);
// instance.end();
});
instance.on(
'message'
,
function
(topic, payload) {
var
data = JSON.parse(payload);
console.log(
"接收到消息----"
, data);
// var dataType = Object.prototype.toString.call(data);
// var isArray = dataType === '[object Array]';
// var isObject = dataType === '[object Object]';
if
(topic === scope.TOPIC_MONIT) {
for
(
var
index
in
data) {
var
currentStatus = data[index];
(scope.monitHandler || defaultMonitHandler)(index, currentStatus);
}
console.log(
"currentStatusOne--"
, JSON.stringify(currentStatus));
}
if
(topic === scope.TOPIC_STATUS) {
for
(
var
index
in
data) {
var
currentStatus = data[index];
(scope.statusHandler || defaultStatusHandler)(index, currentStatus);
}
console.log(
"currentStatusTwo--"
, JSON.stringify(data));
}
});
instance.on(
'connect'
,
function
() {
console.log(
'connected'
);
});
instance.on(
'reconnect'
,
function
() {
console.log(
'reconnected'
);
initSth();
});
instance.on(
'close'
,
function
() {
console.log(
'closed'
);
});
}
//
function
defaultMonitHandler(robotIdentifer, toStatus) {
console.log(
'robot'
, robotIdentifer,
'is'
, toStatus);
}
//
function
defaultStatusHandler(address, value) {
console.log(
'the value on '
, address,
'is change to'
, value);
}
var
holder = {
//先选择机器人
selectRobot:
function
(identifier) {
console.log(
"选中机器人---"
, identifier);
var
OLD_TOPIC_STATUS = scope.TOPIC_STATUS;
var
NEW_TOPIC_STATUS =
'status/'
+ identifier;
scope.statusHandler =
null
;
scope.identifier = identifier;
scope.TOPIC_SET =
'set/'
+ identifier;
scope.TOPIC_STATUS = NEW_TOPIC_STATUS;
if
(OLD_TOPIC_STATUS && instance) {
console.log(
"unsubscribe----"
);
instance.unsubscribe(OLD_TOPIC_STATUS);
}
if
(NEW_TOPIC_STATUS && instance) {
console.log(
"subscribe-----"
, NEW_TOPIC_STATUS);
instance.subscribe(NEW_TOPIC_STATUS);
}
},
//设置具体参数
setValue:
function
(address, value) {
var
TOPIC_SET = scope.TOPIC_SET;
if
(address && value) {
var
payload = {
[address]: value
};
instance.publish(TOPIC_SET, JSON.stringify(payload));
}
},
//在rootscope或者其他位置调用
onMonitPop:
function
(handler) {
scope.monitHandler = handler;
},
onStatusPop:
function
(handler) {
scope.statusHandler = handler;
// console.log("接收到消息---", scope.statusHandler);
},
removeMonitPop:
function
() {
scope.monitHandler =
null
;
},
removeStatusPop:
function
() {
scope.statusHandler =
null
;
},
//登陆先注册
registClient:
function
(user, address) {
console.log(
"user--"
, JSON.stringify(user), address);
if
(instance)
return
instance;
var
defaultOptions = {
keepalive: 60,
reconnectPeriod: 1000,
clientId:
'operator_'
+ user.id,
username: user.username,
password: user.password,
connectTimeout: 30 * 1000,
};
scope.TOPIC_MONIT =
'monit/'
+ user.id;
instance = mqtt.connect(address, defaultOptions);
initSth();
return
instance;
}
};
this
.$get =
function
() {
return
holder;
};
})
})();