Skip to content

Commit

Permalink
fix : mydevice info api bug #15 (#28)
Browse files Browse the repository at this point in the history
* fix : mydevice info api bug

* modify : package.json version

* modify : modify 'undefined' room name to 'Unknown'
  • Loading branch information
shgood authored Jul 2, 2021
1 parent a173390 commit 5dbcbcc
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 95 deletions.
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);

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)
.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)
.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

0 comments on commit 5dbcbcc

Please sign in to comment.