summaryrefslogtreecommitdiff
path: root/test/module/test.py
blob: 9a20345194da4a373941d968254a7754f2ffdb79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gevent
import importlib.util
spec = importlib.util.spec_from_file_location("CortexRPC", "../../rpc/rpc.py")
rpc_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(rpc_module)
CortexRPC = rpc_module.CortexRPC

if __name__ == '__main__':
  params = {}

  def get_params():
    print('get params.')
    return params

  def set_param(key, value):
    print('log param. {}: {}'.format(key, value))
    params[key] = value
    return 'ok'

  def ready(self):
    print("Connected!")
    i = 0
    while 1:
      print('{}...'.format(i))
      i = i+1
      gevent.sleep(1)
    return 'ready'

  def process_cmd(cmd, payload):
    print('process command', cmd, payload)
    return 'processed'

    # print('defer sending a file...')
    # gevent.sleep(1)
    # return self.get_last_frame()

  """
  def get_last_frame(self):
    if self.working:
      self.working = False
      return 'working'
    path = os.getenv('TEST_FRAMES_PATH')
    onlyfiles = sorted([f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))])
    self.working = True

    for f in onlyfiles:
      gevent.sleep(1/10)
      if not self.working:
        return
      output = BytesIO()
      im = Image.open(os.path.join(path, f))
      # im = im.convert('RGB')
      im.save(output, format='JPEG')
      frame = output.getvalue()
      output.close()
      self.send_frame(f.replace('png', 'jpg'), frame)

    self.working = False
    return 'ok'
  """

  # this will block
  print("Connecting to relay...")
  rpc = CortexRPC(get_params, set_param, ready, process_cmd)