【ok210试用体验】项目篇+物联仓库管理系统+前端数据中心 +数据处理05 - 在线问答 - 电子威廉希尔官方网站 论坛 - 最好最受欢迎电子论坛!

【ok210试用体验】项目篇+物联仓库管理系统+前端数据中心 +数据处理05

himol ( 楼主 ) 2015-10-23 22:32:42  只看该作者 倒序浏览
第五节:数据处理模块

1. 功能描述
处理接收到的信息,因为接收到的信息是按一定规律进行编码的,所以进行解码后,激活数据库线程,保存数据,激活内存刷新数据,更新实时环境信息,判断数据是否越界,若越界则激活设备控制线程进行相应的控制。
2. 数据描述
结构体名
成员类型
成员名称
功能
struct getEnvMsg
unsigned char
sto_no
仓库号
tem[2]
温度
hum[2]
湿度
x
三轴
y
z
unsigned int
ill
光照
battery
电池比例
adc
ADC电压比例
struct getGoodsMsg
unsigned char
sto_no
仓库号
io
进出标志
goodsno
货物编号
goodsnum
货物数量
类型
名称
功能
linklist
linkHead
数据缓存链表头,用来读取数据
slinkHead
数据库数据缓存链表头,用来插入数据
pthread_mutex_t
mutex_linklist
数据缓存链表互斥锁
mutex_slinklist
数据库数据缓存链表互斥锁
mutex_analysis
数据处理线程互斥锁
mutex_global
实时仓库信息数据互斥锁
mutex_sms
短信模块互斥锁
mutex_buzzer
蜂鸣器模块互斥锁
pthread_cond_t
cond_analysis
数据处理模块被唤醒条件变量
cond_sqlite
数据库模块被唤醒条件变量
cond_refresh
内存刷新线程被唤醒条件变量
cond_buzzer
蜂鸣器模块唤醒条件变量
cond_sms
SMS模块唤醒条件变量
char
tem_alarm_status[STORAGE_NUM]
各仓库温度是否超标标志位
hum_alarm_status[STORAGE_NUM]
各仓库温度是否超标标志位
ill_alarm_status[STORAGE_NUM]
各仓库光照是否超标标志位
beep_status[STORAGE_NUM]
A8主蜂鸣器状态标志位
unsigned char
dev_sms_mask
sms模块操作标志位
int
msgid
消息队列号
dev_buzzer_mask
蜂鸣器模块操作标志位
struct env_info_clien_addr
all_info_RT
实时环境信息全局变量
3. 程图

4. 详细说明

