什么是设计模式:
1. 设计模式,描述了一组相互紧密作用的类和对象
2. C : 面向过程,一门面向对象不友好的语言
Java:面向对象
3. 建筑设计领域引入到计算机科学来的
4. 一共23中,代码容易被他人理解,保证代码的可靠性,程序的重要性
什么是类和对象
1. 类就相当于,C语言中的结构体类型,对象就是类的具象化
2. 举例:
#include
struct animals {
char *name;
char *sex;
void (*doing)();
void (*eat)();
};
void dogdoing(char *name){
printf("%s看家护院\n",name);
}
void pdoing(char *name){
printf("%s不干人事\n",name);
}
int main()
{
struct animals dog ={
.name = "小黄",
.doing = dogdoing//给函数指针赋值,即给函数执行的首地址赋值
};
struct animals p ={
.name = "老王",
.doing = pdoing
};
dog.doing(dog.name);//调用函数
p.doing(p.name);
return 0;
}
ztj@ubuntu:~$ ./a.out
小黄看家护院
老王不干人事
ztj@ubuntu:~$ ^C
#include
#include
#include
struct device {
char deviceName[32];//设备名
char fileName[32];//文件名
int writePinNum;//输出引脚
int readPinNum;//写入引脚
char key[32];//指令编码
char commandBuf[32];//设备接收的指令
int status;//设备状态码,检测摄像头是否打开,或者火灾是否触发
FILE *fp;//摄像头返回的文件流
void (*open)(struct device *p);//打开设备
void (*close)(struct device *p);//关闭设备
void (*commandhandle)(struct device *p);//执行指令
void (*deviceInit)(struct device *p);//初始化设备
int (*readStatus)(struct device *p);//读取火灾报警器状态
char (*createFile)(char *fileName);//创建文件,人脸识别备用
struct device *next;//下一个设备节点
};
// 客厅有关声明
struct device *addLivingRoomLighttoLink(struct device *phead);
struct device livingRoomLight;
//餐厅有关声明
struct device *addDiningRoomLighttoLink(struct device *phead);
struct device diningRoomLight;
//二楼灯有关声明
struct device *addUpstairRoomLighttoLink(struct device *phead);
struct device upstairRoomLight;
//卫生间灯有关声明
struct device *addBathRoomLighttoLink(struct device *phead);
struct device bathRoomLight;
//人脸锁相关声明
struct device *addFaceLocktoLink(struct device *phead);
struct device faceLock;
//火灾报警有关声明
struct device *addFireAlarmtoLink(struct device *phead);
struct device fireAlarm;
//摄像头相关
struct device *addCameratoLink(struct device *phead);
struct device camera;
-inputCommand.h
#include
struct inputCommand{
char commandName[32];//输入指令的名字
char command[32];//用于指令的操作
char inPutDeviceName[32];//输入设备
char outPutDevicename[32];//输出设备
int s_fd;//服务器句柄
int c_fd;//客户端句柄
int fd;//串口句柄
int (*Init)(struct inputCommand *p,char *ipAddress,char *port);//指令初始化
int (*getInputCommand)(struct inputCommand *p);//获取指令
struct inputCommand *next ;
};
//语音设备相关声明
struct inputCommand *addVoicetoLink(struct inputCommand *phead);
struct inputCommand voice;
//Server相关声明
struct inputCommand *addServertoLink(struct inputCommand *phead);
struct inputCommand server;
#include"controlDevice.h"
#include"inputCommand.h"
#include
#include
void bathRoomLightOpen(struct device *p){
digitalWrite(p->writePinNum,LOW);
}
void bathRoomLightClose(struct device *p){
digitalWrite(p->writePinNum,HIGH);
}
void bathRoomLightInit(struct device *p){
pinMode(p->writePinNum,OUTPUT);
digitalWrite(p->writePinNum,HIGH);
}
void bathRoomLightCommandHandle(struct device *p){
char *buf = p->commandBuf;
if(strcmp(buf,"031") == 0){
p->open(p);
}else if(strcmp(buf,"032") == 0){
p->close(p);
}else{
printf("bathRoomLightCommandHandle fail\n");
}
}
struct device bathRoomLight = {
.deviceName = "bathRoomLight",
.writePinNum = 24,
.key ="031&032",
.open = bathRoomLightOpen,
.close = bathRoomLightClose,
.commandhandle = bathRoomLightCommandHandle,
.deviceInit = bathRoomLightInit
};
struct device *addBathRoomLighttoLink(struct device *phead){
struct device *p;
if(phead == NULL){
phead = &bathRoomLight;
phead->next = NULL;
return phead;
}else{
p = &bathRoomLight;
p->next = phead;
phead = p;
return phead;
}
}
#include"controlDevice.h"
#include"inputCommand.h"
#include
#include
#include
#include
#include
#include
#include
#include
int voiceInit(struct inputCommand *voice,char *ipAddress,char *port){
int fd;
if((fd = serialOpen(voice->inPutDeviceName,9600) ) == -1){
exit(-1);
}
voice->fd = fd;
return fd;
}
int voiceGetCommand(struct inputCommand *voice){
int nread;
memset(voice->command,'\0',32);
nread = read(voice->fd,voice->command,sizeof(voice->command));
if(nread == 0){
printf("uart for voice over time\n");
}else{
return nread;
}
}
struct inputCommand voice = {
.commandName = "voice",
.command = {'\0'},
.inPutDeviceName = "/dev/ttyAMA0",
.outPutDevicename = {'\0'},
.Init = voiceInit,
.getInputCommand = voiceGetCommand,
.next = NULL
};
struct inputCommand *addVoicetoLink(struct inputCommand *phead){
struct inputCommand *p;
if(phead == NULL){
phead = &voice;
phead->next = NULL;
return phead;
}else{
p = &voice;
p->next = phead;
phead = p;
return phead;
}
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include"inputCommand.h"
#include"controlDevice.h"
int serverInit(struct inputCommand *server,char *ipAddress,char *port){
int s_fd;
struct sockaddr_in s_addr;
memset(&s_addr,0,sizeof(struct sockaddr_in));
//1.socket创建套接字
s_fd = socket(AF_INET,SOCK_STREAM, 0);
if(s_fd == -1){
perror("socket");
exit(-1);
}
//2.bind(为套接字绑定地址和端口号)
s_addr.sin_family = AF_INET;//IPV4 因特网
s_addr.sin_port = htons( atoi(port) );//小端变大端
inet_aton(ipAddress,&s_addr.sin_addr) ;//字符串变网络形式
bind(s_fd,(struct sockaddr *)&s_addr,sizeof(struct sockaddr_in));
server->s_fd = s_fd;
printf("socket init success\n");
return s_fd;
}
int serverGetCommand(struct inputCommand *server){
/* int nread;
memset(server->command,'\0',32);
nread = read(server->c_fd,server->command,strlen(server->command));
return nread;*/
return 0;
}
struct inputCommand server = {
.commandName = "server",
.inPutDeviceName = {'\0'},
.outPutDevicename = {'\0'},
.command = {'\0'},
.Init = serverInit,
.getInputCommand = serverGetCommand,
.next = NULL
};
struct inputCommand *addServertoLink(struct inputCommand *phead){
struct inputCommand *p;
if(phead == NULL){
phead = &server;
phead->next = NULL;
return phead;
}else{
p = &server;
p->next = phead;
phead = p;
return phead;
}
}
#include
#include
#include
#include"inputCommand.h"
#include"controlDevice.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct inputCommand *commandHead = NULL;//指令输入工厂头节点
struct device *deviceHead = NULL;//设备工厂头节点
struct inputCommand *sockethandle1 = NULL;//socket线程工具
struct device *sockethandle2 = NULL;
struct inputCommand *voicethandle1 = NULL;//voice线程工具
struct device *voicethandle2 = NULL;
struct device *camerahandle1 = NULL;//camera线程工具
struct device *camerahandle2 = NULL;
char *p = NULL;//客户端地址
//根据设备名字查找设备
struct device *findDeviceByName(char *name,struct device *deviceHead){
struct device *p;
p = deviceHead;
while(p != NULL){
if(strcmp(name,p->deviceName ) == 0 ){
return p;
}
p = p->next;
}
return NULL;
}
//通过命令类型查找设备输入设备
struct inputCommand *findInputByName(char *name,struct inputCommand *commandHead){
struct inputCommand *p;
p = commandHead;
while(p != NULL){
if(strcmp(name,p->commandName ) == 0 ){
return p;
}
p = p->next;
}
return NULL;
}
struct device *findDeviceByCommand(char *command,struct device *deviceHead){
struct device *p;
p = deviceHead;
while(p != NULL){
if(strstr(p->key,command ) != NULL ){
return p;
}
p = p->next;
}
return NULL;
}
void *voicePthread(){
int nread;
voicethandle1 = findInputByName("voice",commandHead);
if(voicethandle1 == NULL){
printf("find voicehandle error\n");
pthread_exit(NULL);
}else{
if( voicethandle1->Init(voicethandle1,NULL,NULL) < 0){
printf("voice init error\n");
pthread_exit(NULL);
}
while(1){
nread = voicethandle1->getInputCommand(voicethandle1);
if(nread == 0){
printf("voice no data\n");
}else{
printf("readvoice %s,%dByte \n",voicethandle1->command,nread);
voicethandle2 = findDeviceByCommand(voicethandle1->command,deviceHead);//找设备
if(voicethandle2 != NULL){//找到设备
strcpy(voicethandle2->commandBuf,voicethandle1->command);//传递指令到设备
voicethandle2->commandhandle(voicethandle2);//设备执行指令
}
}
}
}
return NULL;
}
void *readPthread(){
int nread;
while(1){
memset(sockethandle1->command,'\0',32);
nread = read(sockethandle1->c_fd,sockethandle1->command,sizeof(sockethandle1->command));
if(nread == -1){
perror("read");
}else if(nread > 0){
printf("readclient %s,%dByte \n",sockethandle1->command,nread);
sockethandle2 = findDeviceByCommand(sockethandle1->command,deviceHead);//找设备
if(sockethandle2 != NULL){//找到设备
strcpy(sockethandle2->commandBuf,sockethandle1->command);//传递指令到设备
sockethandle2->commandhandle(sockethandle2);//设备执行指令
}
}else{
printf("%s client quit\n",p);
pthread_exit(NULL);
}
}
return NULL;
}
void *serverPthread(){
int nread = 0;
pthread_t read_thread;
struct sockaddr_in c_addr;
memset(&c_addr,0,sizeof(struct sockaddr_in));
int sizec_addr = sizeof(struct sockaddr_in);
sockethandle1 = findInputByName("server",commandHead);
if(sockethandle1 == NULL){
printf("find sockethandle error\n");
pthread_exit(NULL);
}
//socket初始化
sockethandle1->Init(sockethandle1,"192.168.0.16","8088");
// listen()监听
listen(sockethandle1->s_fd,10 );
printf("socket listen...\n");
while(1){
// 不断地接收客户端请求,接收到就创建一个线程
sockethandle1->c_fd = accept(sockethandle1->s_fd, (struct sockaddr *)&c_addr, &sizec_addr);
if( sockethandle1->c_fd != -1){
p = inet_ntoa(c_addr.sin_addr);
printf("client %s connect\n",p);
pthread_create(&read_thread,NULL,readPthread,NULL);
}
pthread_join(read_thread,NULL);
}
return NULL;
}
void *cameraPthread(){
camerahandle1 = findDeviceByName("camera",deviceHead);
camerahandle2 = findDeviceByName("faceLock",deviceHead);
while(1){
if( strcmp(camerahandle1->commandBuf ,"113") == 0 && camerahandle1->fp ==NULL && camerahandle1->status == 0 ){
system("fswebcam /dev/video0 ~/photo/tmp.jpg");
fflush(stdout);
chdir("/home/pi/httphandle");
fflush(stdout);
FILE* pp =popen("/home/pi/httphandle/post","r");
fflush(stdout);
if(pp != NULL){
char *retbuf =(char *)malloc(128);
memset(retbuf,'\0',128);
fread(retbuf,1,64,pp);
if(strstr(retbuf,"Face recognition successful") != NULL){
printf("\n======================sucsessful========================\n\n");
camerahandle2->open(camerahandle2);
}else{
printf("\n======================failed========================\n\n");
camerahandle2->close(camerahandle2);
}
free(retbuf);
pclose(pp);
memset(camerahandle1->commandBuf,'\0',32);
}
}else{
continue;
}
}
return NULL;
}
void *fireAlarmPthread(){
return NULL;
}
int main(){
pthread_t voice_thread;//语音线程
pthread_t server_thread;//服务器线程
pthread_t camera_thread;//摄像头线程
pthread_t fireAlarm_thread;//火灾线程
if(-1 == wiringPiSetup()){
return -1;
}
//1.工厂模式链表创建
deviceHead = addLivingRoomLighttoLink(deviceHead);
deviceHead = addDiningRoomLighttoLink(deviceHead);
deviceHead = addUpstairRoomLighttoLink(deviceHead);
deviceHead = addBathRoomLighttoLink(deviceHead);
deviceHead = addFaceLocktoLink(deviceHead);
deviceHead = addFireAlarmtoLink(deviceHead);
deviceHead = addCameratoLink(deviceHead);
commandHead = addVoicetoLink(commandHead);
commandHead = addServertoLink(commandHead);
//2.初始化
livingRoomLight.deviceInit(&livingRoomLight);
diningRoomLight.deviceInit(&diningRoomLight);
upstairRoomLight.deviceInit(&upstairRoomLight);
bathRoomLight.deviceInit(&bathRoomLight);
faceLock.deviceInit(&faceLock);
fireAlarm .deviceInit(&fireAlarm);
camera.deviceInit(&camera);
//3.线程池建立:
//3.1 语音线程
int ret1 = pthread_create(&voice_thread,NULL,voicePthread,NULL);//注意第4参数类型void *
if(ret1 != 0){
printf("creat voice_thread fail\n");
exit(-1);
}
//3.2 server线程
int ret2 = pthread_create(&server_thread,NULL,serverPthread,NULL);
if(ret2 != 0){
printf("creat server_thread fail\n");
exit(-1);
}
//3.3 摄像头线程
int ret3 = pthread_create(&camera_thread,NULL,cameraPthread,NULL);
if(ret3 != 0){
printf("creat camera_thread fail\n");
exit(-1);
}
//3.4 火灾线程
int ret4 = pthread_create(&fireAlarm_thread,NULL,fireAlarmPthread,NULL);
if(ret4 != 0){
printf("creat fireAlarm_thread fail\n");
exit(-1);
}
//4.阻塞主进程,等待线程退出
pthread_join(voice_thread,NULL);
pthread_join(server_thread,NULL);
pthread_join(camera_thread,NULL);
pthread_join(fireAlarm_thread,NULL);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#define false 0
#define true 1
typedef unsigned int bool;
char buf[10240] = {'\0'};
size_t readData(void *ptr, size_t size, size_t nmemb,void *stream){
memset(buf,'\0',10240);
strcpy(buf,ptr);
// printf("===============================get data==================================\n");
// printf("%s\n",buf);
}
char *getImgBase64ByPath(char* path){
char *bufpic;
char cmd[128] = {'\0'};
sprintf(cmd,"base64 %s > tmpFile",path);
system(cmd);
int fd = open("./tmpFile",O_RDWR);
int filelen = lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
bufpic = (char*)malloc(filelen+2);
memset(bufpic,'\0',filelen+2);
read(fd,bufpic,filelen);
close(fd);
system("rm -f tmpFile");
return bufpic;
}
bool postUrl()
{
CURL *curl; //提供curl库一个操作句柄
CURLcode res;//curl_easy_perform(curl)的返回值
char* poststring;//post请求主体内容
char *img1; //人脸识别静态照片,开始为jpg格式,要转化成base64流
char *img2;//人脸识别动态照片
char* key = "4VaSoqfU3wtZtpsHKinsdx";
char* secret ="f4dd11a61fc8441c95b1e4777f54c264";
int typeId = 21;
char* format ="xml";
img1 = getImgBase64ByPath("/home/pi/photo/ztj.jpg");
img2 = getImgBase64ByPath("/home/pi/photo/tmp.jpg");
int len = strlen(key)+strlen(secret)+strlen(img1)+strlen(img2)+248;
poststring = (char *)malloc(len);
memset(poststring,'\0',len);
sprintf(poststring,"img1=%s&img2=%s&key=%s&secret=%s&typeId=%d&format=%s",img1,img2,key,secret,typeId,format);
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,poststring); // 指定post内容
curl_easy_setopt(curl, CURLOPT_URL, "https://netocr.com/api/faceliu.do"); // 指定url
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, readData);//将http头输出到fp所指向的文件
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
if(strstr(buf,"是") != NULL){
printf("=========================Face recognition successful===========================\n");
return true;
}else{
printf("===========================Face recognition failed==============================\n");
return false;
}
}
int main(void)
{
bool res;
//getUrl("/tmp/get.html");
res = postUrl();
return res;
}