Coordinator v2

Dependencies:   NerfUSXbee PinDetect EthernetInterface JSON MFRC522 WebSocketClient mbed-rtos mbed

Committer:
Ismael Balafrej
Date:
Tue Apr 11 12:40:05 2017 -0400
Revision:
2:019d8848cf7e
Parent:
1:e1c5259b7d9a
Final version without allies/enemies

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Ismael Balafrej 1:e1c5259b7d9a 1 #pragma once
Ismael Balafrej 1:e1c5259b7d9a 2 #include "ports.hpp"
Ismael Balafrej 1:e1c5259b7d9a 3 #include "EthernetInterface.h"
Ismael Balafrej 1:e1c5259b7d9a 4 #include "Websocket.h"
Ismael Balafrej 1:e1c5259b7d9a 5 #include "jsonParser.hpp"
Ismael Balafrej 1:e1c5259b7d9a 6 #include "dispatcher.hpp"
Ismael Balafrej 1:e1c5259b7d9a 7
Ismael Balafrej 1:e1c5259b7d9a 8 #define USING_DHCP_SERVER 0
Ismael Balafrej 1:e1c5259b7d9a 9
Ismael Balafrej 1:e1c5259b7d9a 10 EthernetInterface eth;
Ismael Balafrej 1:e1c5259b7d9a 11 Websocket ws("ws://1.0.0.1:8080/");
Ismael Balafrej 1:e1c5259b7d9a 12 Mail<ServerEvent, 30> messages_to_server;
Ismael Balafrej 1:e1c5259b7d9a 13 void websocket_message_send(ServerEvent event);
Ismael Balafrej 1:e1c5259b7d9a 14
Ismael Balafrej 1:e1c5259b7d9a 15 void ethernet_initialization() {
Ismael Balafrej 1:e1c5259b7d9a 16 #if USING_DHCP_SERVER
Ismael Balafrej 1:e1c5259b7d9a 17 eth.init();
Ismael Balafrej 1:e1c5259b7d9a 18 #else
Ismael Balafrej 1:e1c5259b7d9a 19 char ip_addr[]="1.0.0.2";
Ismael Balafrej 1:e1c5259b7d9a 20 char mask[]="255.255.255.0";
Ismael Balafrej 1:e1c5259b7d9a 21 char gateway[]="1.0.0.1";
Ismael Balafrej 1:e1c5259b7d9a 22 eth.init(ip_addr, mask, gateway);
Ismael Balafrej 1:e1c5259b7d9a 23 #endif
Ismael Balafrej 1:e1c5259b7d9a 24
Ismael Balafrej 1:e1c5259b7d9a 25 eth.connect();
Ismael Balafrej 1:e1c5259b7d9a 26 toPc("IP Address is %s", eth.getIPAddress());
Ismael Balafrej 1:e1c5259b7d9a 27 toPc("Mask is %s", eth.getNetworkMask());
Ismael Balafrej 1:e1c5259b7d9a 28 }
Ismael Balafrej 1:e1c5259b7d9a 29
Ismael Balafrej 1:e1c5259b7d9a 30 void websocket_initialization() {
Ismael Balafrej 1:e1c5259b7d9a 31 toPc("Connecting to websocket url: \"ws://1.0.0.1:8080/\"");
Ismael Balafrej 1:e1c5259b7d9a 32 ws.connect();
Ismael Balafrej 1:e1c5259b7d9a 33 if (ws.is_connected()) {
Ismael Balafrej 2:019d8848cf7e 34 ws.send("{\"event\":\"mbed\",\"data\":{\"state\": \"connected\"}}");
Ismael Balafrej 1:e1c5259b7d9a 35 }
Ismael Balafrej 1:e1c5259b7d9a 36 }
Ismael Balafrej 1:e1c5259b7d9a 37
Ismael Balafrej 1:e1c5259b7d9a 38 //Thread
Ismael Balafrej 1:e1c5259b7d9a 39 void websocket_message_receiver() {
Ismael Balafrej 1:e1c5259b7d9a 40 ethernet_initialization();
Ismael Balafrej 1:e1c5259b7d9a 41 websocket_initialization();
Ismael Balafrej 1:e1c5259b7d9a 42
Ismael Balafrej 1:e1c5259b7d9a 43 char msgBuffer[500];
Ismael Balafrej 1:e1c5259b7d9a 44 ServerEvent newEvent;
Ismael Balafrej 1:e1c5259b7d9a 45 while(1) {
Ismael Balafrej 1:e1c5259b7d9a 46 if (!ws.is_connected()) {
Ismael Balafrej 1:e1c5259b7d9a 47 toPc("Disconnected from websocket... Attempting to reconnect");
Ismael Balafrej 1:e1c5259b7d9a 48 websocket_initialization();
Ismael Balafrej 1:e1c5259b7d9a 49 } else {
Ismael Balafrej 1:e1c5259b7d9a 50 if (ws.read(msgBuffer)) {
Ismael Balafrej 1:e1c5259b7d9a 51 toPc("New message received from server");
Ismael Balafrej 1:e1c5259b7d9a 52 parse_server_event_from_json(msgBuffer, &newEvent);
Ismael Balafrej 1:e1c5259b7d9a 53 dispatch_event_from_server(&newEvent);
Ismael Balafrej 1:e1c5259b7d9a 54 }
Ismael Balafrej 1:e1c5259b7d9a 55 }
Ismael Balafrej 1:e1c5259b7d9a 56 Thread::wait(100);
Ismael Balafrej 1:e1c5259b7d9a 57 }
Ismael Balafrej 1:e1c5259b7d9a 58 }
Ismael Balafrej 1:e1c5259b7d9a 59
Ismael Balafrej 1:e1c5259b7d9a 60 //Via thread
Ismael Balafrej 1:e1c5259b7d9a 61 void websocket_message_send(ServerEvent event)
Ismael Balafrej 1:e1c5259b7d9a 62 {
Ismael Balafrej 1:e1c5259b7d9a 63 ServerEvent *newEvent = messages_to_server.alloc();
Ismael Balafrej 1:e1c5259b7d9a 64 *newEvent = event;
Ismael Balafrej 1:e1c5259b7d9a 65 messages_to_server.put(newEvent);
Ismael Balafrej 1:e1c5259b7d9a 66 }
Ismael Balafrej 1:e1c5259b7d9a 67
Ismael Balafrej 1:e1c5259b7d9a 68 //Direct send
Ismael Balafrej 1:e1c5259b7d9a 69 void send_message_to_websocket(ServerEvent *event)
Ismael Balafrej 1:e1c5259b7d9a 70 {
Ismael Balafrej 1:e1c5259b7d9a 71 char msgBuffer[500];
Ismael Balafrej 1:e1c5259b7d9a 72 if (strcmp(event->event, "start") == 0) {
Ismael Balafrej 1:e1c5259b7d9a 73 sprintf(msgBuffer, "{"
Ismael Balafrej 1:e1c5259b7d9a 74 "\"event\":\"%s\""
Ismael Balafrej 1:e1c5259b7d9a 75 "}", event->event);
Ismael Balafrej 1:e1c5259b7d9a 76 } else if (strcmp(event->event, "navigate") == 0) {
Ismael Balafrej 1:e1c5259b7d9a 77 sprintf(msgBuffer, "{"
Ismael Balafrej 1:e1c5259b7d9a 78 "\"event\":\"%s\","
Ismael Balafrej 1:e1c5259b7d9a 79 "\"data\":{"
Ismael Balafrej 1:e1c5259b7d9a 80 "\"direction\":\"%s\""
Ismael Balafrej 1:e1c5259b7d9a 81 "}"
Ismael Balafrej 1:e1c5259b7d9a 82 "}", event->event, event->data.direction);
Ismael Balafrej 1:e1c5259b7d9a 83 } else if (strcmp(event->event, "gun") == 0) {
Ismael Balafrej 1:e1c5259b7d9a 84 sprintf(msgBuffer, "{"
Ismael Balafrej 1:e1c5259b7d9a 85 "\"event\":\"%s\","
Ismael Balafrej 1:e1c5259b7d9a 86 "\"data\":{"
Ismael Balafrej 1:e1c5259b7d9a 87 "\"rfid_code\":\"%s\""
Ismael Balafrej 1:e1c5259b7d9a 88 "}"
Ismael Balafrej 1:e1c5259b7d9a 89 "}", event->event, event->data.rfid_code);
Ismael Balafrej 1:e1c5259b7d9a 90 } else { //event == report
Ismael Balafrej 1:e1c5259b7d9a 91 sprintf(msgBuffer, "{"
Ismael Balafrej 1:e1c5259b7d9a 92 "\"event\":\"%s\","
Ismael Balafrej 1:e1c5259b7d9a 93 "\"data\":{"
Ismael Balafrej 1:e1c5259b7d9a 94 "\"targets\": %i,"
Ismael Balafrej 1:e1c5259b7d9a 95 "\"averageReflexTime\": %i,"
Ismael Balafrej 1:e1c5259b7d9a 96 "\"gameLength\": %i,"
Ismael Balafrej 1:e1c5259b7d9a 97 "\"score\": %i"
Ismael Balafrej 1:e1c5259b7d9a 98 "}"
Ismael Balafrej 2:019d8848cf7e 99 "}", event->event, event->data.targets, event->data.averageReflexTime,
Ismael Balafrej 2:019d8848cf7e 100 event->data.gameLength, event->data.score);
Ismael Balafrej 1:e1c5259b7d9a 101 }
Ismael Balafrej 1:e1c5259b7d9a 102
Ismael Balafrej 1:e1c5259b7d9a 103 //toPc("Sending msg to websocket: %s", msgBuffer);
Ismael Balafrej 1:e1c5259b7d9a 104 ws.send(msgBuffer);
Ismael Balafrej 1:e1c5259b7d9a 105 }
Ismael Balafrej 1:e1c5259b7d9a 106
Ismael Balafrej 1:e1c5259b7d9a 107 //Thread
Ismael Balafrej 1:e1c5259b7d9a 108 void websocket_message_sender()
Ismael Balafrej 1:e1c5259b7d9a 109 {
Ismael Balafrej 1:e1c5259b7d9a 110 while(1) {
Ismael Balafrej 1:e1c5259b7d9a 111 osEvent evt = messages_to_server.get();
Ismael Balafrej 1:e1c5259b7d9a 112 if (evt.status == osEventMail) {
Ismael Balafrej 1:e1c5259b7d9a 113 ServerEvent *mail = (ServerEvent*)evt.value.p;
Ismael Balafrej 1:e1c5259b7d9a 114 send_message_to_websocket(mail);
Ismael Balafrej 1:e1c5259b7d9a 115 messages_to_server.free(mail);
Ismael Balafrej 1:e1c5259b7d9a 116 }
Ismael Balafrej 1:e1c5259b7d9a 117 Thread::yield();
Ismael Balafrej 1:e1c5259b7d9a 118 }
Ismael Balafrej 1:e1c5259b7d9a 119 }