def test_update_multi(): for use_message in [False, True]: v = random.randint(1, 100) test_val1 = 'foo-%d' % v test_val2 = 'bar-%d' % v seq_values = { 'test': test_val1, 'testIntSeq': v, 'folder/testSub': test_val2, 'testFloatSeq': v + 0.5 } c.sequences.update_multiple(seq_values, use_message=use_message) logging.info('updated multi sequences (%d)' % v) gevent.sleep(5) # wait so outbound messages are sent resource_client = ResourceClient(c.config) assert test_val1 == resource_client.read_file(c.path_on_server() + '/test').decode() assert test_val2 == resource_client.read_file( c.path_on_server() + '/folder/testSub').decode() assert v == int( resource_client.read_file(c.path_on_server() + '/testIntSeq').decode()) assert round(v + 0.5, 2) == round( float( resource_client.read_file(c.path_on_server() + '/testFloatSeq').decode()), 2)
def test_update_sequence(): v = random.randint(1, 100) test_val1 = 'foo-%d' % v test_val2 = 'bar-%d' % v c.sequences.update(c.path_on_server() + '/test', test_val1, use_websocket=True) c.sequences.update(c.path_on_server() + '/testIntSeq', v, use_websocket=True) c.sequences.update('testFloatSeq', v + 0.5, use_websocket=True) c.sequences.update('folder/testSub', test_val2, use_websocket=True) logging.info('updated sequences (%d)' % v) gevent.sleep(10) # wait so outbound messages are sent # read back using resource client if True: resource_client = ResourceClient(c.config) assert test_val1 == resource_client.read_file(c.path_on_server() + '/test').decode() assert test_val2 == resource_client.read_file( c.path_on_server() + '/folder/testSub').decode() assert v == int( resource_client.read_file(c.path_on_server() + '/testIntSeq').decode()) assert round(v + 0.5, 2) == round( float( resource_client.read_file(c.path_on_server() + '/testFloatSeq').decode()), 2)
def _test_send_image(): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) width = 320 height = 240 image = Image.new('RGB', (width, height)) pixel_data = image.load() for y in range(height): for x in range(width): pixel_data[x, y] = (r, g, b) contents = encode_image(image) c.sequences.update('image', contents, use_websocket=False) logging.info('updated image (%d, %d, %d)' % (r, g, b)) gevent.sleep(2) # wait so outbound messages are sent # read back using resource client if False: resource_client = ResourceClient(c.config) contents = resource_client.read_file(c.path_on_server() + '/image') open('test.jpg', 'w').write(contents)
import os import gevent from rhizo.main import c path_on_server = c.path_on_server() name = path_on_server.rsplit('/', 1)[1] print(name, os.getcwd(), path_on_server) # loop forever sending messages message_index = 0 while True: message = 'msg-%s-%d' % (name, message_index) print('%s: send: %s' % (name, message)) c.send_message(message, {}) gevent.sleep(1) message_index += 1
def test_path_on_server(): assert c.config.server_test_path == c.path_on_server()
def test_send_message_with_folder(): c.messages.add_handler(message_handler) c.messages.send('test_message', {'foo': 'bar'}, folder=c.path_on_server() + '/folder') gevent.sleep(5)
import gevent from rhizo.main import c controller_count = 20 # initialize controllers for i in range(controller_count): name = 'ctrl-%04d' % i print('preparing: %s' % name) # create local folder if not os.path.exists(name): os.mkdir(name) # create server folder server_path = c.path_on_server() + '/' + name if not c.files.file_exists(server_path): print('creating on server: %s' % server_path) params = { 'parent': c.path_on_server(), 'name': name, 'type': 12, } data = c.files.send_request_to_server('POST', '/api/v1/resources', params) data = json.loads(data) new_id = data['id'] params = { 'access_as_controller_id': new_id, } data = c.files.send_request_to_server('POST', '/api/v1/keys', params)