Sergei G / Mbed OS Borsch

Dependencies:   DataStore JobScheduler NetworkServices W5500Interface nanopb protocol

Files at this revision

API Documentation at this revision

Comitter:
sgnezdov
Date:
Thu Aug 03 00:07:50 2017 +0000
Parent:
12:ec140a26367f
Child:
14:65d69aed6540
Commit message:
work in progress on reporting list of jobs; Issue: At time is zero

Changed in this revision

JobScheduler.lib Show annotated file Show diff for this revision Revisions of this file
protocol.lib Show annotated file Show diff for this revision Revisions of this file
source/config.h Show annotated file Show diff for this revision Revisions of this file
source/jobSchedulesUpload.cpp Show annotated file Show diff for this revision Revisions of this file
source/jobSchedulesUpload.h Show annotated file Show diff for this revision Revisions of this file
source/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/JobScheduler.lib	Wed Aug 02 14:48:16 2017 +0000
+++ b/JobScheduler.lib	Thu Aug 03 00:07:50 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/sgnezdov/code/JobScheduler/#684ddfc57199
+https://developer.mbed.org/users/sgnezdov/code/JobScheduler/#f61b62b119dd
--- a/protocol.lib	Wed Aug 02 14:48:16 2017 +0000
+++ b/protocol.lib	Thu Aug 03 00:07:50 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/users/sgnezdov/code/protocol/#83c12dd43854
+https://developer.mbed.org/users/sgnezdov/code/protocol/#67db916a7e08
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/config.h	Thu Aug 03 00:07:50 2017 +0000
@@ -0,0 +1,15 @@
+#pragma once
+
+class Config {
+    public:
+    
+    Config(char* sn): _sn(sn) {}
+    
+    const char* SerialNumber() {
+        return _sn;
+    }
+            
+    private:
+    
+    char* _sn;
+};
\ No newline at end of file
--- a/source/jobSchedulesUpload.cpp	Wed Aug 02 14:48:16 2017 +0000
+++ b/source/jobSchedulesUpload.cpp	Thu Aug 03 00:07:50 2017 +0000
@@ -3,8 +3,83 @@
 #include "mbed-trace/mbed_trace.h"
 #define TRACE_GROUP  "scup"
 
+#include "nanopb/source/protobuf/pb.h"
+#include "nanopb/source/protobuf/pb_encode.h"
+#include "protocol/source/job.pb.h"
+#include <string.h>
+#include "LinkedList.h"
+#include "scheduler.h"
+#include "schedules.h"
+
+// encode signature
+// bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, void * const *arg);
+//bool pbOutStream(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
+//    return true;
+//}
+
+bool fillScheduleData(protocol_Job_ScheduleData_t& pbSchedData, JobScheduler::ISchedule* schedule) {
+    int schedType = schedule->ScheduleType();
+    switch (schedType) {
+        case 1: {
+            // protocol_ScheduleType_RunOnce case
+            tr_debug("encoding RunOnceSchedule");
+            JobScheduler::RunOnceSchedule* s = static_cast<JobScheduler::RunOnceSchedule*>(schedule);
+            protocol_RunOnceSchedule pbSched;
+            pbSched.AtUnixSec = (uint32_t)s->AtTime();
+            
+            tr_debug("sizeof(pbSchedData.bytes): %d, AtUnixSec: %d", sizeof(pbSchedData.bytes), pbSched.AtUnixSec);
+            pb_ostream_t stream = pb_ostream_from_buffer(pbSchedData.bytes, sizeof(pbSchedData.bytes));
+            pb_encode(&stream, protocol_RunOnceSchedule_fields, &pbSched);
+            pbSchedData.size = stream.bytes_written;
+            tr_debug("bytes written: %d", stream.bytes_written);
+            return true;
+        }
+        case 2: {
+            // protocol_ScheduleType_Periodic case
+            tr_debug("encoding RunPeriodicSchedule");
+            //JobScheduler::RunPeriodicSchedule* s = static_cast<JobScheduler::RunPeriodicSchedule*>(schedule);
+            //protocol_PeriodicSchedule pbSched;
+            return true;
+        }
+        default: {
+            tr_error("Unknown schedule type %d", schedType);
+            return false;
+        }
+    }
+}
+
 void JobSchedulesUpload::Run() {
     tr_info("Job Schedules Upload");
+    
+    protocol_JobList pbJobs = protocol_JobList_init_zero;
+    strcpy(pbJobs.sn, this->_conf.SerialNumber());
+    
+    {
+        LinkedList<JobScheduler::Job> jobs;
+        this->_scheduler.JobList(jobs);
+        node<JobScheduler::Job>* jn = jobs.pop(1);
+        int idx = 0;
+        while (jn != NULL) {
+            JobScheduler::Job* job = jn->data;
+            tr_debug("adding job ID: %d, type: %d\n", job->GetID(), job->GetTypeID());
+            pbJobs.items[idx].ID = job->GetID();
+            pbJobs.items[idx].TypeID = job->GetTypeID();
+            pbJobs.items[idx].ScheduleTypeID = static_cast<protocol_ScheduleType>(job->GetSchedule()->ScheduleType());
+            // pbJobs.items[idx].ScheduleData = 
+            if (!fillScheduleData(pbJobs.items[idx].ScheduleData, job->GetSchedule())) {
+                tr_error("job error: failed to fill schedule");
+                return;
+            }
+            tr_debug("schedule data size: %d", pbJobs.items[idx].ScheduleData.size);
+            // pbJobs.items[idx].Data = 
+            
+            jobs.remove(1);
+            jn = jobs.pop(1);
+            idx++;
+        }
+    }
+    
+//    protocol_Job job;
     // time_t now = time(NULL);
     //tr_debug("ADC created time is: %s\n", ctime(&now));
 //    struct AdcMsg msg;
--- a/source/jobSchedulesUpload.h	Wed Aug 02 14:48:16 2017 +0000
+++ b/source/jobSchedulesUpload.h	Thu Aug 03 00:07:50 2017 +0000
@@ -3,6 +3,7 @@
 #include "mbed.h"
 #include "lceProxy.h"
 #include "scheduler.h"
+#include "config.h"
 
 // JobSchedulesUpload reads current jobs schedule and uploads it to LCE
 // using Job protocol buffers structures.
@@ -14,12 +15,14 @@
         self->Run();
     }
     
