#ifndef _MAIN_H
#define _MAIN_H

#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <sys/syscall.h>
#include <linux/watchdog.h>
#define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
char buf;
int wdt_val;
#define FEED_WATCHDOG   buf = '1'; write(fdw,&buf,1)    //喂狗
#define CLOSE_WDT buf = 'V'; write(fdw,&buf,1);close(fdw)       //关闭看门狗
#define REBOOTTIME      3       //看门狗超时时间默认为14秒，可设为1秒，3秒和14秒
#define SET_WDT_TIMEOUT wdt_val = REBOOTTIME; ioctl(fdw,WDIOC_SETTIMEOUT,&wdt_val)      //设置看门狗超时时间

#include "command.h"
#include "queue.h"
#include "uart_can.h"
#include "mqtt_utils.h"
#include "http_utils.h"
#include "md5.h"
#include "main.h"
#include "ping.h"
#include "Hmacsha1.h"

#define SND_BATCHINBEGIN "S01"
#define SND_BATCHINEND "S02"
#define SND_BATCHOUTNORMALBEGIN "S03"
#define SND_BATCHOUTNORMALEND "S04"
#define SND_BATCHOUTREPAIRBEGIN "S05"
#define SND_BATCHOUTREPAIREND "S06"
#define SND_BORROWDEVBEGIN "S07"
#define SND_BORROWDEVEND "S08"
#define SND_RETURNDEVBEGIN "S09"
#define SND_RETURNDEVEND "S10"
#define SND_REPAIRDEVBEGIN "S11"
#define SND_REPAIRDEVEND "S12"
#define SND_REPAIRCELL "S13"
#define SND_DEVREADFAIL "S14"
#define SND_DEVSTILLIN "S15"
#define SND_DOORNOTCLOSED "S16"
#define SND_DOOROPENFAIL "S17"
#define SND_DOWNLOADFAIL "S18"
#define SND_DOWNLOADSUCCESS "S19"
#define SND_DOWNLOADING "S20"
#define SND_READDEVINFOBEGIN "S21"
#define SND_READDEVINFOEND "S22"
#define SND_NETDISCONNECTED "S23"
#define SND_NETWIFI "S24"
#define SND_NETWIFICONNECTBEGIN "S25"
#define SND_NETWIRED "S26"
#define SND_NETWIREDCONNECTBEGIN "S27"
#define SND_NETMOBILE "S28"
#define SND_NETMOBILECONNECTBEGIN "S29"
#define SND_MQTTCONNECTED "S30"
#define SND_MQTTDISCONNECTED "S31"
#define SND_WIFIPARMWRONG "S32"
#define SND_CABINETNOTCONFIGED "S33"
#define SND_SYSTEMBOOTEND "S34"
#define SND_DEVSNGETSUCCESS "S35"
#define SND_DEVSNGETFAIL "S36"
#define SND_DEVSNALREADYEXIST "S37"
#define SND_SCANSUCCESS "S38"
#define SND_DEVNOTBORROWED "S39"
#define SND_PARAMSETSUCCESS "S40"
#define SND_TIMEOUTBATCHIN "S41"
#define SND_TIMEOUTREPAIRDEVIN "S42"
#define SND_TIMEOUTRETURN "S43"

typedef struct
{
        uint32_t name;
        uint16_t battery;
        uint16_t reserve;
}__attribute__((packed)) jt_only_tag_t;

void hgz_init_db_multy(mqtt_parm_t *mqtt_parm);
void hgz_init_db_single(mqtt_parm_t *mqtt_parm);
void hgz_reset_cells(mqtt_parm_t *mqtt_parm,char *operators);
void hgz_remove_db(char *cabinetName);
void hgz_remove_dev_by_cellname(char *cellname,int userid,char *username,char *batchid,char *optype,char *optime,char *opinfo);
void hgz_remove_dev_by_cellname_no_userid(char *cellname,char *batchid,char *optype,char *optime,char *opinfo);
void hgz_add_dev_by_cellname(char *cellname,int userid,char *username,char *devsn,long devpwr,char *optype,char *batchid,
                             char *optime,char *opinfo);
void hgz_repair_dev_by_cellname(char *cellname,int userid,char *username,char *devsn,long devpwr,char *batchid,char *optime,char *opinfo,
                                char *repairtype);
void hgz_repair_dev_by_cellname_misOperate(char *cellname,int userid,char *username,long devpwr,char *batchid,char *optime,char *opinfo,
                                           char *repairtype);
