-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented basis for talking to platform APIs.
- Loading branch information
1 parent
e0abbf4
commit f9ee0af
Showing
11 changed files
with
176 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { createAction } from 'redux-actions'; | ||
import platformApi from '../apis/platform-api'; | ||
import { GET_DEVICES } from '../constants'; | ||
|
||
export const getDevices = createAction(GET_DEVICES, platformApi.getDevices); | ||
export const getDevices2 = createAction(GET_DEVICES, platformApi.getDevices); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import AbstractPlatform from '../libs/platform/AbstractPlatform'; | ||
|
||
export class PlatformApi extends AbstractPlatform { | ||
|
||
setProvider(provider) { | ||
this.provider = provider; | ||
|
||
this._setupProxy(); | ||
} | ||
|
||
getDevices() { | ||
return this.provider.getDevices(); | ||
} | ||
|
||
_getProviderMethodNames() { | ||
return Object.getOwnPropertyNames(Object.getPrototypeOf(this.provider)); | ||
} | ||
|
||
_getExpectedMethodNames() { | ||
return Object.getOwnPropertyNames(AbstractPlatform.prototype).filter( | ||
this._isProxiedMethodName | ||
); | ||
} | ||
|
||
_isProxiedMethodName(methodName) { | ||
if (methodName.substr(0, 1) === '_') { | ||
return false; | ||
} | ||
|
||
if (methodName === 'constructor') { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
_setupProxy() { | ||
const proxiedMethodNames = []; | ||
const expectedMethodNames = this._getExpectedMethodNames(); | ||
|
||
this._getProviderMethodNames().forEach((methodName) => { | ||
if (!this._isProxiedMethodName(methodName)) { | ||
return; | ||
} | ||
|
||
this[methodName] = (...args) => this.provider[methodName](...args); | ||
|
||
proxiedMethodNames.push(methodName); | ||
}); | ||
|
||
this._verifyAbstractPlatformImplemented(proxiedMethodNames, expectedMethodNames); | ||
} | ||
|
||
_verifyAbstractPlatformImplemented(proxiedMethodNames, expectedMethodNames) { | ||
expectedMethodNames.forEach((expectedMethodName) => { | ||
if (proxiedMethodNames.indexOf(expectedMethodName) === -1) { | ||
throw new Error(`Expected platform provider to implement "${expectedMethodName}"`); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default new PlatformApi(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default { | ||
tenant: 'telia', | ||
host: 'cumulocity.com', | ||
protocol: 'https', | ||
username: 'priit.kallas@telia.ee', | ||
password: 'purgisupp', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import request from 'axios'; | ||
import AbstractPlatform from '../platform/AbstractPlatform'; | ||
import DeviceModel from '../platform/models/DeviceModel'; | ||
|
||
export default class CumulocityPlatform extends AbstractPlatform { | ||
|
||
constructor({ | ||
host, | ||
protocol, | ||
tenant, | ||
username, | ||
password, | ||
}) { | ||
super(); | ||
|
||
this.config = { | ||
host, | ||
protocol, | ||
tenant, | ||
username, | ||
password, | ||
}; | ||
} | ||
|
||
getDevices() { | ||
const url = this._buildUrl('/inventory/managedObjects?fragmentType=c8y_IsDevice'); | ||
|
||
return this._get(url) | ||
.then((response) => { | ||
console.log('response', response); | ||
|
||
return response.data.managedObjects.map(this._mapManagedObjectToDevice); | ||
}) | ||
.catch((error) => { | ||
console.error('error', error); | ||
}); | ||
} | ||
|
||
_mapManagedObjectToDevice(managedObject) { | ||
return new DeviceModel({ | ||
name: managedObject.name, | ||
}); | ||
} | ||
|
||
_buildUrl(query) { | ||
return `${this.config.protocol}://${this.config.tenant}.${this.config.host}/${query}`; | ||
} | ||
|
||
_get(url) { | ||
return request({ | ||
url, | ||
method: 'get', | ||
...this._getStandardRequestParameters(), | ||
}); | ||
} | ||
|
||
_getStandardRequestParameters() { | ||
return { | ||
auth: { | ||
username: this.config.username, | ||
password: this.config.password, | ||
}, | ||
}; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default class AbstractPlatform { | ||
|
||
getDevices() {} | ||
// getDeviceInfo() {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default class DeviceModel { | ||
|
||
constructor({ | ||
name, | ||
}) { | ||
this.name = name; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters