//
// 设备数据管理类
#import <Foundation/Foundation.h>
#import "DeviceInfo.h"
@interface DeviceDataManager : NSObject
{
NSMutableArray *dataArray;
}
+(DeviceDataManager *)instance;
-(NSMutableArray *)allDeviceData;//返回所有本地存储的设备
-(DeviceInfo *)getDeviceInfoWith:(NSString *)mac;//根据mac地址返回该设备的信息
-(BOOL)insertOrReqlaceSignalDevice:(DeviceInfo *)devInfo;//插入或修改指定的设备信息
-(BOOL)deleteDeviceWith:(NSString *)mac;//删除指定mac地址的设备
@end
//
// DeviceDataManager.m
// SmartHomeWIFI
//
// Created by 深圳市 秀软科技有限公司 on 14-2-20.
// Copyright (c) 2014年 huhuaxiang. All rights reserved.
//
#import "DeviceDataManager.h"
#import "SQLdataManger.h"
static DeviceDataManager *devInfoManager;
@implementation DeviceDataManager
-(id)init
{
if(self=[superinit])
{
}
returnself;
}
+(DeviceDataManager *)instance
{
if(!devInfoManager)
devInfoManager=[[DeviceDataManageralloc]init];
returndevInfoManager;
}
-(NSMutableArray *)allDeviceData
{
if(!dataArray||dataArray.count<1)
[selfdataArrayInit];
returndataArray;
}
-(DeviceInfo *)getDeviceInfoWith:(NSString *)mac
{
for (DeviceInfo *info indataArray) {
if([info.mac isEqualToString:mac])
return info;
}
returnnil;
}
-(BOOL)insertOrReqlaceSignalDevice:(DeviceInfo *)devInfo
{
if(!devInfo)return NO;
BOOL flag=[[SQLdataMangerinstance]insertOrReqlaceSignalDevice:devInfo];
if(flag)
{
//如果存在则删除原来的,更新现在的
DeviceInfo *info;
BOOL exite=NO;
for (int i=0; i<dataArray.count; i++) {
info=[dataArray objectAtIndex:i];
if([info.mac isEqualToString:devInfo.mac])
{
info=devInfo;
exite=YES;
}
}
if(!exite)
[dataArray addObject:devInfo];
}
return flag;
}
-(BOOL)deleteDeviceWith:(NSString *)mac
{
BOOL flag=[[SQLdataMangerinstance]deleteTableDataWithMAC:mac];
if(flag)
{
for (DeviceInfo *info in dataArray) {
if([info.mac isEqualToString:mac])
[dataArray removeObject:info];
}
}
return flag;
}
-(void)dataArrayInit
{
NSArray *dataA=[[SQLdataMangerinstance]queryDeviceInfoTable];
dataArray=[[NSMutableArrayalloc]initWithArray:dataA];
}
@end