rabin 7 years ago
parent
commit
5093b3464b

+ 47 - 13
cron.py

@@ -39,9 +39,8 @@ def device():
 def control():
 	while 1:
 		model = Demeter.model('device_info')
-		type_model = Demeter.model('device_type')
 		cur = Demeter.time() - 10
-		model.cdate.assgin(cur, '<=')
+		model.operdate.assgin(cur, '<=')
 		model.oper = True
 		model.hardware_type = 3
 		service = Demeter.service('device')
@@ -51,27 +50,62 @@ def control():
 				model.id = v['id']
 				if v['exp'] == '-1':
 					msg = service.notice('timeout')
-					service.mul(v['id'], v['type_id'], v['name'], v['cxnum'], '失败:' + msg, False)
-					model.update(exp=msg)
+					service.mul(v, v['name'] + '失败:' + msg, False)
+					model.update(exp=msg, operdate=Demeter.time())
 				else:
 					msg = '1'
-					service.mul(v['id'], v['type_id'], v['name'], v['cxnum'], '', True)
-					model.update(exp=msg, oper=False)
+					service.mul(v, '', True)
+					model.update(exp=msg, oper=False, operdate=Demeter.time())
 
-		gevent.sleep(timeSleep)
+		gevent.sleep(1)
 		
 
-# 批量控制的队列
+# 批量控制的队列 这块有点乱,但时间太紧,只有两天时间,以后改造优化
 def mulQueue():
 	while 1:
+		mul_model = Demeter.model('device_mul')
+		device_model = Demeter.model('device_info')
 		model = Demeter.model('device_mul_queue')
+
 		model.status = 1
 		data = model.select(type='fetchone', order='device_id asc')
 		service = Demeter.service('device')
-		if data and not data['oper']:
+
+		# 查看当前有没有正在执行的设备,如果有,不能继续执行了
+		device_model.oper = True
+		device_model.hardware_type = 3
+		device = device_model.select(type='fetchone')
+
+		if not device and data and not data['operstate']:
+			service.switch(data['device_id'], switch=data['value'], mul=False, queue=data['id'])
 			model.id = data['id']
-			service.switch(data['device_id'], switch=data['value'])
-			model.update(oper=True)
+			model.update(operstate=True)
+			#mul_model.id = data['mul_id']
+			#mul_model.update(oper='')
+
+		# 将执行完成的进行清理
+		mul_model.oper.nq('')
+		data = mul_model.select()
+		if data:
+			for v in data:
+				devicesNum = len(tuple(eval(v['devices'])))
+				model.status.ins((2,3))
+				cur = Demeter.time() - 10
+				model.operdate.assgin(cur, '<=')
+				queue = model.select()
+				num = len(queue)
+				if num == devicesNum:
+					errorNum = 0
+					model.mul_id = v['id']
+					model.update(status=4)
+					for qv in queue:
+						if qv['status'] == 3:
+							errorNum = errorNum + 1
+					mul_model.id = v['id']
+					if errorNum == devicesNum:
+						mul_model.update(oper='', value=v['old'])
+					else:
+						mul_model.update(oper='')
 
 		gevent.sleep(1)
 
@@ -80,9 +114,9 @@ def mulQueueDrop():
 	while 1:
 		num = 3600*24
 		model = Demeter.model('device_mul_queue')
-		model.status.ins((2,3))
+		model.status = 4
 		cur = Demeter.time() - num
-		model.cdate.assgin(cur, '<=')
+		model.operdate.assgin(cur, '<=')
 		model.delete()
 
 		gevent.sleep(36000)

+ 10 - 5
front/page/device.py

@@ -25,12 +25,17 @@ class mul_path(Load):
 	def get(self):
 		self.data['list'] = self.service('common').list('device_mul', search={'farm_id':self.data['setting']['farm']})
 		if self.data['list']:
+			model = Demeter.model('device_mul_queue')
 			for key, value in enumerate(self.data['list']):
-				model = Demeter.model('device_mul_queue')
-				model.status = 1
-				info = model.select(type='fetchone')
-				if not info:
-					self.data['list'][key]['oper'] = ''
+				model.status.ins((2,3))
+				model.mul_id = value['id']
+				info = model.select()
+				#self.data['list'][key]['oper'] = ''
+				if info:
+					for v in info:
+						if v['oper']:
+							self.data['list'][key]['oper'] = v['oper']
+					
 		self.view("device/mul.html")
 
 	@Web.auth

+ 2 - 2
front/templates/mobile/device/mul.html

@@ -47,7 +47,7 @@
         {%set layer = value['oper'] %}
         {% end %}
 