在本线程中,进行了大量的工作,如数据的解析,激活数据库线程,以及设置各仓库设备的标志位,并进行检查,同时对接收的数据进行临界检测,主要功能代码如下:
#include "data_global.h"
#include "link_list.h"
#include "sqlite_link_list.h"
struct getEnvMsg
{
        unsigned char sto_no;
        unsigned char tem[2];
        unsigned char hum[2];
        unsigned char x;
        unsigned char y;
        unsigned char z;
        unsigned int ill;
        unsigned int battery;
        unsigned int adc;
};
struct getGoodsMsg
{
        unsigned char sto_no;
        unsigned char io;
        unsigned char goodsno;
        unsigned char goodsnum;
};
extern linklist linkHead;
extern linklist slinkHead;
extern pthread_mutex_t mutex_linklist;
extern pthread_mutex_t mutex_slinklist;
extern pthread_mutex_t mutex_analysis;
extern pthread_mutex_t mutex_global;
extern pthread_mutex_t mutex_sms;
extern pthread_mutex_t mutex_buzzer;
extern pthread_cond_t cond_analysis;
extern pthread_cond_t cond_sqlite;
extern pthread_cond_t cond_refresh;
extern pthread_cond_t cond_buzzer;
extern pthread_cond_t cond_sms;
char tem_alarm_status[STORAGE_NUM] = {0};
char hum_alarm_status[STORAGE_NUM] = {0};
char ill_alarm_status[STORAGE_NUM] = {0};
char beep_status[STORAGE_NUM] = {0};
extern unsigned char dev_sms_mask;
extern int msgid;
extern int dev_buzzer_mask;
extern struct env_info_clien_addr all_info_RT;
float dota_atof (char unitl)
{
        if (unitl > 100)
        {
                return unitl / 1000.0;
        }
        else if (unitl > 10)
        {
                return unitl / 100.0;
        }
        else
        {
                return unitl / 10.0;
        }
}
int dota_atoi (const char * cDecade)
{
        int result = 0;
        if (' ' != cDecade[0])
        {
                result = (cDecade[0] - 48) * 10;
        }
        result += cDecade[1] - 48;
        return result;
}
float dota_adc (unsigned int ratio)
{
        return ((ratio * 3.6) / 1024);
}
int storageAllgood (int sto_no)
{
        if ((tem_alarm_status[sto_no] + hum_alarm_status[sto_no] + ill_alarm_status[sto_no]) == 0)
        {
                return 0;
        }
        return 1;
}
int checkEnv (int sto_no, struct storage_info *p)
{
        char flag = 0;
        static char a8_beep_status = 0;
        if (0 == tem_alarm_status[sto_no])
        {
                if (p->temperature > p->temperatureMAX)
                {
                        sendMsgQueue (MSG_LED, MSG_LED_TEM_ON);
                        sendMsgQueue (MSG_M0, MSG_M0_FAN_ON3 | sto_no << 6);
                        p->fan_status = 3;
                        tem_alarm_status[sto_no] = 2;
                        flag = 1;
                }
                else if (p->temperature < p->temperatureMIN)
                {
                        sendMsgQueue (MSG_LED, MSG_LED_TEM_ON);
                        sendMsgQueue (MSG_M0, MSG_M0_FAN_OFF | sto_no << 6);
                        p->fan_status = 0;
                        tem_alarm_status[sto_no] = 1;
                        flag = 1;
                }
                if (flag)
                {
                        pthread_mutex_lock (&mutex_sms);
                        dev_sms_mask = SMS_TEM;
                        pthread_mutex_unlock (&mutex_sms);
                        pthread_cond_signal (&cond_sms);
                        flag = 0;
                        if (beep_status[sto_no] == 0)
                        {
                                beep_status[sto_no] = 1;
                                sendMsgQueue (MSG_M0, MSG_M0_BEEP_ON | sto_no << 6);
                        }
                        if (a8_beep_status == 0)
                        {
                                a8_beep_status = 1;
                                sendMsgQueue (MSG_BEEP, MSG_M0_BEEP_ON);       
                        }
                }
        }
        else
        {
                if (p->temperature < p->temperatureMAX && p->temperature > p->temperatureMIN)
                {
                        sendMsgQueue (MSG_LED, MSG_LED_TEM_OFF);
                        sendMsgQueue (MSG_M0, MSG_M0_FAN_OFF | sto_no << 6);
                        p->fan_status = 0;
                        tem_alarm_status[sto_no] = 0;
                        if (!storageAllgood (sto_no))
                        {
                                beep_status[sto_no] = 0;
                                sendMsgQueue (MSG_M0, MSG_M0_BEEP_OFF | sto_no << 6);
                                a8_beep_status = 0;
                                sendMsgQueue (MSG_BEEP, MSG_BEEP_OFF);
                        }
                }
        }
        if (0 == hum_alarm_status[sto_no])
        {
                if (p->humidity > p->humidityMAX)
                {
                        sendMsgQueue (MSG_LED, MSG_LED_HUM_ON);
                        sendMsgQueue (MSG_M0, MSG_M0_FAN_ON3 | sto_no << 6);
                        p->fan_status = 3;
                        hum_alarm_status[sto_no] = 2;
                        flag = 1;
                }
                else if (p->humidity < p->humidityMIN)
                {
                        sendMsgQueue (MSG_LED, MSG_LED_HUM_ON);
                        sendMsgQueue (MSG_M0, MSG_M0_FAN_OFF | sto_no << 6);
                        p->fan_status = 0;
                        hum_alarm_status[sto_no] = 1;
                        flag = 1;
                }
                if (flag)
                {
                        pthread_mutex_lock (&mutex_sms);
                        dev_sms_mask = SMS_HUM;
                        pthread_mutex_unlock (&mutex_sms);
                        pthread_cond_signal (&cond_sms);
                        flag = 0;
                        if (beep_status[sto_no] == 0)
                        {
                                beep_status[sto_no] = 1;
                                sendMsgQueue (MSG_M0, MSG_M0_BEEP_ON | sto_no << 6);
                        }
                        if (a8_beep_status == 0)
                        {
                                a8_beep_status = 1;
                                sendMsgQueue (MSG_BEEP, MSG_M0_BEEP_ON);       
                        }
                }
        }
        else
        {
                if (p->humidity < p->humidityMAX && p->humidity > p->humidityMIN)
                {
                        sendMsgQueue (MSG_LED, MSG_LED_HUM_OFF);
                        sendMsgQueue (MSG_M0, MSG_M0_FAN_OFF | sto_no << 6);
                        p->fan_status = 0;
                        hum_alarm_status[sto_no] = 0;
                        if (!storageAllgood (sto_no))
                        {
                                beep_status[sto_no] = 0;
                                sendMsgQueue (MSG_M0, MSG_M0_BEEP_OFF | sto_no << 6);
                                a8_beep_status = 0;
                                sendMsgQueue (MSG_BEEP, MSG_BEEP_OFF);
                        }
                }
        }
        if (0 == ill_alarm_status[sto_no])
        {
                if (p->illumination > p->illuminationMAX)
                {
                        sendMsgQueue (MSG_LED, MSG_LED_ILL_ON);
                        sendMsgQueue (MSG_M0, MSG_M0_LED_OFF | sto_no << 6);
                        p->led_status = 0;
                        ill_alarm_status[sto_no] = 2;
                        flag = 1;
                }
                else if (p->illumination < p->illuminationMIN)
                {
                        sendMsgQueue (MSG_LED, MSG_LED_ILL_ON);
                        sendMsgQueue (MSG_M0, MSG_M0_LED_ON | sto_no << 6);
                        p->led_status = 1;
                        ill_alarm_status[sto_no] = 1;
                        flag = 1;
                }
                if (flag)
                {
                        pthread_mutex_lock (&mutex_sms);
                        dev_sms_mask = SMS_ILL;
                        pthread_mutex_unlock (&mutex_sms);
                        pthread_cond_signal (&cond_sms);
                        flag = 0;
                        if (beep_status[sto_no] == 0)
                        {
                                beep_status[sto_no] = 1;
                                sendMsgQueue (MSG_M0, MSG_M0_BEEP_ON | sto_no << 6);
                        }
                        if (a8_beep_status == 0)
                        {
                                a8_beep_status = 1;
                                sendMsgQueue (MSG_BEEP, MSG_M0_BEEP_ON);       
                        }
                }
        }
        else if (ill_alarm_status[sto_no])
        {
                if (p->illumination < p->illuminationMAX && p->illumination > p->illuminationMIN)
                {
                        sendMsgQueue (MSG_LED, MSG_LED_ILL_OFF);
                        sendMsgQueue (MSG_M0, MSG_M0_LED_OFF | sto_no << 6);
                        p->led_status = 0;
                        ill_alarm_status[sto_no] = 0;
                        if (!storageAllgood (sto_no))
                        {
                                beep_status[sto_no] = 0;
                                sendMsgQueue (MSG_M0, MSG_M0_BEEP_OFF | sto_no << 6);
                                a8_beep_status = 0;
                                sendMsgQueue (MSG_BEEP, MSG_BEEP_OFF);
                        }
                }
        }
        return 0;
}
void getEnvPackage (link_datatype *buf)
{
        struct getEnvMsg pack;
        memcpy (&pack, buf->text, 20);
        int sto_no = pack.sto_no;
        pthread_mutex_lock (&mutex_global);
        struct storage_info current = all_info_RT.storage_no[sto_no];
        pthread_mutex_unlock (&mutex_global);
        current.storage_status = 1;
        current.x = pack.x;
        current.y = pack.y;
        current.z = pack.z;
        current.temperature = pack.tem[0] + dota_atof (pack.tem[1]);
        current.humidity = pack.hum[0] + dota_atof (pack.hum[1]);
        current.illumination = pack.ill;
        current.battery = dota_adc (pack.battery);
        current.adc = dota_adc (pack.adc);
        printf ("no = %d tem = %0.2f hum = %0.2f ill = %0.2f battery = %0.2f adc = %0.2f x = %d y %d z = %dn", sto_no,
                        current.temperature, current.humidity, current.illumination, current.battery, current.adc, current.x, current.y, current.z);
        checkEnv (sto_no, ¤t);       
        pthread_mutex_lock (&mutex_global);
        all_info_RT.storage_no[sto_no] = current;
        pthread_mutex_lock (&mutex_slinklist);
        sqlite_InsertLinknode (COLLECT_INSERTER, all_info_RT, sto_no, 0);
        pthread_mutex_unlock (&mutex_slinklist);
        pthread_mutex_unlock (&mutex_global);
        pthread_cond_signal (&cond_refresh);
        pthread_cond_signal (&cond_sqlite);
        return ;
}
void getGoodsPackage (link_datatype *buf)
{
        struct getGoodsMsg pack;
        memcpy (&pack, buf->text, 16);
        int sto_no = pack.sto_no;
        pthread_mutex_lock (&mutex_global);
        struct storage_info current = all_info_RT.storage_no[sto_no];
        pthread_mutex_unlock (&mutex_global);
        current.storage_status = 1;
        current.goods_info[pack.goodsno].goods_type = pack.goodsno;
        current.goods_info[pack.goodsno].goods_count = pack.goodsnum;
        printf ("sto_no = %d, io = %c goods_type = %d, goods_num = %dn", sto_no, pack.io, current.goods_info[pack.goodsno].goods_type, current.goods_info[pack.goodsno].goods_count);       
        pthread_mutex_lock (&mutex_global);
        all_info_RT.storage_no[sto_no] = current;
        pthread_mutex_lock (&mutex_slinklist);
        if (pack.io == 'I')
        {
                sqlite_InsertLinknode (GOODS_ADD, all_info_RT, sto_no, pack.goodsno);//
        }
        else if (pack.io == 'O')
        {
                sqlite_InsertLinknode (GOODS_REDUCE, all_info_RT, sto_no, pack.goodsno);
        }
        pthread_mutex_unlock (&mutex_slinklist);
        pthread_mutex_unlock (&mutex_global);
        pthread_cond_signal (&cond_refresh);
        pthread_cond_signal (&cond_sqlite);
        return ;
}
void *pthread_analysis (void *arg)
{
        linklist node;
        link_datatype buf;
        printf ("pthread_analysis is okn");
        while (1)
        {
                pthread_mutex_lock (&mutex_analysis);
                pthread_cond_wait (&cond_analysis, &mutex_analysis);
                pthread_mutex_unlock (&mutex_analysis);
                while (1)
                {
                        pthread_mutex_lock (&mutex_linklist);
                        if ((node = GetLinknode (linkHead)) == NULL)
                        {
                                pthread_mutex_unlock (&mutex_linklist);
                                break;
                        }
                        buf = node->data;
                        free (node);
                        pthread_mutex_unlock (&mutex_linklist);
                        if ('e' == buf.msg_type)
                        {
                                getEnvPackage (&buf);
                        }
                        else if ('r' == buf.msg_type)
                        {
                                getGoodsPackage (&buf);
                        }
                }
        }
        return 0;
}

0个回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则


关闭

站长推荐上一条 /6 下一条

小黑屋|手机版|Archiver|电子发烧友 ( 湘ICP备2023018690号 )

GMT+8, 2024-9-22 05:24 , Processed in 0.357117 second(s), Total 31, Slave 22 queries .

Powered by 电子发烧友网

© 2015 bbs.elecfans.com

微信扫描
快速回复 返回顶部 返回列表