This repository was archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
181 lines (148 loc) · 5.94 KB
/
ViewController.m
File metadata and controls
181 lines (148 loc) · 5.94 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// ViewController.m
// apio-comunication-library
//
// Created by Matteo Pio Napolitano on 17/01/15.
// Copyright (c) 2015 OnCreate. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <ApioIOSocketDelegate>
@property (weak, nonatomic) IBOutlet UISwitch *switch_piantana;
@property (weak, nonatomic) IBOutlet UISlider *slider_piantana_rosso;
@property (weak, nonatomic) IBOutlet UISlider *slider_piantana_verde;
@property (weak, nonatomic) IBOutlet UISlider *slider_piantana_blu;
@property (nonatomic, strong) ApioIOSocket *aios;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.switch_piantana setSelected:NO];
self.slider_piantana_rosso.minimumValue = 0;
self.slider_piantana_rosso.maximumValue = 255;
self.slider_piantana_rosso.continuous = NO;
self.slider_piantana_verde.minimumValue = 0;
self.slider_piantana_verde.maximumValue = 255;
self.slider_piantana_verde.continuous = NO;
self.slider_piantana_blu.minimumValue = 0;
self.slider_piantana_blu.maximumValue = 255;
self.slider_piantana_blu.continuous = NO;
self.aios = [[ApioIOSocket alloc] initWithHost:@"http://debian.local" andPort:@"8083"];
self.aios.delegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)onSocketReady:(NSDictionary *)initialConfiguration
{
NSLog(@"onSocketReady %@", initialConfiguration);
}
- (void)onSocketConnectionFailed
{
NSLog(@"onSocketConnectionFailed");
}
- (void)onEventReceived:(NSArray*)data
{
NSLog(@"onEventReceived %@", data);
}
- (void)onSocketError:(NSDictionary*)error
{
NSLog(@"onSocketError %@", error);
}
- (void)onSocketDisconnected
{
NSLog(@"onSocketDisconnected");
}
- (void)onSocketReconnect
{
NSLog(@"onSocketReconnect");
}
- (void)onSocketReconnectionError
{
NSLog(@"onSocketReconnectionError");
}
- (IBAction)buttonStatoPiantanaPressed:(id)sender
{
[self getStateInformationForObject:4545];
}
- (IBAction)piantanaChangeStateAction:(id)sender {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *properties = [[NSMutableDictionary alloc] init];
if ([self.switch_piantana isOn]) {
[properties setObject:[NSNumber numberWithInt:1] forKey:@"onoff"];
}
else{
[properties setObject:[NSNumber numberWithInt:0] forKey:@"onoff"];
}
[dict setObject:[NSNumber numberWithInt:4545] forKey:@"objectId"];
[dict setObject:properties forKey:@"properties"];
[dict setObject:@"true" forKey:@"writeToDatabase"];
[dict setObject:@"true" forKey:@"writeToSerial"];
NSDictionary *data = [[NSDictionary alloc] initWithDictionary:dict];
[self.aios emit:@"apio_client_update" data:data];
}
- (IBAction)sliderRossoChangedAction:(id)sender {
NSUInteger valore = (NSUInteger)(self.slider_piantana_rosso.value);
NSLog(@"sliderRossoChangedAction");
[self changeColor:@"rosso" setValue:valore];
}
- (IBAction)sliderVerdeChangedAction:(id)sender {
NSUInteger valore = (NSUInteger)(self.slider_piantana_rosso.value);
NSLog(@"sliderVerdeChangedAction");
[self changeColor:@"verde" setValue:valore];
}
- (IBAction)sliderBluChangedAction:(id)sender {
NSUInteger valore = (NSUInteger)(self.slider_piantana_rosso.value);
NSLog(@"sliderBluChangedAction");
[self changeColor:@"blu" setValue:valore];
}
-(void)changeColor:(NSString*)color setValue:(int)value
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *properties = [[NSMutableDictionary alloc] init];
[properties setObject:[NSNumber numberWithInt:value] forKey:color];
[dict setObject:[NSNumber numberWithInt:4545] forKey:@"objectId"];
[dict setObject:properties forKey:@"properties"];
[dict setObject:@"true" forKey:@"writeToDatabase"];
[dict setObject:@"true" forKey:@"writeToSerial"];
NSDictionary *data = [[NSDictionary alloc] initWithDictionary:dict];
[self.aios emit:@"apio_client_update" data:data];
}
-(void)getStateInformationForObject:(int)objectId
{
HTTPClient *client = [[HTTPClient alloc] init];
NSString *payload = [@"http://debian.local:8083/apio/object/" stringByAppendingString:[NSString stringWithFormat:@"%d", objectId]];
NSURL *url = [NSURL URLWithString: payload];
[client connect:url
method:@"GET"
beforeStart:^{
// puoi implementare callback eseguita prima dell'avvio della connessione
}
duringConnection:^{
// puoi implementare callback eseguita durante la connessione
}
afterComplete:^{
NSDictionary *responsedata = [client responsedata];
if (responsedata != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%@",[client responsedata]);
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Stato Oggetto"
message:[NSString stringWithFormat:@"%@", [client responsedata]]
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[alertView show];
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Stato Oggetto"
message:@"Errore della classe HTTPClient"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[alertView show];
});
}
}];
}
@end