Sergei G / Mbed OS Borsch

Dependencies:   DataStore JobScheduler NetworkServices W5500Interface nanopb protocol

source/jobSchedulesUpload.cpp

Committer:
sgnezdov
Date:
2017-08-03
Revision:
13:0fdbc14c33e7
Parent:
11:acaefb63fc6b
Child:
14:65d69aed6540

File content as of revision 13:0fdbc14c33e7:

#include "jobSchedulesUpload.h"

#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;
    // convert seconds to nanoseconds
//    msg.Created = now * 1e9;
//    tr_debug("Current time: %x sec, %llx nanosecs", now, msg.Created);
//    // swapt for byte order
//    msg.Created = swap_uint64(msg.Created); // swap_uint64(msg.Created);
//    msg.Mask = 0x80; // 0x80 bits: 1000 0000
//    for (int i = 0; i < 8; i++) {
//        msg.Chs[i].Raw = 0;
//        msg.Chs[i].V = 0;
//    }
//    msg.Chs[0].Raw = swap_uint32(_next++);
//    msg.Chs[0].V = msg.Chs[0].Raw;
    // /test can be used to send to test endpoint that responds with
    // a short hardcoded string
    // /uw/adc - to send adc version 1
    // _lce.SendV1("/uw/adc", (uint8_t*)&msg, sizeof(msg), false, now);
}