182 lines
5.8 KiB
C
182 lines
5.8 KiB
C
![]() |
#ifndef _RY_H_
|
||
|
#define _RY_H_
|
||
|
|
||
|
#include <fstream>
|
||
|
#include <string>
|
||
|
#include <map>
|
||
|
#include <unordered_map>
|
||
|
|
||
|
#include <libgen.h>
|
||
|
#include <dirent.h>
|
||
|
#include <fnmatch.h>
|
||
|
#include <signal.h>
|
||
|
|
||
|
#include <nopoll.h>
|
||
|
#include <nopoll_decl.h>
|
||
|
|
||
|
#include <json.h>
|
||
|
#include <md5.h>
|
||
|
#include <base64.h>
|
||
|
#include <mbedtls/aes.h>
|
||
|
#include <mbedtls/error.h>
|
||
|
|
||
|
#include "soe.h"
|
||
|
#include "yxbw.h"
|
||
|
#include "ycbw.h"
|
||
|
#include "yklog.h"
|
||
|
#include "ytlog.h"
|
||
|
#include "public.h"
|
||
|
|
||
|
#define MAX_MSG_COUNT 4096
|
||
|
|
||
|
typedef std::unordered_map<std::string, short> uid2pidmap;
|
||
|
|
||
|
#define CMD_CONTROL_OPERATION 0
|
||
|
#define CMD_CONTROL_SETTING 1
|
||
|
typedef struct {
|
||
|
int uid;
|
||
|
int point;
|
||
|
int order;
|
||
|
int type;
|
||
|
} struct_service_item;
|
||
|
|
||
|
typedef std::unordered_map<std::string, struct_service_item> name2servicemap;
|
||
|
typedef std::unordered_map<std::string, name2servicemap> unitname2servicemap;
|
||
|
|
||
|
typedef struct {
|
||
|
std::string name;
|
||
|
Json::Value value;
|
||
|
} struct_attr;
|
||
|
typedef std::vector<struct_attr> attrvector;
|
||
|
|
||
|
#define WORKER_ID_BITS 5
|
||
|
#define DATA_CENTER_ID_BITS 5
|
||
|
#define SEQUENCE_BITS 12
|
||
|
#define MAX_WORKER_ID ((1 << WORKER_ID_BITS) - 1)
|
||
|
#define MAX_DATA_CENTER_ID ((1 << DATA_CENTER_ID_BITS) - 1)
|
||
|
#define SEQUENCE_MASK ((1 << SEQUENCE_BITS) - 1)
|
||
|
#define EPOCH 1640995200000 // 2022-01-01 00:00:00
|
||
|
|
||
|
typedef struct {
|
||
|
uint64_t worker_id;
|
||
|
uint64_t data_center_id;
|
||
|
uint64_t sequence;
|
||
|
uint64_t last_timestamp;
|
||
|
} Snowflake;
|
||
|
class CRYDevice
|
||
|
{
|
||
|
public:
|
||
|
CRYDevice();
|
||
|
~CRYDevice();
|
||
|
|
||
|
noPollConn *ry_init(const char*, const int, const char*, const char*);
|
||
|
void ry_destroy(void);
|
||
|
bool ry_run(void);
|
||
|
private:
|
||
|
uid2pidmap uid2pid_map;
|
||
|
unitname2servicemap unitname2service_map;
|
||
|
std::string m_traceId;
|
||
|
|
||
|
LONG m_soeload = 0;
|
||
|
LONG m_yxbwload = 0;
|
||
|
LONG m_ycbwload = 0;
|
||
|
|
||
|
Snowflake m_sf = {1, 1, 0, 0};
|
||
|
|
||
|
struSystem config_system32;
|
||
|
struConfig config_config;
|
||
|
struDatabase config_database;
|
||
|
struNodeOption config_nodes;
|
||
|
struUnitStatic config_static_units[UNIT_NUM];
|
||
|
|
||
|
private:
|
||
|
noPollCtx *ctx;
|
||
|
noPollConn *conn;
|
||
|
DWORD last_connect_sec = 0;
|
||
|
int status;
|
||
|
|
||
|
bool m_dataAcquisitionReload = false;
|
||
|
|
||
|
char m_host[256] = {"127.0.0.1"};
|
||
|
int m_port = 7790;
|
||
|
char m_nodeId[128] = {"runyang_dn"};
|
||
|
char m_version[128] = {"v1.0"};
|
||
|
|
||
|
time_t last_sec;
|
||
|
private:
|
||
|
uint64_t current_time() {
|
||
|
struct timeval tv;
|
||
|
gettimeofday(&tv, NULL);
|
||
|
return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
|
||
|
}
|
||
|
|
||
|
uint64_t snowflake_next_id(Snowflake *sf) {
|
||
|
uint64_t timestamp = current_time();
|
||
|
if (timestamp < sf->last_timestamp) {
|
||
|
return 0;
|
||
|
}
|
||
|
if (sf->last_timestamp == timestamp) {
|
||
|
sf->sequence = (sf->sequence + 1) & SEQUENCE_MASK;
|
||
|
if (sf->sequence == 0) {
|
||
|
while (current_time() <= sf->last_timestamp);
|
||
|
}
|
||
|
} else {
|
||
|
sf->sequence = 0; // reset sequence
|
||
|
}
|
||
|
sf->last_timestamp = timestamp;
|
||
|
return ((timestamp - EPOCH) << (WORKER_ID_BITS + DATA_CENTER_ID_BITS + SEQUENCE_BITS)) |
|
||
|
(sf->data_center_id << (WORKER_ID_BITS + SEQUENCE_BITS)) |
|
||
|
(sf->worker_id << SEQUENCE_BITS) |
|
||
|
sf->sequence;
|
||
|
}
|
||
|
|
||
|
std::vector<std::string> split(const std::string &s, char delimiter);
|
||
|
|
||
|
bool configInitializeMemory(void);
|
||
|
bool configWriteSystemCFG(void);
|
||
|
bool configWriteNodeCFG(void);
|
||
|
bool configWriteHardwareCFG(void);
|
||
|
bool configWriteProcessCFG(void);
|
||
|
bool configWriteUnitCFG(void);
|
||
|
bool configWriteStaticUnitCFG(void);
|
||
|
bool configWriteDatabaseCFG(void);
|
||
|
|
||
|
void releaseAllUnits(void);
|
||
|
|
||
|
int GetUnitYXCount(int uid);
|
||
|
int GetUnitYCCount(int uid);
|
||
|
int GetUnitYMCount(int uid);
|
||
|
float GetUnitYCReal(int uid, int order);
|
||
|
float GetUnitYCRealFromValue(int uid, int order, long value);
|
||
|
float GetUnitYMReal(int uid, int order);
|
||
|
BYTE GetUnitYX(int uid, int point);
|
||
|
int GetUnitYXBW(int& uid, BOOLEAN& value, BYTE& qds, int& type, unionCP56Time& st);
|
||
|
int GetUnitSOE(int& uid, BOOLEAN& value, BYTE& qds, unionCP56Time& st);
|
||
|
int GetUnitYCBW(int& uid, LONG& value, BYTE& qds, int& type, unionCP56Time& st);
|
||
|
BOOLEAN GetUnitYK(int uid, int& order, BYTE& value, BYTE& act, BYTE& result);
|
||
|
void SetUnitYK(int uid, int order, BYTE value, BYTE act, BYTE result);
|
||
|
BOOLEAN GetUnitYT(int uid, int& order, DWORD& value, BYTE& act, BYTE& result);
|
||
|
void SetUnitYT(int uid, int order, DWORD value, BYTE act, BYTE result);
|
||
|
int MakeYKFrame(noPollConn* conn, int uid);
|
||
|
int MakeYTFrame(noPollConn* conn, int uid);
|
||
|
bool OnReceivedDeviceCommand(const Json::Value jsonRoot);
|
||
|
BOOLEAN processUartParam(const Json::Value jsonRoot, int ord);
|
||
|
BOOLEAN processNetworkParam(const Json::Value jsonRoot, int pid);
|
||
|
BOOLEAN processHostIEC104ProcessParam(const Json::Value jsonRoot, int pid);
|
||
|
BOOLEAN processModbusPointParam(const Json::Value jsonRoot, int uid, int point, int type);
|
||
|
bool dealConfigFile(const Json::Value jsonRoot);
|
||
|
bool OnReceivedSystemAction(noPollConn* conn, const std::string cmdId, const std::string cmd, const Json::Value data);
|
||
|
void on_message(noPollConn* conn, const char *msg, const int size);
|
||
|
|
||
|
void heart_beat(noPollConn* conn, int status);
|
||
|
bool publishinitDeviceData(noPollConn* conn, int uid);
|
||
|
bool publishAnalogData(noPollConn* conn, int uid);
|
||
|
bool publishStateData(noPollConn* conn, int uid);
|
||
|
bool publishHistoryAnalogData(noPollConn* conn, int uid);
|
||
|
bool publishHistoryStateData(noPollConn* conn, int uid);
|
||
|
|
||
|
BOOLEAN websocket_msg_join(noPollMsg **msg, int msg_count, BYTE* &buffer, int &buffer_size);
|
||
|
int websocket_write(noPollConn* conn, const char * buffer, int buffer_len);
|
||
|
bool publish_sensor_data(noPollConn* conn, const std::string traceId, const char* command, const Json::Value payload);
|
||
|
};
|
||
|
#endif //_RY_H_
|