代码拉取完成,页面将自动刷新
//
// Created by root on 19-7-7.
//
#include "RoadLightController.h"
#include <json/json.h>
#include <fstream>
#include <iostream>
RoadLightController::RoadLightController(const char* config) {
auto params = init(config);
std::vector<LightParam> road1 = std::get<0>(params);
std::vector<LightParam> road2 = std::get<1>(params);
roadLightParam.insert(std::pair<std::string, std::vector<LightParam>>("road1", road1));
roadLightParam.insert(std::pair<std::string, std::vector<LightParam>>("road2", road2));
for(auto param : road1) {
SerialPortForBoost* serial = nullptr;
if(serials.find(param.dev) == serials.end()) {
serial = new SerialPortForBoost();
if(!serial->Open(param.dev.c_str(), 9600)) {
printf("打开串口[%s]失败\n", param.dev.c_str());
}
serials.insert(std::make_pair(param.dev, serial));
} else {
serial = serials.at(param.dev);
}
Light *light = new Light(serial, param.green, param.yellow, param.red);
road1Lights.push_back(light);
}
for(auto param : road2) {
SerialPortForBoost* serial = nullptr;
if(serials.find(param.dev) == serials.end()) {
serial = new SerialPortForBoost();
if(serial->Open(param.dev.c_str(), 9600)) {
printf("打开串口[%s]失败\n", param.dev.c_str());
}
serials.insert(std::make_pair(param.dev, serial));
} else {
serial = serials.at(param.dev);
}
printf("dev:%s, green:%d, yellow:%d, red:%d\n", param.dev.c_str(), param.green, param.yellow, param.red);
Light *light = new Light(serial, param.green, param.yellow, param.red);
road2Lights.push_back(light);
}
}
RoadLightController::~RoadLightController() {
for(auto serial : serials) {
serial.second->Close();
delete serial.second;
}
for(auto light : road1Lights) {
delete light;
}
for(auto light: road2Lights) {
delete light;
}
}
void RoadLightController::red() {
// for(int i = 0; i < 7; i++) {
// if(i % 2 == 0) {
// for(auto light : road1Lights) {
// light->close();
// }
// } else {
// for(auto light : road1Lights) {
// light->green();
// }
// }
// usleep(500 * 1000);
// }
//
// for(auto light : road1Lights) {
// light->yellow();
// }
//
// usleep(2 * 1000 * 1000);
for(auto light : road1Lights) {
light->red();
}
usleep(2000 * 1000);
for(auto light:road2Lights) {
light->green();
}
}
void RoadLightController::green() {
// for(int i = 0; i < 7; i++) {
// if(i % 2 == 0) {
// for(auto light : road2Lights) {
// light->close();
// }
// } else {
// for(auto light : road2Lights) {
// light->green();
// }
// }
// usleep(250 * 1000);
// }
//
// for(auto light : road2Lights) {
// light->yellow();
// }
//
//
// usleep(2 * 1000 * 1000);
for(auto light : road2Lights) {
light->red();
}
usleep(2000 * 1000);
for(auto light:road1Lights) {
light->green();
}
}
void RoadLightController::close() {
for(auto light:road1Lights) {
light->close();
}
for(auto light:road2Lights) {
light->close();
}
}
std::tuple<std::vector<LightParam>, std::vector<LightParam>> RoadLightController::init(const char* config) {
std::vector<LightParam> road1;
std::vector<LightParam> road2;
if(config == nullptr) {
printf("file is null\n");
LightParam l1;
l1.dev = "/dev/ttyS1";
l1.red = 3;
l1.green = 1;
l1.yellow = 2;
road1.push_back(l1);
LightParam l2;
l2.dev = "/dev/ttyS1";
l2.red = 1;
l2.green = 2;
road1.push_back(l2);
LightParam l3;
l3.dev = "/dev/ttyS1";
l3.red = 1;
l3.green = 2;
road2.push_back(l3);
LightParam l4;
l4.dev = "/dev/ttyS1";
l4.red = 3;
l4.green = 4;
road2.push_back(l4);
LightParam l5;
l5.dev = "/dev/ttyS1";
l5.red = 1;
l5.green = 2;
road2.push_back(l5);
Json::Value root;
Json::Value road1Value;
Json::Value road2Value;
for(auto p : road1) {
Json::Value t;
t["dev"] = Json::Value(p.dev.c_str());
t["red"] = Json::Value(p.red);
t["yellow"] = Json::Value(p.yellow);
t["green"] = Json::Value(p.green);
road1Value.append(t);
}
root["road1"] = road1Value;
for(auto p : road2) {
Json::Value t;
t["dev"] = Json::Value(p.dev.c_str());
t["red"] = Json::Value(p.red);
t["yellow"] = Json::Value(p.yellow);
t["green"] = Json::Value(p.green);
road2Value.append(t);
}
root["road2"] = road2Value;
auto str = root.toStyledString();
FILE* f = fopen("light.json", "w");
fputs(str.c_str(), f);
fclose(f);
} else {
printf("open file:%s\n", config);
char buffer[1024 * 4] = {0};
std::fstream file(config);
if(file.fail()) {
return init(nullptr);
}
file.read(buffer, sizeof(buffer));
file.close();
std::cout<<buffer<<std::endl;
Json::CharReaderBuilder b;
Json::CharReader* JsonReader(b.newCharReader());
Json::Value root;
std::string errs;
bool ok = JsonReader->parse(buffer, buffer + strlen(buffer), &root, &errs);
if(!ok) {
return init(nullptr);
} else {
if(root.isMember("road1")) {
Json::Value r = root["road1"];
for(int i = 0; i < r.size(); i++) {
Json::Value tmp = r[i];
LightParam param;
param.dev = tmp["dev"].asString();
param.red = tmp["red"].asInt();
param.green = tmp["green"].asInt();
param.yellow = tmp["yellow"].asInt();
road1.push_back(param);
}
}
if(root.isMember("road2")) {
Json::Value r = root["road2"];
for(int i = 0; i < r.size(); i++) {
Json::Value tmp = r[i];
LightParam param;
param.dev = tmp["dev"].asString();
param.red = tmp["red"].asInt();
param.green = tmp["green"].asInt();
param.yellow = tmp["yellow"].asInt();
road2.push_back(param);
}
}
}
}
return std::tuple<std::vector<LightParam>, std::vector<LightParam>>(road1, road2);
}
void RoadLightController::init() {
for(auto light : road1Lights) {
light->red();
}
for(auto light:road2Lights) {
light->green();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。