-
Notifications
You must be signed in to change notification settings - Fork 0
/
lampConfig.swift
218 lines (161 loc) · 5.84 KB
/
lampConfig.swift
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// lampConfig.swift
// Prism Niligo
//
// Created by amirhosein on 5/2/1396 AP.
// Copyright © 1396 amirhosein. All rights reserved.
//
import UIKit
import Foundation
class lampTabBarController : UITabBarController {
var username = "Niligo Bulb"
var color = UIColor()
var turnOn = false
var fade = 0
var time = 0
var red = 255 ;
var green=255 ;
var blue=255 ;
var configDictionary = [String: [String: AnyObject]]()
var dataDictionary = [String: Any]()
var identityDictionary = [String: Any]()
var serverDictionary = [String : Any]()
var updateState = true ;
var network = NetworkManager() ;
var getStateURL = "http://192.168.1.40/WebServices/Core.svc/GetState"
var setColorWithPowerURL = "http://192.168.1.40/WebServices/Core.svc/SetPowerWithColor"
init() {
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func updateColor() {
self.color = UIColor(red : CGFloat(self.red)/255 , green : CGFloat(self.green)/255 , blue : CGFloat(self.blue)/255 , alpha : 1.0 )
}
func showError( str : String ){
}
func getState() {
self.identityDictionary["SessionKey"] = "E6B6D964B966E71180CC4A9D6544D113"
// self.identityDictionary["SessionKey"] = UserDefaults.standard.string(forKey: "sessionKey") ;
self.serverDictionary["Identity"] = self.identityDictionary ;
if(self.updateState) {
print("11")
if( !network.networkCall(self , self.getStateURL , serverDictionary as [String : AnyObject] ) ) {
print("lampConfig network call Failed !!!")
}
return
}
}
override func viewDidLoad() {
super.viewDidLoad()
print("lampconfig viewDidLoad")
}
override func viewWillAppear(_ animated: Bool) {
print("lampconfig viewWillAppear")
self.getState()
}
func networkCall(sender : AnyObject) -> Bool {
if(sender.tag == 2 ){
serverDictionary["Parameters"] = dataDictionary ;
serverDictionary["Identity"] = identityDictionary ;
}else{
self.dictionaryUpdate()
}
print("\n the server dictionary : \n ",serverDictionary)
return network.networkCall(self,self.setColorWithPowerURL,serverDictionary as [String : AnyObject] )
}
func manageResponse(_ responseJson : [String : Any]) {
print("GET STATE response json in attributes update \n" , responseJson)
guard let parameters = responseJson["Parameters"] as? [String : Any] else {
print("parameters is null")
return
}
guard let status = responseJson["Status"] as? [String : Any] else {
print("status error")
return
}
self.red = parameters["Red"] as! Int
self.green = parameters["Green"] as! Int
self.blue = parameters["Blue"] as! Int
self.updateColor()
self.fade = parameters["FadeTime"] as! Int ;
self.turnOn = parameters["Power"] as! Bool ;
self.time = 0
self.updateState = false
self.dictionaryUpdate()
print("12")
}
func attributesUpdate(){
print("999",dataDictionary)
print("333",dataDictionary["Red"]!)
dataDictionary = dataDictionary as [String : Any]
self.red = ( dataDictionary["Red"] as AnyObject ).integerValue
self.green = ( dataDictionary["Green"] as AnyObject ).integerValue
self.blue = ( dataDictionary["Blue"] as AnyObject ).integerValue
self.updateColor()
self.turnOn = dataDictionary["Power"] as! Bool
self.fade = dataDictionary["FadeTime"] as! Int
self.time = dataDictionary["Delay"] as! Int
}
func dictionaryUpdate() {
print("dictionary update")
red = Int(self.color.Red)!
green=Int(self.color.Green)!
blue=Int(self.color.Blue)!
dataDictionary["Red"] = self.color.Red;
dataDictionary["Green"] = self.color.Green;
dataDictionary["Blue"] = self.color.Blue;
dataDictionary["Power"] = self.turnOn ;
dataDictionary["FadeTime"] = self.fade ;
dataDictionary["Delay"] = self.time ;
identityDictionary["SessionKey"] = "E6B6D964B966E71180CC4A9D6544D113" ;
serverDictionary["Parameters"] = dataDictionary ;
serverDictionary["Identity"] = identityDictionary ;
}
}
extension UIColor {
var hexString : String {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
self.getRed(&r, green: &g, blue: &b, alpha: &a)
return String(
format: "%02X%02X%02X",
Int(r * 0xff),
Int(g * 0xff),
Int(b * 0xff)
)
}
var Red : String {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
self.getRed(&r, green: &g, blue: &b, alpha: &a)
return String(
Int(r * 255)
)
}
var Green : String {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
self.getRed(&r, green: &g, blue: &b, alpha: &a)
return String(
Int(g * 255)
)
}
var Blue : String {
var r: CGFloat = 0
var g: CGFloat = 0
var b: CGFloat = 0
var a: CGFloat = 0
self.getRed(&r, green: &g, blue: &b, alpha: &a)
return String(
Int(b * 255)
)
}
}