from ssl import SSL_ERROR_SSL
import time
import threading
from flask import jsonify
from models import Orders
from config import MAX_TIME , CHARGE_MAX_NUM,SLOW_CHARGE_NUM, FAST_CHARGE_NUM, MAX_LEN
import json
orderList={}
nowTimeStr="2022-06-25 05:55:00"
timeArray = time.strptime(nowTimeStr, "%Y-%m-%d %H:%M:%S")
nowTime=int((time.mktime(timeArray)))
def sysTime():
global nowTime
while 1:
time.sleep(0.1)
nowTime=nowTime+1
class chargeSys():
def __init__(self):
self.slowChargeState=[]
self.fastChargeState=[]
self.fastcharge=[]
self.slowcharge=[]
self.fastWait=[]
self.slowWait=[]
self.threadTimeSlow=[]
self.threadTimeFast=[]
self.threadSlowFlag=[]
self.threadFastFlag=[]
self.slowfault=[]
self.fastfault=[] #故障队列
self.slowChargeLock=threading.RLock()
self.fastChargeLock=threading.RLock()
self.slowSumTime=[]
self.fastSumTime=[]
self.slowSumcount=[]
self.fastSumcount=[]
self.slowSumdegrees=[]
self.fastSumdegrees=[]
self.slowSumChargeCost=[]
self.fastSumChargeCost=[]
self.slowSumServeCost=[]
self.fastSumServeCost=[]
self.slowSumCost=[]
self.fastSumCost=[]
#self.slowfaultLock=threading.RLock()
#self.fastfaultLock=threading.RLock()
for i in range(SLOW_CHARGE_NUM):
self.slowChargeState.append('on')
self.slowcharge.append([])
self.threadTimeSlow.append(0)
self.threadSlowFlag.append(True)
self.slowSumTime.append(0)
self.slowSumcount.append(0)
self.slowSumdegrees.append(0)
self.slowSumChargeCost.append(0)
self.slowSumServeCost.append(0)
self.slowSumCost.append(0)
for i in range(FAST_CHARGE_NUM):
self.fastChargeState.append('on')
self.fastcharge.append([])
self.threadTimeFast.append(0)
self.threadFastFlag.append(True)
self.fastSumTime.append(0)
self.fastSumcount.append(0)
self.fastSumdegrees.append(0)
self.fastSumChargeCost.append(0)
self.fastSumServeCost.append(0)
self.fastSumCost.append(0)
self.thread=threading.Thread(target=sysTime)
self.thread.start()
def get_systTime(self):
time_local = time.localtime(nowTime)
timeStr = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
return timeStr
def line(self,mode,userID,degrees):
if mode=='slow':
self.slowChargeLock.acquire()
if len(self.slowWait)+len(self.fastWait)<MAX_LEN:
self.slowWait.append([userID,degrees])
self.slowChargeLock.release()
else:
self.slowChargeLock.release()
return 0
time_local = time.localtime(nowTime)
create_time = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
order=Orders(userID,mode,degrees,create_time)
Orders.insert_order(order)
orderList[userID]=order
elif mode=='fast':
self.fastChargeLock.acquire()
if len(self.slowWait)+len(self.fastWait)<MAX_LEN:
self.fastWait.append([userID,degrees])
self.fastChargeLock.release()
else:
self.fastChargeLock.release()
return 0
time_local = time.localtime(nowTime)
create_time = time.strftime("%Y-%m-%d %H:%M:%S",time_local)
order=Orders(userID,mode,degrees,create_time)
Orders.insert_order(order)
orderList[userID]=order
#else:
# return #error()############
self.dispatch(mode)
return 1
def is_enough(self,mode):
haveSpace=[]
if mode=='slow':
for i in range(SLOW_CHARGE_NUM):
if len(self.slowcharge[i])<CHARGE_MAX_NUM and self.slowChargeState[i]=='on':
haveSpace.append(i)
return haveSpace
elif mode=='fast':
for i in range(FAST_CHARGE_NUM):
if len(self.fastcharge[i])<CHARGE_MAX_NUM and self.fastChargeState[i]=='on':
haveSpace.append(i)
return haveSpace
def find_min_time(self,mode,spaceList):
(spaceList)
(self.fastcharge)
minTimeno=0
minTime=MAX_TIME
minTimeTem=0
if mode=='slow':
for i in spaceList:
minTimeTem=self.threadTimeSlow[i]
#for j in range(1,len(self.slowcharge[i])):
j=1
while j<len(self.slowcharge[i]):
minTimeTem=minTimeTem+self.slowcharge[i][j][1]/10*3600
j=j+1
if minTimeTem<minTime:
minTime=minTimeTem
minTimeno=i
else:
for i in spaceList:
minTimeTem=self.threadTimeFast[i]
#for j in range(1,len(self.fastcharge[i])):
j=1
while j<len(self.fastcharge[i]):
minTimeTem=minTimeTem+self.fastcharge[i][j][1]/30*3600
j=j+1
if minTimeTem<minTime:
minTime=minTimeTem
minTimeno=i
return minTimeno
def charging(self,mode,minTimeNo):
if mode=='slow':
while len(self.slowcharge[minTimeNo])>0:
#orderList[userID].output()
userID=self.slowcharge[minTimeNo][0][0]
orderList[userID].change_flag(1)
time_local = time.localtime(nowTime)
start_time=time.strftime("%Y-%m-%d %H:%M:%S",time_local)
orderList[userID].set_start_time(start_time)
self.threadSlowFlag[minTimeNo]=True
degrees=self.slowcharge[minTimeNo][0][1]
self.threadTimeSlow[minTimeNo]=degrees/10 *3600
#expectedend_time=nowTime+self.threadTimeSlow[minTimeNo]
#orderList[userID].set_end_time(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(expectedend_time)))
while self.threadSlowFlag[minTimeNo] and self.threadTimeSlow[minTimeNo]>0:
time.sleep(0.1)
self.threadTimeSlow[minTimeNo]=self.threadTimeSlow[minTimeNo]-1
time_local = time.localtime(nowTime)
nowtime=time.strftime("%Y-%m-%d %H:%M:%S",time_local)
current_degrees=degrees*(((degrees/10)*3600-self.threadTimeSlow[minTimeNo])/(degrees/10 *3600))
orderList[userID].set_current_degrees(current_degrees)
orderList[userID].set_cost(orderList[userID].start_time,nowtime)
orderList[userID].set_charge_time()
'''
if self.threadTimeSlow[minTimeNo]<=0:
time_local = time.localtime(nowTime)
end_time=time.strftime("%Y-%m-%d %H:%M:%S",time_local)
orderList[userID].set_end_time(end_time)
orderList[userID].set_cost()
else:
time_local = time.localtime(nowTime)
end_time=time.strftime("%Y-%m-%d %H:%M:%S",time_local)
orderList[userID].set_end_time(end_time)
orderList[userID].set_degrees(degrees*(((degrees/10)*3600-self.threadTimeSlow[minTimeNo])/(degrees/10 *3600)))
orderList[userID].set_cost()
'''
end_time=time.strftime("%Y-%m-%d %H:%M:%S",time_local)
orderList[userID].set_end_time(end_time)
orderList[userID].change_flag(2)
self.slowSumTime[minTimeNo]=self.slowSumTime[minTimeNo]+o