Blame view

DUREX Vendor Control/Sensors.m 3.84 KB
Imanol-Mikel Barba Sabariego authored
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]];
Imanol-Mikel Barba Sabariego authored
23
    [self setProductsSold:[[NSMutableArray alloc]init]];
Imanol-Mikel Barba Sabariego authored
24
25
26
27
28
    [self setChangeAvailable:[[NSMutableArray alloc] init]];

    return self;
}
Imanol-Mikel Barba Sabariego authored
29
- (void) setResponseValue:(NSString *)response
Imanol-Mikel Barba Sabariego authored
30
{
Imanol-Mikel Barba Sabariego authored
31
    [self setResponse:response];
Imanol-Mikel Barba Sabariego authored
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;
Imanol-Mikel Barba Sabariego authored
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;
Imanol-Mikel Barba Sabariego authored
51
Imanol-Mikel Barba Sabariego authored
52
53
    channelMotors = [[[self response] substringWithRange:substrRange] intValue];
    substrRange.location += 5;
Imanol-Mikel Barba Sabariego authored
54
55
56
57
58
59
60

    for(int i = 0; i < MAX_CHANNELS; i++)
    {
        if(channelStatus & 0x01)
        {
            channelCount++;
        }
Imanol-Mikel Barba Sabariego authored
61
        channelStatus >>= 1;
Imanol-Mikel Barba Sabariego authored
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];
        }
Imanol-Mikel Barba Sabariego authored
75
        channelAvailability >>= 1;
Imanol-Mikel Barba Sabariego authored
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];
        }
Imanol-Mikel Barba Sabariego authored
88
        channelMotors >>= 1;
Imanol-Mikel Barba Sabariego authored
89
90
    }
Imanol-Mikel Barba Sabariego authored
91
    if([[self response] characterAtIndex:17] == '1')
Imanol-Mikel Barba Sabariego authored
92
93
94
95
96
97
98
99
    {
        [self setDoorOpen:TRUE];
    }
    else
    {
        [self setDoorOpen:FALSE];
    }
Imanol-Mikel Barba Sabariego authored
100
    if([[self response] characterAtIndex:18] == '1')
Imanol-Mikel Barba Sabariego authored
101
102
103
104
105
106
107
108
    {
        [self setCoilOpen:TRUE];
    }
    else
    {
        [self setCoilOpen:FALSE];
    }
    substrRange.length = 3;
Imanol-Mikel Barba Sabariego authored
109
    substrRange.location = 19;
Imanol-Mikel Barba Sabariego authored
110
Imanol-Mikel Barba Sabariego authored
111
    for(int i = 0; i < MONEY_IN_NUM_UNITS; i++)
Imanol-Mikel Barba Sabariego authored
112
113
114
115
    {
        [[self moneyCollected] insertObject:[NSNumber numberWithInt:[[[self response] substringWithRange:substrRange] intValue]] atIndex:i];
        substrRange.location += 3;
    }
Imanol-Mikel Barba Sabariego authored
116
    for(int i = 0; i < MONEY_OUT_NUM_UNITS; i++)
Imanol-Mikel Barba Sabariego authored
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;
    }
Imanol-Mikel Barba Sabariego authored
129
    if([[self response] characterAtIndex:78] == '1')
Imanol-Mikel Barba Sabariego authored
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];
    }
Imanol-Mikel Barba Sabariego authored
138
    if([[self response] characterAtIndex:79] == '1')
Imanol-Mikel Barba Sabariego authored
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