//
// Created by zx on 2019/12/9.
//

#ifndef PLCDATA_H
#define PLCDATA_H
#include <iostream>
#include <string.h>
#include <mutex>
#include "tools/StdCondition.h"
#include <thread>
#include "tools/s7_plc.h"
#include "unistd.h"
#include "tools/define.h"

#define MAX_REGIONS 15
#define ELE_FENCE_START_ADDR 4
#define ELE_FENCE_DB_NUM  95
#define ELE_FENCE_OFFSET 7
#define ELE_FENCE_COUNT 6

#define HTON(T) ((T)<<8)|((T)>>8)

class PlcData
{
public:
    static PlcData* GetInstance(std::string ip="");
    static void Release();
    void UpdateData(stateCode code,int id);
    static void plcThread(PlcData* p);
private:
    static PlcData*                                 g_instance;
    StdCondition                                    m_cond_exit;
    std::thread*                                    p_send_thread;
    S7PLC*                                          p_plc;
    const int                                       m_send_interval_milli = 200;

    PlcData(std::string ip);

protected:
    std::mutex m_lock;
    short m_data[MAX_REGIONS];
public:
    std::string                                     m_ip_str;
};




#endif //PLCDATA_H