void hgz_lose_dev_by_cellname(char *cellname,int userid,char *username,char *batchid,char *optime);
void hgz_repair_cell_by_cellname(char *cellname,int userid);
void hgz_reset_cell_by_cellname(char *cellname,int userid);
void hgz_get_cellstatus_by_cellname(char *cellname,char *cellstatus);
void hgz_get_devstatus_by_cellname(char *cellname,char *devstatus);
void hgz_get_idlecellnum_by_devtype(char *idlecellnum,char *devtype);
void hgz_get_cabinetnames(char *cabinetnames);
void hgz_get_devtypes(char *devtypes);
void hgz_get_repairdevcellnames_by_devtype(char *cellnames,char *devtype);
void hgz_get_indevcellnames_by_devtype(char *cellnames,char *devtype);
void hgz_get_repaircellcellnames_by_devtype(char *cellnames,char *devtype);
void hgz_get_activecellcellnames_by_devtype(char *cellnames,char *devtype);
void hgz_get_doublecheckcellcellnames_by_cabinetnames(char *cellnames,char *cabinetnames);
void hgz_get_activecellcellnames(char *cellnames);
void hgz_get_allcellnames(char *cellnames);
void hgz_get_idlecellcellnames_by_devtype(char *cellnames,char *devtype);
void hgz_select_cellnames_tosavedev_by_devtype(char *cellnames,char *devtype,int num);
void hgz_select_cellnames_tosavedev_by_operator(char *cellnames,int userid,char *operators);
void hgz_select_cellnames_totakenormaldev_by_devtype(char *cellnames,char *devtype,int num);
void hgz_select_cellnames_totakerepairdev_by_devtype(char *cellnames,char *devtype,int num);
void hgz_get_indevnum_by_devtype(char *indevnum,char *devtype);
void hgz_get_repairdevnum_by_devtype(char *repairdevnum,char *devtype);
void hgz_get_canborrowdevnum_by_devtype(char *canborrowdevnum,char *devtype);
void hgz_get_devsn_by_cellname(char *devsn,char *cellname);
void hgz_do_download(mqtt_parm_download_t *mqtt_parm);
int hgz_check_cabinet_status(void);

void myrand(char *randnum,int len);
void removeValueFromMystr(char *oristr,char *val);
void addValueToMystr(char *oristr,char *val);
int sizeofMystr(char *oristr);
void getCabinetNameFromMystr(char *oristr,char *laststr);
void checkDevice(void);
void sendDataFromCellNames(char *cellNames,char *cmd);
void resetCellFromCellNames(char *cellNames);
void repairCellFromCellNames(char *cellNames);
void powerOnAllUsb(void);
void shutDownAllLed(void);
void openAllDoors(void);
long getCurrentTime(void);
void getTimeStr(char *buffer,int len);
void getFirstValueFromMystr(char *oristr,char *val);
void removeStrFromMystr(char *oristr,char *removestr);
void reportEventToWeb(void);
void reportDbFromCellNames(char *cellNames);
void reportReturndevEventFromCellNames(char *cellNames);
void reportBorrowdevEventFromCellNames(char *cellNames);
void reportLosedevEventFromCellNames(char *cellNames);
void reportManagerEvent(char *eventType,char *opType,char *result);
void cleanOptypeInDbFromCellNames(char *cellNames);
void cleanOptypeInDbByCellName(char *cellName);
void handleQrcode(char *str);
//int buffer_to_file(const char *pathname, const char *data, unsigned int size,const char *mod);
char *file_to_buffer(const char *pathname, unsigned int *size);
void modifyMystr(char *oristr,char *laststr);
int getInnerValueFromStr(char *oristr, char *laststr);
void playAudio(char *num);
int split(const char* str, int strLen, const char* splitChar, int index, char* result, int maxLen);
void getDevInfo(char *devcode);
bool hgz_judge_sn_exist_devin(char *devsn);
bool hgz_judge_sn_exist_devout(char *devsn,char *cellname);
bool hgz_judge_cell_can_borrow(char *cellname);
void getDevRegisterStatus(char *devsn);
void hgz_get_returninfo_by_cellname(char *cellname,int *userid,char *batchid,char *username,char *devsn);
void getDevInfo(char *parm);
void *actHandleQrcode(void *parm);
void station_status_report(void);
#endif
