Skip to content

Commit

Permalink
Implemented basis for talking to platform APIs.
Browse files Browse the repository at this point in the history
  • Loading branch information
kallaspriit committed Aug 4, 2016
1 parent e0abbf4 commit f9ee0af
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "sl",
"rules": {
"eol-last": "off",
"no-console": "off"
"no-console": "off",
"no-underscore-dangle": "off",
},
"globals": {
"document": true
Expand Down
6 changes: 6 additions & 0 deletions actions/platform-actions.js
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);
63 changes: 63 additions & 0 deletions apis/platform-api.js
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();
7 changes: 7 additions & 0 deletions config/cumulocity.js
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',
};
2 changes: 2 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export const INCREASE = 'INCREASE';
export const DECREASE = 'DECREASE';

export const FETCH_USER = 'FETCH_USER';

export const GET_DEVICES = 'GET_DEVICES';
66 changes: 66 additions & 0 deletions libs/cumulocity/CumulocityPlatform.js
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,
},
};
}

}
6 changes: 6 additions & 0 deletions libs/platform/AbstractPlatform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default class AbstractPlatform {

getDevices() {}
// getDeviceInfo() {}

}
9 changes: 9 additions & 0 deletions libs/platform/models/DeviceModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default class DeviceModel {

constructor({
name,
}) {
this.name = name;
}

}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
},
"homepage": "http://redux.js.org",
"dependencies": {
"axios": "^0.13.1",
"extract-text-webpack-plugin": "^1.0.1",
"highcharts": "^4.2.6",
"highcharts-more": "^0.1.2",
"material-ui": "^0.15.3",
"react": "^15.3.0",
"react-addons-update": "^15.3.0",
"react-dom": "^15.3.0",
"react-highcharts": "^10.0.0",
"react-redux": "^4.4.5",
Expand Down
14 changes: 14 additions & 0 deletions views/RootView.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@ import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import css from '../gfx/css/main.scss'; // eslint-disable-line no-unused-vars
import DrawerMenuComponent from './components/DrawerMenuComponent';
import CumulocityPlatform from '../libs/cumulocity/CumulocityPlatform';
import cumulocityConfig from '../config/cumulocity';
import platformApi from '../apis/platform-api';

injectTapEventPlugin();

// setup theme
const muiTheme = getMuiTheme({
// override theme styles
});

// setup platform
const cumulocityPlatform = new CumulocityPlatform(cumulocityConfig);

platformApi.setProvider(cumulocityPlatform);

platformApi.getDevices().then((devices) => {
console.log('devices', devices);
});

// root view
function RootView({ children }) {
return (
<MuiThemeProvider muiTheme={muiTheme}>
Expand Down
1 change: 0 additions & 1 deletion views/components/GaugeComponent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { Component, PropTypes } from 'react';
import update from 'react-addons-update';
import Chart from 'react-highcharts';
import highchartsMore from 'highcharts-more';
import solidGauge from 'highcharts/modules/solid-gauge';
Expand Down

0 comments on commit f9ee0af

Please sign in to comment.