关于ruby的问题,如何按时间发送命令。 20C

这是一个关于ruby的问题。其中一段代码如下。

    def switch_output(option_json)
        log_debug("switch_output command invoked")
        return send_command({command: "startNewOutputFile"})
    end

我需要把这个地方,改成每10分钟发送一次命令(command)。

 send_command({command: "startNewOutputFile"})

我问了原代码的作者,他给的建议如下。
My suggestion is for you to take a look at above daq.rb and learn how to connect to MessageServer via ZeroMQ in Ruby or Python, and then send the startNewOutputFile command at a rate you want (e.g. 1/600 Hz).
我建议您查看上面的daq.rb并了解如何通过Ruby或Python中的ZeroMQ连接到MessageServer,然后以您想要的时间速率发送startNewOutputFile命令(例如1/600 Hz)。

请各位大神帮助。
全部代码如下,文件名是dap.rb,需要改动的地方在最后。

require "rbczmq"
require "json"
require "yaml"
require "socket"
require "growth_controller/logger"
require "growth_controller/controller_module"

module GROWTH

class ControllerModuleDAQ < ControllerModule
    # TCP port number of growth_daq ZeroMQ server
    DAQ_ZMQ_PORT_NUMBER = 10020

    def initialize(name, zmq_context, logger: nil)
        super(name, logger: logger)
        define_command("ping")
        define_command("stop")
        define_command("pause")
        define_command("resume")
        define_command("status")
        define_command("switch_output")

        @context = zmq_context
        connect()
    end

    private
    def connect()
        log_info("Connecting to DAQ ZeroMQ server...")
        @requester = @context.socket(ZMQ::REQ)
        begin
            @requester.connect("tcp://localhost:#{DAQ_ZMQ_PORT_NUMBER}")
            @requester.rcvtimeo = 1000
            @requester.sndtimeo = 1000
            log_info("Connected to DAQ ZeroMQ server")
        rescue
            log_error("Connection failed. It seems the DAQ program is not running")
            @requester = nil
        end
    end

    private
    def send_command(hash)
        # Connect if necessary
        if(@requester == nil)then
            connect()
        end
        if(@requester == nil)then
            return {status: "error", message: "Could not connect to DAQ ZeroMQ server"}
        end
        begin
            @requester.send(hash.to_json.to_s)
        rescue => e
            @requester.close
            @requester = nil
            log_warn("send_command() returning error (#{e})")
            return {status: "error", message: "ZeroMQ send failed (#{e})"}
        end
        return receive_reply()
    end

    private
    def receive_reply()
        begin
            reply_message = @requester.recv()
            return JSON.parse(reply_message)
        rescue => e
            @requester.close
            @requester = nil
            log_warn("receive_reply() returning error (#{e})")
            return {status: "error", message: "ZeroMQ communication failed (#{e}"}
        end     
    end

    #---------------------------------------------
    # Implemented commands
    #---------------------------------------------
    def ping(option_json)
        log_debug("ping command invoked")
        return send_command({command: "ping"})
    end

    def stop(option_json)
        log_debug("stop command invoked")
        return send_command({command: "stop"})
    end

    def pause(option_json)
        log_debug("pause command invoked")
        return send_command({command: "pause"})
    end

    def resume(option_json)
        log_debug("resume command invoked")
        return send_command({command: "resume"})
    end

    def status(optionJSON)
        log_debug("status command invoked")
        return send_command({command: "getStatus"})
    end

    def switch_output(option_json)
        log_debug("switch_output command invoked")
        return send_command({command: "startNewOutputFile"})
    end

end

end

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问