import struct
import time

import cv2
import numpy as np
import zmq

from flandre.config import C
from flandre.nodes.Device import Device, DeviceCmd
from flandre.nodes.Mi import Mi

if __name__ == '__main__':
    context = zmq.Context()
    req_driver_socket = context.socket(zmq.REQ)
    req_driver_socket.connect(C.live_rep_socket)
    magic = 7355608
    cmd = DeviceCmd.GetData
    cv2.namedWindow("test", cv2.WINDOW_AUTOSIZE)
    while True:
        req_driver_socket.send(struct.pack('i', magic) + struct.pack('i', cmd.value))
        b = req_driver_socket.recv()
        print(b[4:12])
        print(struct.unpack('<I', b[8:12]))
        offset = 4 + 8 + 4
        # offset = 0
        arr = np.frombuffer(b, dtype=np.int16, offset=offset).reshape((256, 5002))
        cv2.imshow('test', arr)
        cv2.waitKey(1)
    # print(b.__len__())
