def carLeave(self): #wx_user = carID_to_Wx[self.carID] openid = search_ucs_carID_to_openId(self.carID,'openId') # 获取openid if openid is None: self.raise_error() return #=====计算时间====== stop_Time = time_tool.get_Current_Time() sec = time_tool.time_subtract(self.start_time, stop_Time) # 获取持续时间 cost_money = self.cal_Cost(sec) #根据持续时间计算费用 #wx_user.balance = wx_user.balance - cost_money balance = search_from_user_db(openid,'balance') balance = balance - cost_money #================== #=====给wx发消息===== msg = {} msg['header'] = 'timer_stop' msg['cost'] = cost_money msg['last_time'] = sec msg['current_time'] = stop_Time msg['current_money'] = balance msg = json.dumps(msg) user_state = 0 # 订单完成 try: print(carID_to_Wx[self.carID].Data['userInfo']['nickName'],'还活着') carID_to_Wx[self.carID].sendMessage(msg.encode()) print('%d号车位已空...' % self.carID) except Exception as err: # ++++++++++++++++和用户的连接断开了++++++++++++++++ user_state = 3 # 订单完成但没发数据 print('和用户连接断开...发送订单完成信息失败') # ++++++++++++++++++存入数据库++++++++++++++++++++ #================== #=====注意,更新状态的时候,记得改变用户的状态->cur_state+++++++ #=====更新状态======= #-----order状态------ update_orders_data(openid,self.start_time,'cur_state',user_state) update_user_data(openid,'cur_state',user_state) print('cur_state更新成功为',user_state) update_orders_data(openid,self.start_time,'stop_time',stop_Time) update_orders_data(openid,self.start_time,'last_time',sec) update_orders_data(openid,self.start_time,'cost',cost_money) #------------------- a=update_user_data(openid, 'balance',balance) #更新余额 b=delete_ucs_data(openid) # 删除user to carspace 的映射关系 c=update_user_data(openid , 'start_time', '-') # 用户开始时间为'-',说明未使用 d=update_parking_data(self.carID,'start_time','-') # 车位开始时间变为'-' e=update_parking_data(self.carID,'openning',False) # 关闭可用状态 f=update_parking_data(self.carID,'occupied',False) # 车位变为空
def carCome(self): if self.openning : openid = search_ucs_carID_to_openId(self.carID,'openId') if openid is None: self.raise_error() return #========开始计时======= self.start_time = time_tool.get_Current_Time() #====================== #========给wx发消息====== msg = {} msg['header'] = 'timer_start' msg['time'] = self.start_time msg = json.dumps(msg) try:
def carCome(self): if self.openning: openid = search_ucs_carID_to_openId(self.carID, 'openId') if openid is None: self.raise_error() return #========开始计时======= self.start_time = time_tool.get_Current_Time() #====================== #========给wx发消息====== msg = {} msg['header'] = 'timer_start' msg['time'] = self.start_time msg = json.dumps(msg) try: print(carID_to_Wx[self.carID].Data, '还活着') carID_to_Wx[self.carID].sendMessage(msg.encode()) carID_to_Wx[self.carID].cur_state = 2 print('%d号车位被占...' % self.carID) except Exception as err: print(err) print('和wx用户连接断开...') #+++++++++和微信的连接断开了+++++++++++ pass #+++++++++++++++++++++++++++++++++++ #======================= # 注意。 记得更新用户的状态信息-> cur_state++++++++ #=======更新状态======== order_data = self.get_order_json(openid) # 获取一个order_json数据 a = insert_orders_data(order_data) # 在订单中插入一条 print(order_data) print(a) update_user_data(openid, 'cur_state', 2) # 正在停车状态 update_parking_data(self.carID, 'start_time', self.start_time) # 更新车位数据库的开始时间 update_user_data(openid, 'start_time', self.start_time) # 更新用户计时的开始时间 update_parking_data(self.carID, 'occupied', True) # 车位被占 #====================== else: print('有人非法占领车位!!!!!!!!!!!!!!!!!!!')
def carLeave(self): #wx_user = carID_to_Wx[self.carID] openid = search_ucs_carID_to_openId(self.carID,'openId') # 获取openid if openid is None: self.raise_error() return #=====计算时间====== stop_Time = time_tool.get_Current_Time() sec = time_tool.time_subtract(self.start_time, stop_Time) # 获取持续时间 cost_money = self.cal_Cost(sec) #根据持续时间计算费用 #wx_user.balance = wx_user.balance - cost_money balance = search_from_user_db(openid,'balance') balance = balance - cost_money #================== #=====给wx发消息===== msg = {} msg['header'] = 'timer_stop' msg['cost'] = cost_money msg['last_time'] = sec msg['current_time'] = stop_Time
def onMessage(self,data,isBinary): #send back what the server received #self.sendMessage(data,isBinary) if not isBinary : # isBinary==False <=> bytes string data = data.decode('utf-8') # '$$'是结束标识符 if ( data=='$$' ): self.transport.loseConnection() #=============接收用户信息数据(建立连接)——res_Info和res_state============ if not self.start_operator : try: data = json.loads(data) # 接收到的是一个json字符串 #是res_state if (tuple(data.keys())[0] == 'code' or tuple(data.keys())[1]=='code'): self.res_state = data #使用它来获取session_key param['js_code'] = self.res_state['code'] res = requests.post(url,data=param) try: text = json.loads(res.text) self.session_key = text['session_key'] self.openid = text['openid'] except Exception as err: print('error2.................') print(err) #是res_Info else: self.res_info = data #=================数据接收完毕=========================== #received finished if (self.res_state != '' and self.res_info !=''): self.start_operator = True # 下次开始就是操作了 self.user_info_received() # 调用 当接收完数据时需要触发的函数 except Exception as err: print('error3.................') print(err) else: # 当已经接收完毕的时候,执行wx发来的请求操作 operation, value = data.split(':') print(operation,value) # debug print=>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>操作和值 # 操作一:================充值操作=========================== if operation == 'addMoney': value = float(value) self.balance = self.balance + value print(self.balance) #=>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>剩余金额 if update_user_data(self.openid,'balance',self.balance): print('充值成功!') else: print('充值失败!') msg = {} msg['balance'] = self.balance msg['header'] = 'balance' #发送给微信一个Json字符串,表示充值信息 self.sendMessage(json.dumps(msg).encode()) # 操作二:=============预订车位操作============================ elif operation == 'carID': value = int(value) if self.balance >= 0 : if eval(search_from_ps_db(value,'occupied')) == True: # 已经被占 # 看看是不是这个用户占的 if value == search_ucs_openId_to_carID(self.openid,'carID'): #是这个用户占的 self.carID = value else: #不是这个用户占的 self.carID = -1 else: #-------没有被占, 说明可以预订!----------- self.carID = value #-------是否存在僵尸用户(扫了码不停车)------ pre_openid = search_ucs_carID_to_openId(self.carID,'openId') if pre_openid : # 存在 self.delete_ghost_user() # 删除僵尸用户 # 微信端避免了同一用户多次扫码情况... #---------空车位被占,数据更新操作------------ self.cur_state = 1 # 状态更新为1 => 下了单但没停车 else:# 余额不足 self.carID = -2 #****建立映射关系***** if self.carID>0: carID_to_Wx[self.carID] = self #******************* #===============给wx发送carID================= msgs = {} msgs['carID'] = self.carID msgs['header'] = 'carID' self.sendMessage(json.dumps(msgs).encode()) # 预订一个车位 self.reserve_parking_space() # 预订一个车位 # 操作三:================询问当前时间======================= elif operation == 'askTime': start_t = search_from_user_db(self.openid,'start_time') # 不是'-'并且不是None if start_t != '-' and start_t : last_t = time_tool.time_subtract(start_t, time_tool.get_Current_Time() ) msg = {} msg['header'] = 'last_time' msg['last_time'] = last_t msg = json.dumps(msg) self.sendMessage(msg.encode()) print('last_time信息发送成功...') # 操作四:================历史订单======================== elif operation == 'askOrder': result = get_history_orders(self.openid) if result: res = {} res['header'] = 'his_order' res['len'] = len(result) order_list = ['start_time','stop_time','cost','last_time'] cnt = 1 for e in result: #print('e:',e) #print(type(e)) #print('order:',order_list) assert len(e)==len(order_list) elem = {} for k,v in zip(order_list,e): elem[k] = v res[cnt] = elem cnt = cnt + 1 print(res) res = json.dumps(res) self.sendMessage(res.encode()) print('历史订单发送成功!') else: print('没有用户信息')
if self.balance >= 0 : >>>>>>> 4af6067e442ce1217930abd63a62a1d757df1851 if eval(search_from_ps_db(value,'occupied')) == True: # 已经被占 # 看看是不是这个用户占的 if value == search_ucs_openId_to_carID(self.openid,'carID'): #是这个用户占的 self.carID = value else: #不是这个用户占的 self.carID = -1 else: #-------没有被占, 说明可以预订!----------- self.carID = value #-------是否存在僵尸用户(扫了码不停车)------ pre_openid = search_ucs_carID_to_openId(self.carID,'openId') if pre_openid : # 存在 self.delete_ghost_user() # 删除僵尸用户 # 微信端避免了同一用户多次扫码情况... #---------空车位被占,数据更新操作------------ self.cur_state = 1 # 状态更新为1 => 下了单但没停车 else:# 余额不足 self.carID = -2 #****建立映射关系***** if self.carID>0: carID_to_Wx[self.carID] = self #*******************