CommandClassControlNodeView.m 12 KB

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