-        {% if '成功' in layer or '批量' in layer  %}
+        {% if '成功' in layer or '批量' in layer or '正在' in layer %}
         {%set hint = 'hint--info' %}
         {% else %}
         {%set hint = 'hint--error' %}
@@ -55,7 +55,7 @@
 
         {% if value['device_type'] == 1 %}
         <li class="form-input">
-	        <label><span>{{value['name']}}</span><p><a {% if layer %}data-hint="{{layer}}"{% end %} href="javascript:;" data-status="{% if value['value'] == '1' %}2{% else %}1{% end %}"  onclick="Farm.Switch(this, '/device/mul', '{{value['id']}}')" class="{% if layer %}hint--left hint--info hint--always loading{% end %} btn btn-switch {% if value['value'] == '1' %}open{% end %}"><i></i></a></p></label>
+	        <label><span>{{value['name']}}</span><p><a {% if layer %}data-hint="{{layer}}"{% end %} href="javascript:;" data-status="{% if value['value'] == '1' %}2{% else %}1{% end %}"  onclick="Farm.Switch(this, '/device/mul', '{{value['id']}}')" class="{% if layer %}hint--left {{hint}} hint--always loading{% end %} btn btn-switch {% if value['value'] == '1' %}open{% end %}"><i></i></a></p></label>
 	    </li>
         {% else %}
         <li class="form-input buttons">

+ 1 - 1
front/templates/pc/device/mul.html

@@ -40,7 +40,7 @@
             {%set layer = value['oper'] %}
             {% end %}
 
-            {% if '成功' in layer or '批量' in layer  %}
+            {% if '成功' in layer or '批量' in layer or '正在' in layer %}
             {%set hint = 'hint--info' %}
             {% else %}
             {%set hint = 'hint--error' %}

+ 2 - 0
model/device_info.py

@@ -30,7 +30,9 @@ class Device_info(Model):
 	udate = Fields(type='int', default='time', comment='接入时间')
 	inorder = Fields(type='int', default='0', comment='排序', match='number|排序必须是数字')
 	oper = Fields(type='boolean', default='False', comment='操作类型')
+	operdate = Fields(type='int', comment='操作时间')
 	##ALTER TABLE demeter_device_info ADD exp varchar(500); 
 	#ALTER TABLE demeter_device_info ADD inorder int default 100; 
 	#ALTER TABLE demeter_device_info ADD oper boolean; 
+	#ALTER TABLE demeter_device_info ADD operdate int; 
 	#ALTER TABLE demeter_device_info DROP inorder;   

+ 3 - 1
model/device_mul_queue.py

@@ -16,6 +16,8 @@ class Device_mul_queue(Model):
 	device_id = Fields(type='uuid', comment='设备id')
 	value = Fields(type='int', comment='当前的值')
 	status = Fields(type='int', default='1', comment='数据执行状态,1为队列中,2为执行成功,3执行失败')
-	oper = Fields(type='boolean', default='False', comment='数据执行状态,True为已执行,False为未执行')
+	operstate = Fields(type='boolean', default='False', comment='数据执行状态,True为已执行,False为未执行')
+	operdate = Fields(type='int', comment='操作时间')
+	oper = Fields(type='varchar(500)', comment='操作说明')
 	state = Fields(type='boolean', default='True', comment='数据存在状态')
 	cdate = Fields(type='int', default='time', comment='创建时间')

+ 2 - 2
pub.py

@@ -13,7 +13,6 @@ import time
 import base64
 
 
-"""
 pub = Pub()
 key = 'status/valve/10086/100107'
 value = 'ok'
@@ -25,6 +24,7 @@ value = 'error'
 state = pub.push(key, value)
 
 print state
+"""
 def callback(param, client, userdata, mid):
 	print mid
 
@@ -90,7 +90,7 @@ def pic():
 			print k
 			i = i+1
 
-push()
+#push()
 #pic()
 
 def tcp_sensor():

+ 46 - 54
service/device.py

@@ -51,7 +51,7 @@ class Device(object):
 				self.switch(v, switch)
 
 	# 处理开关-向设备发送请求
-	def switch(self, device_id, switch=1, mul=False):
+	def switch(self, device_id, switch=1, mul=False, queue=False):
 		model = Demeter.model('device_info')
 		model.id = device_id
 		info = model.select(type='fetchone')
@@ -64,10 +64,18 @@ class Device(object):
 			queue_model.value = switch
 			queue_model.insert()
 			return
+		
 		if info and info['status'] == True:
+			"""
+			if queue:
+				oper = self.msg(info['type_id'], switch)
+				queue_model = Demeter.model('device_mul_queue')
+				queue_model.id = queue
+				queue_model.update(oper='正在' + oper + info['name'])
+			"""
 			# 设置为loading状态,cxnum保存当前的值
 			model.id = device_id
