Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/owm report mirrored #109

Merged
merged 16 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 4 additions & 38 deletions src/components/InstrumentControls/Enclosure.vue
Original file line number Diff line number Diff line change
Expand Up @@ -379,17 +379,7 @@

<br>

<div style="margin-bottom: 1em;">
OWM Report
<pre>{{ owm_report }}</pre>
</div>

<div style="margin-bottom: 1em;">
<b-button @click="showOwmStatus">(alternate method) show OWM Status</b-button>
</div>
<b-modal v-model="owmModalVisible">
<pre>{{ owm_report }}</pre>
</b-modal>
<OWMReport />

<div
class="status-toggle-bar"
Expand All @@ -414,15 +404,16 @@ import { user_mixin } from '../../mixins/user_mixin'
import CommandButton from '@/components/FormElements/CommandButton'
import SimpleDeviceStatus from '@/components/status/SimpleDeviceStatus'
import StatusVal from '@/components/status/StatusVal'
import OWMReport from '@/components/status/OWMReport'
import { mapGetters } from 'vuex'
import axios from 'axios'
export default {
name: 'Enclosure',
mixins: [commands_mixin, user_mixin],
components: {
CommandButton,
StatusVal,
SimpleDeviceStatus
SimpleDeviceStatus,
OWMReport
},
data () {
return {
Expand Down Expand Up @@ -453,22 +444,8 @@ export default {
skyTempLimitDangerLevel: 90,
lowestAmbientTemp: '',
highestAmbientTemp: '',

owmModalVisible: false,
owm_report: '...loading...'
}
},

mounted () {
this.getOwmReport()
},
watch: {
'$route.params.sitecode' () {
this.owm_report = '...loading...'
this.getOwmReport()
}
},

methods: {
enclosure_force_roof_state (val) {
return this.wema_base_command('enclosure', 'force_roof_state', '',
Expand All @@ -477,17 +454,6 @@ export default {
}
)
},
getOwmReport () {
const endpoint = this.$store.state.api_endpoints.status_endpoint + '/' + this.wema_name + '/owm_report'
axios.get(endpoint).then(response => {
this.owm_report = JSON.parse(response.data.status.owm_report)
}).catch(() => {
this.owm_report = 'OWM report unavailable'
})
},
showOwmStatus () {
this.owmModalVisible = true
}
},

computed: {
Expand Down
11 changes: 9 additions & 2 deletions src/components/sitepages/SiteHome.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
</div>

<div style="height: 2em;" />

<OWMReport v-if="userIsAdmin" />
</div>
</template>

Expand All @@ -60,20 +62,25 @@ import { mapGetters } from 'vuex'
import { commands_mixin } from '../../mixins/commands_mixin'
import { user_mixin } from '../../mixins/user_mixin'
import SiteEventsModal from '@/components/SiteEventsModal'
import OWMReport from '@/components/status/OWMReport'

export default {
name: 'SiteHome',
props: ['sitecode'],
mixins: [commands_mixin, user_mixin],
components: {
SiteEventsModal
SiteEventsModal,
OWMReport
},
computed: {
...mapGetters('site_config', [
'site_latitude',
'site_longitude',
'site_name'
])
]),
userIsAdmin () {
return this.$store.state.user_data.userIsAdmin
}
}
}

Expand Down
55 changes: 55 additions & 0 deletions src/components/status/OWMReport.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<template>
<div>
<div style="margin-bottom: 1em;">
OWM Report
<pre>{{ owmReport }}</pre>
</div>

<div style="margin-bottom: 1em;">
<b-button @click="showOwmStatus">
(alternate method) show OWM Status
</b-button>
</div>
<b-modal v-model="owmModalVisible">
<pre>{{ owmReport }}</pre>
</b-modal>
</div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
name: 'OWMReport',
data () {
return {
owmModalVisible: false,
owmReport: '...loading...'
}
},
computed: {
...mapGetters('site_config', [
'wema_name'
])
},
mounted () {
this.getOwmReport()
},
watch: {
'$route.params.sitecode' () {
this.owmReport = '...loading...'
this.getOwmReport()
}
},
methods: {
getOwmReport () {
this.$store.dispatch('sitestatus/getLatestOwmReport').then((res) => {
this.owmReport = this.$store.getters['sitestatus/owmReport']
})
},
showOwmStatus () {
this.owmModalVisible = true
}
}
}
</script>
9 changes: 8 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ import user_interface from './modules/user_interface'
import uiSync from './modules/uiSync'

import UiSyncPlugin from './plugins/ui_sync'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

// Todo: research more into other state variables could benefit from a persisted state and seperate into a module
const dataState = createPersistedState({
paths: ['sitestatus.owmReport']
})

const store = new Vuex.Store({
plugins: [
UiSyncPlugin
UiSyncPlugin,
dataState
],
modules: {
site_config,
Expand Down
37 changes: 35 additions & 2 deletions src/store/modules/sitestatus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const state = {

weather: {},
enclosure: {},

owmReport: [],
forecast: [],

screen: {},
Expand All @@ -51,6 +51,12 @@ const getters = {

site: state => state.site,
now: state => state.now,
owmReport: (state, getters, rootState, rootGetters) => {
const wema_name = rootGetters['site_config/wema_name']
if (wema_name) {
return JSON.parse(state.owmReport.find(owmReport => owmReport.wema_name === wema_name).report)
}
},

/**
* Site operational status:
Expand Down Expand Up @@ -267,6 +273,14 @@ const mutations = {
state.obs_settings = status.obs_settings
},

new_owmReport (state, newOwmReport) {
// only keep the most up to date report per site
state.owmReport = state.owmReport.filter(function (reportObj) {
return reportObj.wema_name !== newOwmReport.wema_name
})
state.owmReport.push(newOwmReport)
},

status (state, status) {
state.status = status
const device_types = [
Expand Down Expand Up @@ -311,7 +325,6 @@ const mutations = {

state.forecast = []
}

}

const actions = {
Expand Down Expand Up @@ -403,6 +416,26 @@ const actions = {
}
},

getLatestOwmReport ({ commit, rootState, rootGetters, state }) {
const wema_name = rootGetters['site_config/wema_name']
if (wema_name) {
const owmReportObj = state.owmReport.find(owmReport => owmReport.wema_name === wema_name)
// request and store a new report if not cached or cached more than 1 hour ago
if (owmReportObj == undefined || (Date.now() - owmReportObj.timestamp) > (1000 * 60 * 60)) {
return new Promise((resolve, reject) => {
const url = rootState.api_endpoints.status_endpoint + `/${wema_name}/owm_report`
axios.get(url).then(response => {
commit('new_owmReport', { wema_name, report: response.data.status.owm_report, timestamp: Date.now() })
resolve()
}).catch(e => {
console.log(e)
reject(e)
})
})
}
}
},

// Reset to empty values. Used for sites without any status available.
clearStatus ({ commit }) {
// commit('status',empty_status)
Expand Down
Loading