Skip to content

Commit

Permalink
Refactored measurements not to be platform-specific.
Browse files Browse the repository at this point in the history
  • Loading branch information
kallaspriit committed Aug 9, 2016
1 parent d3ea230 commit 467670d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
Binary file added gfx/images/devices/motion.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/AbstractPlatform.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import request from 'axios';
import keyMirror from 'keymirror';

export default class AbstractPlatform {

static Measurement = keyMirror({
LIGHT: null,
});

getDevices() {}
getDevice(id) {}
getRealtimeUpdates(channel, callback) {}
Expand Down
42 changes: 35 additions & 7 deletions src/CumulocityPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,47 @@ export default class CumulocityPlatform extends AbstractPlatform {
const {
id,
time,
type,
self, // eslint-disable-line
source, // eslint-disable-line
...rest,
} = update.data.data;

const measurement = this._getMeasurement(update);

if (measurement === null) {
return null;
}

return {
id,
time,
type,
...rest,
...measurement,
};
});
})
.filter((update) => update !== null);
}

_getMeasurement(update) {
const parsers = {
c8y_LightMeasurement: this._getLightMeasurement.bind(this),
};

return Object.keys(parsers).reduce((measurement, name) => {
if (typeof update.data.data[name] !== 'undefined') {
measurement = parsers[name](update); // eslint-disable-line
}

return measurement;
}, null);
}

_getLightMeasurement(update) {
const info = update.data.data.c8y_LightMeasurement.e;

return {
type: AbstractPlatform.Measurement.LIGHT,
info: {
value: info.value,
unit: info.unit,
},
};
}

_performRealtimeSubscription(subscription) {
Expand Down
7 changes: 4 additions & 3 deletions views/DeviceView.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ class DeviceView extends Component {

renderCapabilityWidget(info, capability) {
const channel = this.getDeviceMeasurementsChannelName(this.props.params.deviceId);
const realtimeInfo = this.props.realtime[channel] || [];
const realtimeUpdates = this.props.realtime[channel] || [];
const capabilityProps = {
device: info,
realtimeInfo,
info,
realtimeUpdates,
};

switch (capability.type) {
Expand Down Expand Up @@ -177,6 +177,7 @@ class DeviceView extends Component {
const typeToBackgroundMap = {
c8y_Linux: '/gfx/images/Devices/computer.jpg',
Light: '/gfx/images/devices/light.jpg',
Motion: '/gfx/images/devices/motion.jpg',
};
let backgroundImage = null;

Expand Down
20 changes: 10 additions & 10 deletions views/components/capabilities/LightSensorCapabilityComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { connect } from 'react-redux';

import CircularProgress from 'material-ui/CircularProgress';

import AbstractPlatform from '../../../src/AbstractPlatform';
import GaugeComponent from '../GaugeComponent';

class LightSensorCapabilityComponent extends Component {

static propTypes = {
device: PropTypes.object.isRequired,
realtimeInfo: PropTypes.array.isRequired,
info: PropTypes.object.isRequired,
realtimeUpdates: PropTypes.array.isRequired,
};

constructor(props) {
Expand All @@ -24,10 +25,10 @@ class LightSensorCapabilityComponent extends Component {

componentWillReceiveProps(newProps) {
const {
realtimeInfo,
realtimeUpdates,
} = this.props;

const measurement = this.getRealtimeMeasurement(realtimeInfo);
const measurement = this.getRealtimeMeasurement(realtimeUpdates);

if (measurement !== null) {
this.setState({
Expand Down Expand Up @@ -67,19 +68,18 @@ class LightSensorCapabilityComponent extends Component {
);
}

getRealtimeMeasurement(realtimeInfo) {
// TODO cumulocity specific
const measurement = realtimeInfo.find(
(item) => item.type === 'c8y_LightSensor'
getRealtimeMeasurement(updates) {
const measurement = updates.find(
(item) => item.type === AbstractPlatform.Measurement.LIGHT
) || null;

if (!measurement) {
return null;
}

return {
value: measurement.c8y_LightMeasurement.e.value,
unit: measurement.c8y_LightMeasurement.e.unit,
value: measurement.info.value,
unit: measurement.info.unit,
};
}

Expand Down

0 comments on commit 467670d

Please sign in to comment.