2014-05-26 11:21:19 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division
|
|
|
|
|
|
|
|
from Queue import Queue
|
|
|
|
from threading import Thread
|
|
|
|
|
|
|
|
from websocket import trigger_event
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger('oml.websocket')
|
|
|
|
|
|
|
|
class Tasks(Thread):
|
|
|
|
|
2014-08-09 16:33:59 +00:00
|
|
|
def __init__(self):
|
2014-05-26 11:21:19 +00:00
|
|
|
self.q = Queue()
|
|
|
|
self.connected = True
|
|
|
|
Thread.__init__(self)
|
|
|
|
self.daemon = True
|
|
|
|
self.start()
|
2014-05-28 15:36:26 +00:00
|
|
|
self.queue('scan')
|
2014-05-26 11:21:19 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
import item.scan
|
|
|
|
while self.connected:
|
|
|
|
m = self.q.get()
|
|
|
|
if m:
|
|
|
|
action, data = m
|
|
|
|
if action == 'ping':
|
|
|
|
trigger_event('pong', data)
|
|
|
|
elif action == 'import':
|
|
|
|
item.scan.run_import(data)
|
|
|
|
elif action == 'scan':
|
|
|
|
item.scan.run_scan()
|
|
|
|
elif action == 'update':
|
|
|
|
trigger_event('error', {'error': 'not implemented'})
|
|
|
|
else:
|
|
|
|
trigger_event('error', {'error': 'unknown action'})
|
|
|
|
self.q.task_done()
|
|
|
|
|
|
|
|
def join(self):
|
|
|
|
self.connected = False
|
2014-08-09 18:32:41 +00:00
|
|
|
self.q.put(None)
|
2014-05-26 11:21:19 +00:00
|
|
|
self.q.join()
|
|
|
|
return Thread.join(self)
|
|
|
|
|
2014-05-28 15:36:26 +00:00
|
|
|
def queue(self, action, data=None):
|
2014-05-26 11:21:19 +00:00
|
|
|
self.q.put((action, data))
|
|
|
|
|