Esempio n. 1
0
def main():
    cachedHelper = CachedPrestaHelper(config.presta_api_url,
                                      config.presta_api_key,
                                      debug=False)
    lcd = EuropeLcdMatrix(config.lcd_device)
    lcd.create_european_charset()

    lcd.clear_screen()
    lcd.autoscroll(False)
    lcd.activate_lcd(True)
    lcd.write_european_pos(1, 1, u'LcdOrderTrack')
    lcd.write_european_pos(2, 1, u'     starting...')

    # default values for variable
    last_order_id = -1
    last_order_data = None
    bNewPayment = False
    paid_count = 0
    bankwire_count = 0

    # Locate the ID_Payment for "Paiement par carte sur place"
    PAY_AT_MCH = None
    for order_state in cachedHelper.order_states:
        if cachedHelper.order_states.name_from_id(
                order_state.id).upper().find(u'CARTE SUR PLACE') >= 0:
            PAY_AT_MCH = order_state.id

    try:
        # Force initial update
        last_update_time = time.time() - UPDATE_DELAY
        while True:
            if math.floor(time.time() - last_update_time) >= UPDATE_DELAY:
                lcd.clear_screen()
                lcd.write_european(u'Updating...')

                # Identifying the last Order!
                last_order_id = cachedHelper.get_lastorder_id()
                last_orders_data = cachedHelper.get_last_orders(last_order_id,
                                                                count=1)
                # bOpenOrder = cachedHelper.order_states.is_open_order( last_orders_data[0].current_state )
                #   Exclude the REAPPROVISIONNEMENT state from light
                #   the LCD. Payment is effectively DONE but
                #   no work/shipping has to be prepared
                # New order with "Payment sur place" will deliver money shortly
                #   so it is also like a "new payment" --> switch on the light
                bNewPayment = (cachedHelper.order_states.is_new_order(
                    last_orders_data[0].current_state) or
                               (cachedHelper.order_states.name_from_id(
                                   last_orders_data[0].current_state).upper().
                                find(u'CARTE SUR PLACE') >= 0)) and (
                                    last_orders_data[0].current_state !=
                                    OrderStateList.ORDER_STATE_REPLENISH)

                # Activate LCD when receiving a new payment
                setLcdColor(lcd, cachedHelper, last_orders_data[0])
                lcd.activate_lcd(bNewPayment)
                paid_count = len(
                    cachedHelper.get_order_ids(
                        cachedHelper.order_states.ORDER_STATE_PAID))
                bankwire_count = len(
                    cachedHelper.get_order_ids(
                        cachedHelper.order_states.ORDER_STATE_WAIT_BANKWIRE))
                # Also add the "Paiement par carte sur place" in Bankwire count
                if PAY_AT_MCH:
                    bankwire_count += len(
                        cachedHelper.get_order_ids(PAY_AT_MCH))

                last_update_time = time.time()
            else:
                sInfo = u'upd in %i sec' % int(
                    UPDATE_DELAY - math.floor(time.time() - last_update_time))
                sInfo = (sInfo[:16]).ljust(16)
                lcd.write_european_pos(2, 1, sInfo)
                time.sleep(2)

            # Show Count of payments
            sPayInfo = u'Pay %s | Vir %s' % (paid_count, bankwire_count)
            sPayInfo = sPayInfo.center(16)
            lcd.write_european_pos(1, 1, sPayInfo)

            # Show information about last Order
            showOrderInfo(lcd, cachedHelper, last_orders_data[0])

    except Exception, e:
        lcd.clear_screen()
        lcd.write_european_pos('KABOUM!!')
        lcd.write_european_pos(2, 1, u'Restart in 5 Min')
        logging.exception(e)
        time.sleep(5 * 60)
        os.system('sudo reboot')
        return 0