def onMessage(self, msg): if 'origin' in msg: if msg['origin'] == 'server' and 'connect' in msg and msg[ 'connect'] == 'user': username = msg['username'] user_session = msg['user_session'] if self.add_user_with_session(username, user_session): for c in self.consumers: c.new_user_session(username, user_session) elif msg['origin'] == 'server' and 'disconnect' in msg and msg[ 'disconnect'] == 'user': username = msg['username'] user_session = msg['user_session'] if self.remove_user_with_session(username, user_session): for c in self.consumers: c.end_user_session(username, user_session) elif msg['origin'] == 'server' and 'hello' in msg: info = msg['yourinfo'] vin = info['vin'] name = info['name'] description = info[ 'description'] # <-- Where should we display this? print_all('Device Name: {}'.format(name)) print_all('Device VIN: {}'.format(vin)) output = set_hostname(name) log.info("Set hostname, output is: {}".format(output)) for c in self.consumers: c.got_message(msg, self.smart_send)
def _show_verification_text(self, username, verification_text, expire_minutes): text = "Hi {}!\nAuthentication Code: {}".format( username, verification_text) console.big_image('images/pair_pending.png') console.big_status(text) print_all(text + "\n")
def _show_verification_failed(self, reason): reason = re.sub(r'\<.*?\>', '', reason) text = "Error:\n{}".format(reason) console.big_image('images/pair_error.png') console.big_status("Error:\nTry again.") print_all(text + "\n") time.sleep(5) # <-- TODO Remove this hack. Do something that's async. console.big_clear()
def _show_verification_success(self, username): text = "Congrats {}!\nYou are paired with this device.".format( username) console.big_image('images/pair_success.png') console.big_status(text) print_all(text + "\n") time.sleep(5) # <-- TODO Remove this hack. Do something that's async. console.big_clear()
def close_global_camera(verbose=False): global GLOBAL_CAMERA try: GLOBAL_CAMERA # <-- just to see if it exists if verbose: auto.print_all("Closing the global camera object...") GLOBAL_CAMERA.close() del GLOBAL_CAMERA except NameError: # There is no global camera, so nothing needs to be done. pass
def stream(frame, to_console=True, to_labs=False, verbose=False): """ Stream the given `frame` (a numpy ndarray) to your device's console _and_ (optionally) to your `labs` account to be shown in your browser. The `frame` parameter must be a numpy ndarray with one of the following shapes: - (h, w, 3) meaning a single 3-channel RGB image of size `w`x`h` - (h, w, 1) meaning a single 1-channel gray image of size `w`x`h` - (h, w) meaning a single 1-channel gray image of size `w`x`h` """ if frame is None: if to_console: console.clear_image() if to_labs: send_message_to_labs({'base64_img': ''}) return # Publish the uncompressed frame to the console UI. if to_console: if frame.ndim == 3: if frame.shape[2] == 3: pass # all good elif frame.shape[2] == 1: pass # all good else: raise Exception("invalid number of channels") elif frame.ndim == 2: frame = np.expand_dims(frame, axis=2) assert frame.ndim == 3 and frame.shape[2] == 1 else: raise Exception(f"invalid frame ndarray ndim: {frame.ndim}") height, width, channels = frame.shape aspect_ratio = width / height if aspect_ratio != OPTIMAL_ASPECT_RATIO: final_frame = _add_white_bars(frame) height, width, channels = final_frame.shape else: final_frame = frame shape = [width, height, channels] rect = [0, 0, 0, 0] console.stream_image(rect, shape, final_frame.tobytes()) # Convert the frame to a JPG buffer and publish to the network connection. if to_labs: base64_img = base64_encode_image(frame) send_message_to_labs({'base64_img': base64_img}) if verbose: h, w = frame.shape[:2] print_all("Streamed frame of size {}x{}.".format(w, h))
def global_camera(verbose=False): """ Creates (for the first call) or retrieves (for later calls) the global camera object. This is a convenience function to facilitate quickly and easily building a camera singleton. """ global GLOBAL_CAMERA try: return GLOBAL_CAMERA except NameError: GLOBAL_CAMERA = wrap_frame_index_decorator(CameraRGB()) if verbose: auto.print_all("Instantiated a global camera object!") return GLOBAL_CAMERA
def capture(num_frames=1, verbose=False): """ Capture `num_frames` frames from the (global) camera and return them as a numpy ndarray. This is a convenience function to make the most common use-case simpler. """ camera = global_camera(verbose) if num_frames > 1: frames = np.array( [frame for _, frame in zip(range(num_frames), camera.stream())]) if verbose: auto.print_all("Captured {} frames.".format(num_frames)) return frames else: frame = camera.capture() if verbose: auto.print_all("Captured 1 frame.") return frame
def plot(frames, also_stream=True, verbose=False): """ Stitch together the given `frames` (a numpy nd-array) into a single nd-array. If running in a notebook then the PIL image will be returned (and displayed). This function by default also streams the image to your `labs` account. The `frames` parameter must be a numpy ndarray with one of the following shapes: - (n, h, w, 3) meaning `n` 3-channel RGB images of size `w`x`h` - (n, h, w, 1) meaning `n` 1-channel gray images of size `w`x`h` - (h, w, 3) meaning a single 3-channel RGB image of size `w`x`h` - (h, w, 1) meaning a single 1-channel gray image of size `w`x`h` - (h, w) meaning a single 1-channel gray image of size `w`x`h` """ # Ensure the proper shape of `frames`. if frames.ndim == 4: pass elif frames.ndim == 3: frames = np.expand_dims(frames, axis=0) elif frames.ndim == 2: frames = np.expand_dims(frames, axis=2) frames = np.expand_dims(frames, axis=0) else: raise Exception("invalid frames ndarray ndim") if frames.shape[3] != 3 and frames.shape[3] != 1: raise Exception("invalid number of channels") if verbose: n = frames.shape[0] print_all("Plotting {} frame{}...".format(n, 's' if n != 1 else '')) montage = _create_montage(frames) if also_stream: stream(montage, to_labs=True, verbose=False) return PIL.Image.fromarray(np.squeeze(montage)) if _in_notebook() else None
def print_connection_info(): iface = wireless.interface print_all("WiFi interface name: {}".format(iface)) current = wireless.current() print_all("Connected to WiFi SSID: {}".format(current)) if iface and current: print_all("Current IP address: {}".format(get_ip_address(iface)))
def ensure_token(): token = STORE.get('DEVICE_TOKEN', None) if token is not None: # We have a token. All is well. return console.big_image('images/token_error.png') console.big_status('Ready to receive login token.') camera = CameraRGB() system_password = None for i, frame in enumerate(camera.stream()): frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) stream_frame(frame) qrcodes = myzbarlight.qr_scan(frame) if len(qrcodes) > 0: qr_data = qrcodes[0] try: qr_data = json.loads(qr_data.decode('utf-8')) token = qr_data['t'] if 'p' in qr_data: # This allows us to override the default system_password for special-purpose devices. # The default is just... a default. No matter what is set here, it can be changed later. system_password = qr_data['p'] break except: pass console.big_image('images/token_success.png') console.big_status('Success. Token: {}...'.format(token[:5])) camera.close() STORE.put('DEVICE_TOKEN', token) print_all("Stored Device token: {}...".format(token[:5])) jupyter_password = util.token_to_jupyter_password(token) STORE.put('DEVICE_JUPYTER_PASSWORD', jupyter_password) print_all("Stored Jupyter password: {}...".format(jupyter_password[:2])) if system_password is None: # If a particular default system_password was not specified, we will generate a good # default system_password from the token. This is a good thing, since it ensures that # each device is given a strong, unique default system_password. system_password = util.token_to_system_password(token) util.change_system_password(system_priv_user, system_password) print_all("Successfully changed {}'s password!".format(system_priv_user)) time.sleep(5) console.big_clear() console.clear_image()
See: https://github.com/AutoAutoAI/libauto/issues/7 """ high = BATTERY_HIGH_MILLIVOLTS low = BATTERY_LOW_MILLIVOLTS if millivolts > high * 1.05: return None # Something is wrong... the battery can't be this high. if millivolts > high: return 100 if millivolts < low * 0.95: return None # Something is wrong... if the battery was really this low, the battery protection would have kicked in and powered the whole device off. if millivolts < low: return 0 return int(round((millivolts - low) / (high - low) * 100)) while True: millivolts = batt.millivolts() percentage = batt_voltage_to_pct(millivolts) log.info("Battery millivolts={}, percentage={}".format( millivolts, percentage)) if percentage is not None: c.set_battery_percent(percentage) if percentage < 10: buzz.play("EEE") print_all("Warning: Battery <10%") time.sleep(10)
def onError(self, e): print_all('CDP connection error:', e)
def onClose(self): for c in self.consumers: c.disconnected_cdp() print_all('Connection to CDP lost. Reconnecting...')
def onOpen(self): for c in self.consumers: c.connected_cdp() print_all('Connected to CDP. Standing by...')
def onError(self, e): if not self.did_error: print_all('Attempting to connect...') self.did_error = True # We only want to print on the _first_ error.
delegate.smart_send(msg) if __name__ == '__main__': system_up_user = sys.argv[1] # the "UnPrivileged" system user system_priv_user = sys.argv[2] # the "Privileged" system user log.info("Will run the PTY manager using the unprivileged user: {}".format( system_up_user)) log.info("Will run the PTY manager using the privileged user: {}".format( system_priv_user)) log.info("Libauto version: {}".format(libauto_version)) log.info("Controller version: {}".format(cio_version)) print_all("Libauto version: {}".format(libauto_version)) print_all("Controller version: {}".format(cio_version)) consumers = [ PtyManager(system_up_user, system_priv_user), Verification(), Dashboard(), ] send_queue = queue.Queue(maxsize=100) delegate = StandardDelegate(consumers, send_queue) ws_connection = WebSocketConnection(delegate, get_token, BASE_URL) ws_thread = Thread(target=run_forever, args=(ws_connection, send_queue))
def plot(frames, also_stream=True, verbose=False, **fig_kwargs): """ Plot the given `frames` (a numpy ndarray) into a matplotlib figure, returning the figure object which can be shown. This function by default also streams the image to your `labs` account. The `frames` parameter must be a numpy ndarray with one of the following shapes: - (n, h, w, 3) meaning `n` 3-channel RGB images of size `w`x`h` - (n, h, w, 1) meaning `n` 1-channel gray images of size `w`x`h` - (h, w, 3) meaning a single 3-channel RGB image of size `w`x`h` - (h, w, 1) meaning a single 1-channel gray image of size `w`x`h` - (h, w) meaning a single 1-channel gray image of size `w`x`h` """ import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg # Ensure the proper shape of `frames`. if frames.ndim == 4: pass elif frames.ndim == 3: frames = np.expand_dims(frames, axis=0) elif frames.ndim == 2: frames = np.expand_dims(frames, axis=2) frames = np.expand_dims(frames, axis=0) else: raise Exception("invalid frames ndarray ndim") if frames.shape[3] != 3 and frames.shape[3] != 1: raise Exception("invalid number of channels") # Compute the figure grid size (this will be (height x width) subplots). n = frames.shape[0] width = int(round((float(n)**0.5))) height = n // width if (n % width) > 0: height += 1 if verbose: print_all("Plotting {} frame{}...".format(n, 's' if n != 1 else '')) # Create the figure grid. if 'figsize' not in fig_kwargs: fig_kwargs['figsize'] = (5, 5) if n == 1 else (10, 10) fig, axes = plt.subplots(width, height, **fig_kwargs) canvas = FigureCanvasAgg(fig) # Ensure `axes` is a 1d iterable. try: axes = axes.flatten() except AttributeError: # This ^^ exception happens when width=height=1. axes = [axes] # Plot each frame into the grid. from itertools import zip_longest for ax, frame in zip_longest(axes, frames): if frame is not None: if frame.shape[2] == 1: frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB) ax.imshow(frame) ax.axis('off') fig.tight_layout() # Also stream... if told to. if also_stream: if n > 1: canvas.draw() canvas_width, canvas_height = [int(v) for v in fig.get_size_inches() * fig.get_dpi()] canvas_frame = np.fromstring(canvas.tostring_rgb(), dtype='uint8').reshape(canvas_height, canvas_width, 3) stream(canvas_frame, to_labs=True, verbose=False) # We say `verbose=False` here because we don't want ANOTHER printout, even if verbose is True for this `plot()` function. else: stream(frames[0], to_labs=True, verbose=False) # ... same ... return fig, axes[:len(frames)]