/********************************************************************
	ʱ:	2016/8/2 9:18
	ļ: 	alarmdev.h
	:		
	Ŀ:	    豸࣬кб豸һЩԺͷ
				⣬йб豸ľ̬
*********************************************************************/
#ifndef ALARMDEV_H
#define ALARMDEV_H

#include <QtCore>
#include <QtNetwork>
#include "tcpsocket.h"

class CmdFactory;

class AlarmDev
{
public:
    AlarmDev();
	~AlarmDev();
    quint32 devId; // 豸ID
    QHostAddress hostAddr; // 豸ַ
    quint16 port; // 豸˿
	quint8 bufang; // 豸״̬ʶ
    bool onlineStatus; // 豸״̬ʶ

	

    void incrementNoHeartTimes(); // ñ豸ʱ

	void incrementTimeoutTimes(); // ñ豸ָʱʱ

	bool isCmdExecing(); // Ƿָ͵豸δյִз

	void setHeartbeat(); // յ豸ʱ0
						 // ԭ״̬ʱ򴴽Ϣ
	void startCmdTiming(); // ʼ·ִмʱ

	void endCmdExecing();  // ·ִмʱ

	bool isCrcChanged(quint16 crc16); // 豸CRC16УǷı
	void setCrc(quint16 crc16); // ô˴豸CRC16У
inline	bool isCmdTimeout(); // ָǷʱ

public:
	static void setCmdFactory(CmdFactory *pCmdFactory); // 

	static int insertAlarmDevToMap(AlarmDev *pAlarmDev); // µı豸

	static void incrementAllTimer(); // еļʱָʱʱʱ

	static AlarmDev *getAlarmDevByDevId(quint32 devId); // ͨ豸IDȡ豸

	static int delAlarmDevByDevId(quint32 devId); // ͨ豸IDɾӦı豸

private:
	quint16 m_crc16; // 豸CRC16У
    volatile int m_noHeartTimes; // ʱ
	volatile int m_noRtnTimes;   // ָʱ
	volatile bool m_bCmdExecing; // ָǷ·
	volatile bool m_bHeartTimingStart; // ʱʼ
	static CmdFactory *m_pCmdFactory; // 
	static QMap<quint32, AlarmDev *> m_mapDevIdToPAlarmDev; // 豸ID豸map
	static QReadWriteLock mapLock; // m_mapDevIdToPAlarmDevĶд
};

#endif // ALARMDEV_H
