封裝火災報警線程實現智能家居中的火災報警功能
mainPro.c(主函數)
#include <stdio.h>
#include "controlDevice.h"
#include "inputCommand.h"#include <pthread.h>struct Devices *pdeviceHead = NULL; //設備工廠鏈表頭struct Devices* findDeviceByName(struct Devices *phead,char *name) //在設備鏈表中查找設備(語音和socket均可使用)
{struct Devices *tmp = phead;if(tmp == NULL){printf("The devicesLink is NULL");return NULL;}else{while(tmp != NULL){if(strcmp(tmp->deviceName,name) == 0){return tmp;}tmp = tmp->next;}return NULL; }}void* fire_pthread(void *data) //火災報警線程處理函數
{struct Devices *fire;struct Devices *beep;int status;fire = findDeviceByName(pdeviceHead,"fire"); //從設備鏈表找出火災檢測和蜂鳴器beep = findDeviceByName(pdeviceHead,"beep");fire->deviceInit(fire->pinNum); beep->deviceInit(beep->pinNum);while(1){ //檢測到火災通過蜂鳴器報警status = fire->readStatus(fire->pinNum);if(status == 0){beep->open(beep->pinNum);printf("fire warning\n");delay(20000);}elsebeep->close(beep->pinNum);} int main()
{pthread_t fire_pth;if(wiringPiSetup()<0){//初始化wiringPi外設庫printf("wiringPi Init failed\n");return -1;}//1.指令工廠初始化//2.設備控制工廠初始化pdeviceHead = addFireToDeviceLink(pdeviceHead); //將火災檢測加入設備鏈表pdeviceHead = addBeepToDeviceLink(pdeviceHead); //將蜂鳴器加入設備鏈表//3.線程池建立//3.3火災線程ret = pthread_create(&fire_pth,NULL,fire_pthread,NULL);if (ret != 0) {printf("Failed to create firethread.\n");return -1;}//等待線程退出pthread_join(fire_pth,NULL);return 0;
}