Version FC

Dependencies:   DmTftLibrary eeprom SX1280Lib filesystem mbed

Fork of MSNV2-Terminal_V1-5 by Francis CHATAIN

Branch:
Integration
Revision:
38:9b43b2415093
Parent:
23:d7df2e2d28de
Child:
39:13e66d087ae9
--- a/Controller.cpp	Tue Oct 16 11:28:33 2018 +0000
+++ b/Controller.cpp	Fri Oct 19 06:32:59 2018 +0000
@@ -1,141 +1,126 @@
 /*
- * MISNet   
+ * MISNet
  *
- *  Controller.cpp 
+ *  Controller.cpp
  *
  *  Created on: August 17, 2018
  *      Author: Francis CHATAIN
  *
  */
 
-/*
-#include <sstream>
-#include <iostream>
-#include <iomanip>
-#include <string>
-*/
-
 #include "Context.h"
 
 #ifndef TEST_ENVIRONMENT
+#include "mbed
 #include "Tftlcd.h"
 #include "Lora.h"
+
+#else
+#include <unistd.h>
 #endif
 
 
-#include "main.h"
-#include "Controller.hpp"
+//#include "main.h"
 
+#include "TerminalInternalCommand.hpp"
+#include "Controller.hpp"
 #include "DataBase.hpp"
-#include "MessageFactory.hpp"
 #include "ExtMemory.hpp"
 
 
 using namespace misnet;
 
 
-static DataBase dataBase    ;                   // Database creation 
-static MessageFactory messageFactory    ;       // Messages Maker   (Sensors, Config, Synchro ...)
-static ExtMemory extMemory  ;                   // Memoire externe
+static DataBase dataBase;                   // Database creation
+
+DataBase& Controller::getDatabase() {
+    return database;
+}
+
+static ExtMemory extMemory;                   // Memoire externe
 
 #ifndef TEST_ENVIRONMENT
 Tftlcd     tftlcd      ;   // eventuellement a déplacer   (uniquement valable sur les conf nucléo)
 #endif
 
 
