// OMRON600.h: interface for the RfID_Omron600 class.
//
//////////////////////////////////////////////////////////////////////
#pragma once

#include <string>

#include "utility.h"
#include "RfId.h"
#include "RfIdFactory.h"

enum RFID_FORMAT
{
    RFID_ASCII,
    RFID_HEX,
};

#pragma warning(push)
#pragma warning(disable:4481) // nonstandard extension used: override specifier 'override'

class RfID_Omron600 : public RfIdBase
{
public:
    explicit RfID_Omron600(int id);
    virtual ~RfID_Omron600();
    bool    WriteID(const std::string &strID) override;
    bool    ReadID(std::string *pResult) override;
    bool    TestComm() override;
    void    OnReceiveComm(const char *cpcBuffer, int nSize);
    std::string GetErrorMessage() const override;

private:
    bool Send(const std::string&data);
    bool ReadID(int unit, int head, std::string*pResult);
    bool WriteID(int unit, int head, const std::string&strID);

private:
    std::string      m_strMsg;           /**< String containing error message */
    CMutex           m_portMutex;        /**< Mutex to prevent multiple readers from accessing serialport at the same time */
    CEvent           m_responseReceived; /**< Set when a response is received */
    std::string      m_strReceived;      /**< String containing the response received */
    DWORD            m_nTimeOut;         /**< Timeout for waiting for reply */
    int              m_head;             /**< Read head configuration [1..2] */
    int              m_unit;             /**< Unit number [0..31] */
    int              m_nDataLength;      /**< Data length of RFID, currently fixed at 16 [16 or 32] */
    enum RFID_FORMAT m_eDataFormat;      /**< Data Format, Hex or ASCII */

    std::string RestrictStringLength(const std::string& strData);
    std::string CalculateFCSAndFinalizeCmd(const std::string& strCmd);
    std::string GetRFIDErrorCodeString(const std::string&strErrorCode);
    bool    IsReplyError(const std::string&strCmd, const std::string&strReply);
};

#pragma warning (pop)

