import React from 'react'; import { Container, Segment, Menu, List, Grid } from 'semantic-ui-react'; import {getRequest} from '../../../actions'; import DevList from './DevList'; import IPMIPage from './IPMIPage'; import DevModal from './Modals/DevModal'; import IPMIModal from './Modals/IPMIModal'; import IPMIInfoModal from './Modals/IPMIInfoModal'; const stateDefault = ()=>({ list: [], bmctl: [], devSel: { id: null, type: null, data: { snmp: [], ipmi: [] } }, devModal: { open: false, type: 0, data: {} }, ipmiModal: { open: false, type: 0, data: {} }, ipmiInfo: { open: false, ipmi: [], log: [], sensor: [], bmcdata: [], bmcgroup: [] } }) class ServerPage extends React.Component { state = {...stateDefault()} componentDidMount(){ this.getDevList(); this.getBMCtl(); } getDevList = () => { let {showDialog, toggleLoading} = this.props; toggleLoading(1); fetch('/api/server/getdevicelist', getRequest()) .then(response => response.json()) .then(json => { toggleLoading(0); if(json.status != 1) return showDialog(json.message); this.setState({ list: json.data.record || [] }) }) } getBMCtl = () => { fetch('/api/server/getbmctl', getRequest()) .then(response=>response.json()) .then(json => { if(json.status == 1) { this.setState({ bmctl: json.data.record || [] }) } }) } selectDevice = (id, refresh = false) => { if(!id) return ; let {showDialog, toggleLoading} = this.props; toggleLoading(1); fetch('/api/server/getdevice', getRequest({id})) .then(response=>response.json()) .then(json => { toggleLoading(0); if(json.status != 1) return showDialog(json.message); let ipmi = json.data.rt.ipmi || []; let snmp = json.data.rt.snmp || []; let jdata = { ...stateDefault().devSel, id, data: { ipmi, snmp } }; if(refresh) jdata.type = this.state.devSel.type; this.setState({ devSel: jdata }) }) } selectDataType = (type) => { this.setState({ devSel: { ...this.state.devSel, type: type } }) } genSubPage = () => { let {i18n, showDialog, toggleLoading, callConfirm} = this.props; switch(this.state.devSel.type){ case 'ipmi': return default: return null; } } openModal = (mtype, type = 0, data = {}) => { switch(mtype){ case 'device': this.setState({ devModal: { open: true, type, data } }) break; case 'ipmi': this.setState({ ipmiModal:{ open: true, type, data } }); break; case 'ipmiinfo': this.getIPMIData(data.id); break; default: return null; } } closeModal = (mtype, cb = null) => { switch(mtype){ case 'device': this.setState({devModal: {...stateDefault().devModal}}); break; case 'ipmi': this.setState({ipmiModal:{...stateDefault().ipmiModal}}); break; case 'ipmiinfo': this.setState({ipmiInfo: {...stateDefault().ipmiInfo}}); break; } if(cb && typeof cb == 'function') cb(); } submitModal = (mtype, type = 0, data = {}) => { if(!mtype) return ; let {i18n, showDialog, toggleLoading, callConfirm} = this.props; let url = ''; switch(mtype){ case 'device': if(type == 1 && !data.id) return showDialog('資料讀取錯誤'); if(!data.name) return showDialog('請填寫設備名稱'); url = type == 1 ? '/api/server/editdevice' : '/api/server/adddevice'; fetch(url, getRequest(data)) .then(response => response.json()) .then(json => { if(json.status != 1) return showDialog(json.message); this.closeModal(mtype, ()=>{this.getDevList()}); }); break; case 'ipmi': if(type == 1 && !data.id) return showDialog('資料讀取錯誤'); if(!data.ip) return showDialog('請填寫IP'); if(!data.account) return showDialog('請填寫帳號'); if(!data.password) return showDialog('請填寫密碼'); data.devuid = this.state.devSel.id; url = type == 1 ? '/api/server/editipmi' : '/api/server/addipmi'; fetch(url, getRequest(data)) .then(response=>response.json()) .then(json => { if(json.status != 1) return showDialog(json.message); this.closeModal(mtype, ()=>{ this.selectDevice(this.state.devSel.id, true); }) }) default : return null; } } delData = (mtype, id) => { if(!mtype || !id) return ; let {i18n, showDialog, toggleLoading, callConfirm} = this.props; switch(mtype) { case 'device': callConfirm('確定刪除此裝置?', ()=>{ fetch('/api/server/deldevice', getRequest({id})) .then(response=>response.json()) .then(json => { if(json.status != 1) return showDialog(json.message); this.getDevList(); }) }); break; case 'ipmi': callConfirm('確定刪除此筆設定?', ()=>{ fetch('/api/server/delipmi', getRequest({id})) .then(response=>response.json()) .then(json => { if(json.status != 1) return showDialog(json.message); this.selectDevice(this.state.devSel.id, true); }); }); break; } } getIPMIData = (id) => { if(!id) return ; let {i18n, showDialog, toggleLoading, callConfirm} = this.props; toggleLoading(1); fetch('/api/server/getipmiinfo', getRequest({id})) .then(response=>response.json()) .then(json => { toggleLoading(0); if(json.status != 1) return showDialog(json.message); let ipmi = json.data.record || []; let sensor = json.data.rt.sensor || []; let log = json.data.rt.log || []; let bmcgroup = json.data.rt.bmcgroup || []; let bmcdata = json.data.rt.bmcdata || []; this.setState({ ipmiInfo: { open: true, log, sensor, ipmi, bmcdata, bmcgroup } }) }) } runbmctl = (ctlid, ipmuid) => { if(!ctlid || !ipmuid) return ; let {i18n, showDialog, toggleLoading, callConfirm} = this.props; toggleLoading(1); fetch('/api/server/runbmctl', getRequest({ctlid, ipmuid})) .then(response=>response.json()) .then(json => { toggleLoading(0); if(json.status != 1) return showDialog(json.message); return showDialog('命令執行完成'); }) } render() { let {i18n, showDialog, toggleLoading, callConfirm} = this.props; return ( { this.state.devSel.id != null ? ( this.selectDataType('snmp')}/> this.selectDataType('ipmi')}/> ) : null } {this.genSubPage()} ) } } export default ServerPage;