-
-Controller::Controller()  {   // constructeur 
-    // Basic  instance
-    //this.dataBase    =  DataBase;
-    //this.messageFactory     =  Message   ;      // Messages Maker   (Sensors, Config, Synchro ...)
-    uint32_t   aValue = 65985;
-    std::cout << "0x" << std::hex << aValue << std::endl;
-
-    std::stringstream stream;
-    
-    stream << "0x" << std::hex << aValue;
-    std::string result(stream.str());
-
-    std::cout << result.c_str() << std::endl;
+#ifndef TEST_ENVIRONMENT
+void Controller::start () {     // @brief Start the Controller
+    initInterfaces();   // Primary interface uart, i2c, spi ...
+    extMemory.read(&dataBase)  ;   // Read memory to know the list of sensor availables, fill DataBase
+    initSensors();   // Depend of the list of sensors  launch each
+    initActuators();   // Depend of the list of actuator launch each
 
-}
-
-Controller::~Controller() { }   // delete xxx;
-
-
-#ifndef TEST_ENVIRONMENT
-void Controller::start (){     // @brief Start the Controller
-    initInterfaces      ()  ;   // Primary interface uart, i2c, spi ...
-    extMemory.read      (&dataBase)  ;   // Read memory to know the list of sensor availables, fill DataBase
-    initSensors         ()  ;   // Depend of the list of sensors  launch each
-    initActuators       ()  ;   // Depend of the list of actuator launch each
-    
     // Read Radio parameter in Database for Lora interface   (example)
-    uint32_t                        freq    ; 
+    uint32_t                        freq    ;
     RadioLoRaBandwidths_t           bw      ;
-    RadioLoRaSpreadingFactors_t     sf      ;  
-    int8_t                          pwr     ; 
+    RadioLoRaSpreadingFactors_t     sf      ;
+    int8_t                          pwr     ;
     uint8_t                         bsz     ;
     int16_t                         terminal_heartbeat_period;
     int16_t                         payload_heartbeat_period;
-    dataBase.getRadioParameter  ( freq, bw, sf, pwr, bsz, terminal_heartbeat_period, payload_heartbeat_period) ; 
-     
-    initLora        ( freq, bw, sf, pwr ) ;    // Initialise the radio module 
-    
-    printf( "*** APP ***  start   %ld %d %d %d %d %d \r\n",freq, bw, sf, pwr, bsz, time) ; 
+    dataBase.getRadioParameter( freq, bw, sf, pwr, bsz, terminal_heartbeat_period, payload_heartbeat_period) ;
+
+    initLora(freq, bw, sf, pwr);    // Initialise the radio module
+
+    printf("*** APP ***  start   %ld %d %d %d %d %d \r\n", freq, bw, sf, pwr, bsz, time) ;
 
-    tftlcd.Update   ( freq, bw, sf, pwr, bsz,  payload_heartbeat_period ) ;     // Show information 
-    
+    tftlcd.Update(freq, bw, sf, pwr, bsz, payload_heartbeat_period);     // Show information
+
     printf( "*** APP_ ***  Start Controller (Send message GoodHealth)\r\n");
-    messageFactory.buildGoodhealth () ; 
-    //sendMessageLora () ; 
+    messageFactory.buildGoodhealth();
+    //sendMessageLora () ;
 }
 
-void Controller::initInterfaces    ()  {
+
+void Controller::initInterfaces() {
     // Depend of the configuration read on DataBase (memory origin)
-    
-    debugSerial = new RawSerial (USBTX,USBRX, 230400);        // Debug Link 
-    tftlcd.Init () ;  
-    // I2C, SPI, UART .... 
+
+    debugSerial = new RawSerial (USBTX,USBRX, 230400);        // Debug Link
+    tftlcd.Init () ;
+    // I2C, SPI, UART ....
 }
 
 
-void Controller::initSensors(){
-    printf( "*** APPP ***  initSensors \r\n");  
+void Controller::initSensors() {
+    printf("*** APPP ***  initSensors \r\n");
     // depend of the list identified   (example)
     //bme280 = new BME280(i2c_rt);
-    // depend of the list identified 
-    //bme280->init(config->getBME280_MODE()); 
-//    iks01a2.read () ; 
+    // depend of the list identified
+    //bme280->init(config->getBME280_MODE());
+    //    iks01a2.read () ;
 }
 
-void Controller::initActuators(){
+
+void Controller::initActuators() {
     // depend of the list identified   (example)
     //bme280 = new BME280(i2c_rt);
-    // depend of the list identified 
+    // depend of the list identified
     //bme280->init(config->getBME280_MODE());
 }
 
+
+/*
 void Controller::manageSensors     () {
-    readSensors                     () ; 
-    messageFactory.buildSensors           () ; 
-    sendMessage                     () ; 
+readSensors                     () ;
+messageFactory.buildSensors           () ;
+sendMessage                     () ;
 }
 
 void Controller::manageGoodhealth  () {
-    readSensors                     () ; 
-    messageFactory.buildGoodhealth        () ; 
-    //sendMessage                   () ; 
+readSensors                     () ;
+messageFactory.buildGoodhealth        () ;
+//sendMessage                   () ;
 }
 
 void Controller::manageSynchro     () {
-    messageFactory.buildSynchro           () ; 
-    //sendMessage                   () ; 
+messageFactory.buildSynchro           () ;
+//sendMessage                   () ;
 }
 
 void Controller::manageConfiguration   () {
-    messageFactory.buildConfiguration         () ; 
-    //sendMessage                       () ; 
+messageFactory.buildConfiguration         () ;
+//sendMessage                       () ;
 }
 
 void Controller::manageListening () {
@@ -144,31 +129,65 @@
 
 void Controller::getScheduling (uint16_t &TimerPayload,  uint16_t &TimerGoodhealth, uint16_t &TimerSynchro, uint16_t &TimerListening ) {
 
-    TimerPayload        = 3L     ;   // TODO  Dépend de la lecture de la memoire  (ici simulé)
-    TimerGoodhealth     = 60L    ; 
-    TimerSynchro        = 0L     ;  
-    TimerListening      = 0L     ;                  
+    TimerPayload        = 3L;   // TODO  Dépend de la lecture de la memoire  (ici simulé)
+    TimerGoodhealth     = 60L;
+    TimerSynchro        = 0L;
+    TimerListening      = 0L;
 }
 
-void Controller::sendMessage       () {
+void Controller::sendMessage() {
     printf( "*** APP_ ***  sendMessage \r\n");
-    sendMessageLora () ; 
+    sendMessageLora();
 }
 
 
-void Controller::readSensors       () {
+void Controller::readSensors() {
     printf( "*** APP_ ***  readSensors \r\n");
     printf("*** APP *** loop on all channels\r\n");
     short nbSensors = dataBase.getNbService(Service::SENSOR);
     printf("*** APP *** there are %d sensors in this payload\r\n", nbSensors);
-    // depend of the list identified 
+    // depend of the list identified
     // Each sensor sends an event in the db
-    //iks01a2.read () ; 
+    //iks01a2.read () ;
 }
 
-void Controller::writeActuators    () {
-    // depend of the list identified 
+
+void Controller::writeActuators() {
+    // depend of the list identified
     // Each sensor send can put a event in the queu
 }
+*/
+#endif
 
-#endif
+
+void misnet::Controller::loop() {
+    while (1) {
+        DEBUG("Avant test de la file des commandes... ");
+        if (! this->_commands.empty()) {
+            DEBUG("About to read next command...");
+            misnet::TerminalInternalCommand * command = _commands[0].second;
+            DEBUG(" Next command read. About to execute command...");
+            if (command->execute(&database)) {
+                DEBUG(" Command executed and completed.\n");
+                std::pop_heap(_commands.begin(), _commands.end());
+                _commands.pop_back();
+            }
+            else {
+                DEBUG("Command executed but not fully completed.\n");
+                sleep(5);
+            }
+        }
+        else {
+            DEBUG("No command, snoozing.\n");
+            sleep(5);
+        }
+    }
+}
+
+
+void Controller::addCommandInQueue(TerminalInternalCommand * command) {
+    DEBUG("Adding a command in queue\n");
+    //DEBUG("Adding a command in queue :
+    %s\n", command->toString().c_str());
+    this->_commands.push(command);
+}