|
|
|
import json
|
|
|
|
from update.views import get_cinema_update_status
|
|
|
|
from channels.generic.websocket import WebsocketConsumer
|
|
|
|
from django_redis import get_redis_connection
|
|
|
|
|
|
|
|
|
|
|
|
class UpdateConsumer(WebsocketConsumer):
|
|
|
|
def connect(self):
|
|
|
|
self.accept()
|
|
|
|
self.send('你已经连接成功')
|
|
|
|
|
|
|
|
def receive(self, text_data=None, bytes_data=None):
|
|
|
|
|
|
|
|
redis_conn = get_redis_connection()
|
|
|
|
|
|
|
|
if text_data is None:
|
|
|
|
self.send('你发了啥')
|
|
|
|
else:
|
|
|
|
data = json.loads(text_data)
|
|
|
|
# cinema_update_status = get_cinema_update_status()
|
|
|
|
if data.get('msg', False):
|
|
|
|
if data['msg'] == 'ping':
|
|
|
|
self.send(json.dumps({'msg': 'pong'}))
|
|
|
|
if data.get('finish', False):
|
|
|
|
ip = data['finish']
|
|
|
|
redis_key = f'cinema_update_status_{ip}'
|
|
|
|
if redis_conn.exists(redis_key):
|
|
|
|
redis_conn.delete(redis_key)
|
|
|
|
data = json.loads(redis_conn.get(redis_key))
|
|
|
|
print('ws任务完成消息', data)
|
|
|
|
|
|
|
|
if data.get('ip', False):
|
|
|
|
ip = data['ip']
|
|
|
|
redis_key = f'cinema_update_status_{ip}'
|
|
|
|
# print('ws发送消息前', cinema_update_status)
|
|
|
|
data = json.loads(redis_conn.get(redis_key))
|
|
|
|
print('ws发送消息前', data)
|
|
|
|
self.send(json.dumps({'status': {'ip': ip, 'result': data}}))
|
|
|
|
else:
|
|
|
|
self.send(text_data)
|