-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
140 lines (112 loc) · 4.28 KB
/
App.tsx
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
import { useEffect, useState } from 'react';
import './App.css';
import getDispatcher from "waku-dispatcher"
import { DispatchMetadata, Dispatcher, Signer } from 'waku-dispatcher/dist';
import Geohash from "latlon-geohash"
import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet'
enum Units {
F = "F",
C = "C",
}
type TemperatureMsg = {
temperature: number
unit: Units
location: string
timestamp: Date
}
function App() {
const [records, setRecord] = useState<TemperatureMsg[]>([])
const [temp, setTemp] = useState<number>()
const [unit, setUnit] = useState<Units>(Units.C)
const [geo, setGeo] = useState<string>()
const [dispatcher, setDispatcher] = useState<Dispatcher>()
//const [info, setInfo] = useState<any>()
const send = async () => {
if (!dispatcher || !temp || !unit || !geo) return
const res = await dispatcher.emit("hello", {temperature: temp, unit: unit, timestamp: new Date(), location: geo} as TemperatureMsg)
console.log(res)
}
const getLocation = () =>{
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position: GeolocationPosition) => {
setGeo(Geohash.encode(position.coords.latitude, position.coords.longitude, 4))
});
}
}
useEffect(() => {
if (dispatcher) return;
(async () => {
const d = await getDispatcher(undefined, "/dispatcher-demo/1/example/json", "temperature", false, [ "/dns4/waku.bloxy.one/tcp/8000/wss/p2p/16Uiu2HAmMJy3oXGzRjt2iKmYoCnaEkj55rE55YperMpemtGs9Da2"])
if (d === null) return
setDispatcher(d)
console.log("Dispatched ready")
})()
}, [])
useEffect(() => {
if (!dispatcher) return
dispatcher.on("hello", (msg:TemperatureMsg, signer: Signer, meta: DispatchMetadata) => {
console.log("received")
setRecord((x) => [...x, msg])
})
console.log("Executing local query")
dispatcher.dispatchLocalQuery()
},[dispatcher])
/*useEffect(() => {
if (!dispatcher) return;
(async () => {
setInfo(await dispatcher.getConnectionInfo())
}
)()
}, [dispatcher])*/
return (
<div className="App">
<div>
<MapContainer style={{width: "600px", height: "400px", margin: "1rem auto", position: "relative"}} center={[47.0519926878143, 6.201351588162976]} zoom={4} >
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
{records.map((r) =>
<Marker position={[Geohash.decode(r.location).lat, Geohash.decode(r.location).lon]}>
<Popup>
{r.temperature} ˚{r.unit}
</Popup>
</Marker>
)}
</MapContainer>
</div>
<div>
<input type="text" onChange={(e) => setTemp(parseFloat(e.target.value))} />
<select onChange={(e) => setUnit(e.target.value as Units)}>
<option value={Units.C}>°{Units[Units.C]}</option>
<option value={Units.F}>°{Units[Units.F]}</option>
</select>
<div>
{geo &&<div>Geohash: #{geo}</div>}<div><button onClick={() => getLocation()}>{geo ? "Update" : "Get"} Location (~20km accuracy)</button></div>
</div>
<button disabled={!dispatcher || !temp || !unit || !geo} onClick={() => send()}>Send</button>
</div>
<div>
{records.map((r, i) => <div key={i.toString()}>{r.temperature} ˚{r.unit} ({r.location}, {r.timestamp.toLocaleString()})</div>)}
</div>
</div>
);
}
export default App;
/*<!-- <pre>
{JSON.stringify(info, undefined, 2)}
</pre>*/
/*
<MapContainer style={{width: "600px", height: "400px", margin: "1rem auto", position: "relative"}} center={[45.0519926878143, 6.201351588162976]} zoom={7} >
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
/>
{records.map((r) =>
<Marker position={[Geohash.decode(r.location).lat, Geohash.decode(r.location).lon]}>
<Popup>
{r.temperature} ˚{r.unit}
</Popup>
</Marker>
)}
</MapContainer>*/