CommandClassControlNodeView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 "CustomTextField.h"
  13. #import "CommandClassControlNodeView.h"
  14. #import "MultiLevelPopupView.h"
  15. #import "CustomRadioGroup.h"
  16. #import "CommandClassControlView.h"
  17. #define kfCommandNodeSwitchLabelPadding 0.0f
  18. #define kfCommandNodeSwitchLeftPadding 0.0f
  19. #define kfCommandNodeSwitchRightPadding 0.0f
  20. #define kfCommandNodeLockRightPadding 0.0f
  21. #define kfControlNodeDefaultWidth 256.0f
  22. //#define kfControlMaxWidth IPHONE_WIDTH - 105.0f
  23. #define kfControlNodeDefaultHeight 36.0f
  24. #define kfControlNodeDefaultMargin 0.0f
  25. @implementation CommandClassControlNodeView
  26. + (CommandClassControlNodeView *)viewForCommandClass:(CmdClsType)cmdClsType {
  27. CommandClassControlNodeView *controlView = nil;
  28. for (UIView *view in [CommonUtil nibViews:@"CommandClassControlNodeView"]) {
  29. if (view.tag == cmdClsType) {
  30. controlView = (CommandClassControlNodeView *)view;
  31. controlView.width = kfControlNodeDefaultWidth;
  32. controlView.height = kfControlNodeDefaultHeight;
  33. break;
  34. }
  35. }
  36. return controlView;
  37. }
  38. //- (void)setNodeEnable:(BOOL)nodeEnable {
  39. // _nodeEnable = nodeEnable;
  40. //
  41. // [self setLayoutUI];
  42. //}
  43. //
  44. //- (void)setLayoutUI {
  45. //
  46. //}
  47. - (CGSize)sizeForIntrinsic {
  48. return CGSizeMake(kfControlNodeDefaultWidth, kfControlNodeDefaultHeight);
  49. }
  50. - (CGSize)sizeForIntrinsicForItemCount:(NSInteger)count {
  51. return CGSizeMake(kfControlNodeDefaultWidth, kfControlNodeDefaultHeight);
  52. }
  53. - (void)controlNodeAction {
  54. if ([self.delegate respondsToSelector:@selector(didControlNodeAction:)]) {
  55. [self.delegate didControlNodeAction:self];
  56. }
  57. }
  58. @end
  59. @implementation CommandClassControlNodeSensorBinaryView
  60. @end
  61. @implementation CommandClassControlNodeValveView
  62. - (void)willMoveToSuperview:(UIView *)newSuperview {
  63. if (!newSuperview)
  64. return;
  65. [self initControl];
  66. }
  67. - (void)initControl {
  68. [self setLayoutUI];
  69. }
  70. - (void)setLayoutUI {
  71. NSString *stmp = _subItem.cmdclsValueMsgLongest;
  72. CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnOpen.titleLabel.font width:FLT_MAX].width; // + kfSwitchLabelPadding;
  73. CGFloat eWidth = width + kfCommandNodeSwitchLeftPadding + kfCommandNodeSwitchRightPadding;
  74. _constraintBtnOpenWidth.constant = eWidth;
  75. CmdClsValueModel *cmdclsValueOn = nil, *cmdclsValueOff = nil;
  76. if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) {
  77. cmdclsValueOn = _subItem.cmdclsValueList[0];
  78. cmdclsValueOff = _subItem.cmdclsValueList[1];
  79. //default setting
  80. _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOn.cmdclsValue;
  81. //layout setting
  82. [_btnOpen setTitle:cmdclsValueOn.cmdclsValueMsg forState:UIControlStateNormal];
  83. [_btnClose setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal];
  84. } else if ([_subItem.cmdclsTypeId isEqualToString:@"36002"]) {//only close
  85. cmdclsValueOff = _subItem.cmdclsValueList[0];
  86. //default setting
  87. _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOff.cmdclsValue;
  88. //layout setting
  89. [_btnClose setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal];
  90. [_btnOpen removeFromSuperview];
  91. [self removeConstraint:_constraintBtnSameWidth];
  92. _constraintBtnOpenWidth.constant = 0;
  93. _constraintBtnSpace.constant = 0;
  94. _constraintBtnCloseWidth.constant = eWidth;
  95. }
  96. if (_nodeEnable) {
  97. if ([_subItem.cmdclsValue isEqualToString:cmdclsValueOn.cmdclsValue]) {
  98. [self btnOpenTouched:nil];
  99. } else {
  100. [self btnCloseTouched:nil];
  101. }
  102. } else {
  103. _btnOpen.selected = NO;
  104. _btnClose.selected = NO;
  105. if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) {
  106. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
  107. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]];
  108. } else if ([_subItem.cmdclsTypeId isEqualToString:@"36002"]) {//only close
  109. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
  110. }
  111. }
  112. }
  113. - (IBAction)btnOpenTouched:(id)sender {
  114. if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) {
  115. [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]];
  116. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]];
  117. }
  118. _btnOpen.selected = YES;
  119. _btnClose.selected = NO;
  120. if (sender) {//트리거 설정
  121. [self controlNodeAction];
  122. }
  123. }
  124. - (IBAction)btnCloseTouched:(id)sender {
  125. if ([_subItem.cmdclsTypeId isEqualToString:@"36001"]) {
  126. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
  127. [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[1]];
  128. _btnOpen.selected = NO;
  129. _btnClose.selected = YES;
  130. } else if ([_subItem.cmdclsTypeId isEqualToString:@"36002"]) {//only close
  131. [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]];
  132. _btnClose.selected = YES;
  133. }
  134. if (sender) {//트리거 설정
  135. [self controlNodeAction];
  136. }
  137. }
  138. @end
  139. @implementation CommandClassControlNodeSwitchBinaryView
  140. - (void)willMoveToSuperview:(UIView *)newSuperview {
  141. if (!newSuperview)
  142. return;
  143. [self initControl];
  144. }
  145. - (void)initControl {
  146. [self setLayoutUI];
  147. }
  148. - (void)setLayoutUI {
  149. NSString *stmp = _subItem.cmdclsValueMsgLongest;
  150. CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnOff.titleLabel.font width:FLT_MAX].width;// + kfSwitchLabelPadding;
  151. CGFloat eWidth = width + kfCommandNodeSwitchLeftPadding + kfCommandNodeSwitchRightPadding;
  152. _constraintBtnOnWidth.constant = eWidth;
  153. CmdClsValueModel *cmdclsValueOn = _subItem.cmdclsValueList[0];
  154. CmdClsValueModel *cmdclsValueOff = _subItem.cmdclsValueList[1];
  155. //default setting
  156. _subItem.cmdclsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : cmdclsValueOn.cmdclsValue;
  157. [_btnOn setTitle:cmdclsValueOn.cmdclsValueMsg forState:UIControlStateNormal];
  158. [_btnOff setTitle:cmdclsValueOff.cmdclsValueMsg forState:UIControlStateNormal];
  159. if (_nodeEnable) {
  160. if ([_subItem.cmdclsValue isEqualToString:cmdclsValueOn.cmdclsValue]) {
  161. [self btnOnTouched:nil];
  162. } else {
  163. [self btnOffTouched:nil];
  164. }
  165. } else {
  166. _btnOn.selected = NO;
  167. _btnOff.selected = NO;
  168. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
  169. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]];
  170. }
  171. }
  172. - (IBAction)btnOnTouched:(id)sender {
  173. CmdClsValueModel *cmdclsValueOn = _subItem.cmdclsValueList[0];
  174. _subItem.cmdclsValue = cmdclsValueOn.cmdclsValue;
  175. [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[0]];
  176. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[1]];
  177. _btnOn.selected = YES;
  178. _btnOff.selected = NO;
  179. if (sender) {//트리거 설정
  180. [self controlNodeAction];
  181. }
  182. }
  183. - (IBAction)btnOffTouched:(id)sender {
  184. CmdClsValueModel *cmdclsValueOff = _subItem.cmdclsValueList[1];
  185. _subItem.cmdclsValue = cmdclsValueOff.cmdclsValue;
  186. [[JDFacade facade] setRadioButtonStatus:@NO object:_subItem.cmdclsValueList[0]];
  187. [[JDFacade facade] setRadioButtonStatus:@YES object:_subItem.cmdclsValueList[1]];
  188. _btnOn.selected = NO;
  189. _btnOff.selected = YES;
  190. if (sender) {//트리거 설정
  191. [self controlNodeAction];
  192. }
  193. }
  194. - (CGSize)sizeForIntrinsic {
  195. CGFloat width = (_constraintBtnOnWidth.constant * 2) + kfControlNodeDefaultMargin;
  196. return CGSizeMake(width, kfControlDefaultHeight);
  197. }
  198. @end
  199. @interface CommandClassControlNodeSwitchMultiLevelView () {
  200. MultiLevelPopupView *_mpopup;
  201. }
  202. @end
  203. @implementation CommandClassControlNodeSwitchMultiLevelView
  204. - (void)willMoveToSuperview:(UIView *)newSuperview {
  205. if (!newSuperview)
  206. return;
  207. [self initControl];
  208. }
  209. - (void)initControl {
  210. [self setLayoutUI];
  211. }
  212. - (void)setLayoutUI {
  213. CGFloat half = ([_subItem.controlMaxValue floatValue] - [_subItem.controlMinValue floatValue]) / 2;
  214. NSString *cmdClsValue = _subItem.cmdclsValue && ![_subItem.cmdclsValue isEmptyString] ? _subItem.cmdclsValue : [CommandClassControlView cmdclsValueForType:_subItem.dataTypeCode value:half];
  215. _subItem.conditionTypeCode = _subItem.conditionTypeCode && ![_subItem.conditionTypeCode isEmptyString] ? _subItem.conditionTypeCode : ksConditionTypeCodeGreatOrEqual;
  216. NSString *condition = _subItem.conditionTypeCode && ![_subItem.conditionTypeCode isEmptyString] ?
  217. ([_subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하") : @"이상";
  218. NSString *contentValue = [NSString stringWithFormat:@"%@%@ %@", cmdClsValue, _subItem.unit, condition];
  219. _subItem.cmdclsValue = cmdClsValue;
  220. _subItem.cmdclsValueMsg = contentValue;
  221. [_btnMultiLevel setTitle:contentValue forState:UIControlStateNormal];
  222. _btnMultiLevel.selected = _nodeEnable;
  223. }
  224. - (IBAction)btnMutilLevelTouched:(id)sender {
  225. //call popup
  226. if (!_mpopup) {
  227. _mpopup = [[MultiLevelPopupView alloc] initFromNib];
  228. }
  229. _mpopup.lblTitle.text = _subItem.sourceSubName;
  230. _mpopup.refSubItem = _subItem;
  231. [_mpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  232. if (buttonIndex == 0) {//OK
  233. NSString *condition = [_subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하";
  234. NSString *contentValue = [NSString stringWithFormat:@"%@%@ %@", _subItem.cmdclsValue, _subItem.unit, condition];
  235. _subItem.cmdclsValueMsg = contentValue;
  236. [_btnMultiLevel setTitle:contentValue forState:UIControlStateNormal];
  237. if (sender) {//트리거 설정
  238. [self controlNodeAction];
  239. }
  240. }
  241. }];
  242. }
  243. @end
  244. @implementation CommandClassControlNodeSensorMultiLevelView
  245. @end
  246. @implementation CommandClassControlNodeBatteryView
  247. @end
  248. @implementation CommandClassControlNodeMeterView
  249. @end
  250. @implementation CommandClassControlNodeElecPlugView
  251. @end
  252. @implementation CommandClassControlNodeTotalElecPlugView
  253. @end
  254. @implementation CommandClassControlNodeValveLockView
  255. @end