1
0
beerbox/bluetooth.ino

538 lines
18 KiB
Arduino
Raw Normal View History

void bluetoothCommands() {
if (Serial3.available() > 0) {
char command = Serial3.read();
char subCommand = Serial3.read();
char incomingByte = Serial3.read();
String data;
while (incomingByte != ';') {
data = String(data + incomingByte);
incomingByte = Serial3.read();
}
if (command == '#') { //1 General Info Command
if (subCommand == '1') { // Get All Drinks
print_drinks_JSON(box, &Serial3);
} else if (subCommand == '2') { // Get All persons
Serial3.print("{\"persons\":[");
for (int i = 0; i < box->personCount; i++) {
if (i != 0)Serial3.print(",");
print_person_JSON(box, &persons[i], &Serial3, true);
print_person_JSON(box, &persons[i], &Serial, false);
delay(100);
}
Serial3.println("]}");
}
else if (subCommand == '3') { //reset all counts
for (int i = 0; i < box->personCount; i++) {
reset_person_drinks(box, &persons[i], i);
print_person_JSON(box, &persons[i], &Serial, false);
}
Serial3.println("{\"Success\"}");
}
} else if (command == '!') { //2 User Managment Command
if (subCommand == '1') { // Create new User
char incomingByte = Serial3.read();
String personName = data;
if (debug)Serial.println(personName);
lcd.clear();
lcd.print(personName);
if (debug)Serial.println(box->maxID);
//lcd.setCursor(0,1);
//lcd.print("Fin pls");
int fingerReturn = -1;
while (fingerReturn != 0)
{
fingerReturn = getFingerprintEnroll(box->maxID + 1);
}
lcd.clear();
lcd.print(personName);
lcd.setCursor(0, 1);
lcd.print("RFID pls");
zugang = "";
while (zugang == "") {
getRFIDEnroll();
}
if (debug)Serial.println(zugang);
box->maxID++;
Person* newPerson = new Person;
personName.toCharArray(newPerson->name, personName.length() + 1);
char tmp[FILE_NAME_LEN + 1];
sprintf(tmp, "p%03d%s", box->maxID, FILE_EXTENSION);
strcpy(newPerson->file_name, tmp);
zugang.toCharArray(newPerson->rfid_uuid, zugang.length() + 1);
newPerson->finger_uuid = box->maxID;
newPerson->credits_left = 1000;
for (int i = 0; i < box->maxDrink; i++) {
newPerson->drinks_taken[i] = 0;
}
write_pers_file(newPerson);
zugang = ""; // reset here
lcd.clear();
lcd.print(personName);
lcd.setCursor(0, 1);
lcd.print("Sucess!");
delay(1000);
//and revert to normal:
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
}//new User
else if (subCommand == '2') { //get user by filename
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
Person* aperson = &persons[getFileIndex(filename)];
print_person_JSON(box, aperson, &Serial3, false);
} else {
Serial3.println("{\"Error\":\"no such file\"}");
}
}//get user
else if (subCommand == '3') { //trigger user scan
lcd.clear();
lcd.print("Scanning");
lcd.setCursor(0, 1);
lcd.print("...");
box->scanning = true;
while (box->scanning) {
checkFinger();
checkRFID();
}
//After scan reset display
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
}//user scan
else if (subCommand == '4') { //revoke user finger by filename
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
Person* changePerson = &persons[getFileIndex(filename)];
lcd.clear();
lcd.print(changePerson->name);
int fingerReturn = -1;
while (fingerReturn != 0)
{
fingerReturn = getFingerprintEnroll(changePerson->finger_uuid);
}
Serial3.println("{\"Success\"}");
//No need to save finger since id is not changed in the process
} else {
Serial3.println("{\"Error\":\"no such file\"}");
}
//reset display
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
}//revoke finger
else if (subCommand == '5') { //revoke user rfid by filename
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
lastPersonIndex = getFileIndex(filename);
Person* changePerson = &persons[lastPersonIndex];
lcd.clear();
lcd.print(changePerson->name);
lcd.setCursor(0, 1);
lcd.print("RFID pls");
zugang = "";
while (zugang == "") {
getRFIDEnroll();
}
if (debug)Serial.println(zugang);
//save zugang
zugang.toCharArray(changePerson->rfid_uuid, zugang.length() + 1);
//lastPersonIndex is set in the beginning
update_pers_file();
Serial3.println("{\"Success\"}");
//reset display
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
} else {
Serial3.println("{\"Error\":\"no such file\"}");
}
}//revoke rfid
else if (subCommand == '6') { //delete user by filename
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
//Delete file
SD.remove(filename);
//rescan it!
delete [] persons; // When done, free memory pointed to
persons = NULL;
persons = readAllPersons();
if (debug)Serial.println(sizeof(persons));
} else {
Serial3.println("{\"Error\":\"no such file\"}");
}
}//delete user
else if (subCommand == '7') { //set user credits
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
//Scann second time for credits
char incomingByte = Serial3.read();
String data2;
while (incomingByte != ';') {
data2 = String(data2 + incomingByte);
incomingByte = Serial3.read();
}
char credits_string[data2.length() + 1];
data2.toCharArray(credits_string, data2.length() + 1);
lastPersonIndex = getFileIndex(filename);
Person* setPerson = &persons[lastPersonIndex];
setPerson->credits_left = atoi(credits_string);
update_pers_file();
print_person_JSON(box, setPerson, &Serial3, false);
} else {
Serial3.println("{\"Error\":\"no such file\"}");
}
}//set credits
else if (subCommand == '8') { //set name by filename
//TODO: Better namechecking
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
//Scann second time for newName
char incomingByte = Serial3.read();
String data2;
while (incomingByte != ';') {
data2 = String(data2 + incomingByte);
incomingByte = Serial3.read();
}
char name_string[11];
data2.toCharArray(name_string, data2.length() + 1);
lastPersonIndex = getFileIndex(filename);
Person* setPerson = &persons[lastPersonIndex];
strcpy(setPerson->name, name_string);
update_pers_file();
print_person_JSON(box, setPerson, &Serial3, false);
} else {
Serial3.println("{\"Error\":\"no such file\"}");
}
}//set name
} // User Managment Command
} //BLE available
}//end function
void serialCom() {
if (Serial.available() > 0) {
char command = Serial.read();
char subCommand = Serial.read();
char incomingByte = Serial.read();
String data;
while (incomingByte != ';') {
data = String(data + incomingByte);
incomingByte = Serial.read();
}
if (command == '#') { //1 General Info Command
if (subCommand == '1') { // Get All Drinks
print_drinks_JSON(box, &Serial);
}
else if (subCommand == '2') { // Get All persons
Serial.print("{\"persons\":[");
for (int i = 0; i < box->personCount; i++) {
if (i != 0)Serial.print(",");
print_person_JSON(box, &persons[i], &Serial, true);
print_person_JSON(box, &persons[i], &Serial, false);
delay(100);
}
Serial.println("]}");
}
else if (subCommand == '3') { //reset all counts
for (int i = 0; i < box->personCount; i++) {
reset_person_drinks(box, &persons[i], i);
print_person_JSON(box, &persons[i], &Serial, false);
}
Serial.println("{\"Success\"}");
}
}
else if (command == '!') { //2 User Managment Command
if (subCommand == '1') { // Create new User
char incomingByte = Serial.read();
String personName = data;
if (debug)Serial.println(personName);
lcd.clear();
lcd.print(personName);
if (debug)Serial.println(box->maxID);
//lcd.setCursor(0,1);
//lcd.print("Fin pls");
int fingerReturn = -1;
while (fingerReturn != 0)
{
fingerReturn = getFingerprintEnroll(box->maxID + 1);
}
lcd.clear();
lcd.print(personName);
lcd.setCursor(0, 1);
lcd.print("RFID pls");
zugang = "";
while (zugang == "") {
getRFIDEnroll();
}
if (debug)Serial.println(zugang);
box->maxID++;
Person* newPerson = new Person;
personName.toCharArray(newPerson->name, personName.length() + 1);
char tmp[FILE_NAME_LEN + 1];
sprintf(tmp, "p%03d%s", box->maxID, FILE_EXTENSION);
strcpy(newPerson->file_name, tmp);
zugang.toCharArray(newPerson->rfid_uuid, zugang.length() + 1);
newPerson->finger_uuid = box->maxID;
newPerson->credits_left = 1000;
for (int i = 0; i < box->maxDrink; i++) {
newPerson->drinks_taken[i] = 0;
}
write_pers_file(newPerson);
zugang = ""; // reset here
lcd.clear();
lcd.print(personName);
lcd.setCursor(0, 1);
lcd.print("Sucess!");
delay(1000);
//and revert to normal:
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
}//new User
else if (subCommand == '2') { //get user by filename
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
Person* aperson = &persons[getFileIndex(filename)];
print_person_JSON(box, aperson, &Serial, false);
} else {
Serial.println("{\"Error\":\"no such file\"}");
}
}//get user
else if (subCommand == '3') { //trigger user scan
lcd.clear();
lcd.print("Scanning");
lcd.setCursor(0, 1);
lcd.print("...");
box->scanning = true;
while (box->scanning) {
checkFinger();
checkRFID();
}
//After scan reset display
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
}//user scan
else if (subCommand == '4') { //revoke user finger by filename
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
Person* changePerson = &persons[getFileIndex(filename)];
lcd.clear();
lcd.print(changePerson->name);
int fingerReturn = -1;
while (fingerReturn != 0)
{
fingerReturn = getFingerprintEnroll(changePerson->finger_uuid);
}
Serial.println("{\"Success\"}");
//No need to save finger since id is not changed in the process
} else {
Serial.println("{\"Error\":\"no such file\"}");
}
//reset display
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
}//revoke finger
else if (subCommand == '5') { //revoke user rfid by filename
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
lastPersonIndex = getFileIndex(filename);
Person* changePerson = &persons[lastPersonIndex];
lcd.clear();
lcd.print(changePerson->name);
lcd.setCursor(0, 1);
lcd.print("RFID pls");
zugang = "";
while (zugang == "") {
getRFIDEnroll();
}
if (debug)Serial.println(zugang);
//save zugang
zugang.toCharArray(changePerson->rfid_uuid, zugang.length() + 1);
//lastPersonIndex is set in the beginning
update_pers_file();
Serial.println("{\"Success\"}");
//reset display
lcd.clear();
lcd.print("Drink:");
lcd.setCursor(0, 1);
lcd.print(box->drinks[currentDrink].name);
} else {
Serial.println("{\"Error\":\"no such file\"}");
}
}//revoke rfid
else if (subCommand == '6') { //delete user by filename
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
//Delete file
SD.remove(filename);
//rescan it!
delete [] persons; // When done, free memory pointed to
persons = NULL;
persons = readAllPersons();
if (debug)Serial.println(sizeof(persons));
} else {
Serial.println("{\"Error\":\"no such file\"}");
}
}//delete user
else if (subCommand == '7') { //set user credits
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
//Scann second time for credits
char incomingByte = Serial.read();
String data2;
while (incomingByte != ';') {
data2 = String(data2 + incomingByte);
incomingByte = Serial.read();
}
char credits_string[data2.length() + 1];
data2.toCharArray(credits_string, data2.length() + 1);
lastPersonIndex = getFileIndex(filename);
Person* setPerson = &persons[lastPersonIndex];
setPerson->credits_left = atoi(credits_string);
update_pers_file();
print_person_JSON(box, setPerson, &Serial, false);
} else {
Serial.println("{\"Error\":\"no such file\"}");
}
}//set credits
else if (subCommand == '8') { //set name by filename
//TODO: Better namechecking
char filename[data.length() + 1];
Serial.println(data);
data.toCharArray(filename, data.length() + 1);
if (personExists(filename)) {
//Scann second time for newName
char incomingByte = Serial.read();
String data2;
while (incomingByte != ';') {
data2 = String(data2 + incomingByte);
incomingByte = Serial.read();
}
char name_string[11];
data2.toCharArray(name_string, data2.length() + 1);
lastPersonIndex = getFileIndex(filename);
Person* setPerson = &persons[lastPersonIndex];
strcpy(setPerson->name, name_string);
update_pers_file();
print_person_JSON(box, setPerson, &Serial, false);
} else {
Serial.println("{\"Error\":\"no such file\"}");
}
}//set name
} // User Managment Command
} //Serial available
}//end function
void print_person_JSON(Beerbox *box, Person *aperson, HardwareSerial* com, bool less) {
if (less) {
sprintf(toprint, "{\"name\": \"%s\",\"filename\":\"%s\"}", aperson->name, aperson->file_name);
com->write(toprint);
} else {
sprintf(toprint, "{\"name\": \"%s\",\"filename\":\"%s\",\"creditsleft\":\"%i\",\"drinks\":[", aperson->name, aperson->file_name, aperson->credits_left);
com->write(toprint);
for (int i = 0; i < box->maxDrink; i++) {
if (aperson->drinks_taken[i] != -1) {
if (i == 0) sprintf(toprint, "{\"%s\":\"%i\"}", box->drinks[i].name, aperson->drinks_taken[i]);
else sprintf(toprint, ",{\"%s\":\"%i\"}", box->drinks[i].name, aperson->drinks_taken[i]);
com->write(toprint);
}
}
com->println("]}");
box->scanning = false;
}
} // end print JSON
void print_drinks_JSON(Beerbox *box, HardwareSerial* com) {
com->print("{\"drinks\":[");
for (int i = 0; i < box->maxDrink; i++) {
if (box->drinks[i].price != -1) {
if (i == 0) sprintf(toprint, "{\"id\":\"%i\",\"name\":\"%s\",\"price\":\"%i\"}", i, box->drinks[i].name, box->drinks[i].price);
else sprintf(toprint, ",{\"id\":\"%i\",\"name\":\"%s\",\"price\":\"%i\"}", i, box->drinks[i].name, box->drinks[i].price);
com->write(toprint);
}
}
com->print("]}");
}
void reset_person_drinks(Beerbox *box,Person *aperson,int personIndex){
for (int i = 0; i < box->maxDrink; i++) {
if (box->drinks[i].price != -1) {
aperson->drinks_taken[i] = 0;
lastPersonIndex = personIndex;
update_pers_file();
}
}
}