import storage from './storage'
import * as https from './http'
import ws from './websocket'
import * as utils from './utils'

export default{
	swapConfirmDTO:null,
	swapInfoDTO:null,
	swapStepList:[],
	InitWs(){
		if(storage.get("exchangeNo")){
			let msg = {
				type: "subscribeExchange",
				payLoad: {
					exchangeNo: storage.get("exchangeNo")
				}
			}
			if(ws.socketTask ==null){
				ws.connect()
				ws.onSocketOpen=()=>{
					ws.onSocketMsg=(res)=>{
						let data=JSON.parse(res.data)
						console.log(data)
						if(data.type === 'subscribe'){
							console.log(data.state)
						}else if(data.type === 'swapInfo' && data.state === 0){
							this.swapConfirmDTO=data.swapConfirmDTO
							this.swapStepList=data.swapStepList
							this.swapInfoDTO=data.swapInfoDTO
						}else if(data.type === 'start' && data.state === 0 ){
							utils.msg('启动换电成功')
						}
					}
					ws.sendmessage(msg)
				}
			}	
		}else{
			utils.msg('未获取到换电编号')
		}
	},
}