-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.js
95 lines (72 loc) · 2.81 KB
/
fetch.js
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
/**
* gas per ccf is about $0.96
*
*
* nice to have:
* map zooming when a house if found
* loading progress indicator for neighborhood data
*/
var ALL_SENSORS_URL = 'https://cors-anywhere.herokuapp.com/https://bmon.analysisnorth.com/api/v1/sensors/'
var SENSOR_DATA_URL = 'https://cors-anywhere.herokuapp.com/https://bmon.analysisnorth.com/api/v1/readings/'
var AGE_LIMIT = 7 // record age in days
function buildSensorUrl(sensor_name, averaging) {
let sd = new Date()
sd.setDate(sd.getDate() - 7)
let start_date_string = sd.toISOString().split('T')[0]
return `${SENSOR_DATA_URL}${sensor_name}?averaging=${averaging}&start_ts=${start_date_string}`
}
// gets a list of all sensors
async function getAllSensors() {
if (window.ALL_SENSORS_CACHE) {
return Promise.resolve(ALL_SENSORS_CACHE.data.sensors)
}
let response = await fetch(ALL_SENSORS_URL)
let json = await response.json()
return json.data.sensors
}
// takes the number from the gas meter
async function getSensorNameForId(sensor_id) {
let sensors = await getAllSensors()
let sensor = sensors.find(sensor => {
return sensor.sensor_id.endsWith('_' + sensor_id)
})
return sensor.sensor_id
}
// takes the numeric id
async function getDataForSensorId(sensor_id) {
let sensor_name = await getSensorNameForId(sensor_id)
let response = await fetch(buildSensorUrl(sensor_name, '1D'))
let json = await response.json()
return json.data
}
// takes the full name (like "joehouse_2345")
async function getDataForSensorName(sensor_name) {
let response = await fetch(buildSensorUrl(sensor_name, '1D'))
let json = await response.json()
return json.data
}
// takes the full name (like "joehouse_2345")
async function getDailyDataForSensorId(sensor_id) {
let sensor_name = await getSensorNameForId(sensor_id)
let sd = new Date()
sd.setDate(sd.getDate() - 2)
let start_date_string = sd.toISOString().split('T')[0]
let url = `${SENSOR_DATA_URL}${sensor_name}?&start_ts=${start_date_string}`
let response = await fetch(url)
let json = await response.json()
return json.data
}
// takes the number from the gas meter, returns all data for the neighborhood
async function getDataForNeighborhood(sensor_id) {
let all_sensors = await getAllSensors()
let sensor_name = all_sensors
.find(sensor => {
return sensor.sensor_id && sensor.sensor_id.endsWith('_' + sensor_id)
})
.sensor_id
let sensor_prefix = sensor_name.split('_' + sensor_id)[0]
let regex = new RegExp(`${sensor_prefix}_\\d\\d\\d\\d\\d\\d\\d\\d$`)
let neighborhood_sensors = all_sensors.filter(sensor => regex.test(sensor.sensor_id))
let promise_array = neighborhood_sensors.map(sensor => getDataForSensorName(sensor.sensor_id))
return Promise.all(promise_array)
}