Version FC

Dependencies:   DmTftLibrary eeprom SX1280Lib filesystem mbed

Fork of MSNV2-Terminal_V1-5 by Francis CHATAIN

Committer:
patrick_duc
Date:
Fri Oct 19 06:32:59 2018 +0000
Branch:
Integration
Revision:
38:9b43b2415093
Parent:
23:d7df2e2d28de
Child:
39:13e66d087ae9
Suite d'int?gration. Compilation  non encore test?e.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
patrick_duc 21:8524d815c587 1 /*
patrick_duc 38:9b43b2415093 2 * MISNet
patrick_duc 21:8524d815c587 3 *
patrick_duc 38:9b43b2415093 4 * Controller.cpp
patrick_duc 21:8524d815c587 5 *
patrick_duc 21:8524d815c587 6 * Created on: August 17, 2018
patrick_duc 21:8524d815c587 7 * Author: Francis CHATAIN
patrick_duc 21:8524d815c587 8 *
patrick_duc 21:8524d815c587 9 */
patrick_duc 21:8524d815c587 10
patrick_duc 21:8524d815c587 11 #include "Context.h"
patrick_duc 21:8524d815c587 12
patrick_duc 21:8524d815c587 13 #ifndef TEST_ENVIRONMENT
patrick_duc 38:9b43b2415093 14 #include "mbed
patrick_duc 21:8524d815c587 15 #include "Tftlcd.h"
patrick_duc 21:8524d815c587 16 #include "Lora.h"
patrick_duc 38:9b43b2415093 17
patrick_duc 38:9b43b2415093 18 #else
patrick_duc 38:9b43b2415093 19 #include <unistd.h>
patrick_duc 21:8524d815c587 20 #endif
patrick_duc 21:8524d815c587 21
patrick_duc 21:8524d815c587 22
patrick_duc 38:9b43b2415093 23 //#include "main.h"
patrick_duc 21:8524d815c587 24
patrick_duc 38:9b43b2415093 25 #include "TerminalInternalCommand.hpp"
patrick_duc 38:9b43b2415093 26 #include "Controller.hpp"
patrick_duc 21:8524d815c587 27 #include "DataBase.hpp"
patrick_duc 21:8524d815c587 28 #include "ExtMemory.hpp"
patrick_duc 21:8524d815c587 29
patrick_duc 21:8524d815c587 30
patrick_duc 21:8524d815c587 31 using namespace misnet;
patrick_duc 21:8524d815c587 32
patrick_duc 21:8524d815c587 33
patrick_duc 38:9b43b2415093 34 static DataBase dataBase; // Database creation
patrick_duc 38:9b43b2415093 35
patrick_duc 38:9b43b2415093 36 DataBase& Controller::getDatabase() {
patrick_duc 38:9b43b2415093 37 return database;
patrick_duc 38:9b43b2415093 38 }
patrick_duc 38:9b43b2415093 39
patrick_duc 38:9b43b2415093 40 static ExtMemory extMemory; // Memoire externe
patrick_duc 21:8524d815c587 41
patrick_duc 21:8524d815c587 42 #ifndef TEST_ENVIRONMENT
patrick_duc 21:8524d815c587 43 Tftlcd tftlcd ; // eventuellement a déplacer (uniquement valable sur les conf nucléo)
patrick_duc 21:8524d815c587 44 #endif
patrick_duc 21:8524d815c587 45
patrick_duc 21:8524d815c587 46
patrick_duc 38:9b43b2415093 47 #ifndef TEST_ENVIRONMENT
patrick_duc 38:9b43b2415093 48 void Controller::start () { // @brief Start the Controller
patrick_duc 38:9b43b2415093 49 initInterfaces(); // Primary interface uart, i2c, spi ...
patrick_duc 38:9b43b2415093 50 extMemory.read(&dataBase) ; // Read memory to know the list of sensor availables, fill DataBase
patrick_duc 38:9b43b2415093 51 initSensors(); // Depend of the list of sensors launch each
patrick_duc 38:9b43b2415093 52 initActuators(); // Depend of the list of actuator launch each
patrick_duc 23:d7df2e2d28de 53
patrick_duc 21:8524d815c587 54 // Read Radio parameter in Database for Lora interface (example)
patrick_duc 38:9b43b2415093 55 uint32_t freq ;
patrick_duc 21:8524d815c587 56 RadioLoRaBandwidths_t bw ;
patrick_duc 38:9b43b2415093 57 RadioLoRaSpreadingFactors_t sf ;
patrick_duc 38:9b43b2415093 58 int8_t pwr ;
patrick_duc 21:8524d815c587 59 uint8_t bsz ;
patrick_duc 21:8524d815c587 60 int16_t terminal_heartbeat_period;
patrick_duc 21:8524d815c587 61 int16_t payload_heartbeat_period;
patrick_duc 38:9b43b2415093 62 dataBase.getRadioParameter( freq, bw, sf, pwr, bsz, terminal_heartbeat_period, payload_heartbeat_period) ;
patrick_duc 38:9b43b2415093 63
patrick_duc 38:9b43b2415093 64 initLora(freq, bw, sf, pwr); // Initialise the radio module
patrick_duc 38:9b43b2415093 65
patrick_duc 38:9b43b2415093 66 printf("*** APP *** start %ld %d %d %d %d %d \r\n", freq, bw, sf, pwr, bsz, time) ;
patrick_duc 21:8524d815c587 67
patrick_duc 38:9b43b2415093 68 tftlcd.Update(freq, bw, sf, pwr, bsz, payload_heartbeat_period); // Show information
patrick_duc 38:9b43b2415093 69
patrick_duc 21:8524d815c587 70 printf( "*** APP_ *** Start Controller (Send message GoodHealth)\r\n");
patrick_duc 38:9b43b2415093 71 messageFactory.buildGoodhealth();
patrick_duc 38:9b43b2415093 72 //sendMessageLora () ;
patrick_duc 21:8524d815c587 73 }
patrick_duc 21:8524d815c587 74
patrick_duc 38:9b43b2415093 75
patrick_duc 38:9b43b2415093 76 void Controller::initInterfaces() {
patrick_duc 21:8524d815c587 77 // Depend of the configuration read on DataBase (memory origin)
patrick_duc 38:9b43b2415093 78
patrick_duc 38:9b43b2415093 79 debugSerial = new RawSerial (USBTX,USBRX, 230400); // Debug Link
patrick_duc 38:9b43b2415093 80 tftlcd.Init () ;
patrick_duc 38:9b43b2415093 81 // I2C, SPI, UART ....
patrick_duc 21:8524d815c587 82 }
patrick_duc 21:8524d815c587 83
patrick_duc 21:8524d815c587 84
patrick_duc 38:9b43b2415093 85 void Controller::initSensors() {
patrick_duc 38:9b43b2415093 86 printf("*** APPP *** initSensors \r\n");
patrick_duc 21:8524d815c587 87 // depend of the list identified (example)
patrick_duc 21:8524d815c587 88 //bme280 = new BME280(i2c_rt);
patrick_duc 38:9b43b2415093 89 // depend of the list identified
patrick_duc 38:9b43b2415093 90 //bme280->init(config->getBME280_MODE());
patrick_duc 38:9b43b2415093 91 // iks01a2.read () ;
patrick_duc 21:8524d815c587 92 }
patrick_duc 21:8524d815c587 93
patrick_duc 38:9b43b2415093 94
patrick_duc 38:9b43b2415093 95 void Controller::initActuators() {
patrick_duc 21:8524d815c587 96 // depend of the list identified (example)
patrick_duc 21:8524d815c587 97 //bme280 = new BME280(i2c_rt);
patrick_duc 38:9b43b2415093 98 // depend of the list identified
patrick_duc 21:8524d815c587 99 //bme280->init(config->getBME280_MODE());
patrick_duc 21:8524d815c587 100 }
patrick_duc 21:8524d815c587 101
patrick_duc 38:9b43b2415093 102
patrick_duc 38:9b43b2415093 103 /*
patrick_duc 21:8524d815c587 104 void Controller::manageSensors () {
patrick_duc 38:9b43b2415093 105 readSensors () ;
patrick_duc 38:9b43b2415093 106 messageFactory.buildSensors () ;
patrick_duc 38:9b43b2415093 107 sendMessage () ;
patrick_duc 21:8524d815c587 108 }
patrick_duc 21:8524d815c587 109
patrick_duc 21:8524d815c587 110 void Controller::manageGoodhealth () {
patrick_duc 38:9b43b2415093 111 readSensors () ;
patrick_duc 38:9b43b2415093 112 messageFactory.buildGoodhealth () ;
patrick_duc 38:9b43b2415093 113 //sendMessage () ;
patrick_duc 21:8524d815c587 114 }
patrick_duc 21:8524d815c587 115
patrick_duc 21:8524d815c587 116 void Controller::manageSynchro () {
patrick_duc 38:9b43b2415093 117 messageFactory.buildSynchro () ;
patrick_duc 38:9b43b2415093 118 //sendMessage () ;
patrick_duc 21:8524d815c587 119 }
patrick_duc 21:8524d815c587 120
patrick_duc 21:8524d815c587 121 void Controller::manageConfiguration () {
patrick_duc 38:9b43b2415093 122 messageFactory.buildConfiguration () ;
patrick_duc 38:9b43b2415093 123 //sendMessage () ;
patrick_duc 21:8524d815c587 124 }
patrick_duc 21:8524d815c587 125
patrick_duc 21:8524d815c587 126 void Controller::manageListening () {
patrick_duc 21:8524d815c587 127 }
patrick_duc 21:8524d815c587 128
patrick_duc 21:8524d815c587 129
patrick_duc 21:8524d815c587 130 void Controller::getScheduling (uint16_t &TimerPayload, uint16_t &TimerGoodhealth, uint16_t &TimerSynchro, uint16_t &TimerListening ) {
patrick_duc 21:8524d815c587 131
patrick_duc 38:9b43b2415093 132 TimerPayload = 3L; // TODO Dépend de la lecture de la memoire (ici simulé)
patrick_duc 38:9b43b2415093 133 TimerGoodhealth = 60L;
patrick_duc 38:9b43b2415093 134 TimerSynchro = 0L;
patrick_duc 38:9b43b2415093 135 TimerListening = 0L;
patrick_duc 21:8524d815c587 136 }
patrick_duc 21:8524d815c587 137
patrick_duc 38:9b43b2415093 138 void Controller::sendMessage() {
patrick_duc 21:8524d815c587 139 printf( "*** APP_ *** sendMessage \r\n");
patrick_duc 38:9b43b2415093 140 sendMessageLora();
patrick_duc 21:8524d815c587 141 }
patrick_duc 21:8524d815c587 142
patrick_duc 21:8524d815c587 143
patrick_duc 38:9b43b2415093 144 void Controller::readSensors() {
patrick_duc 21:8524d815c587 145 printf( "*** APP_ *** readSensors \r\n");
patrick_duc 21:8524d815c587 146 printf("*** APP *** loop on all channels\r\n");
patrick_duc 21:8524d815c587 147 short nbSensors = dataBase.getNbService(Service::SENSOR);
patrick_duc 21:8524d815c587 148 printf("*** APP *** there are %d sensors in this payload\r\n", nbSensors);
patrick_duc 38:9b43b2415093 149 // depend of the list identified
patrick_duc 21:8524d815c587 150 // Each sensor sends an event in the db
patrick_duc 38:9b43b2415093 151 //iks01a2.read () ;
patrick_duc 21:8524d815c587 152 }
patrick_duc 21:8524d815c587 153
patrick_duc 38:9b43b2415093 154
patrick_duc 38:9b43b2415093 155 void Controller::writeActuators() {
patrick_duc 38:9b43b2415093 156 // depend of the list identified
patrick_duc 21:8524d815c587 157 // Each sensor send can put a event in the queu
patrick_duc 21:8524d815c587 158 }
patrick_duc 38:9b43b2415093 159 */
patrick_duc 38:9b43b2415093 160 #endif
patrick_duc 21:8524d815c587 161
patrick_duc 38:9b43b2415093 162
patrick_duc 38:9b43b2415093 163 void misnet::Controller::loop() {
patrick_duc 38:9b43b2415093 164 while (1) {
patrick_duc 38:9b43b2415093 165 DEBUG("Avant test de la file des commandes... ");
patrick_duc 38:9b43b2415093 166 if (! this->_commands.empty()) {
patrick_duc 38:9b43b2415093 167 DEBUG("About to read next command...");
patrick_duc 38:9b43b2415093 168 misnet::TerminalInternalCommand * command = _commands[0].second;
patrick_duc 38:9b43b2415093 169 DEBUG(" Next command read. About to execute command...");
patrick_duc 38:9b43b2415093 170 if (command->execute(&database)) {
patrick_duc 38:9b43b2415093 171 DEBUG(" Command executed and completed.\n");
patrick_duc 38:9b43b2415093 172 std::pop_heap(_commands.begin(), _commands.end());
patrick_duc 38:9b43b2415093 173 _commands.pop_back();
patrick_duc 38:9b43b2415093 174 }
patrick_duc 38:9b43b2415093 175 else {
patrick_duc 38:9b43b2415093 176 DEBUG("Command executed but not fully completed.\n");
patrick_duc 38:9b43b2415093 177 sleep(5);
patrick_duc 38:9b43b2415093 178 }
patrick_duc 38:9b43b2415093 179 }
patrick_duc 38:9b43b2415093 180 else {
patrick_duc 38:9b43b2415093 181 DEBUG("No command, snoozing.\n");
patrick_duc 38:9b43b2415093 182 sleep(5);
patrick_duc 38:9b43b2415093 183 }
patrick_duc 38:9b43b2415093 184 }
patrick_duc 38:9b43b2415093 185 }
patrick_duc 38:9b43b2415093 186
patrick_duc 38:9b43b2415093 187
patrick_duc 38:9b43b2415093 188 void Controller::addCommandInQueue(TerminalInternalCommand * command) {
patrick_duc 38:9b43b2415093 189 DEBUG("Adding a command in queue\n");
patrick_duc 38:9b43b2415093 190 //DEBUG("Adding a command in queue :
patrick_duc 38:9b43b2415093 191 %s\n", command->toString().c_str());
patrick_duc 38:9b43b2415093 192 this->_commands.push(command);
patrick_duc 38:9b43b2415093 193 }