Version FC

Dependencies:   DmTftLibrary eeprom SX1280Lib filesystem mbed

Fork of MSNV2-Terminal_V1-5 by Francis CHATAIN

Committer:
patrick_duc
Date:
Fri Oct 19 15:08:35 2018 +0000
Branch:
Integration
Revision:
39:13e66d087ae9
Parent:
38:9b43b2415093
Suite d'int?gration (ne compile pas encore).

Who changed what in which revision?

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