增加守护进程代码
This commit is contained in:
parent
91a5de2d13
commit
095f175529
|
@ -0,0 +1,42 @@
|
|||
cmake_minimum_required(VERSION 3.0.0)
|
||||
project(sleep_processes)
|
||||
add_compile_options(-std=c++14)
|
||||
#set (CMAKE_GENERATOR "/home/holdtecs/zzm-linux/rk3288-linux/prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
|
||||
|
||||
|
||||
|
||||
FILE(GLOB SRC "*.cpp" "*.h" "*.c")
|
||||
FILE(GLOB IMAGEPRO "imageprocess/*.h" "imageprocess/*.cpp" )
|
||||
FILE(GLOB FPGA "fpga/*.h" "fpga/*.cpp" )
|
||||
|
||||
set(CMAKE_BUILD_TYPE "Release")
|
||||
|
||||
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SRC})
|
||||
|
||||
#优化代码大小
|
||||
SET(-CMAKE_BUILD_TYPE "Release")
|
||||
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
|
||||
|
||||
|
||||
add_custom_command(TARGET ${PROJECT_NAME}
|
||||
POST_BUILD
|
||||
COMMAND $ENV{STRIP} -s ${PROJECT_NAME})
|
||||
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS_RELEASE "-s")
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs turbojpeg pthread )
|
||||
target_link_libraries(${PROJECT_NAME} pthread )
|
||||
|
|
@ -0,0 +1,161 @@
|
|||
#include "JsonConfig.h"
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#define SCAN_INFO_TITLE "ScanInfo"
|
||||
#define SCAN_TOTAL "TotalScanned"
|
||||
#define SCAN_ROLLERNUM "RollerNum"
|
||||
#define SCAN_CUONUM "CuoNum"
|
||||
#define SCAN_JAMINNUM "JamInNum"
|
||||
#define SCAN_JAMOUTNUM "JamOutNum"
|
||||
#define SCAN_DOUNUM "DoubleNum"
|
||||
#define SCAN_ERRORNUM "ErrorNum"
|
||||
#define SCAN_SERIALNUM "SerialNum"
|
||||
#define SCAN_FWVERSION "FWVersion"
|
||||
#define SCAN_HRATIO "HRatio"
|
||||
#define SCAN_VRATIO "VRatio"
|
||||
#define SCAN_SLEEPTIME "SleepTime"
|
||||
#define SCAN_TOKEN "ScannerToken"
|
||||
#define SCAN_SPEEDMODE "SpeedMode"
|
||||
#define SCAN_VID "VID"
|
||||
#define SCAN_PID "PID"
|
||||
|
||||
#define TOKEN_DEFAULT " "
|
||||
#define FWVERSION "G345230918"
|
||||
#define SERIALNUM "HS6010A38001P1"
|
||||
JsonConfig::JsonConfig()
|
||||
{
|
||||
if (access(SCANNER_SCAN_INFO_PATH, F_OK) == -1) //不存在
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
JsonConfig::JsonConfig(ConfigType type) : m_cfgType(type)
|
||||
{
|
||||
}
|
||||
|
||||
JsonConfig::~JsonConfig()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ScannerScanInfo JsonConfig::GetScanInfo()
|
||||
{
|
||||
ScannerScanInfo info;
|
||||
if (access(SCANNER_SCAN_INFO_PATH, F_OK) != 0)
|
||||
{
|
||||
return info;
|
||||
}
|
||||
std::ifstream i(SCANNER_SCAN_INFO_PATH);
|
||||
auto pos = i.tellg();
|
||||
i.seekg(0, ios::end);
|
||||
if(i.tellg()<=2)
|
||||
{
|
||||
return info;
|
||||
}
|
||||
json j;
|
||||
i.seekg(pos);
|
||||
{
|
||||
std::lock_guard<std::mutex> m_c(m_lock);
|
||||
i >> j;
|
||||
i.close();
|
||||
}
|
||||
|
||||
auto &jobject = j[SCAN_INFO_TITLE];
|
||||
|
||||
if (jobject.contains(SCAN_TOTAL))
|
||||
jobject[SCAN_TOTAL].get_to(info.TotalScanned);
|
||||
else
|
||||
info.TotalScanned = 0;
|
||||
|
||||
if (jobject.contains(SCAN_ROLLERNUM))
|
||||
jobject[SCAN_ROLLERNUM].get_to(info.RollerNum);
|
||||
else
|
||||
info.RollerNum = 0;
|
||||
|
||||
if (jobject.contains(SCAN_CUONUM))
|
||||
jobject[SCAN_CUONUM].get_to(info.CuoNum);
|
||||
else
|
||||
info.CuoNum = 0;
|
||||
|
||||
if (jobject.contains(SCAN_JAMINNUM))
|
||||
jobject[SCAN_JAMINNUM].get_to(info.JamInNum);
|
||||
else
|
||||
info.JamInNum = 0;
|
||||
|
||||
if (jobject.contains(SCAN_JAMOUTNUM))
|
||||
jobject[SCAN_JAMOUTNUM].get_to(info.JamOutNum);
|
||||
else
|
||||
info.JamOutNum = 0;
|
||||
|
||||
if (jobject.contains(SCAN_DOUNUM))
|
||||
jobject[SCAN_DOUNUM].get_to(info.DoubleNum);
|
||||
else
|
||||
info.DoubleNum = 0;
|
||||
|
||||
if (jobject.contains(SCAN_ERRORNUM))
|
||||
jobject[SCAN_ERRORNUM].get_to(info.ErrorNum);
|
||||
else
|
||||
info.ErrorNum = 0;
|
||||
|
||||
if (jobject.contains(SCAN_SERIALNUM))
|
||||
jobject[SCAN_SERIALNUM].get_to(info.SerialNum);
|
||||
else
|
||||
info.SerialNum = SERIALNUM;
|
||||
|
||||
if (jobject.contains(SCAN_FWVERSION))
|
||||
jobject[SCAN_FWVERSION].get_to(info.FWVersion);
|
||||
|
||||
|
||||
if (jobject.contains(SCAN_HRATIO))
|
||||
jobject[SCAN_HRATIO].get_to(info.HRatio);
|
||||
else
|
||||
info.HRatio = 1065353216;//默认 1.0f
|
||||
|
||||
if (jobject.contains(SCAN_VRATIO))
|
||||
jobject[SCAN_VRATIO].get_to(info.VRatio);
|
||||
else
|
||||
info.VRatio = 1065353216;//默认 1.0f
|
||||
|
||||
if (jobject.contains(SCAN_SLEEPTIME))
|
||||
jobject[SCAN_SLEEPTIME].get_to(info.SleepTime);
|
||||
else
|
||||
info.SleepTime = 3600;//默认 1.0f
|
||||
|
||||
if(jobject.contains(SCAN_TOKEN))
|
||||
jobject[SCAN_TOKEN].get_to(info.Token);
|
||||
else
|
||||
{
|
||||
info.Token=TOKEN_DEFAULT;
|
||||
}
|
||||
|
||||
if(jobject.contains(SCAN_SPEEDMODE))
|
||||
jobject[SCAN_SPEEDMODE].get_to(info.SpeedMode);
|
||||
else
|
||||
{
|
||||
info.SpeedMode = 4;
|
||||
}
|
||||
|
||||
if(jobject.contains(SCAN_VID))
|
||||
jobject[SCAN_VID].get_to(info.VID);
|
||||
else
|
||||
{
|
||||
info.VID = 0x3072;
|
||||
}
|
||||
|
||||
|
||||
if(jobject.contains(SCAN_PID))
|
||||
jobject[SCAN_PID].get_to(info.PID);
|
||||
else
|
||||
{
|
||||
#ifdef G400
|
||||
info.PID = 0x0400;
|
||||
#else
|
||||
info.PID = 0x0300;
|
||||
#endif
|
||||
}
|
||||
return info;
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
#pragma once
|
||||
#include <unistd.h>
|
||||
#include "json.hpp"
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
struct ScannerScanInfo
|
||||
{
|
||||
unsigned int TotalScanned; //扫描总页数
|
||||
unsigned int RollerNum;
|
||||
unsigned int CuoNum; //搓纸总数
|
||||
unsigned int JamInNum; //搓纸失败次数
|
||||
unsigned int JamOutNum; //卡纸次数
|
||||
unsigned int DoubleNum; //双张次数
|
||||
unsigned int ErrorNum; //其他异常次数
|
||||
unsigned int HRatio; //横向校正系数
|
||||
unsigned int VRatio; //纵向校正系数
|
||||
unsigned int SleepTime; //休眠时间 单位s
|
||||
unsigned int SpeedMode; //速率模式
|
||||
unsigned int VID; //vid
|
||||
unsigned short PID; //pid
|
||||
std::string SerialNum; //序列号
|
||||
std::string FWVersion; //固件版本号
|
||||
std::string Token; //token 令牌
|
||||
};
|
||||
|
||||
#define SCANNER_SCAN_INFO_PATH "/mnt/flash-disk/huago/jsonconfig.json"
|
||||
using namespace std;
|
||||
using json = nlohmann::json;
|
||||
class JsonConfig
|
||||
{
|
||||
public:
|
||||
enum class ConfigType
|
||||
{
|
||||
Color_Flat,
|
||||
Color_Correct,
|
||||
Gray_Flat,
|
||||
Gray_Correct
|
||||
};
|
||||
private:
|
||||
map<ConfigType,string> cfgPaths={
|
||||
{ConfigType::Color_Flat,"/usr/local/etc/huago/Color_Flat.json"},
|
||||
{ConfigType::Color_Correct,"/usr/local/etc/huago/Color_Correct.json"},
|
||||
{ConfigType::Gray_Flat,"/usr/local/etc/huago/Gray_Flat.json"},
|
||||
{ConfigType::Gray_Correct,"/usr/local/etc/huago/Gray_Correct.json"}
|
||||
};
|
||||
|
||||
map<ConfigType,string> cfgPrefix={
|
||||
{ConfigType::Color_Flat,"Color_Flat"},
|
||||
{ConfigType::Color_Correct,"Color_Correct"},
|
||||
{ConfigType::Gray_Flat,"Gray_Flat"},
|
||||
{ConfigType::Gray_Correct,"Gray_Correct"}
|
||||
};
|
||||
ConfigType m_cfgType;
|
||||
public:
|
||||
JsonConfig();
|
||||
JsonConfig(ConfigType type);
|
||||
~JsonConfig();
|
||||
|
||||
|
||||
ScannerScanInfo GetScanInfo();
|
||||
|
||||
private:
|
||||
std::mutex m_lock;
|
||||
};
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,101 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <thread>
|
||||
#include"JsonConfig.h"
|
||||
#define erroe_status -1
|
||||
#define start_sleep 1
|
||||
#define close_sleep 0
|
||||
|
||||
int readfile(int fd, char* buf,int num )
|
||||
{
|
||||
lseek(fd, 0, SEEK_SET);
|
||||
int ret = read(fd, buf, num);
|
||||
if (ret == -1)
|
||||
{
|
||||
printf("readfile error:%s\r\n",strerror(ret));
|
||||
return erroe_status;
|
||||
}
|
||||
if (ret == 0)
|
||||
{
|
||||
printf("readfile error:%s\r\n","service connection is lost");
|
||||
return erroe_status;
|
||||
}
|
||||
printf("sleep processes len :%d\r\n",ret);
|
||||
return atoi(buf);
|
||||
}
|
||||
void start_enter_lowpwoer(int time)
|
||||
{
|
||||
int seconds = time;
|
||||
|
||||
if(time < 0 || time < 180)
|
||||
seconds = 180;
|
||||
else
|
||||
seconds = time;
|
||||
|
||||
std::string str="sh /mnt/flash-disk/countdown.sh start 0 0 "+std::to_string(seconds) + " &";
|
||||
printf("start_enter_lowpwoer %s \n",str.c_str());
|
||||
|
||||
int ret = system(str.c_str());
|
||||
}
|
||||
void stop_countdown()
|
||||
{
|
||||
int ret = system("sh /mnt/flash-disk/killtimer.sh");
|
||||
printf("system stop_countdown :%d\r\n",ret);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int fd = -1;
|
||||
int ret = 0;
|
||||
pollfd pfd;
|
||||
ret = mkfifo("/home/root/dev/sleep", 0666);
|
||||
if(ret != 0)
|
||||
{
|
||||
perror("/home/root/dev/sleep");
|
||||
}
|
||||
|
||||
printf("before open\n");
|
||||
fd = open("/home/root/dev/sleep", O_RDONLY);//等着只写
|
||||
if(fd < 0)
|
||||
{
|
||||
perror("open fifo");
|
||||
}
|
||||
|
||||
printf("after open\n");
|
||||
|
||||
char buffer[12];
|
||||
JsonConfig js;
|
||||
|
||||
|
||||
while (true)
|
||||
{
|
||||
ret = readfile(fd,buffer,sizeof(buffer));
|
||||
printf("while read buf:[%d]\n", ret);
|
||||
if (ret == -1)
|
||||
{
|
||||
system("sh /mnt/flash-disk/leds.sh 1");
|
||||
sleep(3);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ret == 1)
|
||||
{
|
||||
ScannerScanInfo info = js.GetScanInfo();
|
||||
stop_countdown();
|
||||
sleep(3);
|
||||
start_enter_lowpwoer(info.SleepTime);
|
||||
}
|
||||
else
|
||||
stop_countdown();
|
||||
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue