Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DataStore JobScheduler NetworkServices W5500Interface nanopb protocol
source/jobSchedulesUpload.cpp
- Committer:
- sgnezdov
- Date:
- 2017-08-03
- Revision:
- 15:f83485cc5a2c
- Parent:
- 14:65d69aed6540
- Child:
- 16:bef1673b199e
File content as of revision 15:f83485cc5a2c:
#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::Appointment& apt) { JobScheduler::Job* job = apt.GetJob(); JobScheduler::ISchedule* schedule = job->GetSchedule(); 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(); // Time should've been pushed to Appointment structure in all cases. if (pbSched.AtUnixSec != 0) { tr_warn("Run once has not be assigned to appointment yet. AtUnixSec: %d", pbSched.AtUnixSec); } else { pbSched.AtUnixSec = (uint32_t)apt.GetTime(); } 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("sizeof(pbSchedData.bytes): %d, AtUnixSec: %d, bytes written: %d", sizeof(pbSchedData.bytes), pbSched.AtUnixSec, 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::Appointment> apts; this->_scheduler.AppointmentList(apts); node<JobScheduler::Appointment>* aptn = apts.pop(1); int idx = 0; pbJobs.items_count = 0; while (aptn != NULL) { JobScheduler::Appointment* apt = aptn->data; JobScheduler::Job* job = apt->GetJob(); 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, *apt)) { tr_error("job error: failed to fill schedule"); return; } tr_debug("schedule data size: %d", pbJobs.items[idx].ScheduleData.size); // pbJobs.items[idx].Data = pbJobs.items_count = idx; apts.remove(1); aptn = apts.pop(1); idx++; } } pb_ostream_t sizeStream = {0}; pb_encode(&sizeStream, protocol_JobList_fields, &pbJobs); tr_debug("JobsList payload size: %d", sizeStream.bytes_written); uint8_t* outBuf = new uint8_t[sizeStream.bytes_written]; pb_ostream_t outStream = pb_ostream_from_buffer(outBuf, sizeof(outBuf)); pb_encode(&outStream, protocol_JobList_fields, &pbJobs); delete outBuf; // 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); }