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:
- 17:b79ce8109995
- Parent:
- 16:bef1673b199e
- Child:
- 18:133a51becbe1
File content as of revision 17:b79ce8109995:
#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" 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++; // } // } // // // encode to nothing to detect buffer size // pb_ostream_t sizeStream = {0}; // pb_encode(&sizeStream, protocol_JobList_fields, &pbJobs); // // // allocate output buffer and encode into it // uint8_t* outBuf = new uint8_t[sizeStream.bytes_written]; // pb_ostream_t outStream = pb_ostream_from_buffer(outBuf, sizeStream.bytes_written); // pb_encode(&outStream, protocol_JobList_fields, &pbJobs); // // tr_array(outBuf, outStream.bytes_written); // send CoAP message time_t now = time(NULL); uint8_t msg[] = { 0x0a, 0x07, 0x4e, 0x75, 0x63, 0x6c, 0x65, 0x6f, 0x31 }; size_t msgLen = 9; _lce.SendV1("/uw/joblist", msg, msgLen, false, now); //_lce.SendV1("/uw/joblist", outBuf, outStream.bytes_written, false, now); // delete outBuf; }