Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : mydevice info api bug #15 #28

Merged
merged 3 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions 101-SmartThings.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,15 +548,15 @@ module.exports = function (RED) {
if (RED.nodes.getCredentials(n.id) && RED.nodes.getCredentials(n.id).stAccessToken) {
pat = RED.nodes.getCredentials(n.id).stAccessToken;
}
SmartThingsProfile.addPersonalToken(n.id, pat);
SmartThingsProfile.addMydeviceInfo(n.id, pat);
dplaya-min marked this conversation as resolved.
Show resolved Hide resolved

RED.nodes.createNode(this, n);
Object.assign(this, n)
stCompatibleCheck(this)

this.on('close', function (removed, done) {
const pat = (RED.nodes.getCredentials(this.id)) ? RED.nodes.getCredentials(this.id).stAccessToken : null;
SmartThingsProfile.addPersonalToken(this.id, pat);
SmartThingsProfile.addMydeviceInfo(this.id, pat);
done();
});
}
Expand All @@ -572,15 +572,15 @@ module.exports = function (RED) {
if (RED.nodes.getCredentials(n.id) && RED.nodes.getCredentials(n.id).stAccessToken) {
pat = RED.nodes.getCredentials(n.id).stAccessToken
}
SmartThingsProfile.addPersonalToken(n.id, pat);
SmartThingsProfile.addMydeviceInfo(n.id, pat);

RED.nodes.createNode(this, n);
Object.assign(this, n);
stCompatibleCheck(this);

this.on('close', function (removed, done) {
const pat = (RED.nodes.getCredentials(this.id)) ? RED.nodes.getCredentials(this.id).stAccessToken : null;
SmartThingsProfile.addPersonalToken(this.id, pat);
SmartThingsProfile.addMydeviceInfo(this.id, pat);
done();
});
}
Expand Down
176 changes: 86 additions & 90 deletions lib/SmartThingsProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,102 +42,98 @@ function getCapability(vid,version) {
}
return _capabilities[vid] || null;
}
function addPersonalToken(nodeId,pat,isRefresh){
function addMydeviceInfo(nodeId,pat){
if(!pat){
_mydevices[nodeId]=Promise.resolve(null);
return Promise.resolve(null);
}
if(!_mydevices[nodeId]||isRefresh){
const PAT = {};
let failed = [];
const getLocationsPR = SmartThingsAPI.getLocations(pat)
.then(response=>{
if(response.statusCode==200 && response.body){
let locationsResult = JSON.parse(response.body);
return Promise.all(
locationsResult.items
.sort((l1,l2) => l1.name.localeCompare(l2.name))
.map(location=>{
return SmartThingsAPI.getLocationsRooms(pat, location.locationId)
.then(roomResponse => {
if(roomResponse.statusCode==200){
const roomResult=JSON.parse(roomResponse.body);
location.rooms=roomResult.items;
location.rooms.sort((r1,r2)=> r1.name.localeCompare(r2.name));
location.rooms.push({locationId:location.locationId,roomId:'undefined',name:"'undefined'"});
}
})
}))
.then(()=>PAT.locations = locationsResult.items)
.catch(e=>console.warn(e))
}else{
return Promise.reject(response.statusCode+' '+response.statusMessage);
}
}).catch(e=>failed.push(`getLocations failed : ${e}`))
const PAT = {};
let failed = [];
const getLocationsPR = SmartThingsAPI.getLocations(pat)
dplaya-min marked this conversation as resolved.
Show resolved Hide resolved
.then(response=>{
if(response.statusCode==200 && response.body){
let locationsResult = JSON.parse(response.body);
return Promise.all(
locationsResult.items
.sort((l1,l2) => l1.name.localeCompare(l2.name))
.map(location=>{
return SmartThingsAPI.getLocationsRooms(pat, location.locationId)
.then(roomResponse => {
if(roomResponse.statusCode==200){
const roomResult=JSON.parse(roomResponse.body);
location.rooms=roomResult.items;
location.rooms.sort((r1,r2)=> r1.name.localeCompare(r2.name));
location.rooms.push({locationId:location.locationId,roomId:'undefined',name:"'unknown'"});
}
})
}))
.then(()=>PAT.locations = locationsResult.items)
.catch(e=>console.warn(e))
}else{
return Promise.reject(response.statusCode+' '+response.statusMessage);
}
}).catch(e=>failed.push(`getLocations failed : ${e}`))

const getDevicesPR = SmartThingsAPI.getDevices(pat)
.then(response=>{
if(response.statusCode==200){
let devicesResult = JSON.parse(response.body);
return devicesResult.items
.map(d=>{
return {
deviceId:d.deviceId,
name:d.label||d.name,
locationId:d.locationId,
roomId:d.roomId,
components:d.components
}
})
}else{
return Promise.reject(response.statusCode+' '+response.statusMessage);
}
}).then(devices=>{
PAT.devices=devices;
PAT.customCps = PAT.devices.map(d=>d.components).flat()
.map(cmp=>cmp.capabilities).flat()
.map(cp=> cp.id+'_v'+cp.version)
.filter(cpId=>/\./.test(cpId))//only custom capabilities
.filter((cpId,idx,arr)=>arr.indexOf(cpId)==idx)//remove duplicate items
.sort();
return devices;
}).then(devices=>{
var prs = devices.map(d=>d.components).flat()
.map(cmp=>cmp.capabilities).flat()
.filter(cp=>!getCapability(cp.id,cp.version))
.map(cp=>SmartThingsAPI.getCapability(pat,cp.id,cp.version)
.then(response=>{
if(response.statusCode==200&&typeof response.body == 'string'){
const cp = JSON.parse(response.body);
addCapability(cp);
return cp;
}
})
)
return Promise.all(prs).catch(()=>{})
}).catch(e=>{
failed.push(`getDevices failed : ${e}`)
_mydevices[nodeId]=Promise.resolve(null);
return Promise.resolve(null);
})
const getDevicesPR = SmartThingsAPI.getDevices(pat)
dplaya-min marked this conversation as resolved.
Show resolved Hide resolved
.then(response=>{
if(response.statusCode==200){
let devicesResult = JSON.parse(response.body);
return devicesResult.items
.map(d=>{
return {
deviceId:d.deviceId,
name:d.label||d.name,
locationId:d.locationId,
roomId:d.roomId,
components:d.components
}
})
}else{
return Promise.reject(response.statusCode+' '+response.statusMessage);
}
}).then(devices=>{
PAT.devices=devices;
PAT.customCps = PAT.devices.map(d=>d.components).flat()
.map(cmp=>cmp.capabilities).flat()
.map(cp=> cp.id+'_v'+cp.version)
.filter(cpId=>/\./.test(cpId))//only custom capabilities
.filter((cpId,idx,arr)=>arr.indexOf(cpId)==idx)//remove duplicate items
.sort();
return devices;
}).then(devices=>{
var prs = devices.map(d=>d.components).flat()
.map(cmp=>cmp.capabilities).flat()
.filter(cp=>!getCapability(cp.id,cp.version))
.map(cp=>SmartThingsAPI.getCapability(pat,cp.id,cp.version)
.then(response=>{
if(response.statusCode==200&&typeof response.body == 'string'){
const cp = JSON.parse(response.body);
addCapability(cp);
return cp;
}
})
)
return Promise.all(prs).catch(()=>{})
}).catch(e=>{
failed.push(`getDevices failed : ${e}`)
_mydevices[nodeId]=Promise.resolve(null);
return Promise.resolve(null);
})


_mydevices[nodeId]=getLocationsPR
.finally(()=>getDevicesPR)
.then((results)=>{
if(failed.length>0){
console.log(`SmartThings pat=${pat} failed, ${failed.join(' / ')}`);
}
if(PAT.devices){
_mydevices[nodeId]=Promise.resolve(PAT);
}else{
_mydevices[nodeId]=Promise.resolve(null);
}
return _mydevices[nodeId];
})
}else if(_mydevices[nodeId]){
return _mydevices[nodeId];
}
_mydevices[nodeId]=getLocationsPR
.finally(()=>getDevicesPR)
.then((results)=>{
if(failed.length>0){
console.log(`SmartThings pat=${pat} failed, ${failed.join(' / ')}`);
}
if(PAT.devices){
_mydevices[nodeId]=Promise.resolve(PAT);
}else{
_mydevices[nodeId]=Promise.resolve(null);
}
return _mydevices[nodeId];
})
}
function getMyDevices(){
const result = {};
Expand All @@ -147,6 +143,6 @@ function getMyDevices(){
}
module.exports = {
getCapabilities:getCapabilities,
addPersonalToken:addPersonalToken,
addMydeviceInfo:addMydeviceInfo,
getMyDevices:getMyDevices
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-samsung-automation-studio-nodes",
"version": "1.1.7",
"version": "1.1.8",
"description": "Samsung Automation Studio Nodes for Node-RED",
"keywords": [
"SmartThings",
Expand Down