CommandClassControlView.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //
  2. // CommandClassControlView.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 3/16/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "JDJSONModel.h"
  11. #import "DeviceModel.h"
  12. #import "CustomLabel.h"
  13. #import "CommonUtil.h"
  14. #import "CustomButton.h"
  15. #import "CommandClassControlView.h"
  16. #import "ThingsDetailViewController.h"
  17. #import "ThingsViewController.h"
  18. #import "MainViewController.h"
  19. @implementation CommandClassControlView
  20. + (CommandClassControlView *)viewForCommandClass:(CmdClsType)cmdClsType {
  21. CommandClassControlView *controlView = nil;
  22. for (UIView *view in [CommonUtil nibViews:@"CommandClassControlView"]) {
  23. if (view.tag == cmdClsType) {
  24. controlView = (CommandClassControlView *)view;
  25. controlView.width = kfControlDefaultWidth;
  26. controlView.height = kfControlDefaultHeight;
  27. break;
  28. }
  29. }
  30. return controlView;
  31. }
  32. - (void)setDevice:(DeviceModel *)device {
  33. _device = device;
  34. }
  35. - (void)setNode:(NodeModel *)node {
  36. _node = node;
  37. }
  38. - (NSDictionary *)parameterForNodeCommand {
  39. NSDictionary *parameter = nil;
  40. if (_device) {
  41. parameter = @{@"device_id": _device.deviceId,
  42. @"node_id": _device.nodeId};
  43. } else if (_node) {
  44. parameter = @{@"device_id": _node.deviceId,
  45. @"node_id": _node.nodeId};
  46. }
  47. return parameter;
  48. }
  49. - (CGSize)sizeForIntrinsic {
  50. return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
  51. }
  52. - (CGSize)sizeForIntrinsicForItemCount:(NSInteger)count {
  53. return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
  54. }
  55. - (void)requestNodeCommand:(NSDictionary *)parameters requestValue:(NSString *)requestValue completionHandler:(NodeCommandCompletionBlock)completion failureHandler:(NodeCommandFailureBlock)failure {
  56. if (_device.isRequesting) {
  57. return;
  58. }
  59. _device.isRequesting = YES;
  60. NSString *deviceId = parameters[@"device_id"];
  61. NSString *nodeId = parameters[@"node_id"];
  62. NSDictionary *param = @{@"request_value": requestValue};
  63. NSString *path = [NSString stringWithFormat:API_POST_NODE_COMMAND, deviceId, nodeId];
  64. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:param modelClass:[DeviceModel class] completion:^(id responseObject) {
  65. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  66. return;
  67. }
  68. DeviceModel *result = (DeviceModel *)responseObject;
  69. if (result && result.commandId && ![result.commandId isEmptyString]) {
  70. _device.requestTime = result.requestTime;
  71. ThingsViewController *vc = [JDFacade facade].mainViewController.tvc;
  72. if (vc && [vc isKindOfClass:[ThingsViewController class]]) {
  73. [vc requestPollingCommandStatusOfDeviceInBackground:_device];
  74. }
  75. }
  76. if (completion) {
  77. completion(responseObject);
  78. }
  79. } failure:^(id errorObject) {
  80. JDErrorModel *error = (JDErrorModel *)errorObject;
  81. [[JDFacade facade] alert:error.errorMessage];
  82. if (failure) {
  83. failure(error);
  84. }
  85. }];
  86. }
  87. - (NSString *)cmdclsValueForType:(NSString *)dataTypeCode value:(CGFloat)value {
  88. NSString *result = nil;
  89. if ([dataTypeCode isEqualToString:ksDataTypeCodeInteger]) {
  90. result = [NSString stringWithFormat:@"%zd", (NSInteger)value];
  91. } else if ([dataTypeCode isEqualToString:ksDataTypeCodeFloat]) {
  92. result = [NSString stringWithFormat:@"%.1f", value];
  93. } else {
  94. result = [NSString stringWithFormat:@"%zd", (NSInteger)value];
  95. }
  96. return result;
  97. }
  98. - (void)updateThingsViewContoller {
  99. if ([[JDFacade facade].currentViewController isKindOfClass:[ThingsDetailViewController class]]) {
  100. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  101. if (vc) {
  102. [vc prepareViewDidLoad];
  103. }
  104. }
  105. }
  106. @end
  107. @implementation CommandClassSwitchView
  108. - (void)willMoveToSuperview:(UIView *)newSuperview {
  109. if (!newSuperview)
  110. return;
  111. [self initControl];
  112. }
  113. - (void)initControl {
  114. _requestValueForOff = @"OFF";
  115. _requestValueForOn = @"ON";
  116. [self setLayoutUI];
  117. }
  118. - (void)setLayoutUI {
  119. _device = _device ? _device : _node;
  120. _lblContentValueMsg.text = _device.contentValueMsg;
  121. _isON = [_device.contentValue isEqualToString:_requestValueForOn];
  122. _device.cmdclsValue = _isON ? _requestValueForOff : _requestValueForOn;
  123. [self setSwitchBinary];
  124. // if (!_isON) {
  125. // [_btnSwitch faceOffImage];
  126. // }
  127. }
  128. - (IBAction)btnSwitchTouched:(id)sender {
  129. _device.cmdclsValue = _isON ? _requestValueForOn : _requestValueForOff;
  130. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  131. return;
  132. NSString *requestValue = _isON ? _requestValueForOff : _requestValueForOn;
  133. [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  134. // _isON = !_isON;
  135. // _device.contentValue = _isON ? _requestValueForOn : _requestValueForOff;
  136. // [self toggleSwitch];
  137. } failureHandler:nil];
  138. }
  139. - (void)setSwitchBinary {
  140. [_btnSwitch setTitle:_device.cmdclsValueMsg forState:UIControlStateNormal];
  141. if (_isON) {//OFF
  142. // _btnSwitch.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  143. // _btnSwitch.rectForCapBackground = CGRectMake(30, 0, 0, 40);
  144. } else {//ON
  145. // _btnSwitch.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
  146. // _btnSwitch.rectForCapBackground = CGRectMake(40, 0, 0, 30);
  147. }
  148. }
  149. - (void)toggleSwitch {
  150. [self setSwitchBinary];
  151. // [_btnSwitch faceOffImage];
  152. [self updateThingsViewContoller];
  153. }
  154. @end
  155. @implementation CommandClassSwitchMultiLevelView
  156. - (void)willMoveToSuperview:(UIView *)newSuperview {
  157. if (!newSuperview)
  158. return;
  159. [self initControl];
  160. }
  161. - (void)initControl {
  162. [self setLayoutUI];
  163. }
  164. - (void)setLayoutUI {
  165. _device = _device ? _device : _node;
  166. _device.cmdclsValue = _device.cmdclsValue ? _device.cmdclsValue : [self cmdclsValueForType:_device.dataTypeCode value:[_device.controlMaxValue floatValue]/ 2];
  167. _currentValue = !_isConditionMode ? [_device.contentValue floatValue] : [_device.cmdclsValue floatValue];
  168. _minValue = [_device.controlMinValue floatValue];
  169. _maxValue = [_device.controlMaxValue floatValue];
  170. _step = [_device.controlStep floatValue];
  171. NSString *sMax = [self cmdclsValueForType:_device.dataTypeCode value:_maxValue];
  172. CGFloat width = [CommonUtil getSizeFromString:sMax font:_btnLabel.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
  173. CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
  174. eWidth = eWidth < kfControlDefaultWidth ? kfControlDefaultWidth : eWidth;
  175. _constraintBtnLabelWidth.constant = eWidth;
  176. CGRect sr = self.frame;
  177. sr.size.width = eWidth;
  178. self.frame = sr;
  179. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [self cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  180. }
  181. - (void)setEnable:(BOOL)enable {
  182. _enable = enable;
  183. _btnMinus.userInteractionEnabled = _enable;
  184. _btnPlus.userInteractionEnabled = _enable;
  185. if (_enable) {
  186. [_btnLabel setTitleColor:kUITextColor02 forState:UIControlStateNormal];
  187. [_btnMinus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_minus"] forState:UIControlStateNormal];
  188. [_btnPlus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_plus"] forState:UIControlStateNormal];
  189. } else {
  190. [_btnLabel setTitleColor:kUITextColor03 forState:UIControlStateNormal];
  191. [_btnMinus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_minus_disable"] forState:UIControlStateNormal];
  192. [_btnPlus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_plus_disable"] forState:UIControlStateNormal];
  193. }
  194. }
  195. - (IBAction)btnPlusTouchDown:(id)sender {
  196. if (!_pressTimer) {
  197. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(addValue) userInfo:nil repeats:YES];
  198. }
  199. }
  200. - (void)btnMinusTouchDown:(id)sender {
  201. if (!_pressTimer) {
  202. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(subtractValue) userInfo:nil repeats:YES];
  203. }
  204. }
  205. - (IBAction)btnPlusTouchUp:(id)sender {
  206. [_pressTimer invalidate];
  207. _pressTimer = nil;
  208. [self addValue];
  209. [self requestCommand];
  210. }
  211. - (IBAction)btnMinusTouchUp:(id)sender {
  212. [_pressTimer invalidate];
  213. _pressTimer = nil;
  214. [self subtractValue];
  215. [self requestCommand];
  216. }
  217. - (void)addValue {
  218. _currentValue = _currentValue + _step > _maxValue ? _minValue : _currentValue + _step;
  219. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [self cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  220. if (_device) {
  221. _device.cmdclsValue = [self cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  222. [self updateThingsViewContoller];
  223. }
  224. }
  225. - (void)subtractValue {
  226. _currentValue = _currentValue - _step < _minValue ? _maxValue : _currentValue - _step;
  227. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [self cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  228. if (_device) {
  229. _device.cmdclsValue = [self cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  230. [self updateThingsViewContoller];
  231. }
  232. }
  233. - (void)requestCommand {
  234. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  235. return;
  236. NSString *requestValue = _device.cmdclsValue;
  237. [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  238. _device.contentValue = [self cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  239. } failureHandler:nil];
  240. }
  241. @end
  242. @implementation CommandClassValveView
  243. - (void)willMoveToSuperview:(UIView *)newSuperview {
  244. if (!newSuperview)
  245. return;
  246. [self initControl];
  247. }
  248. - (void)initControl {
  249. _requestValueForOff = @"CLOSE";
  250. _requestValueForOn = @"OPEN";
  251. [self setLayoutUI];
  252. }
  253. - (void)setLayoutUI {
  254. _device = _device ? _device : _node;
  255. _lblContentValueMsg.text = _device.contentValueMsg;
  256. _isOpened = [_device.contentValue isEqualToString:_requestValueForOn];
  257. _device.cmdclsValue = _isOpened ? _requestValueForOff : _requestValueForOn;
  258. if ([_device.cmdclsTypeId isEqualToString:@"36002"]) {
  259. // [_btnSwitch setImage:[UIImage imageNamed:@"tp_01_img_control_switch_lock_unlocking"] forState:UIControlStateNormal];
  260. }
  261. [self setSwitchBinary];
  262. // if (!_isON) {
  263. // [_btnSwitch faceOffImage];
  264. // }
  265. }
  266. - (void)setSwitchBinary {
  267. [_btnSwitch setTitle:_device.cmdclsValueMsg forState:UIControlStateNormal];
  268. if (_isOpened) {//Unlocked
  269. // _btnLabel.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
  270. // _btnLabel.rectForCapBackground = CGRectMake(40, 0, 0, 30);
  271. } else {//Locked
  272. // _btnLabel.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  273. // _btnLabel.rectForCapBackground = CGRectMake(30, 0, 0, 40);
  274. }
  275. }
  276. - (void)btnSwitchTouched:(id)sender {
  277. _device.cmdclsValue = _isOpened ? _requestValueForOn : _requestValueForOff;
  278. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  279. return;
  280. NSString *requestValue = _isOpened ? @"CLOSE" : @"OPEN";
  281. [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  282. // if ([_device.cmdclsTypeId isEqualToString:@"14001"]) {//Lock.Default일 경우에만 컨트롤을 토글시킴.
  283. // _isLocked = !_isLocked;
  284. // _device.contentValue = _isLocked ? @"LOCKED" : @"UNLOCKED";
  285. // } else if ([_device.cmdclsTypeId isEqualToString:@"14002"]) {
  286. // _isLocked = YES;
  287. // _device.contentValue = @"LOCKED";
  288. // } else if ([_device.cmdclsTypeId isEqualToString:@"14003"]) {
  289. // _isLocked = NO;
  290. // _device.contentValue = @"UNLOCKED";
  291. // }
  292. [self toggleSwitch];
  293. } failureHandler:nil];
  294. }
  295. - (void)toggleSwitch {
  296. [self setSwitchBinary];
  297. // [_btnSwitch faceOffImage];
  298. [self updateThingsViewContoller];
  299. }
  300. @end
  301. #pragma mark - Sensors View
  302. @implementation CommandClassSensorBinaryView
  303. - (void)willMoveToSuperview:(UIView *)newSuperview {
  304. if (!newSuperview)
  305. return;
  306. [self initControl];
  307. }
  308. - (void)initControl {
  309. _lblControl.textColor = kUITextColor02;
  310. [self setLayoutUI];
  311. }
  312. - (void)setLayoutUI {
  313. _device = _device ? _device : _node;
  314. NSString *contentValue = _device.contentValueMsg;
  315. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  316. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  317. if (_node) {//디바이스 상세일 경우,
  318. self.lblControl.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
  319. self.lblControl.font = [UIFont systemFontOfSize:kUIFontSize07];
  320. if ([contentValue isEqualToString:@"TRUE"]) {
  321. [self.lblControl setColor:kUITextColor02 text:contentValue];
  322. }
  323. } else if (_device) {
  324. self.lblControl.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
  325. if ([contentValue isEqualToString:@"TRUE"]) {
  326. [self.lblControl setColor:kUITextColor02 text:contentValue];
  327. }
  328. }
  329. _constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblControl.text font:self.lblControl.font width:kfControlMaxWidth].width;
  330. CGRect sr = self.frame;
  331. sr.size.width = _constraintControlLabelWidth.constant;
  332. self.frame = sr;
  333. }
  334. @end
  335. @implementation CommandClassSensorMultiLevelView
  336. - (void)setLayoutUI {
  337. _device = _device ? _device : _node;
  338. NSString *contentValue = (!_device.contentValue || [_device.contentValue isEmptyString] ||[_device.contentValue isEqualToString:@"none"]) ? @"-" : _device.contentValue;
  339. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  340. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  341. if (_node) {//디바이스 상세일 경우,
  342. self.lblControl.text = [NSString stringWithFormat:@"%@%@", contentValue, unit];
  343. self.lblControl.font = [UIFont systemFontOfSize:kUIFontSize07];
  344. } else if (_device) {
  345. self.lblControl.text = [NSString stringWithFormat:@"%@ : %@%@", _device.nodeName, contentValue, unit];
  346. [self.lblControl setColor:kUITextColor02 text:contentValue];
  347. }
  348. self.constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblControl.text font:self.lblControl.font width:kfControlMaxWidth].width;
  349. CGRect sr = self.frame;
  350. sr.size.width = self.constraintControlLabelWidth.constant;
  351. self.frame = sr;
  352. }
  353. @end
  354. @implementation CommandClassMeterCurrentView
  355. - (void)willMoveToSuperview:(UIView *)newSuperview {
  356. if (!newSuperview)
  357. return;
  358. [self initControl];
  359. }
  360. - (void)initControl {
  361. _lblMeter.textColor = kUITextColor02;
  362. [self setLayoutUI];
  363. }
  364. - (void)setLayoutUI {
  365. _device = _device ? _device : _node;
  366. NSString *contentValue = (!_device.contentValue || [_device.contentValue isEmptyString] ||[_device.contentValue isEqualToString:@"none"]) ? @"-" : _device.contentValue;
  367. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  368. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  369. if (_node) {//디바이스 상세일 경우,
  370. self.lblMeter.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
  371. self.lblMeter.font = [UIFont systemFontOfSize:kUIFontSize07];
  372. } else if (_device) {
  373. self.lblMeter.text = [NSString stringWithFormat:@"%@ : %@ %@", _device.nodeName, contentValue, unit];
  374. [self.lblMeter setColor:kUITextColor02 text:contentValue];
  375. }
  376. self.constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblMeter.text font:self.lblMeter.font width:kfControlMaxWidth].width;
  377. CGRect sr = self.frame;
  378. sr.size.width = self.constraintControlLabelWidth.constant;
  379. self.frame = sr;
  380. }
  381. @end
  382. @implementation CommandClassMeterTotalView
  383. @end