def post_status(queue: beanstalk.Connection, message: str) -> None: """post a simple message to whomever is listening""" print(message) # echo message to stdout if queue: queue.use(STATUS_QUEUE) status_json = json.dumps({'msg': message}) queue.put(status_json)
def send_cancel_request(queue: beanstalk.Connection) -> int: """send a cancel request to the rig controller""" clear_status_queue(queue) # so we see what home command triggers queue.use(CANCEL_QUEUE) task_body = json.dumps({'task': 'cancel'}) return queue.put(task_body)
def take_picture(camera: gp.camera, rotation_pos: int, declination_pos: int, queue: beanstalk.Connection) -> str: """take a picture and save it to the USB drive or the google drive, if specified""" # take the picture file_path = gp.check_result(gp.gp_camera_capture( camera, gp.GP_CAPTURE_IMAGE)) file_name = 'P{dec:02d}{rot:02d}_'.\ format(dec=declination_pos, rot=rotation_pos) + file_path.name # read the photo from the camera camera_file = gp.check_result(gp.gp_camera_file_get(camera, file_path.folder, file_path.name, gp.GP_FILE_TYPE_NORMAL)) # if a google drive isn't specified, write to the local USB drive # read the image from the camera into memory # and upload it file_data = gp.check_result(gp.gp_file_get_data_and_size(camera_file)) # okay, upload to the Google drive via a thread... byte_stream = io.BytesIO(file_data) camera_bytes = byte_stream.read(-1) job = {'task': 'photo', 'filename': file_name, 'data': base64.encodebytes(camera_bytes).decode('ascii')} # now send the photo to the Google Drive process queue.use(google_drive.GDRIVE_QUEUE) job_str = json.dumps(job) print("photo job size is {0} bytes".format(len(job_str))) queue.put(job_str) return file_name
def send_home_command(queue: beanstalk.Connection) -> int: """send a home command to home the rig""" clear_status_queue(queue) # so we see what home command triggers queue.use(TASK_QUEUE) task_body = json.dumps({'task': 'home'}) return queue.put(task_body)
def send_token(queue: beanstalk.Connection, body_str: str) -> int: """send the google token so we can save photos to a google drive""" queue.use(TASK_QUEUE) job_body = json.dumps({'task':'token', 'value': body_str}) job_id = queue.put(job_body) # ********************** # test_write_file(queue) # ********************** return job_id
def clear_all_queues(queue: beanstalk.Connection) -> None: """clear out all the currently known tubes""" for tube in [ CANCEL_QUEUE, STATUS_QUEUE, TASK_QUEUE, google_drive.GDRIVE_QUEUE ]: queue.use(tube) while True: dummy = queue.reserve(timeout=0) if dummy: dummy.delete() else: break
def send_scan_command(queue: beanstalk.Connection, declination_steps: int, rotation_steps: int, start: int, stop: int) -> int: """this is it - time to scan. send the # of steps for each axis and return""" queue.use(TASK_QUEUE) task_body = json.dumps({'task': 'scan', 'steps': {'declination': declination_steps, 'rotation': rotation_steps}, 'offsets': {'start': start, 'stop': stop} }) return queue.put(task_body)
def clear_tube(queue: beanstalk.Connection, tube: str): """ flush all messages tubes to the rig""" queue.use(tube) while True: try: job = queue.reserve(timeout=0) if job is None: return job.delete() except beanstalk.CommandFailed: pass except beanstalk.DeadlineSoon: pass
def main(): parser = OptionParser() parser.add_option("--i", default=50) parser.add_option("--n", default=10) parser.add_option("--tube", default="default") options, args = parser.parse_args() conn = Connection(HOST, PORT) conn.use(options.tube) for i in range(int(options.n)): conn.put(str(i))
def test_write_file(queue: beanstalk.Connection) -> None: """simple program to test out google drive file writing""" queue.use(TASK_QUEUE) queue.watch(TASK_QUEUE) job = queue.reserve(timeout=2) if job is None: return None job_json = json.loads(job.body) if job_json['task'] != 'token': return None # we have a token, read it out access_info = json.loads(job_json['value']) drive_obj = google_drive.GoogleDrive(access_info) drive_obj.find_root_folder('rpipg') return None
class JobWorkerClient(object): """ job client. """ def __init__(self, host, port, tube=None): self.__host = host self.__port = port self.__tube = tube self.__bs = None def _ensure_connect(self): if self.__bs is None: print 'beanstalkc client connecting ...' self.__bs = Connection(self.__host, self.__port) print 'beanstalkc client connected to %s:%s:[%s]' % ( self.__host, self.__port, self.__tube) if not self.__tube is None: self.__bs.use(self.__tube) print 'beanstalkc client using %s' % self.__bs.using() def put(self, obj): self._ensure_connect() self.__bs.put(json.dumps(obj)) def use_put(self, tube, obj): self._ensure_connect() self.__bs.use(tube) self.__bs.put(json.dumps(obj)) def use(self, tube): self._ensure_connect() self.__bs.use(tube) self.__tube = tube def close(self): try: self.__bs.close() except: pass
class JobWorkerClient(object): """ job client. """ def __init__(self, host, port, tube=None): self.__host = host self.__port = port self.__tube = tube self.__bs = None def _ensure_connect(self): if self.__bs is None: print 'beanstalkc client connecting ...' self.__bs = Connection(self.__host, self.__port) print 'beanstalkc client connected to %s:%s:[%s]' % (self.__host, self.__port, self.__tube) if not self.__tube is None: self.__bs.use(self.__tube) print 'beanstalkc client using %s' % self.__bs.using() def put(self,obj): self._ensure_connect() self.__bs.put(json.dumps(obj)) def use_put(self, tube, obj): self._ensure_connect() self.__bs.use(tube) self.__bs.put(json.dumps(obj)) def use(self, tube): self._ensure_connect() self.__bs.use(tube) self.__tube = tube def close(self): try: self.__bs.close() except: pass
def send_cancel(queue: beanstalk.Connection) -> int: """send a cancel to the rig software to stop whatever is happening""" queue.use(CANCEL_QUEUE) return queue.put('STOP!') # anything will do
def session_start(queue: beanstalk.Connection) -> None: """before scanning, we need to start a 'session', which basically means doing any pre-scanning work. So we will shoot the drive process a message to kick off this activity""" queue.use(google_drive.GDRIVE_QUEUE) queue.put(json.dumps({'task': 'session_start'}))
def forward_authorization(queue: beanstalk.Connection, job: dict): """Forward the Google Drive authentication credentials to our Google Drive process""" queue.use(google_drive.GDRIVE_QUEUE) queue.put(json.dumps(job))