def send_modify_result(self, _id, status=None): L.debug("Send modify result %d" % _id) pickle_msg = pickle.dumps({ 'response': 'modify', 'id': _id, 'status': status}) osc.sendMsg('/android_park', [pickle_msg, ], port=3334)
def start_modify_request(self): if not self.service_running: self.error.text = "El Servicio en segundo plano no está corriendo" self.error.open() return L.debug("Send start") days = self.get_days_with_change() operations = {} for d in days: if d.day.status != DayStatus.RESERVED and d.day.status != DayStatus.REQUESTED: # request operations[d.day.date] = DayStatus.TO_REQUEST elif d.day.status == DayStatus.RESERVED: # free operations[d.day.date] = DayStatus.TO_FREE elif d.day.status == DayStatus.REQUESTED: # unrequest operations[d.day.date] = DayStatus.TO_UNREQUEST free_count = len([x for x in operations if operations[x] == DayStatus.TO_FREE]) unrequest_count = len([x for x in operations if operations[x] == DayStatus.TO_UNREQUEST]) request_count = len([x for x in operations if operations[x] == DayStatus.TO_REQUEST]) # Now add to the operations those days that are already requested. requested_days = self.get_days_requested() for d in requested_days: operations[d.day.date] = DayStatus.TO_REQUEST total_request_count = len([x for x in operations if operations[x] == DayStatus.TO_REQUEST]) self.confirm.text = "Se liberarán {0:d} plazas, se quitará la solicitud para {1:d} plazas y se solicitarán {2:d} nuevas plazas. {3:d} plazas están solicitadas" \ .format(free_count, unrequest_count, request_count, total_request_count - request_count) self.confirm.on_accept = lambda: self.modify_request(operations) self.confirm.open()
def send_ping_result(self, _id): L.debug("Send ping result %d" % _id) pickle_msg = pickle.dumps({ 'response': 'ping', 'id': _id, 'config': self.config}) osc.sendMsg('/android_park', [pickle_msg, ], port=3334)
def add_pattern(self): pattern = "" if self.config is not None: pattern = self.config.get("general", "pattern") dates_from_pattern = [t[1] for t in self.parse_spec(pattern) if t[0]] L.debug("Using pattern %s, adding %s" % (pattern, str(dates_from_pattern))) self.pending.update({t: DayStatus.TO_REQUEST for t in dates_from_pattern}) if len(dates_from_pattern) > 0: self.last_time = None # Force a modify.
def send_query_result(self, _id, result, pending, status=None): L.debug("Send query result %d" % _id) pickle_msg = pickle.dumps({ 'response': 'query', 'id': _id, 'result': result, 'pending': pending, 'status': status}) osc.sendMsg('/android_park', [pickle_msg, ], port=3334)
def modify(self, operations, partial): status = self.query() last_text = "" today = datetime.datetime.now(tz=self.met) for i in sorted(operations): if i.month == 1 and today.month == 12: mes = 1 else: mes = i.month - today.month if mes not in (0, 1): L.error(str(i) + " is neither current nor next month") partial(str(i) + " no es parte del mes actual o siguiente") continue if operations[i] == DayStatus.TO_REQUEST: if i not in status or status[i].status in ( DayStatus.RESERVED, DayStatus.NOT_AVAILABLE, DayStatus.REQUESTED, DayStatus.BUSY, ): partial(str(i) + " está ya solicitado") continue if operations[i] in (DayStatus.TO_FREE, DayStatus.TO_UNREQUEST): # Operation is to free/unrequest if i not in status or status[i].status in (DayStatus.AVAILABLE, DayStatus.NOT_AVAILABLE): partial(str(i) + " está ya liberado") continue try: L.debug("Requesting " + str(i) + ": " + self.map_operation(operations[i])) r = self.session.post( "http://" + self.host + "/perfil.php", data={"dia": i.day, "mes": mes, "libre": operations[i]} ) if r.status_code != requests.codes.ok: L.error("Failed request on " + self.host + ", response:" + r) partial(str(i) + "`: Failed request on server response:" + r) continue # Parse the web page to obtain the result message last_text = self.get_response_text(r) result = self.parse_result(last_text) if result == "": result = "La modificación de " + str(i) + " no fue aceptada" partial(result) L.info(str(i) + ": " + result) except (KeyboardInterrupt, requests.ConnectionError): L.debug("Petición fallida en " + self.host + ", con:\n" + traceback.format_exc()) if last_text != "": return self.parse_months(last_text) else: # We didn't send any operation, use the status retrieved # when we entered the method. return status
def refresh_request(self): if not self.is_config_ready(): return if not self.service_running: self.error.text = "El Servicio en segundo plano no está corriendo" self.error.open() return L.debug("Query start") self.last_update = datetime.datetime.now() self.querying.open() self.status_bar.text = "Consultando al servidor del parking..." self.app.do_refresh()
def handle_message(self, message, *args): pickle_msg = message[2] msg = pickle.loads(pickle_msg) L.debug("Received message %s" % msg['request']) self.config = msg['config'] self.server.config(self.config) if msg['request'] == 'ping': # Reply immediatelly to pings. self.ping(msg['id']) if msg['request'] == 'query': self.internal_queue.put(lambda: self.query(msg['id'])) if msg['request'] == 'modify': self.pending = msg['operations'] self.internal_queue.put(lambda: self.modify(msg['id'])) L.debug("End Received message %s" % msg['request'])
def handle_message(self, message, *args): pickle_msg = message[2] msg = pickle.loads(pickle_msg) if 'response' in msg: if msg['id'] == -1: self.add_fake_request(msg) keep = 'is_partial' in msg and msg['is_partial'] req = self.get_id(msg['id'], keep) if req is not None: if keep: req.callback_partial(msg) else: req.callback(msg) else: L.debug("Ignoring unknown response %d" % msg['id']) else: L.debug("Got a request %s for %d" % (msg['response'], msg['id']))
def update_info(self, state, pending, status=None): self.status_bar.text = "" if not state: L.error("state is None") self.querying.dismiss() if status is not None: self.error.text = status else: self.error.text = "Error desconocido" self.error.open() return self.unrequest_month(self.current_month) self.unrequest_month(self.next_month) self.current_month.update(state, pending) self.next_month.update(state, pending) L.debug("Update ends") self.querying.dismiss()
def login(self): L.debug("Starting login") # # Start a session so that we can reuse cookies # self.session = requests.Session() # self.session.proxies= { # "http": "http://es.proxy.lucent.com:8000", # "https": "http://es.proxy.lucent.com:8000", # } # # Prepare the login request # try: r = self.session.post( "http://" + self.host + "/index.php", data={"usuario": self.username, "contrasena": self.password, "aceptar": "ACEPTAR"}, ) if r.status_code != requests.codes.ok: status = "Fallo al hacer login " + self.host + ", respuesta:" + r raise ServerException(status) except (KeyboardInterrupt, requests.ConnectionError) as e: status = str(e) raise ServerException(status) # # Parse the web page we have read # L.debug("Parsing query result") state = self.parse_months(self.get_response_text(r)) if state is None: raise ServerException( "Problema consultando con el servidor, es probable que el usuario/contraseña sean incorrectos" ) return state
def __init__(self): L.debug("Service is running") # Initialize internal queue self.thread = threading.Thread(name='execution', target=self.async_run) self.internal_queue = Queue() self.thread.start() # Initialize OSC osc.init() self.oscid = osc.listen(ipAddr='127.0.0.1', port=3333) osc.bind(self.oscid, self.handle_message, '/android_park') self.server = ServerInterface() self.pending = {} self.last_time = None self.notified = True self.config = None # set to indicate that a month has been processed by the pattern self.pattern_processed=set() # Initialize timezones self.met = pytz.timezone('Europe/Madrid') self.t1 = datetime.time(hour=0, minute=0, second=0, tzinfo=self.met) # 00:00 self.t2 = datetime.time(hour=15, minute=0, second=0, tzinfo=self.met) # 15:00 self.t3 = datetime.time(hour=17, minute=30, second=0, tzinfo=self.met) # 17:30 self.t4 = datetime.time(hour=23, minute=59, second=59, tzinfo=self.met) # 23:59
def query(self): L.debug("Starting query") if self.session is None: # # No session, start a login session # the result is fine as a query result # state = self.login() return state # # Reuse session # try: r = self.session.get("http://" + self.host + "/perfil.php") if r.status_code != requests.codes.ok: status = "Error consultando al " + self.host + ", respuesta:" + r raise ServerException(status) # # Parse the web page we have read # L.debug("Parsing query result") state = self.parse_months(self.get_response_text(r)) if state is None: L.debug("parsing failed, login might have failed") self.session = None return self.query() except (KeyboardInterrupt, requests.ConnectionError, ServerException): L.error(traceback.format_exc()) # try to login again self.session = None return self.query() return state
def on_stop(self): L.debug("on_stop()") osc.dontListen(self.oscid) self.oscid = None self.pending = {} self.stopped = True
def callback(self, msg): if self.calendar is not None: L.debug("Got a response %s for %d" % (msg['response'], msg['id'])) self.calendar.refresh_callback(msg['result'], msg['pending'], msg['status'])
def callback(self, msg): if self.calendar is not None: L.debug("Got a response %s for %d" % (msg['response'], msg['id'])) self.calendar.modify_callback()
def callback_partial(self, msg): if self.calendar is not None: L.debug("Got a partial response %s for %d" % (msg['response'], msg['id'])) self.calendar.modify_partial_callback(msg['status'])
def refresh_callback(self, state, pending, status=None): if status is not None: L.debug("Update status: " + str(status) + " pending operations: " + str(pending)) Clock.schedule_once(lambda (dt): self.update_info(state, pending, status))
def ping_callback(self, value, config): if self.service_running != value: L.debug("Service state change: " + str(value)) self.service_running = value