-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.jsx
More file actions
165 lines (142 loc) · 5.03 KB
/
main.jsx
File metadata and controls
165 lines (142 loc) · 5.03 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// MarkerComponent
//
const COLLECTOR_ENDPOINT = "http://localhost:8080"
class MarkerComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
hovering: false,
count: -1, // undefined
location: "unknown",
ipAddress: "unknown"
}
}
countToDiameter(count) {
if (count == -1) {
return 30;
}
return ((count * 20) + 30);
}
hovering(bool) {
this.setState({
hovering: bool
});
}
hoverProps = () => {
return {
onMouseEnter: () => this.hovering(true),
onMouseLeave: () => this.hovering(false)
}
}
loadConfiguration = () => {
const url = `${COLLECTOR_ENDPOINT}/devices/${this.props.device}`
fetch(url)
.then(response => {
// forcing to run into the catch block when HTTP status code errors
if (response.ok) {
return response.json()
}
throw new Error('Something went wrong ...')
})
.then(device => this.setState({ location: device.location, ipAddress: device.ipAddress }))
.catch(err => {
// something went wrong
console.log(`could not load configuration for device: ${this.props.device}, err: ${err}`)
})
}
updatePeopleCount = () => {
const url = `${COLLECTOR_ENDPOINT}/devices/${this.props.device}/average?period=30`
fetch(url)
.then(response => {
// forcing to run into the catch block when HTTP status code errors
if (response.ok) {
return response.json();
} else {
throw new Error('Something went wrong ...');
}
})
.then(data => this.setState({ count: data.peopleCount }))
.catch(error => {
// something went wrong
this.setState({ count: -1 })
})
}
componentDidMount() {
// load settings for device
this.loadConfiguration()
// regularly update counter
const self = this;
setInterval(function () {
self.updatePeopleCount()
}, 3 * 1000); // in milliseconds
}
render() {
const { x, y } = this.props;
const { location, ipAddress, count } = this.state;
// Pick the outer circle style depending on the PeopleCount value
let className = "Standby" // Defaults for -1 (not counting) and undefined (no capability, wrong address)
if (count > 3)
className = "High"
else if (count > 0)
className = "Low"
else if (count == 0)
className = "Empty"
const diameter = `${this.countToDiameter(count)}px`
let countLabel;
if (count === undefined)
countLabel = "N/A"; // configuration error
else if (count === -1)
countLabel == "not counting"
else countLabel = "" + Math.round(count + 0.49) + " (" + Math.round(count*100)/100 + ")"
return (
<div className="marker-container" style={{ top: y, left: x }}>
<div className="marker-inner">
<div className={"count-circle " + className}
style={{ width: diameter, height: diameter }} />
<div className="hover-circle"
{...this.hoverProps()} />
<div className="inner-circle" {...this.hoverProps()} />
{this.state.hovering &&
<div className="info-card"
style={{ position: "absolute" }}
{...this.hoverProps()}>
<div>
<span>Location</span>
<span>{location}</span>
</div>
<div>
<span>IP Address</span>
<span>{ipAddress}</span>
</div>
<div>
<span>People Count</span>
<span>{countLabel}</span>
</div>
</div>
}
</div>
</div>
)
}
}
//
// MapComponent
//
class MapComponent extends React.Component {
render() {
return (
<div className="map-container">
<img src="img/devnetcreate-map.png" />
<MarkerComponent device="Workbench1" x="819px" y="398px" />
<MarkerComponent device="Workbench2" x="662px" y="536px" />
<MarkerComponent device="Workbench3" x="954px" y="536px" />
<MarkerComponent device="Workbench4" x="1113px" y="280px" />
</div>
)
}
}
ReactDOM.render(
<MapComponent />
, document.querySelector("#container")
)