Skip to content

Commit

Permalink
Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanM04 committed Oct 12, 2019
1 parent 63a6109 commit 6c84284
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 55 deletions.
20 changes: 17 additions & 3 deletions arduino/bus/uno/uno.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,23 @@ void startLCDTimeout()
timeFromLastLCD = millis();
}

// LCD helper
void writeOnLCD(int col, int row, String str)
{
lcd.setCursor(col, row);
lcd.print(str);
}

/*
There's an animation when loading:
". " > ".. " > "... " > "...." (and repeat)
This function does it.
*/
void connectingDots(int msgCol, String msg)
{
if (!connecting || connectingAccum == 4)
{
connecting = false;
connectingAccum = 0;
lcd.clear();
writeOnLCD(msgCol, 1, msg);
Expand All @@ -54,6 +61,7 @@ void connectingDots(int msgCol, String msg)
connecting = true;
}

// When a new message comes in the Serial, this runs
void newWifiStatus(String wifiStatus)
{
if (wifiStatus.startsWith("viajes:"))
Expand Down Expand Up @@ -83,9 +91,9 @@ void newWifiStatus(String wifiStatus)
startLCDTimeout();
}
else if (wifiStatus == "connected")
setLCDDefault();
else if (wifiStatus == "connecting")
connectingDots(3, "Arrancando");

else if (wifiStatus == "connecting")
connectingDots(3, "Arrancando");
else if (wifiStatus == "connection-lost")
connectingDots(2, "Reconectando");
}
Expand All @@ -99,6 +107,7 @@ void setup()

void loop()
{
// Serial reader
while (Serial.available() > 0)
{
serialAcum.concat(char(Serial.read()));
Expand All @@ -109,6 +118,11 @@ void loop()
serialAcum = "";
}

/*
Timeouts
LCD = 3s
Buzzer = .5s
*/
if (LCDTimeoutOn && millis() - timeFromLastLCD > 3000)
setLCDDefault();
if (BuzzerTimeoutOn && millis() - timeFromLastBuzzer > 500)
Expand Down
11 changes: 11 additions & 0 deletions arduino/bus/wemos/wemos.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bool connecting = false;
extern const unsigned char caCert[] PROGMEM;
extern const unsigned int caCertLen;

// HTTPS Request
void useViaje(String UID)
{
client.print(String("") +
Expand All @@ -37,10 +38,15 @@ void useViaje(String UID)
break;
}

/*
'!' is because the responses are "viajes:2!", "error!", etc.
It is like that because of performance
*/
String res = client.readStringUntil('!');
Serial.print(res);
}

// I don't know exactly how it works, but it works.
String getUID()
{
String content = "";
Expand Down Expand Up @@ -93,6 +99,11 @@ void loop()
Serial.print("connected");
}

/*
So, "mfrcRecentlyUsed" exists because of some weirdness of the two functions below.
If I have my card in the scanner indefinitely, these function will return [true, false, true, false, true, ...].
"mfrcRecentlyUsed" bypass that :)
*/
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial())
{
mfrcRecentlyUsed = true;
Expand Down
13 changes: 13 additions & 0 deletions arduino/checker/uno/uno.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ void startLCDTimeout()
timeFromLastLCD = millis();
}

// LCD helper
void writeOnLCD(int col, int row, String str)
{
lcd.setCursor(col, row);
lcd.print(str);
}

