网络信息
#import <Foundation/Foundation.h>
@interface NetworkInfo : NSObject
@property (nonatomic, strong) NSMutableDictionary *localInfoDic;
@property (nonatomic, strong) NSMutableDictionary *webInfoDic;
@property (nonatomic, strong) NSMutableDictionary *rcuInfoDic;
@property (nonatomic, strong) NSMutableDictionary *userInfoDic;
+ (instancetype)sharedNetworkInfo;
- (BOOL)networkInfoDictionaryWriteToLocatedFile;
@end
#import "NetworkInfo.h"
@interface NetworkInfo ()
@property (nonatomic, strong) NSMutableDictionary *networkInfoDic;
@property (nonatomic, copy) NSString *networkInfoDicPlistPath;
@property (nonatomic, copy) NSString *currentIpAddress;
@end
@implementation NetworkInfo
#pragma mark - lazy load
- (NSString *)currentIpAddress{
if (!_currentIpAddress) {
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
_currentIpAddress = address;
}
return _currentIpAddress;
}
- (NSString *)networkInfoDicPlistPath{
if (!_networkInfoDicPlistPath) {
NSArray *documentDirectoryArray = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *myDocumentDirectory = [documentDirectoryArray firstObject];
_networkInfoDicPlistPath = [myDocumentDirectory stringByAppendingPathComponent:@"NetworkInfoDic.plist"];
}
return _networkInfoDicPlistPath;
}
- (NSMutableDictionary *)NetworkInfoDic{
if (!_networkInfoDic) {
_networkInfoDic = [NSMutableDictionary dictionaryWithContentsOfFile:self.networkInfoDicPlistPath];
if (!_networkInfoDic) {
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:self.networkInfoDicPlistPath contents:nil attributes:nil];
NSDictionary *dic = [NSDictionary dictionary];
[dic writeToFile:self.networkInfoDicPlistPath atomically:YES];
//_networkInfoDic = [NSMutableDictionary dictionaryWithContentsOfFile:self.networkInfoDicPlistPath];
}
}
return _networkInfoDic;
}
- (NSMutableDictionary *)localInfoDic{
if (!_localInfoDic) {
_localInfoDic = [[NSMutableDictionary alloc] initWithDictionary:[self.networkInfoDic objectForKey:@"localInfo"]];
//local ip reset
[_localInfoDic setObject:self.currentIpAddress forKey:@"localIp"];
}
return _localInfoDic;
}
- (NSMutableDictionary *)webInfoDic{
if (!_webInfoDic) {
_webInfoDic = [[NSMutableDictionary alloc] initWithDictionary:[self.networkInfoDic objectForKey:@"webInfo"]];
}
return _webInfoDic;
}
- (NSMutableDictionary *)rcuInfoDic{
if (!_rcuInfoDic) {
_rcuInfoDic = [[NSMutableDictionary alloc] initWithDictionary:[self.networkInfoDic objectForKey:@"rcuInfo"]];
}
return _rcuInfoDic;
}
- (NSMutableDictionary *)userInfoDic{
if (!_userInfoDic) {
_userInfoDic = [[NSMutableDictionary alloc] initWithDictionary:[self.networkInfoDic objectForKey:@"userInfo"]];
}
return _userInfoDic;
}
#pragma mark-creating once
static NetworkInfo *sharedNetworkInfo = nil;
+ (instancetype)sharedNetworkInfo{
return [[self alloc] init];
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedNetworkInfo = [super allocWithZone:zone];
});
return sharedNetworkInfo;
}
- (instancetype)init{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedNetworkInfo = [super init];
});
return sharedNetworkInfo;
}
- (id)copyWithZone:(NSZone *)zone{
return sharedNetworkInfo;
}
+ (id)copyWithZone:(struct _NSZone *)zone{
return sharedNetworkInfo;
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone{
return sharedNetworkInfo;
}
- (id)mutableCopyWithZone:(NSZone *)zone{
return sharedNetworkInfo;
}
#pragma mark - method
- (BOOL)networkInfoDictionaryWriteToLocatedFile{
[self.networkInfoDic setObject:self.localInfoDic forKey:@"localInfo"];
[self.networkInfoDic setObject:self.webInfoDic forKey:@"webInfo"];
[self.networkInfoDic setObject:self.rcuInfoDic forKey:@"rcuInfo"];
[self.networkInfoDic setObject:self.userInfoDic forKey:@"userInfo"];
if ([self.networkInfoDic writeToFile:self.networkInfoDicPlistPath atomically:YES]){
return true;
}else{
return false;
}
}
//- (BOOL)isEqualToTheCurrentIpAddressWithIpAddress:(NSString *)ipAddress{
// return [self.currentIpAddress isEqualToString:ipAddress];
//}
@end
@@@@@@@@@@@eg:事件处理中心@@@@@@@@@
#import <Foundation/Foundation.h>
@interface EPCore : NSObject
+ (instancetype)sharedEPCore;
+ (void) buttonClickedProcessingWithInfoDictionary:(NSDictionary *)infoDic;
+ (void) receiveDataProcessingWithData:(NSData *)data;
@end
#import "EPCore.h"
@implementation EPCore
#pragma mark-creating once
static EPCore *sharedEPCore;
+ (instancetype)sharedEPCore{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedEPCore = [[self alloc] init];
});
return sharedEPCore;
}
#pragma mark -- 按钮点击处理
+ (void)buttonClickedProcessingWithInfoDictionary:(NSDictionary *)infoDic{
//得到字典中所有的key
NSEnumerator *enumeratorkey=[infoDic keyEnumerator];
// //得到字典中所有的value
// NSEnumerator *enumeratorvalue=[infoDic objectEnumerator];
NSMutableDictionary *mutInfoDic = [[NSMutableDictionary alloc] init];
for (NSObject *obj in enumeratorkey) {
//for循环随机,所以排列顺序未知
// string = [NSString stringWithFormat:@"%@%@", string, [self oneCharacterToDoubleCharacter:(NSString *)obj]];
[mutInfoDic setValue:[self oneCharacterToDoubleCharacter:[infoDic objectForKey:obj]] forKey:(NSString *)obj];
}
NSString *string = [NSString stringWithFormat:@"%@%@%@%@"
, [mutInfoDic objectForKey:@"equipmentNum"]
, [mutInfoDic objectForKey:@"viewNum"]
, [mutInfoDic objectForKey:@"buttonNum"]
, [mutInfoDic objectForKey:@"state"]
];
NSLog(@"%@", string);
[[UDPNetwork sharedUDPNetwork] sendDataToRCU:[NSData dataWithBytes:&string length:string.length]];
}
//测试,小于10的编号前面加0
+ (NSString *)oneCharacterToDoubleCharacter:(NSString *)oneStr{
if (oneStr.length < 2) {
return [NSString stringWithFormat:@"0%@",oneStr];
}else{
return oneStr;
}
}
#pragma mark -- 接收到的数据处理
+ (void)receiveDataProcessingWithData:(NSData *)data{
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (dataStr.length != 8) {
NSLog(@"The data format is wrong !");
return;
}
NSDictionary *dic = @{@"equipmentNum":[dataStr substringWithRange:NSMakeRange(0, 2)]
, @"viewNum":[dataStr substringWithRange:NSMakeRange(2, 2)]
, @"buttonNum":[dataStr substringWithRange:NSMakeRange(4, 2)]
, @"state":[dataStr substringWithRange:NSMakeRange(6, 2)]
};
//根据设备号分类发送通知
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
switch ([dic[@"equipmentNum"] integerValue]) {
case 1:
[defaultCenter postNotificationName:@"LightsControllerNotification" object:nil userInfo:dic];
break;
case 2:
[defaultCenter postNotificationName:@"AirconControllerNotification" object:nil userInfo:dic];
break;
case 3:
[defaultCenter postNotificationName:@"ServerControllerNotification" object:nil userInfo:dic];
break;
default:
break;
}
}
@end