forked from GladysAssistant/Gladys
/
Device.jsx
114 lines (105 loc) · 4.09 KB
/
Device.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { Text, MarkupText } from 'preact-i18n';
import { Component } from 'preact';
import { Link } from 'preact-router/match';
import cx from 'classnames';
import get from 'get-value';
import { RequestStatus } from '../../../../../utils/consts';
import { DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } from '../../../../../../../server/utils/constants';
import MqttDeviceForm from './DeviceForm';
import BatteryLevelFeature from '../../../../../components/device/view/BatteryLevelFeature';
class MqttDeviceBox extends Component {
saveDevice = async () => {
this.setState({ loading: true });
try {
await this.props.saveDevice(this.props.device, this.props.deviceIndex);
} catch (e) {
this.setState({ error: RequestStatus.Error });
}
this.setState({ loading: false });
};
deleteDevice = async () => {
this.setState({ loading: true, tooMuchStatesError: false, statesNumber: undefined });
try {
await this.props.deleteDevice(this.props.device, this.props.deviceIndex);
} catch (e) {
const status = get(e, 'response.status');
const dataMessage = get(e, 'response.data.message');
if (status === 400 && dataMessage && dataMessage.includes('Too much states')) {
const statesNumber = new Intl.NumberFormat().format(dataMessage.split(' ')[0]);
this.setState({ tooMuchStatesError: true, statesNumber });
} else {
this.setState({ error: RequestStatus.Error });
}
}
this.setState({ loading: false });
};
getDeviceProperty = () => {
if (!this.props.device.features) {
return null;
}
const batteryLevelDeviceFeature = this.props.device.features.find(
deviceFeature =>
deviceFeature.category === DEVICE_FEATURE_CATEGORIES.BATTERY &&
deviceFeature.type === DEVICE_FEATURE_TYPES.BATTERY.INTEGER
);
const batteryLevel = get(batteryLevelDeviceFeature, 'last_value');
let mostRecentValueAt = null;
this.props.device.features.forEach(feature => {
if (feature.last_value_changed && new Date(feature.last_value_changed) > mostRecentValueAt) {
mostRecentValueAt = new Date(feature.last_value_changed);
}
});
return {
batteryLevel,
mostRecentValueAt
};
};
render(props, { loading, tooMuchStatesError, statesNumber }) {
const { batteryLevel, mostRecentValueAt } = this.getDeviceProperty();
return (
<div class="col-md-6">
<div class="card">
<div class="card-header">
{props.device.name || <Text id="integration.mqtt.device.noNameLabel" />}{' '}
{batteryLevel && (
<div class="page-options d-flex">
<BatteryLevelFeature batteryLevel={batteryLevel} />
</div>
)}
</div>
<div
class={cx('dimmer', {
active: loading
})}
>
<div class="loader" />
<div class="dimmer-content">
<div class="card-body">
{tooMuchStatesError && (
<div class="alert alert-warning">
<MarkupText id="device.tooMuchStatesToDelete" fields={{ count: statesNumber }} />
</div>
)}
<MqttDeviceForm {...props} mostRecentValueAt={mostRecentValueAt} />
<div class="form-group">
<button onClick={this.saveDevice} class="btn btn-success mr-2">
<Text id="integration.mqtt.device.saveButton" />
</button>
<button onClick={this.deleteDevice} class="btn btn-danger mr-2">
<Text id="integration.mqtt.device.deleteButton" />
</button>
<Link href={`/dashboard/integration/device/mqtt/edit/${props.device.selector}`}>
<button class="btn btn-secondary float-right">
<Text id="integration.mqtt.device.editButton" />
</button>
</Link>
</div>
</div>
</div>
</div>
</div>
</div>
);
}
}
export default MqttDeviceBox;