const useR_D_EnvironmentStore = defineStore(
    'r_d_environment',
    {
        state: () => ({
            sensorData: {
                "noise": [
                    {
                        "deviceId": "41210bb0-1591-11ee-b4df-a9653aef169c",
                        "data": "16",
                        "field": null,
                        "name": "噪声监测1",
                        "typeId": "noise",
                        "label": "noise-1",
                        "deptId": "113",
                        "type": "Noise_Reg",
                        "x": null,
                        "y": null,
                        "deptName": "4楼",
                        "status": false
                    }
                ],
                "Smoke": [
                    {
                        "deviceId": "14927870-969d-11ef-91f3-abd609c7e488",
                        "data": "0",
                        "field": null,
                        "name": "烟雾传感器10",
                        "typeId": "Smoke",
                        "label": "Smoke-10",
                        "deptId": "113",
                        "type": null,
                        "x": null,
                        "y": null,
                        "deptName": "4楼",
                        "status": false
                    }
                ],
                "dust": [
                    {
                        "deviceId": "25c78580-1594-11ee-b4df-a9653aef1699",
                        "data": "0",
                        "field": null,
                        "name": "粉尘监测传感器",
                        "typeId": "dust",
                        "label": "Dust_02",
                        "deptId": "113",
                        "type": "HIGH_PM25_Reg",
                        "x": null,
                        "y": null,
                        "deptName": "4楼",
                        "status": false
                    }, {
                        "deviceId": "25c78580-1594-11ee-b4df-a9653aef169c",
                        "data": "25",
                        "field": null,
                        "name": "粉尘监测传感器",
                        "typeId": "dust",
                        "label": "Dust_01",
                        "deptId": "114",
                        "type": "HIGH_PM10_Reg",
                        "x": null,
                        "y": null,
                        "deptName": "5楼",
                        "status": false
                    }
                ],
                "temp_humi": [
                    {
                        "deviceId": "1",
                        "humidity": "0",
                        "name": "温湿度监测1",
                        "label": "temp_humi_1",
                        "temp": "0",
                        "deptId": "113",
                        "deptName": "4楼",
                        "status": false
                    },
                    {
                        "deviceId": "1111",
                        "humidity": "0",
                        "name": "温湿度监测2",
                        "label": "temp_humi_2",
                        "temp": "0",
                        "deptId": "114",
                        "deptName": "5楼",
                        "status": false
                    }
                ],
                "TVOC_CH2O": [
                    {
                        "deviceId": "c65d80e0-158e-11ee-b4df-a9653aef1191",
                        "data": "0",
                        "field": null,
                        "name": "TVOC/甲醛监测传感器",
                        "typeId": "TVOC_CH2O",
                        "label": "TVOC_CH2O_01",
                        "deptId": "113",
                        "type": "CH2O",
                        "x": null,
                        "y": null,
                        "deptName": "4楼",
                        "status": false
                    },
                    {
                        "deviceId": "c65d80e0-158e-11ee-b4df-a9653aef169c",
                        "data": "0.41",
                        "field": null,
                        "name": "TVOC/甲醛监测传感器",
                        "typeId": "TVOC_CH2O",
                        "label": "TVOC_CH2O_02",
                        "deptId": "114",
                        "type": "TVOC",
                        "x": null,
                        "y": null,
                        "deptName": "5楼",
                        "status": false
                    }
                ]
            }
        }),
        actions: {},
        getters: {
            //获取四层，五层的平均温湿度
            getLayerData: (state) => {

                let layerData = {
                    four: {
                        title: '研发中心四楼',
                        temp: 0,
                        humi: 0
                    },
                    five: {
                        title: '研发中心五楼',
                        temp: 0,
                        humi: 0
                    },
                }
                let sensorData = state.sensorData

                if (sensorData['temp_humi'] && sensorData['temp_humi'].length > 0) {
                    let fourTemp = 0
                    let fourHumi = 0
                    let fourCount = 0
                    let fiveTemp = 0
                    let fiveHumi = 0
                    let fiveCount = 0
                    sensorData['temp_humi'].forEach(item => {
                        if (item.deptId === '113') {
                            fourTemp += +item.temp
                            fourHumi += +item.humidity
                            fourCount++
                        } else if (item.deptId === '114') {
                            fiveTemp += +item.temp
                            fiveHumi += +item.humidity
                            fiveCount++
                        }
                    })
                    layerData.four.temp = (fourTemp / fourCount).toFixed(1)
                    layerData.four.humi = (fourHumi / fourCount).toFixed(1)
                    layerData.five.temp = (fiveTemp / fiveCount).toFixed(1)
                    layerData.five.humi = (fiveHumi / fiveCount).toFixed(1)
                }
                return layerData
            },
            //获取四层，五层的噪声传感器数据
            getNoiseData: (state) => {
                let resNoiseData = {
                    four: {
                        public_noise: {},
                        office_noise: []
                    },
                    five: {
                        public_noise: {},
                        office_noise: []
                    }
                }
                let noiseData = state.sensorData['noise'] || []
                let fourNoiseData = noiseData.filter(item => item.deptId === '113')
                let fiveNoiseData = noiseData.filter(item => item.deptId === '114')
                resNoiseData.four.public_noise = fourNoiseData.find(item => item.name === '噪声监测1')
                resNoiseData.four.office_noise = fourNoiseData.filter(item => item.name !== '噪声监测1')

                resNoiseData.five.public_noise = fiveNoiseData.find(item => item.name === '噪声监测10')
                resNoiseData.five.office_noise = fiveNoiseData.filter(item => item.name !== '噪声监测10')
                return resNoiseData
            },

        }
    }
)


export default useR_D_EnvironmentStore