// // Sensors.m // DUREX Vendor Control // // Created by Imanol Barba on 9/5/14. // Copyright (c) 2014 Emmoco. All rights reserved. // #import "Sensors.h" @interface Sensors () @end @implementation Sensors - (id) init { [self setChannelProductAvailability:[[NSMutableArray alloc]init]]; [self setChannelStatus:[[NSMutableArray alloc]init]]; [self setMoneyCollected:[[NSMutableArray alloc]init]]; [self setMoneyReturned:[[NSMutableArray alloc]init]]; [self setChangeAvailable:[[NSMutableArray alloc] init]]; return self; } - (void) setResponse:(NSString *)response { self.response = response; [self parseResponse]; } - (void) parseResponse { uint8_t channelCount = 0; uint16_t channelStatus = 0; uint16_t channelAvailability = 0; uint16_t channelMotors = 0; channelStatus |= [[self response] characterAtIndex:2]; channelStatus |= ([[self response] characterAtIndex:3] << 8); channelAvailability |= [[self response] characterAtIndex:4]; channelAvailability |= ([[self response] characterAtIndex:5] << 8); channelMotors |= [[self response] characterAtIndex:6]; channelMotors |= ([[self response] characterAtIndex:7] << 8); for(int i = 0; i < MAX_CHANNELS; i++) { if(channelStatus & 0x01) { channelCount++; } channelStatus <<= 1; } [self setNumChannels:channelCount]; for(int i = 0; i < MAX_CHANNELS; i++) { if(channelAvailability & 0x01) { [[self channelProductAvailability] insertObject:[NSNumber numberWithBool:TRUE] atIndex:i]; } else { [[self channelProductAvailability] insertObject:[NSNumber numberWithBool:FALSE] atIndex:i]; } channelAvailability <<= 1; } for(int i = 0; i < MAX_CHANNELS; i++) { if(channelMotors & 0x01) { [[self channelStatus] insertObject:[NSNumber numberWithBool:TRUE] atIndex:i]; } else { [[self channelStatus] insertObject:[NSNumber numberWithBool:FALSE] atIndex:i]; } channelMotors <<= 1; } if([[self response] characterAtIndex:8] == '1') { [self setDoorOpen:TRUE]; } else { [self setDoorOpen:FALSE]; } if([[self response] characterAtIndex:9] == '1') { [self setCoilOpen:TRUE]; } else { [self setCoilOpen:FALSE]; } NSRange substrRange; substrRange.length = 3; substrRange.location = 10; for(int i = 0; i < MONEY_NUM_UNITS; i++) { [[self moneyCollected] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:substrRange] intValue]] atIndex:i]; substrRange.location += 3; } for(int i = 0; i < MONEY_NUM_UNITS; i++) { [[self moneyReturned] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:substrRange] intValue]] atIndex:i]; substrRange.location += 3; } substrRange.length = 2; for(int i = 0; i < MAX_PRODUCTS; i++) { [[self productsSold] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:substrRange] intValue]] atIndex:i]; substrRange.location += 2; } if([[self response] characterAtIndex:64] == '1') { [[self changeAvailable]insertObject:[NSNumber numberWithBool:TRUE] atIndex:0]; } else { [[self changeAvailable]insertObject:[NSNumber numberWithBool:FALSE] atIndex:0]; } if([[self response] characterAtIndex:65] == '1') { [[self changeAvailable]insertObject:[NSNumber numberWithBool:TRUE] atIndex:1]; } else { [[self changeAvailable]insertObject:[NSNumber numberWithBool:FALSE] atIndex:1]; } } @end