CommandClassControlNodeView.m 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //
  2. // CommandClassControlNodeView.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/5/15.
  6. // Copyright © 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "JDJSONModel.h"
  11. #import "DeviceModel.h"
  12. #import "CommandClassControlNodeView.h"
  13. #import "MultiLevelPopupView.h"
  14. #import "CustomRadioGroup.h"
  15. #import "CommandClassControlView.h"
  16. #define kfSwitchLabelPadding 20.0f
  17. //#define kfCommandClassSwitchLeftPadding 20.0f
  18. //#define kfCommandClassSwitchRightPadding 20.0f
  19. #define kfCommandClassLockRightPadding 20.0f
  20. //#define kfControlDefaultWidth 215.0f;
  21. //#define kfControlMaxWidth IPHONE_WIDTH - 105.0f
  22. #define kfControlDefaultHeight 35.0f
  23. @implementation CommandClassControlNodeView
  24. + (CommandClassControlNodeView *)viewForCommandClass:(CmdClsType)cmdClsType
  25. {
  26. CommandClassControlNodeView *controlView = nil;
  27. for (UIView *view in [CommonUtil nibViews:@"CommandClassControlNodeView"]) {
  28. if (view.tag == cmdClsType) {
  29. controlView = (CommandClassControlNodeView *)view;
  30. controlView.width = kfControlDefaultWidth;
  31. controlView.height = kfControlDefaultHeight;
  32. break;
  33. }
  34. }
  35. return controlView;
  36. }
  37. - (CGSize)sizeForIntrinsic {
  38. return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
  39. }
  40. - (CGSize)sizeForIntrinsicForItemCount:(NSInteger)count {
  41. return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
  42. }
  43. - (void)controlNodeAction {
  44. if ([self.delegate respondsToSelector:@selector(didControlNodeAction:)]) {
  45. [self.delegate didControlNodeAction:self];
  46. }
  47. }
  48. @end
  49. @implementation CommandClassControlNodeSensorBinaryView
  50. - (void)willMoveToSuperview:(UIView *)newSuperview {
  51. if (!newSuperview)
  52. return;
  53. [self initControl];
  54. }
  55. - (void)initControl {
  56. [self setLayoutUI];
  57. }
  58. - (void)setLayoutUI {
  59. }
  60. - (IBAction)btnOpenTouched:(id)sender {
  61. }
  62. - (IBAction)btnCloseTouched:(id)sender {
  63. }
  64. @end
  65. @implementation CommandClassControlNodeSensorMultiLevelView
  66. @end
  67. @implementation CommandClassControlNodeValveView
  68. - (void)willMoveToSuperview:(UIView *)newSuperview {
  69. if (!newSuperview)
  70. return;
  71. [self initControl];
  72. }
  73. - (void)initControl {
  74. [self setLayoutUI];
  75. }
  76. - (void)setLayoutUI {
  77. NSString *stmp = _subItem.cmdclsValueMsgLongest;
  78. CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnOpen.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
  79. CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
  80. _constraintBtnOpenWidth.constant = eWidth;
  81. // CGRect sr = self.frame;
  82. // sr.size.width = eWidth;
  83. // self.frame = sr;
  84. CmdClsValueModel *cmdclsValueOn = nil, *cmdclsValueOff = nil;
  85. if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) {
  86. cmdclsValueOn = _subItem.cmdclsValueList[0];
  87. cmdclsValueOff = _subItem.cmdclsValueList[1];
  88. //default setting
  89. _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOn.cmdclsValue;
  90. //layout setting
  91. [_btnOpen setTitle:cmdclsValueOn.cmdclsValueMsg forState:UIControlStateNormal];
  92. [_btnClose setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal];
  93. _btnOpen.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0);
  94. _btnClose.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0);
  95. } else if ([_subItem.cmdclsTypeId isEqualToString:@"36002"]) {//only close
  96. cmdclsValueOff = _subItem.cmdclsValueList[0];
  97. //default setting
  98. _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOff.cmdclsValue;
  99. //layout setting
  100. [_btnClose setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal];
  101. _btnClose.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0);
  102. [_btnOpen removeFromSuperview];
  103. [self removeConstraint:_constraintBtnSameWidth];
  104. _constraintBtnOpenWidth.constant = 0;
  105. _constraintBtnSpace.constant = 0;
  106. _constraintBtnCloseWidth.constant = 50;
  107. }
  108. if ([_subItem.cmdclsValue isEqualToString:cmdclsValueOn.cmdclsValue]) {
  109. [self btnOpenTouched:nil];
  110. } else {
  111. [self btnCloseTouched:nil];
  112. }
  113. }
  114. - (IBAction)btnOpenTouched:(id)sender {
  115. if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) {
  116. [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]];
  117. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]];
  118. _btnOpen.selected = YES;
  119. _btnClose.selected = NO;
  120. }
  121. if (sender) {//트리거 설정
  122. [self controlNodeAction];
  123. }
  124. }
  125. - (IBAction)btnCloseTouched:(id)sender {
  126. if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) {
  127. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
  128. [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[1]];
  129. _btnOpen.selected = NO;
  130. _btnClose.selected = YES;
  131. } else if ([_subItem.cmdclsTypeId isEqualToString:@"36002"]) {//only close
  132. [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]];
  133. _btnClose.selected = YES;
  134. }
  135. if (sender) {//트리거 설정
  136. [self controlNodeAction];
  137. }
  138. }
  139. @end
  140. @implementation CommandClassControlNodeSwitchBinaryView
  141. - (void)willMoveToSuperview:(UIView *)newSuperview {
  142. if (!newSuperview)
  143. return;
  144. [self initControl];
  145. }
  146. - (void)initControl {
  147. [self setLayoutUI];
  148. }
  149. - (void)setLayoutUI {
  150. NSString *stmp = _subItem.cmdclsValueMsgLongest;
  151. CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnOff.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
  152. CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
  153. _constraintBtnOnWidth.constant = eWidth;
  154. CmdClsValueModel *cmdclsValueOn = _subItem.cmdclsValueList[0];
  155. CmdClsValueModel *cmdclsValueOff = _subItem.cmdclsValueList[1];
  156. //default setting
  157. _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOn.cmdclsValue;
  158. [_btnOn setTitle:cmdclsValueOn.cmdclsValueMsg forState:UIControlStateNormal];
  159. [_btnOff setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal];
  160. _btnOn.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0);
  161. _btnOff.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0);
  162. if ([_subItem.cmdclsValue isEqualToString:cmdclsValueOn.cmdclsValue]) {
  163. [self btnOnTouched:nil];
  164. } else {
  165. [self btnOffTouched:nil];
  166. }
  167. }
  168. - (IBAction)btnOnTouched:(id)sender {
  169. [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]];
  170. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]];
  171. _btnOn.selected = YES;
  172. _btnOff.selected = NO;
  173. if (sender) {//트리거 설정
  174. [self controlNodeAction];
  175. }
  176. }
  177. - (IBAction)btnOffTouched:(id)sender {
  178. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
  179. [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[1]];
  180. _btnOn.selected = NO;
  181. _btnOff.selected = YES;
  182. if (sender) {//트리거 설정
  183. [self controlNodeAction];
  184. }
  185. }
  186. @end
  187. @interface CommandClassControlNodeSwitchMultiLevelView () {
  188. MultiLevelPopupView *_mpopup;
  189. }
  190. @end
  191. @implementation CommandClassControlNodeSwitchMultiLevelView
  192. - (void)willMoveToSuperview:(UIView *)newSuperview {
  193. if (!newSuperview)
  194. return;
  195. [self initControl];
  196. }
  197. - (void)initControl {
  198. [self setLayoutUI];
  199. }
  200. - (void)setLayoutUI {
  201. CGFloat half = ([_subItem.controlMaxValue floatValue] - [_subItem.controlMinValue floatValue]) / 2;
  202. NSString *cmdClsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : [CommandClassControlView cmdclsValueForType:_subItem.dataTypeCode value:half];
  203. _subItem.conditionTypeCode = _subItem.conditionTypeCode && ![_subItem.conditionTypeCode isEmptyString] ? _subItem.conditionTypeCode : ksConditionTypeCodeGreatOrEqual;
  204. NSString *condition = _subItem.conditionTypeCode && ![_subItem.conditionTypeCode isEmptyString] ? [_subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하" : @"이상";
  205. NSString *contentValue = [NSString stringWithFormat:@"%@%@ %@", cmdClsValue, _subItem.unit, condition];
  206. _subItem.cmdclsValueMsg = contentValue;
  207. [_btnMultiLevel setTitle:contentValue forState:UIControlStateNormal];
  208. }
  209. - (IBAction)btnMutilLevelTouched:(id)sender {
  210. //call popup
  211. if (!_mpopup) {
  212. _mpopup = [[MultiLevelPopupView alloc] initFromNib];
  213. }
  214. _mpopup.refSubItem = _subItem;
  215. [_mpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  216. if (buttonIndex == 0) {//OK
  217. NSString *condition = [_subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하";
  218. NSString *contentValue = [NSString stringWithFormat:@"%@%@ %@", _subItem.cmdclsValue, _subItem.unit, condition];
  219. _subItem.cmdclsValueMsg = contentValue;
  220. [_btnMultiLevel setTitle:contentValue forState:UIControlStateNormal];
  221. if (sender) {//트리거 설정
  222. [self controlNodeAction];
  223. }
  224. }
  225. }];
  226. }
  227. @end