/*
There's an animation when loading:
". " > ".. " > "... " > "...." (and repeat)
This function does it.
*/
void connectingDots(int msgCol, String msg)
{
if (!connecting || connectingAccum == 4)
{
connecting = false;
connectingAccum = 0;
lcd.clear();
writeOnLCD(msgCol, 1, msg);
Expand All @@ -43,6 +50,7 @@ void connectingDots(int msgCol, String msg)
connecting = true;
}

// When a new message comes in the Serial, this runs
void newWifiStatus(String wifiStatus)
{
if (wifiStatus.startsWith("res:"))
Expand Down Expand Up @@ -83,6 +91,7 @@ void setup()

void loop()
{
// Serial reader
while (Serial.available() > 0)
{
serialAcum.concat(char(Serial.read()));
Expand All @@ -93,6 +102,10 @@ void loop()
serialAcum = "";
}

/*
Timeouts
LCD = 5s
*/
if (LCDTimeoutOn && millis() - timeFromLastLCD > 5000)
setLCDDefault();

Expand Down
11 changes: 11 additions & 0 deletions arduino/checker/wemos/wemos.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bool connecting = false;
extern const unsigned char caCert[] PROGMEM;
extern const unsigned int caCertLen;

// HTTPS Request
void useViaje(String UID)
{
client.print(String("") +
Expand All @@ -36,10 +37,15 @@ void useViaje(String UID)
break;
}

/*
'!' is because the responses are "viajes:2!", "error!", etc.
It is like that because of performance
*/
String res = client.readStringUntil('!');
Serial.print(res);
}

// I don't know exactly how it works, but it works.
String getUID()
{
String content = "";
Expand Down Expand Up @@ -92,6 +98,11 @@ void loop()
Serial.print("connected");
}

/*
So, "mfrcRecentlyUsed" exists because of some weirdness of the two functions below.
If I have my card in the scanner indefinitely, these function will return [true, false, true, false, true, ...].
"mfrcRecentlyUsed" bypass that :)
*/
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial())
{
mfrcRecentlyUsed = true;
Expand Down
1 change: 1 addition & 0 deletions arduino/control-center/uno/uno.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

// I don't know exactly how it works, but it works.
String getUID()
{
String content = "";
Expand Down
1 change: 1 addition & 0 deletions control-center/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function sendPorts() {
ipcMain.on('get-ports', sendPorts)

ipcMain.on('use-port', (e, port) => {
// An event listener to Serial
const serial = new SerialPort(port, { baudRate: 115200 })
const parser = serial.pipe(new Readline())

Expand Down
1 change: 1 addition & 0 deletions control-center/src/components/PortSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default ({ setPortSetted }) => {

useEffect(() => {
ipcRenderer.on('ports', (_, portsRes) => {
// We don't like COM1
portsRes = portsRes.filter(port => port.comName !== 'COM1')
setPorts(portsRes)
if (portsRes.length > 0) setPortSelected(portsRes[0].comName)
Expand Down
52 changes: 4 additions & 48 deletions control-center/utils/listeners.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const path = require('path'),
fetch = require('node-fetch'),
{ ApolloClient, HttpLink, InMemoryCache, gql } = require('apollo-boost'),
{ ApolloClient, HttpLink, InMemoryCache } = require('apollo-boost'),
queries = require('./queries'),
isDev = require('electron-is-dev')

require('dotenv').config({
path: path.resolve(__dirname, `../.env${isDev ? '.test' : ''}`),
})

// Apollo Client
const client = new ApolloClient({
link: new HttpLink({
uri: `${process.env.BASE_URL}/api/graphql`,
Expand All @@ -18,53 +20,6 @@ const client = new ApolloClient({
cache: new InMemoryCache(),
})

const queries = {
getUser: gql`
query($id: ID, $uid: String, $dni: Int) {
user(id: $id, uid: $uid, dni: $dni) {
id
uid
dni
viajes
}
}
`,
getPrices: gql`
query {
metadata {
pasePrice
viajePrice
}
}
`,
addViajes: gql`
mutation($id: ID, $newViajes: Int!) {
addViajes(id: $id, newViajes: $newViajes)
}
`,
createUser: gql`
mutation($uid: String!, $dni: Int!) {
createUser(uid: $uid, dni: $dni) {
id
}
}
`,
updateUser: gql`
mutation($id: ID!, $uid: String!, $dni: Int!, $viajes: Int!) {
updateUser(id: $id, uid: $uid, dni: $dni, viajes: $viajes) {
id
}
}
`,
updatePrices: gql`
mutation($pasePrice: Int!, $viajePrice: Int!) {
updateMetadata(pasePrice: $pasePrice, viajePrice: $viajePrice) {
id
}
}
`,
}

const sendStatusError = (err, e) => {
e.reply('status', {
type: 'ERROR',
Expand All @@ -73,6 +28,7 @@ const sendStatusError = (err, e) => {
}

module.exports = ipcMain => {
// Yep, there's a lot of listeners
ipcMain.on('get-user', (e, variables) =>
client
.query({ query: queries.getUser, variables })
Expand Down
48 changes: 48 additions & 0 deletions control-center/utils/queries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { gql } = require('apollo-boost')

module.exports = {
getUser: gql`
query($id: ID, $uid: String, $dni: Int) {
user(id: $id, uid: $uid, dni: $dni) {
id
uid
dni
viajes
}
}
`,
getPrices: gql`
query {
metadata {
pasePrice
viajePrice
}
}
`,
addViajes: gql`
mutation($id: ID, $newViajes: Int!) {
addViajes(id: $id, newViajes: $newViajes)
}
`,
createUser: gql`
mutation($uid: String!, $dni: Int!) {
createUser(uid: $uid, dni: $dni) {
id
}
}
`,
updateUser: gql`
mutation($id: ID!, $uid: String!, $dni: Int!, $viajes: Int!) {
updateUser(id: $id, uid: $uid, dni: $dni, viajes: $viajes) {
id
}
}
`,
updatePrices: gql`
mutation($pasePrice: Int!, $viajePrice: Int!) {
updateMetadata(pasePrice: $pasePrice, viajePrice: $viajePrice) {
id
}
}
`,
}
1 change: 1 addition & 0 deletions web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const isDev = process.env.NODE_ENV !== 'production'
module.exports = withSass(
withOffline({
webpack: config => {
// Aliases that let you do "import component from 'components'" instead of "import component from '../components'"
config.resolve.alias['pages'] = path.join(__dirname, 'src/pages')
config.resolve.alias['components'] = path.join(
__dirname,
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/api/checker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { text, send } = require('micro')
const { text } = require('micro')
const { prisma } = require('prisma')

module.exports = async (req, res) => {
Expand Down
6 changes: 6 additions & 0 deletions web/src/pages/api/graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const checkAuth = authed => {
if (!authed) throw new AuthenticationError('Inavlid Secret')
}

/*
Read slowly, because Prettier don't allow me to use enters :c
"checkAuth" checks if authed (which is validated in the context function in 'index.js')
*/

export default {
Query: {
user: async (_, { id, uid, dni }) => await prisma.user({ id, uid, dni }),
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/api/use-viaje.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { text, send } = require('micro')
const { text } = require('micro')
const { prisma } = require('prisma')

module.exports = async (req, res) => {
Expand Down
Loading

0 comments on commit 6c84284

Please sign in to comment.