Skip to content

Commit dd1e05b

Browse files
authored
Merge pull request #109 from LCOGT/feature/owm-report-mirrored
Feature/owm report mirrored
2 parents 12c7ae8 + 3c0bbd3 commit dd1e05b

File tree

7 files changed

+137
-65
lines changed

7 files changed

+137
-65
lines changed

package-lock.json

Lines changed: 26 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"vue-material-design-icons": "^4.6.0",
7272
"vue-router": "^3.2.0",
7373
"vuex": "^3.4.0",
74-
"vuex-persistedstate": "^2.7.1",
74+
"vuex-persistedstate": "^4.1.0",
7575
"ws": "^7.3.0"
7676
},
7777
"devDependencies": {

src/components/InstrumentControls/Enclosure.vue

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -379,17 +379,7 @@
379379

380380
<br>
381381

382-
<div style="margin-bottom: 1em;">
383-
OWM Report
384-
<pre>{{ owm_report }}</pre>
385-
</div>
386-
387-
<div style="margin-bottom: 1em;">
388-
<b-button @click="showOwmStatus">(alternate method) show OWM Status</b-button>
389-
</div>
390-
<b-modal v-model="owmModalVisible">
391-
<pre>{{ owm_report }}</pre>
392-
</b-modal>
382+
<OWMReport />
393383

394384
<div
395385
class="status-toggle-bar"
@@ -414,15 +404,16 @@ import { user_mixin } from '../../mixins/user_mixin'
414404
import CommandButton from '@/components/FormElements/CommandButton'
415405
import SimpleDeviceStatus from '@/components/status/SimpleDeviceStatus'
416406
import StatusVal from '@/components/status/StatusVal'
407+
import OWMReport from '@/components/status/OWMReport'
417408
import { mapGetters } from 'vuex'
418-
import axios from 'axios'
419409
export default {
420410
name: 'Enclosure',
421411
mixins: [commands_mixin, user_mixin],
422412
components: {
423413
CommandButton,
424414
StatusVal,
425-
SimpleDeviceStatus
415+
SimpleDeviceStatus,
416+
OWMReport
426417
},
427418
data () {
428419
return {
@@ -453,22 +444,8 @@ export default {
453444
skyTempLimitDangerLevel: 90,
454445
lowestAmbientTemp: '',
455446
highestAmbientTemp: '',
456-
457-
owmModalVisible: false,
458-
owm_report: '...loading...'
459-
}
460-
},
461-
462-
mounted () {
463-
this.getOwmReport()
464-
},
465-
watch: {
466-
'$route.params.sitecode' () {
467-
this.owm_report = '...loading...'
468-
this.getOwmReport()
469447
}
470448
},
471-
472449
methods: {
473450
enclosure_force_roof_state (val) {
474451
return this.wema_base_command('enclosure', 'force_roof_state', '',
@@ -477,17 +454,6 @@ export default {
477454
}
478455
)
479456
},
480-
getOwmReport () {
481-
const endpoint = this.$store.state.api_endpoints.status_endpoint + '/' + this.wema_name + '/owm_report'
482-
axios.get(endpoint).then(response => {
483-
this.owm_report = JSON.parse(response.data.status.owm_report)
484-
}).catch(() => {
485-
this.owm_report = 'OWM report unavailable'
486-
})
487-
},
488-
showOwmStatus () {
489-
this.owmModalVisible = true
490-
}
491457
},
492458
493459
computed: {

src/components/sitepages/SiteHome.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
</div>
5353

5454
<div style="height: 2em;" />
55+
56+
<OWMReport v-if="userIsAdmin" />
5557
</div>
5658
</template>
5759

@@ -60,20 +62,25 @@ import { mapGetters } from 'vuex'
6062
import { commands_mixin } from '../../mixins/commands_mixin'
6163
import { user_mixin } from '../../mixins/user_mixin'
6264
import SiteEventsModal from '@/components/SiteEventsModal'
65+
import OWMReport from '@/components/status/OWMReport'
6366
6467
export default {
6568
name: 'SiteHome',
6669
props: ['sitecode'],
6770
mixins: [commands_mixin, user_mixin],
6871
components: {
69-
SiteEventsModal
72+
SiteEventsModal,
73+
OWMReport
7074
},
7175
computed: {
7276
...mapGetters('site_config', [
7377
'site_latitude',
7478
'site_longitude',
7579
'site_name'
76-
])
80+
]),
81+
userIsAdmin () {
82+
return this.$store.state.user_data.userIsAdmin
83+
}
7784
}
7885
}
7986
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<template>
2+
<div>
3+
<div style="margin-bottom: 1em;">
4+
OWM Report
5+
<pre>{{ owmReport }}</pre>
6+
</div>
7+
8+
<div style="margin-bottom: 1em;">
9+
<b-button @click="showOwmStatus">
10+
(alternate method) show OWM Status
11+
</b-button>
12+
</div>
13+
<b-modal v-model="owmModalVisible">
14+
<pre>{{ owmReport }}</pre>
15+
</b-modal>
16+
</div>
17+
</template>
18+
19+
<script>
20+
import { mapGetters } from 'vuex'
21+
22+
export default {
23+
name: 'OWMReport',
24+
data () {
25+
return {
26+
owmModalVisible: false,
27+
owmReport: '...loading...'
28+
}
29+
},
30+
computed: {
31+
...mapGetters('site_config', [
32+
'wema_name'
33+
])
34+
},
35+
mounted () {
36+
this.getOwmReport()
37+
},
38+
watch: {
39+
'$route.params.sitecode' () {
40+
this.owmReport = '...loading...'
41+
this.getOwmReport()
42+
}
43+
},
44+
methods: {
45+
getOwmReport () {
46+
this.$store.dispatch('sitestatus/getLatestOwmReport').then((res) => {
47+
this.owmReport = this.$store.getters['sitestatus/owmReport']
48+
})
49+
},
50+
showOwmStatus () {
51+
this.owmModalVisible = true
52+
}
53+
}
54+
}
55+
</script>

src/store/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@ import user_interface from './modules/user_interface'
1616
import uiSync from './modules/uiSync'
1717

1818
import UiSyncPlugin from './plugins/ui_sync'
19+
import createPersistedState from 'vuex-persistedstate'
1920

2021
Vue.use(Vuex)
2122

23+
/**
24+
* Todo: research more into other state variables could benefit from a persisted
25+
* state and seperate into a module persisting this state to save api calls when page refreshes
26+
*/
27+
const dataState = createPersistedState({
28+
paths: ['sitestatus.siteOwmReports']
29+
})
30+
2231
const store = new Vuex.Store({
2332
plugins: [
24-
UiSyncPlugin
33+
UiSyncPlugin,
34+
dataState
2535
],
2636
modules: {
2737
site_config,

src/store/modules/sitestatus/index.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import selector_getters from './getters/selector_getters'
1717
import wema_settings_getters from './getters/wema_settings_getters'
1818
import obs_settings_getters from './getters/obs_settings_getters'
1919
import accumulated_getters from './getters/accumulated_getters'
20+
import moment from 'moment'
2021

2122
const hasKey = (obj, key) => { return Object.keys(obj).includes(key) }
2223

@@ -30,7 +31,7 @@ const state = {
3031

3132
weather: {},
3233
enclosure: {},
33-
34+
siteOwmReports: {},
3435
forecast: [],
3536

3637
screen: {},
@@ -51,6 +52,12 @@ const getters = {
5152

5253
site: state => state.site,
5354
now: state => state.now,
55+
owmReport: (state, getters, rootState, rootGetters) => {
56+
const wema_name = rootGetters['site_config/wema_name']
57+
if (wema_name) {
58+
return JSON.parse(state.siteOwmReports[wema_name].report)
59+
}
60+
},
5461

5562
/**
5663
* Site operational status:
@@ -267,6 +274,10 @@ const mutations = {
267274
state.obs_settings = status.obs_settings
268275
},
269276

277+
storeNewOwmReport (state, { wema_name, newReport, newTimestamp }) {
278+
state.siteOwmReports[wema_name] = { report: newReport, timestamp: newTimestamp }
279+
},
280+
270281
status (state, status) {
271282
state.status = status
272283
const device_types = [
@@ -311,7 +322,6 @@ const mutations = {
311322

312323
state.forecast = []
313324
}
314-
315325
}
316326

317327
const actions = {
@@ -403,6 +413,25 @@ const actions = {
403413
}
404414
},
405415

416+
getLatestOwmReport ({ commit, rootState, rootGetters, state }) {
417+
const wema_name = rootGetters['site_config/wema_name']
418+
if (wema_name) {
419+
// request and store a new report if not cached or cached more than 1 hour ago
420+
if (!(wema_name in state.siteOwmReports) || moment(state.siteOwmReports[wema_name].timestamp).isBefore(moment().subtract(1, 'hours'))) {
421+
return new Promise((resolve, reject) => {
422+
const url = rootState.api_endpoints.status_endpoint + `/${wema_name}/owm_report`
423+
axios.get(url).then(response => {
424+
commit('storeNewOwmReport', { wema_name, newReport: response.data.status.owm_report, newTimestamp: moment() })
425+
resolve()
426+
}).catch(e => {
427+
console.log(e)
428+
reject(e)
429+
})
430+
})
431+
}
432+
}
433+
},
434+
406435
// Reset to empty values. Used for sites without any status available.
407436
clearStatus ({ commit }) {
408437
// commit('status',empty_status)

0 commit comments

Comments
 (0)