Skip to content

Commit 9cb1cd1

Browse files
committed
refactor code
1 parent 36a9bc9 commit 9cb1cd1

File tree

1 file changed

+39
-61
lines changed

1 file changed

+39
-61
lines changed

utils/arduino-connection-manager.js

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ async function getToken(connectionConfig) {
5050
}
5151
}
5252

53+
function getMqttOptions(clientId,token,RED){
54+
return {
55+
host: arduinoCloudHost,
56+
token: token,
57+
onDisconnect: async () => {
58+
console.log(`connection lost for ${clientId}`);
59+
RED.nodes.eachNode((n)=>{
60+
if(n.type === "property in"){
61+
const node = RED.nodes.getNode(n.id);
62+
node.status({ fill: "red", shape: "dot", text: "Connection Error" });
63+
}
64+
});
65+
66+
await reconnectMqtt(clientId);
67+
68+
},
69+
onOffline: async () => {
70+
console.log(`connection lost for ${clientId}`);
71+
RED.nodes.eachNode((n)=>{
72+
if(n.type === "property in"){
73+
const node = RED.nodes.getNode(n.id);
74+
node.status({ fill: "red", shape: "dot", text: "Offline" });
75+
}
76+
});
77+
},
78+
onConnected: () =>{
79+
RED.nodes.eachNode((n)=>{
80+
if(n.type === "property in"){
81+
const node = RED.nodes.getNode(n.id);
82+
node.status({});
83+
}
84+
});
85+
},
86+
useCloudProtocolV2: true
87+
};
88+
}
89+
5390
async function getClientMqtt(connectionConfig, RED) {
5491

5592
if (!connectionConfig || !connectionConfig.credentials) {
@@ -63,31 +100,7 @@ async function getClientMqtt(connectionConfig, RED) {
63100
clientMqtt = new ArduinoClientMqtt.ArduinoClientMqtt();
64101
const tokenInfo = await getToken(connectionConfig);
65102
if (tokenInfo !== undefined) {
66-
const ArduinoCloudOptions = {
67-
host: arduinoCloudHost,
68-
token: tokenInfo.token,
69-
onDisconnect: async () => {
70-
console.log(`connection lost for ${connectionConfig.credentials.clientid}`);
71-
RED.nodes.eachNode((n)=>{
72-
if(n.type === "property in"){
73-
const node = RED.nodes.getNode(n.id);
74-
node.status({ fill: "red", shape: "dot", text: "Connection Error" });
75-
}
76-
});
77-
78-
// await reconnectMqtt(connectionConfig.credentials.clientid);
79-
80-
},
81-
onConnected: () =>{
82-
RED.nodes.eachNode((n)=>{
83-
if(n.type === "property in"){
84-
const node = RED.nodes.getNode(n.id);
85-
node.status({});
86-
}
87-
});
88-
},
89-
useCloudProtocolV2: true
90-
};
103+
const ArduinoCloudOptions = getMqttOptions(connectionConfig.credentials.clientid,tokenInfo.token,RED)
91104
const timeout = setTimeout(() => { updateToken(connectionConfig) }, tokenInfo.expires_in * 1000);
92105
connections.push({
93106
clientId: connectionConfig.credentials.clientid,
@@ -99,50 +112,15 @@ async function getClientMqtt(connectionConfig, RED) {
99112
timeoutUpdateToken: timeout
100113
});
101114
await clientMqtt.connect(ArduinoCloudOptions);
102-
103-
104-
105115
} else {
106-
// TODO: what happens when token is undefined?
107116
clientMqtt = undefined;
108117
}
109118
} else {
110119
if (connections[user].clientMqtt !== null) {
111120
clientMqtt = connections[user].clientMqtt;
112121
} else {
113122
clientMqtt = new ArduinoClientMqtt.ArduinoClientMqtt();
114-
const ArduinoCloudOptions = {
115-
host: arduinoCloudHost,
116-
token: connections[user].token,
117-
onDisconnect: async () => {
118-
console.log(`connection lost for ${connectionConfig.credentials.clientid}`);
119-
RED.nodes.eachNode((n)=>{
120-
if(n.type === "property in"){
121-
const node = RED.nodes.getNode(n.id);
122-
node.status({ fill: "red", shape: "dot", text: "Disconnected" });
123-
}
124-
});
125-
await reconnectMqtt(connectionConfig.credentials.clientid);
126-
},
127-
onOffline: async () => {
128-
console.log(`connection lost for ${connectionConfig.credentials.clientid}`);
129-
RED.nodes.eachNode((n)=>{
130-
if(n.type === "property in"){
131-
const node = RED.nodes.getNode(n.id);
132-
node.status({ fill: "red", shape: "dot", text: "Offline" });
133-
}
134-
});
135-
},
136-
onConnected: () =>{
137-
RED.nodes.eachNode((n)=>{
138-
if(n.type === "property in"){
139-
const node = RED.nodes.getNode(n.id);
140-
node.status({});
141-
}
142-
});
143-
},
144-
useCloudProtocolV2: true
145-
};
123+
const ArduinoCloudOptions = getMqttOptions(connectionConfig.credentials.clientid,connections[user].token,RED)
146124
connections[user].clientMqtt = clientMqtt;
147125
await clientMqtt.connect(ArduinoCloudOptions);
148126

0 commit comments

Comments
 (0)