[cart + cash] partial product arrays
when you cash out, instead of sending many packets, you can aggregate multiple products in the same packet, doing an array; thus you have better power efficiency, and less delay; so both Anastasi and Marcelloni would be very proud of us
This commit is contained in:
parent
a051841635
commit
35f5041b7f
@ -21,8 +21,8 @@ PROCESS_THREAD(cart_main_process, ev, data) {
|
|||||||
|
|
||||||
/*** Variables initialization ***/
|
/*** Variables initialization ***/
|
||||||
// status = NOT_ASSOCIATED; // TODO DEBUG
|
// status = NOT_ASSOCIATED; // TODO DEBUG
|
||||||
|
status = SHOPPING;
|
||||||
|
|
||||||
status = NOT_ASSOCIATED; // SHOPPING; // NOT_ASSOCIATED;
|
|
||||||
etimer_set(&broadcast_timer, 5 * CLOCK_SECOND);
|
etimer_set(&broadcast_timer, 5 * CLOCK_SECOND);
|
||||||
|
|
||||||
/*** Subsystem initialization ***/
|
/*** Subsystem initialization ***/
|
||||||
|
@ -6,7 +6,8 @@ enum CartEvent {
|
|||||||
CART_EVENT_ASSIGNED,
|
CART_EVENT_ASSIGNED,
|
||||||
CART_EVENT_CASH_OUT,
|
CART_EVENT_CASH_OUT,
|
||||||
CART_EVENT_NEW_PRODUCT,
|
CART_EVENT_NEW_PRODUCT,
|
||||||
CART_EVENT_CASH_OUT_ACK
|
CART_EVENT_CASH_OUT_ACK,
|
||||||
|
CART_EVENT_CASH_OUT_PARTIAL_LIST_ACK
|
||||||
} event;
|
} event;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -44,10 +44,13 @@ void net_recv(const void* data, uint16_t len, const linkaddr_t* src, const linka
|
|||||||
process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL);
|
process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL);
|
||||||
break;
|
break;
|
||||||
case START_OF_LIST_PRODUCTS_MSG:
|
case START_OF_LIST_PRODUCTS_MSG:
|
||||||
case PRODUCT_ACK:
|
|
||||||
event = CART_EVENT_CASH_OUT_ACK;
|
event = CART_EVENT_CASH_OUT_ACK;
|
||||||
process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL);
|
process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL);
|
||||||
break;
|
break;
|
||||||
|
case PRODUCT_PARTIAL_LIST_ACK:
|
||||||
|
event = CART_EVENT_CASH_OUT_PARTIAL_LIST_ACK;
|
||||||
|
process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
LOG_INFO("[W] message type %d unknown\n", ((msg*)data)->msg_type);
|
LOG_INFO("[W] message type %d unknown\n", ((msg*)data)->msg_type);
|
||||||
break;
|
break;
|
||||||
|
@ -8,6 +8,7 @@ linkaddr_t assigner_address;
|
|||||||
linkaddr_t cash_address;
|
linkaddr_t cash_address;
|
||||||
uint32_t customer_id = 1234;
|
uint32_t customer_id = 1234;
|
||||||
uint8_t nprod = 0;
|
uint8_t nprod = 0;
|
||||||
|
uint8_t remaining = 0;
|
||||||
uint16_t totalPrice = 0;
|
uint16_t totalPrice = 0;
|
||||||
uint8_t nprod_index = 0; //variable used to keep track of the index of the product to be sent
|
uint8_t nprod_index = 0; //variable used to keep track of the index of the product to be sent
|
||||||
product_t list[MAX_PRODUCT];
|
product_t list[MAX_PRODUCT];
|
||||||
@ -86,37 +87,52 @@ void s_shopping(process_event_t ev, process_data_t data) {
|
|||||||
void s_cash_out_wait4ack(process_event_t ev, process_data_t data) {
|
void s_cash_out_wait4ack(process_event_t ev, process_data_t data) {
|
||||||
/* Just wait for cash ack */
|
/* Just wait for cash ack */
|
||||||
if (event == CART_EVENT_CASH_OUT_ACK) {
|
if (event == CART_EVENT_CASH_OUT_ACK) {
|
||||||
printf("[I] Acknoweledgment received fromc cash. Now I'll send the list.\n");
|
printf("[I] Acknoweledgment received from cash. Total price is %d. Now I'll send the list.\n", totalPrice);
|
||||||
|
remaining = nprod;
|
||||||
status = CASH_OUT_SEND_LIST;
|
status = CASH_OUT_SEND_LIST;
|
||||||
/* this wakes up the process that otherwise would wait indefinitely for an event that will never occurr */
|
/* this wakes up the process that otherwise would wait indefinitely for an event that will never occur */
|
||||||
process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL);
|
process_post(&cart_main_process, PROCESS_EVENT_MSG, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void s_cash_out_send_list(process_event_t ev, process_data_t data) {
|
void s_cash_out_send_list(process_event_t ev, process_data_t data) {
|
||||||
/* Send list, then go back to initial state */
|
/* Send list, then go back to initial state */
|
||||||
printf("[I] Total price is %d\n", totalPrice);
|
|
||||||
|
|
||||||
if (nprod_index<nprod) {
|
printf("[I] Remaining %d products\n", (int)remaining);
|
||||||
LOG_INFO("[I] Sending product %d of %d: id %d, price: %d\n", nprod_index, nprod - 1, (int)list[nprod_index].product_id, (int)list[nprod_index].price);
|
|
||||||
product_msg m;
|
product_partial_list_msg m;
|
||||||
m.msg_type = PRODUCT_MSG;
|
m.msg_type = PRODUCT_PARTIAL_LIST_MSG;
|
||||||
m.customer_id = customer_id;
|
m.customer_id = customer_id;
|
||||||
m.product_id = list[nprod_index].product_id;
|
m.array_len = (remaining < PRODUCT_ARRAY_MAX_LEN) ? remaining : PRODUCT_ARRAY_MAX_LEN;
|
||||||
m.price = list[nprod_index].price;
|
|
||||||
net_send(&m, sizeof(m), &cash_address);
|
uint8_t done = nprod - remaining;
|
||||||
nprod_index++;
|
|
||||||
|
for (uint8_t i = 0; i < ((remaining < PRODUCT_ARRAY_MAX_LEN) ? remaining : PRODUCT_ARRAY_MAX_LEN); ++i) {
|
||||||
|
printf("[I] Adding %d to the list\n", (int)i);
|
||||||
|
m.p[i] = list[done + i];
|
||||||
}
|
}
|
||||||
if (nprod_index == nprod) {
|
|
||||||
|
printf("[I] sending list right now\n");
|
||||||
|
net_send(&m, sizeof(m), &cash_address);
|
||||||
|
|
||||||
|
remaining -= (remaining < PRODUCT_ARRAY_MAX_LEN) ? remaining : PRODUCT_ARRAY_MAX_LEN;
|
||||||
|
|
||||||
|
printf("[I] and now remaining %d products\n", (int)remaining);
|
||||||
|
|
||||||
|
if (remaining == 0) {
|
||||||
|
printf("[I] it is the end, so now I reset the state\n");
|
||||||
|
/* reset everything */
|
||||||
nprod = 0;
|
nprod = 0;
|
||||||
nprod_index = 0;
|
nprod_index = 0;
|
||||||
customer_id = 1234;
|
customer_id = 1234;
|
||||||
|
totalPrice = 0;
|
||||||
memset(&cash_address, 0, sizeof(cash_address));
|
memset(&cash_address, 0, sizeof(cash_address));
|
||||||
|
|
||||||
printf("[I] END. Go back to ASSOCIATED status\n");
|
printf("[I] List END. Go back to ASSOCIATED status\n");
|
||||||
etimer_restart(&broadcast_timer);
|
etimer_restart(&broadcast_timer);
|
||||||
totalPrice = 0;
|
// status = ASSOCIATED; // TODO DEBUG
|
||||||
status = ASSOCIATED;
|
status = SHOPPING;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ static void input_callback(const void* data, uint16_t len, const linkaddr_t* sou
|
|||||||
received_msg = (msg*) (&pkt.data);
|
received_msg = (msg*) (&pkt.data);
|
||||||
|
|
||||||
//we need to receive an additional message to start the process of receiving the products because if we start receiving the products immediately
|
//we need to receive an additional message to start the process of receiving the products because if we start receiving the products immediately
|
||||||
//in the case of parallel processes we wouldnt know to what client and what basket that product is assosiated with
|
//in the case of parallel processes we wouldnt know to what client and what basket that product is associated with
|
||||||
if (received_msg->msg_type == BASKET_MSG) {
|
if (received_msg->msg_type == BASKET_MSG) {
|
||||||
basket_msg *basket = (basket_msg*) (received_msg);
|
basket_msg *basket = (basket_msg*) (received_msg);
|
||||||
uint8_t index = index_free_spot(invoices);
|
uint8_t index = index_free_spot(invoices);
|
||||||
@ -101,23 +101,22 @@ static void input_callback(const void* data, uint16_t len, const linkaddr_t* sou
|
|||||||
NETSTACK_NETWORK.output(&(invoices[index].address_basket));
|
NETSTACK_NETWORK.output(&(invoices[index].address_basket));
|
||||||
} else
|
} else
|
||||||
printf("Reached max number of customers\n");
|
printf("Reached max number of customers\n");
|
||||||
} else if (received_msg->msg_type == PRODUCT_MSG) {
|
} else if (received_msg->msg_type == PRODUCT_PARTIAL_LIST_MSG) {
|
||||||
product_msg *product = (product_msg*)(received_msg);
|
product_partial_list_msg* product_list = (product_partial_list_msg*)(received_msg);
|
||||||
printf("Received id: %d, price %d\n", (int)product->product_id, (int)product->price);
|
|
||||||
uint8_t index = invoice_index(product->customer_id, invoices);
|
uint8_t index = invoice_index(product_list->customer_id, invoices);
|
||||||
|
|
||||||
|
printf("Receiving %d items from customer %d\n", ((int)product_list->array_len) & 0xff, (int)product_list->customer_id);
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < product_list->array_len; ++i) {
|
||||||
|
product_t p = product_list->p[i];
|
||||||
|
|
||||||
|
printf("Received id: %d, price %d\n", (int)p.product_id, (int)p.price);
|
||||||
|
|
||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
if (invoices[index].n_prods > 0) {
|
if (invoices[index].n_prods > 0) {
|
||||||
invoices[index].total_sum += product->price;
|
invoices[index].total_sum += p.price;
|
||||||
invoices[index].n_prods--;
|
invoices[index].n_prods--;
|
||||||
//send an ack to the cart so that it knows the cassa has received the product and can send the next product
|
|
||||||
msg product_ack;
|
|
||||||
product_ack.msg_type = PRODUCT_ACK;
|
|
||||||
nullnet_buf = (uint8_t*)&product_ack;
|
|
||||||
|
|
||||||
LOG_INFO("Sending acknowledgment for the product\n ");
|
|
||||||
nullnet_len = sizeof(product_ack);
|
|
||||||
NETSTACK_NETWORK.output(&(invoices[index].address_basket));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
if (invoices[index].n_prods == 0) {
|
if (invoices[index].n_prods == 0) {
|
||||||
printf("Total sum for client %d is %d\n", (int)invoices[index].customer_id, (int)invoices[index].total_sum);
|
printf("Total sum for client %d is %d\n", (int)invoices[index].customer_id, (int)invoices[index].total_sum);
|
||||||
@ -126,6 +125,16 @@ static void input_callback(const void* data, uint16_t len, const linkaddr_t* sou
|
|||||||
} else
|
} else
|
||||||
printf("Customer with that id is not associated to any basket!\n");
|
printf("Customer with that id is not associated to any basket!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//send an ack to the cart so that it knows the cash register has received the partial product list and can send the next part
|
||||||
|
msg product_ack;
|
||||||
|
product_ack.msg_type = PRODUCT_PARTIAL_LIST_ACK;
|
||||||
|
nullnet_buf = (uint8_t*)&product_ack;
|
||||||
|
LOG_INFO("Sending acknowledgment for the partial product list\n ");
|
||||||
|
nullnet_len = sizeof(product_ack);
|
||||||
|
NETSTACK_NETWORK.output(&(invoices[index].address_basket));
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include "product.h"
|
#include "product.h"
|
||||||
|
|
||||||
|
#define PRODUCT_ARRAY_MAX_LEN 5
|
||||||
|
|
||||||
enum message_type {
|
enum message_type {
|
||||||
ASSOCIATION_REQUEST_MSG,
|
ASSOCIATION_REQUEST_MSG,
|
||||||
ASSOCIATION_REPLY_MSG,
|
ASSOCIATION_REPLY_MSG,
|
||||||
@ -10,10 +12,10 @@ enum message_type {
|
|||||||
ASSIGNMENT_MSG,
|
ASSIGNMENT_MSG,
|
||||||
CASH_OUT_MSG,
|
CASH_OUT_MSG,
|
||||||
ITEM_MSG, /* from item to cart */
|
ITEM_MSG, /* from item to cart */
|
||||||
PRODUCT_MSG, /* from cart to cash */
|
PRODUCT_PARTIAL_LIST_MSG, /* from cart to cash */
|
||||||
BASKET_MSG,
|
BASKET_MSG,
|
||||||
START_OF_LIST_PRODUCTS_MSG,
|
START_OF_LIST_PRODUCTS_MSG,
|
||||||
PRODUCT_ACK //everytime the cassa receives a product message , it sends an ack to the cart so that the cart can send the next product message
|
PRODUCT_PARTIAL_LIST_ACK /* everytime the cash register receives a product message, it sends an ack to the cart so that the cart can send the next product message */
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct msg {
|
typedef struct msg {
|
||||||
@ -68,14 +70,13 @@ typedef struct cash_out_msg
|
|||||||
}cash_out_msg;
|
}cash_out_msg;
|
||||||
|
|
||||||
|
|
||||||
typedef struct product_msg
|
typedef struct product_partial_list_msg
|
||||||
{
|
{
|
||||||
enum message_type msg_type;
|
enum message_type msg_type;
|
||||||
uint32_t customer_id;
|
uint32_t customer_id;
|
||||||
uint32_t product_id;
|
uint8_t array_len;
|
||||||
uint32_t price;
|
product_t p[PRODUCT_ARRAY_MAX_LEN];
|
||||||
|
}product_partial_list_msg;
|
||||||
}product_msg;
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user