|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//
// 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]];
|
|
23
|
[self setProductsSold:[[NSMutableArray alloc]init]];
|
|
24
25
26
27
28
|
[self setChangeAvailable:[[NSMutableArray alloc] init]];
return self;
}
|
|
29
|
- (void) setResponseValue:(NSString *)response
|
|
30
|
{
|
|
31
|
[self setResponse:response];
|
|
32
33
34
35
36
37
38
39
40
41
|
[self parseResponse];
}
- (void) parseResponse
{
uint8_t channelCount = 0;
uint16_t channelStatus = 0;
uint16_t channelAvailability = 0;
uint16_t channelMotors = 0;
|
|
42
43
44
45
46
47
48
49
50
|
NSRange substrRange;
substrRange.length = 5;
substrRange.location = 2;
channelStatus = [[[self response] substringWithRange:substrRange] intValue];
substrRange.location += 5;
channelAvailability = [[[self response] substringWithRange:substrRange] intValue];
substrRange.location += 5;
|
|
51
|
|
|
52
53
|
channelMotors = [[[self response] substringWithRange:substrRange] intValue];
substrRange.location += 5;
|
|
54
55
56
57
58
59
60
|
for(int i = 0; i < MAX_CHANNELS; i++)
{
if(channelStatus & 0x01)
{
channelCount++;
}
|
|
61
|
channelStatus >>= 1;
|
|
62
63
64
65
66
67
68
69
70
71
72
73
74
|
}
[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];
}
|
|
75
|
channelAvailability >>= 1;
|
|
76
77
78
79
80
81
82
83
84
85
86
87
|
}
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];
}
|
|
88
|
channelMotors >>= 1;
|
|
89
90
|
}
|
|
91
|
if([[self response] characterAtIndex:17] == '1')
|
|
92
93
94
95
96
97
98
99
|
{
[self setDoorOpen:TRUE];
}
else
{
[self setDoorOpen:FALSE];
}
|
|
100
|
if([[self response] characterAtIndex:18] == '1')
|
|
101
102
103
104
105
106
107
108
|
{
[self setCoilOpen:TRUE];
}
else
{
[self setCoilOpen:FALSE];
}
substrRange.length = 3;
|
|
109
|
substrRange.location = 19;
|
|
110
|
|
|
111
|
for(int i = 0; i < MONEY_IN_NUM_UNITS; i++)
|
|
112
113
114
115
|
{
[[self moneyCollected] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:substrRange] intValue]] atIndex:i];
substrRange.location += 3;
}
|
|
116
|
for(int i = 0; i < MONEY_OUT_NUM_UNITS; i++)
|
|
117
118
119
120
121
122
123
124
125
126
127
128
|
{
[[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;
}
|
|
129
|
if([[self response] characterAtIndex:78] == '1')
|
|
130
131
132
133
134
135
136
137
|
{
[[self changeAvailable]insertObject:[NSNumber numberWithBool:TRUE] atIndex:0];
}
else
{
[[self changeAvailable]insertObject:[NSNumber numberWithBool:FALSE] atIndex:0];
}
|
|
138
|
if([[self response] characterAtIndex:79] == '1')
|
|
139
140
141
142
143
144
145
146
147
148
|
{
[[self changeAvailable]insertObject:[NSNumber numberWithBool:TRUE] atIndex:1];
}
else
{
[[self changeAvailable]insertObject:[NSNumber numberWithBool:FALSE] atIndex:1];
}
}
@end
|