コード例 #1
0
class SerialWorker:
    def __init__(self):
        self.trigger = False
        self.result_queue = RedisQueue(Config.UP_QUEUE_NAME)
        self.command_queue = RedisQueue(Config.DOWN_QUEUE_NAME)
        self.port = serial.Serial("/dev/ttyS0",
                                  9600,
                                  parity=serial.PARITY_NONE,
                                  stopbits=serial.STOPBITS_ONE,
                                  bytesize=serial.EIGHTBITS,
                                  timeout=Config.SERIAL_WAIT)
        self.start()

    def start(self):
        while True:
            self.executeTask()
            time.sleep(Config.SERIAL_CYC)

    def executeTask(self):
        GPIO.output(Config.EN_485, GPIO.HIGH)

        command = self.command_queue.get_nowait()
        if not command:
            self.trigger = not self.trigger
            if self.trigger:
                command = DEFAULT_COMMAND
            else:
                command = DEFAULT_COMMAND2

        print 'write to 485 %s' % command

        command = CommandHelper.toWriteable(command)
        self.port.write(command)

        while self.port.out_waiting > 0:
            time.sleep(0.01)

        GPIO.output(Config.EN_485, GPIO.LOW)
        result = self.port.readall()

        if result:
            result = CommandHelper.toReadable(result)
            print 'receive from 485 %s' % result
            self.result_queue.put(result)
コード例 #2
0
class SocketWorker:
    def __init__(self):
        self.command_queue = RedisQueue(Config.DOWN_QUEUE_NAME)
        self.result_queue = RedisQueue(Config.UP_QUEUE_NAME)

        self.socket = websocket.WebSocketApp(HOST,
                                             on_open=self.on_open,
                                             on_message=self.on_message,
                                             on_error=self.on_error,
                                             on_close=self.on_close)

        while True:
            try:
                self.socket.run_forever(ping_interval=100)
            except:
                pass
            time.sleep(5)

    def on_open(self):
        print 'socket connected'
        self.socket.send(
            json.dumps({
                'type': 'verify',
                'data': 'device',
                'id': self.getSerial()
            }))
        thread.start_new_thread(self.start, ())

    def on_error(self, error):
        print 'socket error %s' % error

    def on_message(self, message):
        print 'socket get message %s' % message
        try:
            message = json.loads(message)
            self.handle_message(message)
        except Exception as e:
            print e
            print 'message parse fail'

    def handle_message(self, message):
        if message['type'] == 'message':
            self.command_queue.put(message['data'])
        elif message['type'] == 'unverified':
            self.socket.send(
                json.dumps({
                    'type': 'verify',
                    'data': 'device',
                    'id': self.getSerial()
                }))

    def on_close(self):
        print 'socket close'

    def start(self):
        while True:
            self.execTask()
            time.sleep(0.5)

    def execTask(self):
        result = self.result_queue.get_nowait()
        if result:
            print 'socket send result %s' % result
            self.socket.send(json.dumps({'type': 'message', 'data': result}))

    def getSerial(self):
        cpuserial = "0000000000000000"
        try:
            f = open('/proc/cpuinfo', 'r')
            for line in f:
                if line[0:6] == 'Serial':
                    cpuserial = line[10:26]
            f.close()
        except:
            cpuserial = "ERROR000000000"

        return cpuserial
コード例 #3
0
ファイル: async_insert.py プロジェクト: github4n/python_code
q = RedisQueue('rq')

sum = 0
timeout = 0

start_time = arrow.now().timestamp

try:
    while True:
        if timeout == 180:
            msg = "超时 3 分钟 队列结束"
            logging.info(msg)
            print(msg)
            break

        result = q.get_nowait()
        if not result:
            timeout += 5
            msg = "超时 " + str(timeout) + " 秒" + " 等待获取..."
            print(msg)
            logging.info(msg)
            time.sleep(5)
            continue
        else:
            sum += 1

            msg = "第 " + str(sum) + " 条 SQL 正在执行: " + str(result)
            print(msg)
            msg = "剩余队列: " + str(q.qsize())
            print(msg)