WLG/common/common_func.hpp

499 lines
13 KiB
C++
Raw Normal View History

2024-10-22 19:04:25 +08:00
#ifndef COMMON_FUNC_HPP_
#define COMMON_FUNC_HPP_
2024-10-24 16:01:21 +08:00
#include <string.h>
#include <sys/statfs.h>
2024-10-23 22:25:03 +08:00
#include <string>
#include <vector>
2024-10-24 16:01:21 +08:00
#include <iostream>
2024-10-22 20:56:21 +08:00
// #include <stdio.h>
// #include <unistd.h>
// #include <sys/socket.h>
// #include <netinet/in.h>
// #include <net/if.h>
// #include <stdlib.h>
// #include <memory.h>
// #include <string>
// #include <list>
// #include <boost/algorithm/string/split.hpp>
// #include <boost/thread/mutex.hpp>
// #include <boost/algorithm/string/classification.hpp>
// #include <iostream>
// #include <fstream>
// #include <arpa/inet.h>
// #include <sys/ioctl.h>
// #include <boost/lexical_cast.hpp>
// #include <sys/statfs.h>
// #include <json/json.h>
// #include <sys/mman.h>
// #include <fcntl.h>
// #include <memory.h>
// #include <sys/ipc.h> //ipc
// #include <sys/shm.h>
// #include "dirent.h"
// #include <iconv.h>
// #include <termios.h>
// #include <netinet/in.h>
// #include <netinet/ip.h>
// #include <netinet/ip_icmp.h>
// #include <netdb.h>
typedef struct statfs DISK, *pDISK;
2024-10-19 16:02:41 +08:00
#define SECTION_MAX_LEN 256
#define STRVALUE_MAX_LEN 256
#define LINE_CONTENT_MAX_LEN 256
//配置文件位置
#define NETWORK "/etc/network/interfaces"
2024-10-22 20:56:21 +08:00
#define SYSTEMINFOFILE "/opt/configenv/SystemInfo.json" //系统信息
2024-10-19 16:02:41 +08:00
#define SERVERCONFIG "/opt/configenv/ServerConfig.json"
#define NETWORKCONFIG "/opt/configenv/NetWorkConfig.json"
2024-10-22 20:56:21 +08:00
#define SOFTWARE_RUN_LOG "/opt/log/"
#define BOARDTYPE "/opt/configenv/boardtype" //设备类型
#define ZIGBEECONFIG "/opt/configenv/ZigbeeConfig.json"
#define SN "/opt/system/sn" //设备序列号
#define SYSTEMSTART "/opt/system/start" //系统启动类型标志 0正常启动 1重启 2:
#define BUILD_UINT16(x, y) (((x & 0x00FFu) << 8u) | (y & 0x00FFu))
#define BUILD_UINT2(x, y) (((x) << 2u) | (y))
2024-10-19 16:02:41 +08:00
// 生成UINT32 数据
2024-10-22 20:56:21 +08:00
#define BUILD_UINT32(u, v, x, y) (((u & 0x00FFu) << 24u) | (v & 0x00FFu) << 16u) | (((x & 0x00FFu) << 8u) | (y & 0x00FFu))
2024-10-21 16:45:22 +08:00
// 生成UINT64 数据
2024-10-22 20:56:21 +08:00
#define BUILD_UINT48(u, v, x, y, x1, y1) (((u & 0x00FFu) << 40u) | ((u & 0x00FFu) << 32u) | ((x & 0x00FFu) << 24u) | (y & 0x00FFu) << 16u) | (((x1 & 0x00FFu) << 8u) | (y1 & 0x00FFu))
2024-10-21 16:45:22 +08:00
2024-10-19 16:02:41 +08:00
// 获取UINT32的高低字节
2024-10-22 20:56:21 +08:00
#define UINT32_HIGH_1(x) ((x & 0xFF000000u) >> 24u)
#define UINT32_HIGH_2(x) ((x & 0x00FF0000u) >> 16u)
#define UINT32_LOW_1(x) ((x & 0x0000FF00u) >> 8u)
#define UINT32_LOW_2(x) ((x & 0x000000FFu))
#define GET_BIT(x, bit) ((x & (1 << bit)) >> bit) /* 获取第bit位 */
2024-10-19 16:02:41 +08:00
// 获取UINT32的高低字节
2024-10-22 20:56:21 +08:00
#define UINT16_HIGH(x) ((x & 0xFF00u) >> 8u)
#define UINT16_LOW(x) ((x & 0x00FFu))
2024-10-19 16:02:41 +08:00
2024-10-23 16:10:23 +08:00
enum TIME_SIZE {
TIME_MINUTE = 5,
TIME_SECEOND = 8
};
2024-10-19 16:02:41 +08:00
2024-10-22 20:56:21 +08:00
struct DevData {
2024-10-19 16:02:41 +08:00
char mData[128];
char mDataMing[128];
int mLen;
2024-10-22 20:56:21 +08:00
DevData() : mLen(0) {
2024-10-19 16:02:41 +08:00
memset(mData, 0, 128);
memset(mDataMing, 0, 128);
}
};
2024-10-22 20:56:21 +08:00
struct compressWaveChannel {
int compressChannelX;
2024-10-19 16:02:41 +08:00
int compressChannelY;
int compressChannelZ;
2024-10-22 20:56:21 +08:00
int CountX;
int CountY;
int CountZ;
compressWaveChannel() {
compressChannelX = 0;
compressChannelY = 0;
compressChannelZ = 0;
CountX = 0;
CountY = 0;
CountZ = 0;
}
2024-10-19 16:02:41 +08:00
};
2024-10-22 20:56:21 +08:00
struct DevDataOfGwid {
2024-10-19 16:02:41 +08:00
std::string mDevdata;
std::string mDevid;
bool mIsSimulate;
2024-10-22 20:56:21 +08:00
DevDataOfGwid() : mDevdata(""), mDevid(""), mIsSimulate(false) {}
2024-10-19 16:02:41 +08:00
};
2024-10-22 20:56:21 +08:00
typedef void (*onReceiveUart)(DevDataOfGwid& devData);
2024-10-19 16:02:41 +08:00
struct sys_data {
2024-10-22 20:56:21 +08:00
char data[10240];
2024-10-19 16:02:41 +08:00
};
typedef struct {
2024-10-22 20:56:21 +08:00
int total;
int count;
int type;
2024-10-19 16:02:41 +08:00
int number;
2024-10-22 20:56:21 +08:00
int flag;
2024-10-19 16:02:41 +08:00
char channelId[16];
2024-10-22 20:56:21 +08:00
char SensorEngineeringUnit[32];
float waveData[32000];
2024-10-19 16:02:41 +08:00
} WAVE_CONTAIN;
struct ZigbeeInfo {
2024-10-22 20:56:21 +08:00
int DevMode;
int Channel;
std::string PanID;
std::string MyAddr;
std::string DstAddr;
std::string RetryNum;
std::string TranTimeout;
2024-10-19 16:02:41 +08:00
};
struct ZIGBEE {
2024-10-22 20:56:21 +08:00
char reserve[4];
char DevName[16];
char DevPwd[16];
unsigned char DevMode;
unsigned char Chan;
short PanID;
short MyAddr;
unsigned char MyIEEE[8];
short DstAddr;
unsigned char DstIEEE[8];
unsigned char Reserve0;
unsigned char PowerLevel;
unsigned char RetryNum;
unsigned char TranTimeout;
unsigned char Serial_Rate;
unsigned char Serial_DataB;
unsigned char Serial_StopB;
unsigned char Serial_ParityB;
unsigned char Reserve[10];
2024-10-19 16:02:41 +08:00
};
struct RecvData {
2024-10-22 20:56:21 +08:00
unsigned char Head[3];
unsigned char ShortAddr[2];
unsigned char Type;
unsigned char Order;
unsigned char Data[92];
unsigned char Crc;
2024-10-19 16:02:41 +08:00
};
struct DataNodeInfo {
2024-10-22 20:56:21 +08:00
int InitFlag;
int AccFlag;
int ZigbeeFlag;
int TemTopFlag;
int TemBotFlag;
int EquipSta; //设备状态
std::string ZigbeeLongAddr;
std::string HardVersion;
std::string SoftVersion;
std::string BpNo;
std::string SerialNo;
std::string FirstPowerTime;
int WakeupTime;
int StaticTime;
int WaveTime;
int BateryV;
std::string ProductNo;
int RSSI; // 接收信号强度
int ConfigFlag;
unsigned int FeatureInterVal; //特征值发送时间间隔,单位分钟
unsigned int WaveInterVal; //原始数据发送时间间隔,单位分钟
std::string ZigbeePanId;
int ZigbeeChannel;
std::string ZigbeeShortAddr;
std::string ZigbeeDesAddr;
int ZigbeePower;
int ZigbeeRetry;
int ZigbeeRetryGap;
int Range; //量程
int SamplingRate; //采样率
int ACCSampleTime; //采样时间
std::string StartBrands; //频带能量参数 1,2,3,4,5,START
std::string StopBrands; //频带能量参数 1,2,3,4,5,END
std::string EnvelopeBandPass; //冲击带通频率
std::string FaultFrequency; //故障频率1,2,3,4
std::string ConfigDate; //配置时间
int VIntegralFilterFrequency; //速度积分滤波频率
DataNodeInfo() {
FeatureInterVal = 0;
WaveInterVal = 0;
}
2024-10-19 16:02:41 +08:00
};
struct DataRecvStatic {
2024-10-22 20:56:21 +08:00
float TemTop;
float TemBot;
int Dip;
int Voltage;
float nodeWorkTime;
float nodeSendTime;
2024-10-19 16:02:41 +08:00
};
struct DataRecvDym {
2024-10-22 20:56:21 +08:00
float DiagnosisPk;
float IntegratPk;
float IntegratRMS;
float RmsValues;
float EnvelopEnergy;
float Amp1;
float Amp2;
float Amp3;
float Amp4;
float Amp5;
long Time;
float Phase1;
float Phase2;
float Phase3;
float Phase4;
2024-10-19 16:02:41 +08:00
};
2024-10-22 20:56:21 +08:00
struct TopicList {
std::string mPubData; //每秒特征数据上传主题
std::string mPubStatus; //状态上传主题
std::string mPubHeart;
std::string mPubConfig; //上传配置主题
std::string mSubData; //订阅主题
std::string mPubWaveData; //原始数据发布主题
std::string mPubWaveSecondData; //原始数据发布主题
std::string mPubCmd; //命令控制发布主题
std::string mPubRep;
std::string mPubTiming; //校时
std::string mPubLocalWifi;
std::string mPubLocalWaveServer;
std::string mPubLocalWaveQt;
std::string mPubLocalTrigger;
std::string mPubLocalConfig;
std::string mPubLocalCmd;
2024-10-19 16:02:41 +08:00
};
//系统描述文件数据定义
2024-10-22 20:56:21 +08:00
typedef struct {
2024-10-22 21:07:10 +08:00
std::string siteID; // Unique ID for each site
std::string siteName; // Controller site name
std::string siteCountry; // Controller location country
std::string siteProvince; // province
std::string siteCity; // city
2024-10-22 20:56:21 +08:00
double siteLongitude; // longitude
double siteLatitude; // latitude
2024-10-22 21:07:10 +08:00
std::string siteTimeZone;
std::string plantName; // Unique plant number
std::string plantNo;
std::string equipmentName; // Equipment Description in the plant
std::string equipmentNo;
std::string dataWatchName; // DNS Name for the DataWatch
2024-10-22 20:56:21 +08:00
long dataWachAddedDate; // Date of settings creation (Time Stamp)
2024-10-22 21:07:10 +08:00
std::string dataWatchAssetID; // Unique equipment Asset ID
std::string deviceType;
std::string dataWachAddedBy; // User who edited settings
std::string serialNumber;
std::string softVersion;
2024-10-22 20:56:21 +08:00
} SystemInfo;
2024-10-19 16:02:41 +08:00
2024-10-22 20:56:21 +08:00
typedef struct {
int zigAckrep;
int zigAckreset;
int zigReset;
int zigDef;
int alarmLight;
int commPower; // 4G,5G 电源开关
int vol3_8; // 5G 3.8v电源
int commRest; // 4G,5G复位
int wifiPower; // WiFi 电源开关
int wifiReset; // WiFi 复位
int hardWatchDog;
int power;
int runLed;
int errorLed;
int netResetNet0;
int netResetNet1;
2024-10-19 16:02:41 +08:00
} GPIOInfo;
2024-10-22 21:07:10 +08:00
struct DataNodeUpdate {
2024-10-22 20:56:21 +08:00
std::string strUpdataFileName;
std::string strSoftVersion;
std::vector<std::string> hwVersion;
2024-10-19 16:02:41 +08:00
};
struct ethtool_value {
2024-10-22 20:56:21 +08:00
unsigned int cmd;
unsigned int data;
2024-10-19 16:02:41 +08:00
};
2024-10-23 17:15:40 +08:00
void hexToAscii(const char* hexStr, char* asciiStr);
void stringToHex(const char* str, char* hexStr);
std::string GetLocalTimeWithMs(void);
2024-10-25 18:45:26 +08:00
int InitGpio(unsigned int gpioN, unsigned int inout);
2024-10-23 17:15:40 +08:00
int gpio_set(unsigned int gpioN, char x);
int gpio_read(unsigned int gpioN);
int config_uart(const char* Port, int speed);
int write_data(int fd, char* buff, int len);
int read_data(int fd, char* buff, int len, int timeout);
int ModifyMac(char* buff);
void mssleep(unsigned long microseconds);
2024-10-19 16:02:41 +08:00
/**
* @brief
* @return string 2018-11-18 17:16:10
*/
2024-10-23 17:15:40 +08:00
std::string GetCurrentTime();
2024-10-19 16:02:41 +08:00
2024-10-23 17:15:40 +08:00
tm* get_current_date();
2024-10-19 16:02:41 +08:00
/**
* @brief
* @param cmd ls
* @param buf buf里
* @return -1 0
*/
2024-10-23 17:15:40 +08:00
int system_custom(const char* cmd, char* buf);
2024-10-19 16:02:41 +08:00
/**
* @brief
*/
2024-10-23 17:15:40 +08:00
void WriteMemoryInfo();
2024-10-19 16:02:41 +08:00
/**
* @brief
* @return string
*/
2024-10-23 17:15:40 +08:00
std::string GetDayDate();
2024-10-19 16:02:41 +08:00
/**
* @brief 1200
2024-10-22 20:56:21 +08:00
* @return void
2024-10-19 16:02:41 +08:00
*/
2024-10-23 17:15:40 +08:00
void GetTime_(char* time_buff, TIME_SIZE len);
2024-10-19 16:02:41 +08:00
/**
* @brief
* @param timebuf buf存储数据
* @param type 0 1
* @return void
*/
2024-10-23 17:15:40 +08:00
void GetTimeNet(char* timebuf, int type);
2024-10-22 20:56:21 +08:00
2024-10-23 17:15:40 +08:00
std::string GetRTC(char* timebuf, int& millisecond);
2024-10-22 20:56:21 +08:00
2024-10-19 16:02:41 +08:00
/**
* @brief
* @param filename
* @param config
* @param option
* @return std::string
*/
2024-10-23 17:15:40 +08:00
std::string ReadStrByOpt(std::string filename, std::string config, std::string option);
2024-10-19 16:02:41 +08:00
/**
* @brief
* @param filename
* @param config
* @param option
* @param value
* @return int 0
*/
2024-10-23 17:15:40 +08:00
int WriteStr2Config(std::string filename, std::string config, std::string option, std::string value, bool listable = false);
2024-10-19 16:02:41 +08:00
/**
* @brief MAC地址做为设备的唯一标识
* @return std::string Mac地址
*/
2024-10-23 17:15:40 +08:00
std::string GetLocalMac(const char* net);
2024-10-19 16:02:41 +08:00
/**
* @brief IP
* @return std::string IP地址
*/
2024-10-23 17:15:40 +08:00
std::string IpAddrInit();
2024-10-19 16:02:41 +08:00
/**
* @brief
2024-10-22 20:56:21 +08:00
* @return std::string
2024-10-19 16:02:41 +08:00
*/
2024-10-23 16:10:23 +08:00
// extern std::string GetWorkNic();
2024-10-19 16:02:41 +08:00
/**
* @brief
* @return std::string CPU MEM DISK info
*/
2024-10-23 17:15:40 +08:00
std::string GetSysInfo();
2024-10-19 16:02:41 +08:00
/**
* @brief
2024-10-22 20:56:21 +08:00
* @return std::string
2024-10-19 16:02:41 +08:00
*/
2024-10-23 17:15:40 +08:00
std::string& ClearAllSpace(std::string& str);
2024-10-19 16:02:41 +08:00
/**
* @brief data数据
2024-10-22 20:56:21 +08:00
* @return void
2024-10-19 16:02:41 +08:00
*/
2024-10-23 17:15:40 +08:00
void StartWriteToDat();
2024-10-19 16:02:41 +08:00
/**
* @brief
2024-10-22 20:56:21 +08:00
* @return void
2024-10-19 16:02:41 +08:00
*/
2024-10-23 17:15:40 +08:00
void BackupDatFile();
2024-10-19 16:02:41 +08:00
/**
* @brief
* @return int
*/
2024-10-23 17:15:40 +08:00
int SetTime(unsigned long seconds, int milliseconds = 0);
2024-10-19 16:02:41 +08:00
/**
* @brief
* @param filename
* @param line
* @return std::string
*/
2024-10-23 17:15:40 +08:00
std::string GetFileContent(std::string filename, int line);
2024-10-19 16:02:41 +08:00
/**
* @brief
* @param zoneid ID
* @return void
*/
2024-10-23 17:15:40 +08:00
void ZoneConfig(std::string zoneid);
2024-10-19 16:02:41 +08:00
/**
* @brief
* @return std::string CPU MEM DISK info
*/
2024-10-23 17:15:40 +08:00
std::string GetSysStatus();
2024-10-19 16:02:41 +08:00
double GetHardDiskFree();
2024-10-23 17:15:40 +08:00
bool CheckIP(const char* ip);
2024-10-19 16:02:41 +08:00
bool IsValidMask(std::string mask);
2024-10-22 20:56:21 +08:00
// read update config file
2024-10-23 17:15:40 +08:00
std::vector<DataNodeUpdate> ReadStrUpdate(std::string filename);
2024-10-19 16:02:41 +08:00
2024-10-23 17:15:40 +08:00
void ReadStrConfig(std::string filename);
void ImportConfig(std::string filename);
int UpdataDataNodeConfig(std::string filename);
char* solve(char* dest, const char* src);
void swap(char* data);
int hexStringToBytes(const char* hexStr, unsigned char* bytes, size_t bytesSize);
2024-10-24 16:01:21 +08:00
void int2bytes(int i, unsigned char *bytes, int size);
2024-10-23 16:10:23 +08:00
int OpenWatchDog();
int WriteWatchDog(int fd);
2024-10-19 16:02:41 +08:00
//获取4G信号强度
2024-10-23 17:15:40 +08:00
int getcsq();
std::string GetGwIp_(const char* eth_name);
std::string GetOneContent(const char* szData, int nRow, const char* szSeparate);
2024-10-24 16:01:21 +08:00
int readStringValue(const char* section, const char* key, char* val, const char* file);
int writeStringVlaue(const char* section, const char* key, char* val, const char* file);
int readIntValue(const char* section, const char* key, const char* file);
int writeIntValue(const char* section, const char* key, int val, const char* file);
2024-10-19 16:02:41 +08:00
2024-10-22 20:56:21 +08:00
int getDiskInfo(char* diskTotal, char* diskFree);
2024-10-19 16:02:41 +08:00
2024-10-22 20:56:21 +08:00
unsigned short cal_chksum(unsigned short* addr, int len);
2024-10-19 16:02:41 +08:00
int socketHeart(const char* pSendData);
2024-10-23 17:15:40 +08:00
int Ping(const char* ips, int timeout);
int get_netlink_status(const char* if_name);
int compareVersions(const std::string& version1, const std::string& version2);
void Binary_Bit(unsigned char* p_data, unsigned char position, int flag);
2024-10-22 19:04:25 +08:00
#endif // COMMON_FUNC_HPP_