-			model.update(exp='-1', cxnum=switch, oper=True)
+			model.update(exp='-1', cxnum=switch, oper=True, operdate=Demeter.time())
 
 			model = Demeter.model('device_gateway')
 			model.id = info['gateway_id']
@@ -94,8 +102,8 @@ class Device(object):
 		else:
 			msg = self.notice('offline')
 			model.id = device_id
-			model.update(exp=msg, cxnum=switch)
-			self.mul(info['id'], info['type_id'], info['name'], switch, '失败:' + msg, False)
+			model.update(exp=msg, cxnum=switch, oper=True, operdate=Demeter.time())
+			self.mul(info, info['name'] + '失败:' + msg, False)
 			#Demeter.error('offline')
 
 	# 处理开关-响应
@@ -110,19 +118,19 @@ class Device(object):
 			if msg == 'ok':
 				model = Demeter.model('device_info')
 				model.id = info['id']
-				model.update(value=param['switch'], exp='1', oper=False)
+				model.update(value=param['switch'], exp='1', oper=False, operdate=Demeter.time())
 
 				# 如果有批量控制
-				self.mul(info['id'], info['type_id'], info['name'], param['switch'], '成功', True)
+				self.mul(info, info['name'] + '成功', True)
 			else:
 				msg = self.notice(msg)
 				model = Demeter.model('device_info')
 				model.id = info['id']
-				model.update(exp=msg, oper=False)
+				model.update(exp=msg, oper=True, operdate=Demeter.time())
 
 				# 如果有批量控制
-				oper = self.mul(info['id'], info['type_id'], info['name'], param['switch'], '失败:' + msg, False)
-
+				self.mul(info, info['name'] + '失败:' + msg, False)
+				oper = self.msg(info['type_id'], param['switch'])
 				content = oper + gateway['name'] + '下的'+ info['name'] + '失败,错误提示:' + msg
 				Demeter.service('record').msg(info['id'], content, info['farm_id'], 3)
 
@@ -136,54 +144,38 @@ class Device(object):
 			msg = info['name']
 		return msg
 
-	def mul(self, id, type_id, name, switch, msg, state):
+	def msg(self, type_id, switch):
+		oper = ''
+		type_model = Demeter.model('device_type')
+		type_model.id = type_id
+		type_info = type_model.select(type='fetchone')
+		if type_info['unit'] == 'button':
+			if switch == 1:
+				oper = '升起'
+			elif switch == 2:
+				oper = '降下'
+			elif switch == 5:
+				oper = '停止'
+		else:
+			if switch == 1:
+				oper = '开启'
+			else:
+				oper = '关闭'
+		return oper
+
+	def mul(self, info, msg, state):
 		model = Demeter.model('device_mul_queue')
-		model.device_id = id
+		mul_model = Demeter.model('device_mul')
+		model.device_id = info['id']
 		model.status = 1
 		queue = model.select()
+
 		if queue:
-			model.device_id = id
+			for v in queue:
+				model.status = 1
+				model.device_id = info['id']
+				oper = self.msg(info['type_id'], v['value']) + msg
 			if state:
-				model.update(status=2)
-				#model.device_id = id
-				#model.delete()
-			else:
-				model.update(status=3)
-			mul_model = Demeter.model('device_mul')
-			type_model = Demeter.model('device_type')
-			type_model.id = type_id
-			type_info = type_model.select(type='fetchone')
-			if type_info['unit'] == 'button':
-				if switch == '1':
-					oper = '升起'
-				elif switch == '2':
-					oper = '降下'
-				elif switch == '5':
-					oper = '停止'
+				model.update(status=2, oper=oper, operdate=Demeter.time())
 			else:
-				if switch == '1':
-					oper = '开启'
-				else:
-					oper = '关闭'
-			if msg:
-				for v in queue:
-					# 判断是否全部失败,全部失败,则将回执状态值
-					old = False
-					if state == False:
-						mul_model.id = v['mul_id']
-						info = mul_model.select(type='fetchone')
-						model.mul_id = v['mul_id']
-						model.status = 3
-						queueMulNum = len(model.select())
-						devicesNum = len(tuple(eval(info['devices'])))
-						if queueMulNum == devicesNum:
-							old = info['old']
-
-					mul_model.id = v['mul_id']
-					
-					oper = oper + name + msg
-					if old:
-						mul_model.update(oper=oper, value=old)
-					else:
-						mul_model.update(oper=oper)
-			return oper
+				model.update(status=3, oper=oper, operdate=Demeter.time())