Sensors.m
3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// 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