-    JobSchedulesUpload(LceProxy& lce, JobScheduler::Scheduler& scheduler): _lce(lce), _scheduler(scheduler) {}
+    JobSchedulesUpload(Config& conf, LceProxy& lce, JobScheduler::Scheduler& scheduler): 
+        _conf(conf), _lce(lce), _scheduler(scheduler) {}
     
     void Run();
     
     private:
     
+    Config _conf;
     LceProxy& _lce;
     JobScheduler::Scheduler& _scheduler;
 };
\ No newline at end of file
--- a/source/main.cpp	Wed Aug 02 14:48:16 2017 +0000
+++ b/source/main.cpp	Thu Aug 03 00:07:50 2017 +0000
@@ -7,6 +7,7 @@
 #include "schedules.h"
 #include "jobService.h"
 #include "netstack.h"
+#include "config.h"
 #include "lceProxy.h"
 
 #include "jobTestPrint.h"
@@ -61,7 +62,9 @@
     mbed_trace_mutex_release_function_set( tracingRelease ); // only if thread safety is needed
     mbed_trace_init();       // initialize the trace library
     
-    tr_info("**Started**");    //-> "[INFO][main]: this is an info msg"    
+    tr_info("**Started**");    //-> "[INFO][main]: this is an info msg"
+    
+    Config conf("Nucleo1");
 
     uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x45, 0x56, 0x67};
     NetworkInterface* nif = initNetworkStack(mac_addr);
@@ -92,7 +95,7 @@
     */
 
     // command #2 is List Schedules in ISOM.
-    JobSchedulesUpload jSchedulesUpload(lce, scheduler);
+    JobSchedulesUpload jSchedulesUpload(conf, lce, scheduler);
     js.Register(2, JobSchedulesUpload::RunAdapter, &jSchedulesUpload);
     
     JobTestPrint jtp;
@@ -107,8 +110,10 @@
     time_t nowSecs = time(NULL);
     // NOTE: don't schedule run once with at 0, because 0 means never.
     JobScheduler::Response<JobScheduler::JobID> res =
-          scheduler.JobAdd(1002, new JobScheduler::RunPeriodicSchedule(3, 5), NULL);
-    res = scheduler.JobAdd(2, new JobScheduler::RunOnceSchedule(2), NULL);
+          //scheduler.JobAdd(1002, new JobScheduler::RunPeriodicSchedule(3, 2), NULL);
+          scheduler.JobAdd(2, new JobScheduler::RunOnceSchedule(nowSecs+1), NULL);
+    res = 
+        scheduler.JobAdd(1001, new JobScheduler::RunOnceSchedule(nowSecs+5), NULL);
         //scheduler.JobAdd(1002, new JobScheduler::RunOnceSchedule(1), NULL);
         //scheduler.JobAdd(1001, new JobScheduler::RunOnceSchedule(nowSecs + 2), NULL);
     //res = scheduler.JobAdd(1002, new JobScheduler::RunPeriodicSchedule(2, 5), NULL);