CommandClassControlDetailView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // CommandClassControlDetailView.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 "CustomRadioGroup.h"
  16. #import "UIImageView+WebCache.h"
  17. #import "CommandClassControlDetailView.h"
  18. #import "CommandClassControlView.h"
  19. #import "CustomImageView.h"
  20. #define GRAPH_ANI_DUR 0.5f
  21. #define kfControlDetailSwtchLeftPadding 45.0f
  22. #define kfControlDetailSwtchRightPadding 28.0f
  23. #define kfControlDetailDefaultWidth 320.0f
  24. #define kfControlDetailDefaultHeight 285.0f
  25. typedef void(^NodeCommandCompletionBlock)(id result);
  26. typedef void(^NodeCommandFailureBlock)(id error);
  27. @implementation CommandClassControlDetailView
  28. + (CommandClassControlView *)viewForCommandClass:(CmdClsType)cmdClsType
  29. {
  30. CommandClassControlView *controlView = nil;
  31. for (UIView *view in [CommonUtil nibViews:@"CommandClassControlDetailView"]) {
  32. if (view.tag == cmdClsType) {
  33. controlView = (CommandClassControlView *)view;
  34. controlView.width = IPHONE_WIDTH;
  35. controlView.height = kfControlDetailDefaultHeight;
  36. break;
  37. }
  38. }
  39. if (!controlView) {//상위 부모에서 가져옴.
  40. controlView = [CommandClassControlView viewForCommandClass:cmdClsType];
  41. }
  42. return controlView;
  43. }
  44. @end
  45. @implementation CommandClassSwitchDetailView
  46. - (void)setLayoutUI {
  47. [super setLayoutUI];
  48. // self.height = 347.0f;
  49. // NSString *stmp = @"켜기";
  50. // CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnOff.titleLabel.font width:FLT_MAX].width;// + kfSwitchLabelPadding;
  51. // CGFloat eWidth = width + kfControlDetailSwtchLeftPadding + kfControlDetailSwtchRightPadding;
  52. // _constraintBtnSwitchWidth.constant = eWidth;
  53. // _btnOn.titleEdgeInsets = UIEdgeInsetsMake(0, 45, 0, 28);
  54. // _btnOff.titleEdgeInsets = UIEdgeInsetsMake(0, 45, 0, 28);
  55. _btnNode.enabled = _node.isDeviceOnline;
  56. _btnNode.selected = _btnNode.enabled && _isON;
  57. [self checkOffline];
  58. }
  59. - (IBAction)btnOnTouched:(id)sender {
  60. if (![self checkOnlineHub]) return;
  61. if (_isON) {
  62. [[JDFacade facade] toast:@"이미 켜진 상태입니다"];
  63. return;
  64. }
  65. // [self requestNodeCommand:self.parameterForNodeCommand requestValue:_requestValueForOn completionHandler:nil failureHandler:nil];
  66. // [self requestNodeCommand:_requestValueForOn];
  67. [self requestNodeCommand:self.parameterForNodeCommand requestValue:[self requestValueForNodeCommand:_requestValueForOn]];
  68. }
  69. - (IBAction)btnOffTouched:(id)sender {
  70. if (![self checkOnlineHub]) return;
  71. if (!_isON) {
  72. [[JDFacade facade] toast:@"이미 꺼진 상태입니다"];
  73. return;
  74. }
  75. // [self requestNodeCommand:self.parameterForNodeCommand requestValue:_requestValueForOff completionHandler:nil failureHandler:nil];
  76. // [self requestNodeCommand:_requestValueForOff];
  77. [self requestNodeCommand:self.parameterForNodeCommand requestValue:[self requestValueForNodeCommand:_requestValueForOff]];
  78. }
  79. - (BOOL)checkOnlineHub {
  80. if (![[JDFacade facade].loginUser onlineHomeHubFromSelectedHub:_selectHub]) {
  81. [[JDFacade facade] alert:@"허브가 오프라인 상태입니다. 허브 상태 확인 후 다시 시도해주세요." completionHander:nil];
  82. return NO;
  83. }
  84. return YES;
  85. }
  86. - (void)setSwitchBinary {
  87. _pcontainer.hidden = !_node.isRequesting;
  88. if (!_pcontainer.hidden) {
  89. [self startProgressAni];
  90. } else {
  91. [self stopProgressAni];
  92. }
  93. // _btnOn.enabled = _btnOff.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
  94. NSLog(@"_node.isDeviceOnline : %d", _node.isDeviceOnline);
  95. NSLog(@"_node.isDeviceOnline : %@", _selectHub);
  96. _btnOn.enabled = _node.isDeviceOnline ;
  97. _lblContentValueMsg.text = _node.contentValueMsg;
  98. _btnNode.userInteractionEnabled = NO;
  99. [_btnOn setImage:[_btnOn imageForState:_btnOn.state] forState:_btnOn.state];
  100. // [_btnOff setImage:[_btnOff imageForState:_btnOff.state] forState:_btnOff.state];
  101. }
  102. - (void)startProgressAni {
  103. //chagne button image
  104. [UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear animations:^{
  105. _imgvProgress.transform = CGAffineTransformMakeRotation(M_PI);
  106. } completion:nil];
  107. }
  108. - (void)stopProgressAni {
  109. [UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear animations:^{
  110. _imgvProgress.transform = CGAffineTransformMakeRotation(0);
  111. } completion:^(BOOL finished) {
  112. _btnNode.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
  113. _btnNode.selected = _btnNode.enabled && _isON;
  114. [self checkOffline];
  115. }];
  116. }
  117. - (void)checkOffline {
  118. if (!_btnNode.enabled) {//online?
  119. self.lblContentValueMsg.text = @"OFFLINE";
  120. [self.lblContentValueMsg setColor:kUITextColor01 text:self.lblContentValueMsg.text];
  121. }
  122. }
  123. @end
  124. @implementation CommandClassValveDetailView
  125. - (void)setLayoutUI {
  126. [super setLayoutUI];
  127. // self.height = 347.0f;
  128. // NSString *stmp = @"잠그기";
  129. // CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnClose.titleLabel.font width:FLT_MAX].width;// + kfSwitchLabelPadding;
  130. // CGFloat eWidth = width + kfControlDetailSwtchLeftPadding + kfControlDetailSwtchRightPadding;
  131. // _constraintBtnWidth.constant = eWidth;
  132. // _btnClose.titleEdgeInsets = UIEdgeInsetsMake(0, 45, 0, 28);
  133. [self checkOffline];
  134. }
  135. - (IBAction)btnCloseTouched:(id)sender {
  136. if (![[JDFacade facade].loginUser onlineHomeHubFromSelectedHub:_selectHub]) {
  137. [[JDFacade facade] alert:@"허브가 오프라인 상태입니다. 허브 상태 확인 후 다시 시도해주세요." completionHander:nil];
  138. return;
  139. }
  140. if (!_isOpened) {
  141. [[JDFacade facade] toast:@"이미 잠긴 상태입니다"];
  142. return;
  143. }
  144. //NSLog(@"Parameter : %@", self.parameterForNodeCommand);
  145. // [self requestNodeCommand:self.parameterForNodeCommand requestValue:_requestValueForClose completionHandler:nil failureHandler:nil];
  146. // [self requestNodeCommand:_requestValueForClose];
  147. [self requestNodeCommand:self.parameterForNodeCommand requestValue:[self requestValueForNodeCommand:_requestValueForClose]];
  148. }
  149. - (void)setSwitchBinary {
  150. _pcontainer.hidden = !_node.isRequesting;
  151. if (!_pcontainer.hidden) {
  152. [self startProgressAni];
  153. } else {
  154. [self stopProgressAni];
  155. }
  156. _btnClose.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
  157. _lblContentValueMsg.text = _node.contentValueMsg;
  158. if ([_node.contentValueMsg isEqualToString:@"잠김"]) {
  159. [self valveIsLock:YES];
  160. } else {
  161. [self valveIsLock:NO];
  162. }
  163. [_btnClose setImage:[_btnClose imageForState:_btnClose.state] forState:_btnClose.state];
  164. }
  165. - (void)valveIsLock:(BOOL)lock{
  166. if (lock) {
  167. _lblContentValueMsg.textAlignment = NSTextAlignmentCenter;
  168. _btnClose.hidden = YES;
  169. [self.lblContentValueMsg setColor:kUITextColor01 text:self.lblContentValueMsg.text];
  170. _lblLeadingSpace.constant = 15.0f;
  171. _btnCloseWidth.constant = 0.0f;
  172. } else {
  173. _lblContentValueMsg.textAlignment = NSTextAlignmentLeft;
  174. _btnClose.hidden = NO;
  175. [self.lblContentValueMsg setColor:kUITextColor02 text:self.lblContentValueMsg.text];
  176. _lblLeadingSpace.constant = 23.0f;
  177. _btnCloseWidth.constant = 60.0f;
  178. }
  179. }
  180. - (void)startProgressAni {
  181. //chagne button image
  182. [UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveLinear animations:^{
  183. _imgvProgress.transform = CGAffineTransformMakeRotation(M_PI);
  184. } completion:nil];
  185. }
  186. - (void)stopProgressAni {
  187. [UIView animateWithDuration:0.0f delay:0.0f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear animations:^{
  188. _imgvProgress.transform = CGAffineTransformMakeRotation(0);
  189. } completion:^(BOOL finished) {
  190. _btnNode.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
  191. _btnNode.selected = _btnNode.enabled && _isOpened;
  192. [self checkOffline];
  193. }];
  194. }
  195. - (void)checkOffline {
  196. if (!_btnNode.enabled) {//online?
  197. _btnClose.hidden = YES;
  198. _btnCloseWidth.constant = 0.0f;
  199. self.lblContentValueMsg.text = @"OFFLINE";
  200. self.lblContentValueMsg.textAlignment = NSTextAlignmentCenter;
  201. [self.lblContentValueMsg setColor:kUITextColor01 text:self.lblContentValueMsg.text];
  202. }
  203. }
  204. @end
  205. #pragma mark - Sensors View
  206. @implementation CommandClassSensorBinaryDetailView
  207. - (void)setLayoutUI {
  208. [super setLayoutUI];
  209. _lblNodeName.hidden = !_isMandatoryNode;
  210. if (!_lblNodeName.hidden) {
  211. _lblNodeName.text = _device.nodeName;
  212. }
  213. _btnNode.userInteractionEnabled = NO;
  214. [_btnNode setImage:[_node imageForMandataryForState:UIControlStateNormal] forState:UIControlStateNormal];
  215. [_btnNode setImage:[_node imageForMandataryForState:UIControlStateSelected] forState:UIControlStateSelected];
  216. [_btnNode setImage:[_node imageForMandataryForState:UIControlStateDisabled] forState:UIControlStateDisabled];
  217. _btnNode.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
  218. _btnNode.selected = _btnNode.enabled && [_node.contentValue isEqualToString:@"TRUE"];
  219. if (_btnNode.enabled) {//online?
  220. [self.lblControl setColor:kUITextColor01 text:self.lblControl.text];
  221. if ([_device.contentValue isEqualToString:@"TRUE"]) {
  222. [self.lblControl setColor:kUITextColor02 text:self.lblControl.text];
  223. }
  224. } else {
  225. self.lblControl.text = @"OFFLINE";
  226. [self.lblControl setColor:kUITextColor01 text:self.lblControl.text];
  227. }
  228. }
  229. @end
  230. @implementation CommandClassSensorMultiLevelDetailView
  231. - (void)setLayoutUI {
  232. [super setLayoutUI];
  233. _btnNode.userInteractionEnabled = NO;
  234. _lblNodeName.hidden = !_isMandatoryNode;
  235. if (!_lblNodeName.hidden) {
  236. _lblNodeName.text = _device.nodeName;
  237. }
  238. [_btnNode setImage:[_node imageForMandataryForState:UIControlStateNormal] forState:UIControlStateNormal];
  239. [_btnNode setImage:[_node imageForMandataryForState:UIControlStateSelected] forState:UIControlStateSelected];
  240. [_btnNode setImage:[_node imageForMandataryForState:UIControlStateDisabled] forState:UIControlStateDisabled];
  241. _btnNode.enabled = _node.isDeviceOnline && [JDFacade facade].loginUser.isHomehubOnline;
  242. _btnNode.selected = _btnNode.enabled && [_node.contentValue isEqualToString:@"TRUE"];
  243. _lblCondition.hidden = !_node.isDeviceOnline || ![JDFacade facade].loginUser.isHomehubOnline;
  244. _lblCondition.text = [_node conditionForMandatary];
  245. if (_btnNode.enabled) {//online?
  246. [self.lblControl setColor:kUITextColor01 text:self.lblControl.text];
  247. } else {
  248. self.lblControl.text = @"OFFLINE";
  249. [self.lblControl setColor:kUITextColor01 text:self.lblControl.text];
  250. }
  251. }
  252. @end