Ejemplo n.º 1
0
def refresh_device(device):
    address = Config.get('services', 'client_address')
    port = Config.get('services', 'client_port')
    address = "tcp://" + address + ":" + port

    socket = zmq.Context().socket(zmq.REQ)
    socket.setsockopt(zmq.LINGER, 0)
    socket.connect(address)

    message = ['00000000', 'control', 'refresh', device]
    mdp_request(socket, 'device', message, 1)
Ejemplo n.º 2
0
    def send(self, message):

        if not self.can_send:
            self.connection_attempts += 1
            logger.error("DeviceConnection is not in state to send")
            return None
        else:
            self.connection_attempts = 0

        self.can_send = False

        logger.debug("DeviceConnection sending {0}".format(message))

        reply = mdp_request(self.socket,
                            DeviceConnection.SERVICE_NAME,
                            message,
                            DeviceConnection.TIMEOUT)

        if reply:
            logger.debug("DeviceConnection reply received: {0}".format(reply))
            self.can_send = True
            return reply
        else:
            # Timeout! Will be restarted at next heartbeat
            logger.warn("DeviceConnection timeout. Will be restarted at next heartbeat")
            self.connection_attempts = DeviceConnection.CONNECTION_ATTEMPS
            return None
Ejemplo n.º 3
0
    def __call__(self, method, *args, **kwargs):
        # open connection
        socket = self._client._rpc_context.socket(zmq.REQ)
        socket.setsockopt(zmq.LINGER, 0)
        socket.connect(self._client._rpc_endpoint)

        # encode message
        msg_struct = ((method,) + args, kwargs)
        msg = msgpack.packb(msg_struct)

        # send request
        raw_rep = mdp_request(socket, bytes(self._service), msg, 2.0) # TODO
        if not raw_rep:
            return # TODO  - better handling of non responses
        rep_decoded = msgpack.unpackb(raw_rep[1])

        # parse formatted response
        formatted_reply = None
        if rep_decoded[0] == 'OK':
            formatted_reply = rep_decoded[1]

        elif rep_decoded[0] == 'ERR':
            if rep_decoded[1] == 'NXMETHOD':
                formatted_reply = 'No Such Method: {}'.format('foo')
            elif rep_decoded[1] == 'EXC':
                formatted_reply = "\n".join([
                    str(rep_decoded[2]),
                    str(rep_decoded[4][0]),
                    str(rep_decoded[3]),
                ])
        # return formatted_replyonse
        return formatted_reply
Ejemplo n.º 4
0
def delete_device(device):
    address = Config.get('services', 'client_address')
    port = Config.get('services', 'client_port')
    address = "tcp://" + address + ":" + port

    socket = zmq.Context().socket(zmq.REQ)
    socket.setsockopt(zmq.LINGER, 0)
    socket.connect(address)

    message = ['00000000', 'control', 'remove', device.id]
    mdp_request(socket, 'device', message, 1)

    rrd = RRD(device.id, 'device')
    rrd.remove(remove_folder=True)

    db_session.query(Device).filter_by(id=device.id).delete()
    db_session.commit()

    return
Ejemplo n.º 5
0
    def send(self, msg):
        if not self._sock:
            raise Exception("BinderClient not connected. Cannot send")

        if not isinstance(msg, str):
            if isinstance(msg, dict):
                msg = json.dumps(msg)
            else:
                raise ValueError("cannot send message of type: {}".format(type(msg)))

        if self.name == "binderd":
            # sending msg to a standard RES socket
            pass
        else:
            # using MDP module to send to service
            res = mdp_request(self._sock, bytes(self.name), [bytes(msg),], 2.0)
            # the first element in the list is the service name
            return json.loads(res[1]) if res else None
Ejemplo n.º 6
0
    def rescan(self, bus_number):

        logger.info("sending rescan for grainbin: {0}".format(bus_number))
        info = GrainbinInfo(self.device_id, 'farm_monitor')
        info.create()

        message = [pickle.dumps(info)]

        socket = self.context.socket(zmq.REQ)
        socket.setsockopt(zmq.LINGER, 0)
        socket.connect(self.address)

        reply = mdp_request(socket, 'grainbin', message, 5)
        if reply:
            message = pickle.loads(reply[1])
            logger.debug("reply from farm monitor for rescan: {0}".format(message.reply))

        return
Ejemplo n.º 7
0
    def update_individual(self, bus_number):

        if internal_dealer.check_connection():
            logger.info("Sending update for grainbin: {0}".format(bus_number))
            socket = self.context.socket(zmq.REQ)
            socket.setsockopt(zmq.LINGER, 0)
            socket.connect(self.address)

            update = GrainbinUpdate(self.device_id, 'farm_monitor')
            update.create(bus_number=bus_number)
            message = [pickle.dumps(update)]

            reply = mdp_request(socket, 'grainbin', message, 5)
            if reply:
                message = pickle.loads(reply[1])
                logger.debug("reply from farm monitor for update: {0}".format(message.reply))

        return
Ejemplo n.º 8
0
    def update(self):
        self.updater.stop()

        if internal_dealer.check_connection():
            logger.info("Sending update for all grainbins")
            socket = self.context.socket(zmq.REQ)
            socket.setsockopt(zmq.LINGER, 0)
            socket.connect(self.address)

            update = GrainbinUpdate(self.device_id, 'farm_monitor')
            update.create()
            message = [pickle.dumps(update)]

            reply = mdp_request(socket, 'grainbin', message, 5)
            if reply:
                message = pickle.loads(reply[1])
                logger.debug("reply from farm monitor for update: {0}".format(message.reply))

        # delay = seconds_till_next('hour', duration=1) + 10  # 10 second buffer
        delay = 60 * 60  # send next update in one hour
        self.updater = DelayedCallback(self.update, delay * 1000)
        self.updater.start()
        return
Ejemplo n.º 9
0
import zmq
from zmq.eventloop.ioloop import IOLoop

from mdp.client import MDPClient, mdp_request

if __name__ == '__main__':
    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    socket.setsockopt(zmq.LINGER, 0)
    socket.connect("tcp://127.0.0.1:5555")
    res = mdp_request(socket, b'echo', [b'TEST'], 2.0)
    if res:
        print "Reply:", repr(res)
    else:
        print 'Timeout!'
    socket.close()