RulesAddViewController.m 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. //
  2. // RulesAddViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/20/15.
  6. // Copyright © 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "ItemModel.h"
  11. #import "RuleModel.h"
  12. #import "DeviceModel.h"
  13. #import "CustomLabel.h"
  14. #import "CustomButton.h"
  15. #import "CustomImageView.h"
  16. #import "CustomTableView.h"
  17. #import "CustomCheckBox.h"
  18. #import "CustomTextView.h"
  19. #import "CustomTextField.h"
  20. #import "UIImageView+WebCache.h"
  21. #import "ValidateUtil.h"
  22. #import "DeviceSelectPopupView.h"
  23. #import "DeviceNodePopupView.h"
  24. #import "TriggerSelectPopupView.h"
  25. #import "ConditionSelectPopupView.h"
  26. #import "RulesConditionHomeModePopupView.h"
  27. #import "RulesConditionViewController.h"
  28. #import "RulesAddViewController.h"
  29. #import "RulesAddCompleteViewController.h"
  30. #import "RulesDetailViewController.h"
  31. #import "RulesViewController.h"
  32. #import "TimePickerPopupView.h"
  33. #import "DaylightPopupView.h"
  34. #import "ExternHeatPopupView.h"
  35. //규칙이름
  36. @implementation RulesAddTitleTableViewCell
  37. #pragma mark - textfield delegate
  38. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  39. return YES;
  40. }
  41. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  42. NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
  43. BOOL input = text.length == 0;
  44. _btnConfirm.hidden = input;
  45. _btnAdd.hidden = !input;
  46. return YES;
  47. }
  48. @end
  49. //알림 메시지
  50. @interface RulesAddTextTableViewCell() <UITextViewDelegate>
  51. @property (nonatomic, strong) void (^heightChangeBlock)(CGFloat height) ;
  52. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *constTxtHeight;
  53. @property CGRect previousRect;
  54. @end
  55. @implementation RulesAddTextTableViewCell
  56. - (void)awakeFromNib {
  57. [super awakeFromNib];
  58. _previousRect = CGRectZero;
  59. _previousRect.origin.y = 11.f;
  60. }
  61. - (void)registHeightChangeBlock:(void (^)(CGFloat height)) heightChangeBlock {
  62. _heightChangeBlock = heightChangeBlock ;
  63. }
  64. - (void)invokeChangeHeight:(CGFloat)height {
  65. if( _heightChangeBlock != nil )
  66. _heightChangeBlock(height) ;
  67. }
  68. #pragma mark - textView delegate
  69. - (void)textViewDidChange:(UITextView *)textView {
  70. UITextPosition* pos = _txtView.endOfDocument;
  71. CGRect currentRect = [_txtView caretRectForPosition:pos];
  72. CGFloat previousY = _previousRect.origin.y;
  73. if (previousY != currentRect.origin.y) {
  74. CGFloat height = currentRect.origin.y - previousY;
  75. _constTxtHeight.constant = _constTxtHeight.constant + height < 48.f ? 48.f : _constTxtHeight.constant + height;
  76. [self invokeChangeHeight:height];
  77. }
  78. _previousRect = currentRect;
  79. }
  80. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
  81. NSString *string = [textView.text stringByReplacingCharactersInRange:range withString:text];
  82. BOOL input = string.length == 0;
  83. _btnConfirm.hidden = input;
  84. _btnAdd.hidden = !input;
  85. return YES;
  86. }
  87. - (void)textViewDidBeginEditing:(UITextView *)textView {
  88. _imgTxtBg.highlighted = YES;
  89. }
  90. - (void)textViewDidEndEditing:(UITextView *)textView {
  91. _imgTxtBg.highlighted = NO;
  92. }
  93. @end
  94. @implementation RulesAddHeaderTableViewCell
  95. @end
  96. @implementation RulesAddTableViewCell
  97. @end
  98. @implementation RulesAddPushTableViewCell
  99. @end
  100. @implementation RulesAddConditionHeaderTableViewCell
  101. @end
  102. @implementation RulesAddConditionTableViewCell
  103. @end
  104. @implementation RulesAddFooterTableViewCell
  105. @end
  106. @interface RulesAddViewController () <KeyboardFrameChangedDelegate> {
  107. NSMutableArray<ItemModel> *_triggerDevices, *_actionDevices, *_conditionDevices;
  108. BOOL _isNotFirstLoading, _hasCondition;
  109. NSMutableArray *_arrayForHeader, *_arrayForFooter;
  110. CGFloat textCellChangeHeight; //알림 메세지 셀 사이즈 변동
  111. CustomTextField *_txtRuleTitle;
  112. CustomTextView *_txvMessage;
  113. CustomButton *_btnTriggerAdd, *_btnPushAdd;
  114. CustomCheckBox *_chkConditions;
  115. RulesConditionHomeModePopupView *_mpopup;
  116. RulesConditionViewController *_rcv;
  117. __weak IBOutlet NSLayoutConstraint *constTableBottom;
  118. }
  119. @property (strong, nonatomic) NSMutableArray<ItemModel> *triggers;
  120. @property (strong, nonatomic) NSMutableArray<ItemModel> *actions;
  121. @property (strong, nonatomic) NSMutableArray<ItemModel> *conditions;
  122. @end
  123. @implementation RulesAddViewController
  124. - (void)viewDidLoad {
  125. [super viewDidLoad];
  126. [self initProperties];
  127. [self initUI];
  128. [self prepareViewDidLoad];
  129. }
  130. - (void)viewWillAppear:(BOOL)animated {
  131. [super viewWillAppear:animated];
  132. [self addObserverKeyboardFrameChanged:self] ;
  133. }
  134. - (void)viewWillDisappear:(BOOL)animated {
  135. [super viewWillDisappear:animated];
  136. [self removeObserverKeyboard] ;
  137. }
  138. #pragma mark - Keyboard Frame Changed Delegate
  139. - (void)keyboardFrameChanged:(CGRect)frame duration:(CGFloat)duration display:(BOOL)display {
  140. [UIView animateWithDuration:duration animations:^{
  141. if( display ) {
  142. constTableBottom.constant = frame.size.height;
  143. }
  144. else {
  145. constTableBottom.constant = 50.0f;
  146. }
  147. [self.view layoutIfNeeded];
  148. }] ;
  149. }
  150. #pragma mark - init
  151. - (void)initProperties {
  152. _arrayForHeader = [[NSMutableArray alloc] init];
  153. _arrayForFooter = [[NSMutableArray alloc] init];
  154. if (!_triggers) {
  155. _triggers = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
  156. }
  157. if (!_actions) {
  158. _actions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
  159. }
  160. if (!_conditions) {
  161. _conditions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] init];
  162. }
  163. }
  164. - (void)initUI {
  165. [self initTableViewAsDefaultStyle:_tableView];
  166. if (_refRuleDetail) {
  167. _lblTitle.text = @"규칙 편집";
  168. }
  169. }
  170. - (void)prepareViewDidLoad {
  171. [self requestDeviceListForAction:ksItemTypeCodeTrigger];
  172. [self requestDeviceListForAction:ksItemTypeCodeAction];
  173. }
  174. //말줄임표 conv
  175. - (NSString*)convMaxLengthName:(NSString*)name len:(NSInteger)len {
  176. if (name.length > len) {
  177. return [NSString stringWithFormat:@"%@..", [name substringToIndex:len]];
  178. }
  179. return name;
  180. }
  181. #pragma mark - Main Logic
  182. - (void)requestDeviceListForAction:(NSString *)typeCode {
  183. //parameters
  184. // NSDictionary *parameter = @{@"item_type_code": typeCode};
  185. NSArray *arr = @[typeCode];
  186. // NSString *path = [NSString stringWithFormat:API_GET_ITEM_DEVICES, ksItemTypeCodeAction];
  187. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_GET_ITEM_DEVICES arguments:arr];
  188. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[ItemListModel class] completion:^(id responseObject) {
  189. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  190. return;
  191. }
  192. ItemListModel *fetchedItemList = (ItemListModel *)responseObject;
  193. if (fetchedItemList && fetchedItemList.list && fetchedItemList.list.count) {//API 성공 ,
  194. if ([typeCode isEqualToString:ksItemTypeCodeAction]) {
  195. _actionDevices = fetchedItemList.list;
  196. if (_refRuleDetail) {//편집일 경우,
  197. ItemModel *actionDevice = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  198. [self matchValueOfDevices:actionDevice devices:_actionDevices];
  199. }
  200. } else {
  201. _triggerDevices = fetchedItemList.list;
  202. _conditionDevices = [(NSMutableArray<ItemModel> *)[NSMutableArray alloc] initWithArray:_triggerDevices copyItems:YES];
  203. if (_refRuleDetail) {//편집일 경우,
  204. ItemModel *triggerDevice = [_triggers objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  205. ItemModel *conditionDevice = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  206. [self matchValueOfDevices:triggerDevice devices:_triggerDevices];
  207. [self matchValueOfDevices:conditionDevice devices:_conditionDevices];
  208. }
  209. [self requestDeviceListForAction:ksItemTypeCodeAction];
  210. }
  211. }
  212. [_tableView reloadData];
  213. } failure:^(id errorObject) {
  214. JDErrorModel *error = (JDErrorModel *)errorObject;
  215. [[JDFacade facade] alert:error.errorMessage];
  216. }];
  217. }
  218. - (void)matchValueOfDevices:(ItemModel *)deviceItem devices:(NSMutableArray<ItemModel> *)devices {
  219. for (ItemSubModel *subItem in deviceItem.subItems) {
  220. NSArray *matchedArray = [devices matchedArrayInSubArrays:@"subItems" predicateFormat:@"sourceId == %@ && sourceSubId == %@", subItem.sourceId, subItem.sourceSubId];
  221. if (matchedArray.count) {
  222. ItemSubModel *matchedSubItem = matchedArray.firstObject;
  223. [[JDFacade facade] setRadioButtonStatus:@YES object:matchedSubItem];
  224. CmdClsValueModel *pCmdClsValue = [matchedSubItem.cmdclsValueList objectKey:@"cmdclsValue" eqaulToString:subItem.cmdclsValue];
  225. [[JDFacade facade] setRadioButtonStatus:@YES object:pCmdClsValue];
  226. }
  227. }
  228. }
  229. - (void)requestRegisterRule {
  230. NSLog(@"requestRegisterRule");
  231. NSArray *triggers = nil;//[self items:_triggers];
  232. NSLog(@"triggers");
  233. NSArray *actions = [self items:_actions];
  234. NSLog(@"actions");
  235. // BOOL hasConditionForRegister = [_conditions matchedArrayInSubArrays:@"subItems" predicateFormat:@"conditionTypeCode == %@", @"09"].count || _chkConditions.checked;
  236. // NSArray *conditions = hasConditionForRegister ? [self conditions:_conditions] : nil;
  237. NSArray *conditions = [self conditions:_conditions];
  238. NSLog(@"conditions");
  239. //validate
  240. if (!triggers || !triggers.count) {
  241. [[JDFacade facade] alert:NSLocalizedString(@"규칙을 실행할 조건을 설정하세요.", @"규칙을 실행할 조건을 설정하세요.")];
  242. return;
  243. }
  244. if (!actions || !actions.count) {
  245. [[JDFacade facade] alert:NSLocalizedString(@"실행 조건에 맞춰 동작할 장치나 알림 메시지 중 최소 하나 이상을 설정하세요.", @"실행 조건에 맞춰 동작할 장치나 알림 메시지 중 최소 하나 이상을 설정하세요.")];
  246. return;
  247. }
  248. //parameters
  249. NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"rule_name" : _txtRuleTitle.text,
  250. @"use_yn" : ksYES,
  251. @"triggers": triggers ? triggers : [NSNull null],
  252. @"actions": actions ? actions : [NSNull null]}];
  253. if (conditions) {
  254. [parameter setObject:conditions forKey:@"conditions"];
  255. }
  256. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_POST_RULE_ADD aditional:@""];
  257. //[NSString stringWithFormat:API_POST_RULE];
  258. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path
  259. parameters:parameter
  260. modelClass:[RuleModel class]
  261. completion:^(id responseObject) {
  262. if (!responseObject) return;
  263. RuleModel *newRule = (RuleModel *) responseObject;
  264. if (newRule) {//API 성공 ,
  265. ItemModel *item = _triggers.firstObject;
  266. if (item) {
  267. newRule.ruleName = _txtRuleTitle.text;
  268. newRule.triggerItemSubTypeCode = item.itemSubTypeCode;
  269. }
  270. RulesAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"RulesAddCompleteViewController" storyboardName:@"Rules"];
  271. vc.refRule = newRule;
  272. vc.providesPresentationContextTransitionStyle = YES;
  273. vc.definesPresentationContext = YES;
  274. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  275. [self presentViewController:vc animated:YES completion:nil];
  276. }
  277. } failure:^(id errorObject) {
  278. JDErrorModel *error = (JDErrorModel *)errorObject;
  279. [[JDFacade facade] alert:error.errorMessage];
  280. }];
  281. }
  282. - (void)requestModifyRule {
  283. NSArray *triggers = [self items:_triggers];
  284. NSArray *actions = [self items:_actions];
  285. BOOL hasConditionForRegister = [_conditions matchedArrayInSubArrays:@"subItems" predicateFormat:@"conditionTypeCode == %@", @"09"].count || _chkConditions.checked;
  286. NSArray *conditions = hasConditionForRegister ? [self conditions:_conditions] : nil;
  287. //validate
  288. if (!triggers || !triggers.count) {
  289. [[JDFacade facade] alert:NSLocalizedString(@"실행할 때를 선택하세요", @"실행할 때를 선택하세요")];
  290. return;
  291. }
  292. if (!actions || !actions.count) {
  293. [[JDFacade facade] alert:NSLocalizedString(@"실행할 장치를 선택하세요", @"실행할 장치를 선택하세요")];
  294. return;
  295. }
  296. //parameters
  297. NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"rule_name" : _txtRuleTitle.text,
  298. @"use_yn" : ksYES,
  299. @"triggers": triggers ? triggers : [NSNull null],
  300. @"actions": actions ? actions : [NSNull null]}];
  301. if (conditions) {
  302. [parameter setObject:conditions forKey:@"conditions"];
  303. }
  304. NSString *path = [NSString stringWithFormat:API_POST_RULE_MODIFY, _refRuleDetail.homegrpRuleId];
  305. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[RuleModel class] completion:^(id responseObject) {
  306. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  307. return;
  308. }
  309. RuleModel *rule = (RuleModel *) responseObject;
  310. if (rule) {//API 성공
  311. RulesDetailViewController *vc = [[JDFacade facade] viewControllerOnPresentingViewController:self viewControllerClass:[RulesDetailViewController class]];
  312. if (vc) {
  313. [vc requestRuleDetail];
  314. } else {
  315. RulesViewController *rvc = [[JDFacade facade] viewControllerOnPresentingViewController:self viewControllerClass:[RulesViewController class]];
  316. [rvc prepareViewDidLoad];
  317. }
  318. [self dismissViewControllerAnimated:YES completion:^{
  319. [[JDFacade facade] toast:@"편집을 완료합니다"];
  320. }];
  321. }
  322. } failure:^(id errorObject) {
  323. JDErrorModel *error = (JDErrorModel *)errorObject;
  324. [[JDFacade facade] alert:error.errorMessage];
  325. }];
  326. }
  327. //아이템 배열을 리턴함.
  328. - (NSArray *)items:(NSArray<ItemModel> *)items {
  329. //triggers, actions, conditions
  330. NSMutableArray *rItems = [[NSMutableArray alloc] init];
  331. for (ItemModel *item in items) {
  332. NSLog(@"item : %@", item);
  333. NSDictionary *dic = nil;
  334. NSMutableArray *subItems = [[NSMutableArray alloc] init];
  335. NSArray *pdevices = [self subItemsForType:item.subItems itemSubType:ksItemSubTypeCodeDevice];
  336. NSLog(@"pdevices : %@", pdevices);
  337. NSArray *pushes = [self subItemsForType:item.pushes itemSubType:ksItemSubTypeCodeAppPush];
  338. NSLog(@"pushes : %@", pushes);
  339. NSArray *timers = [self subItemsForType:item.timers itemSubType:ksItemSubTypeCodeTimer];
  340. NSLog(@"timers : %@", timers);
  341. NSArray *daylights = [self subItemsForType:item.daylights itemSubType:ksItemSubTypeCodeDaylight];
  342. NSLog(@"daylights : %@", daylights);
  343. NSArray *heats = [self subItemsForType:item.heats itemSubType:ksItemSubTypeCodeHeat];
  344. NSLog(@"heats : %@", heats);
  345. if (pdevices && pdevices.count) {//device
  346. [subItems addObjectsFromArray:pdevices];
  347. }
  348. if (pushes && pushes.count) {//push
  349. [subItems addObjectsFromArray:pushes];
  350. }
  351. if (timers && timers.count) {//timer
  352. [subItems addObjectsFromArray:timers];
  353. }
  354. if (daylights && daylights.count) {//해뜰때/질때
  355. [subItems addObjectsFromArray:daylights];
  356. }
  357. if (heats && heats.count) {//더울떄 / 추울때
  358. [subItems addObjectsFromArray:heats];
  359. }
  360. if (subItems.count) {
  361. dic = @{@"item_name": item.itemName,
  362. @"item_sub_type_code": item.itemSubTypeCode,
  363. @"item_sub": subItems};
  364. [rItems addObject:dic];
  365. NSLog(@"dic : %@ %@", dic, item.itemName);
  366. }
  367. }
  368. return rItems;
  369. }
  370. //서브아이템 배열을 리턴함.
  371. - (NSArray *)subItemsForType:(NSArray *)subItems itemSubType:(NSString *)itemSubTypeCode {
  372. NSLog(@"itemSubTypeCode : %@", itemSubTypeCode);
  373. NSMutableArray *rSubItems = [[NSMutableArray alloc] init];
  374. if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
  375. NSArray<ItemSubModel> *pdevices = (NSArray<ItemSubModel> *)[subItems matchedArrayByObjectName:ksCustomRadioButtonStatus condition:YES];
  376. for (ItemSubModel *pdevice in pdevices) {
  377. NSDictionary *rSubItem = @{@"source_id": pdevice.sourceId,
  378. @"source_sub_id": pdevice.sourceSubId,
  379. @"cmdcls_value": pdevice.cmdclsValue,
  380. @"condition_type_code": pdevice.conditionTypeCode,
  381. @"data_type_code": pdevice.dataTypeCode};
  382. [rSubItems addObject:rSubItem];
  383. }
  384. }
  385. else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeAppPush]) {//푸시일 경우,
  386. //at once.
  387. for (ItemSubModel *subItem in subItems) {
  388. NSDictionary *rSubItem = @{@"condition_type_code": subItem.conditionTypeCode,
  389. @"cmdcls_value": subItem.cmdclsValue};
  390. [rSubItems addObject:rSubItem];
  391. }
  392. }
  393. else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
  394. for (ItemSubModel *subItem in subItems) {
  395. NSDictionary *rSubItem = @{@"hour": subItem.hour,
  396. @"minute": subItem.minute,
  397. @"dayofweek": subItem.daysOfWeek};
  398. [rSubItems addObject:rSubItem];
  399. }
  400. }
  401. else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 일 경우,
  402. for (ItemSubModel *subItem in subItems) {
  403. NSDictionary *rSubItem = @{@"source_id": subItem.sourceId,
  404. @"source_sub_id": subItem.sourceSubId,
  405. @"dayofweek": subItem.daysOfWeek};
  406. [rSubItems addObject:rSubItem];
  407. }
  408. }
  409. else if ([itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//온도
  410. for (ItemSubModel *subItem in subItems) {
  411. NSDictionary *rSubItem = @{@"source_id": subItem.sourceId,
  412. @"condition_type_code": subItem.conditionTypeCode,
  413. @"cmdcls_value": subItem.cmdclsValue,
  414. @"dayofweek": subItem.daysOfWeek};
  415. [rSubItems addObject:rSubItem];
  416. }
  417. }
  418. return rSubItems;
  419. }
  420. - (NSArray *)conditions:(NSArray<ItemModel> *)items {//trigger만 해당됨.
  421. //conditions
  422. NSMutableArray *rConditions = [[NSMutableArray alloc] init];
  423. for (ItemModel *condition in items) {//item = condition
  424. NSMutableArray *rSubItems = [[NSMutableArray alloc] init];
  425. if ([condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeHomeMode]) {
  426. for (ItemSubModel *mode in condition.subItems) {
  427. NSLog(@"mode : %@", mode);
  428. NSDictionary *rSubItem = @{@"source_id": mode.sourceId,
  429. @"condition_type_code": mode.conditionTypeCode,
  430. @"cmdcls_value": mode.cmdclsValue,
  431. @"condition_type_code": mode.conditionTypeCode};
  432. [rSubItems addObject:rSubItem];
  433. }
  434. }
  435. else if ([condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDueDate]) {
  436. for (ItemSubModel *subItem in condition.subItems) {
  437. NSLog(@"subItem : %@", subItem);
  438. NSDictionary *rSubItem = @{@"source_id": subItem.sourceId,
  439. @"source_sub_id": subItem.sourceSubId,
  440. @"condition_type_code": subItem.conditionTypeCode,
  441. @"cmdcls_value": subItem.cmdclsValue,
  442. @"condition_type_code": subItem.conditionTypeCode};
  443. [rSubItems addObject:rSubItem];
  444. }
  445. }
  446. else if ([condition.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  447. for (ItemSubModel *pdevice in condition.subItems) {
  448. NSLog(@"pdevice : %@", pdevice);
  449. NSDictionary *rSubItem = @{@"source_id": pdevice.sourceId,
  450. @"source_sub_id": pdevice.sourceSubId,
  451. @"cmdcls_value": pdevice.cmdclsValue,
  452. @"data_type_code": pdevice.dataTypeCode,
  453. @"condition_type_code": pdevice.conditionTypeCode};
  454. [rSubItems addObject:rSubItem];
  455. }
  456. }
  457. else if ([condition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDaysOfWeek]) {
  458. for (ItemSubModel *subItem in condition.subItems) {
  459. NSLog(@"subItem : %@", subItem);
  460. NSDictionary *rSubItem = @{@"cmdcls_value": subItem.cmdclsValue,
  461. @"condition_type_code": subItem.conditionTypeCode};
  462. [rSubItems addObject:rSubItem];
  463. }
  464. }
  465. if (rSubItems.count) {
  466. NSDictionary *dic = @{@"item_name": condition.itemName,
  467. @"item_sub_type_code": condition.itemSubTypeCode,
  468. @"item_sub": rSubItems};
  469. [rConditions addObject:dic];
  470. NSLog(@"dic : %@ %@", dic, condition.itemName);
  471. }
  472. }
  473. return rConditions;
  474. }
  475. - (void)cloneSubItemsForItemSubType:(NSArray<ItemModel> *)items {
  476. for (ItemModel *item in items) {
  477. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
  478. //기존 아이템을 그대로 사용함.
  479. for (ItemSubModel *subItem in item.subItems) {
  480. [[JDFacade facade] setRadioButtonStatus:@YES object:subItem];
  481. [[JDFacade facade] setCheckBoxStatus:@YES object:subItem];
  482. }
  483. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeAppPush]) {//푸시일 경우,
  484. item.pushes = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
  485. item.subItems = nil;
  486. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
  487. item.timers = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
  488. item.subItems = nil;
  489. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 경우,
  490. item.daylights = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
  491. item.subItems = nil;
  492. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//더울때/추울때 경우,
  493. item.heats = (NSMutableArray<ItemSubModel> *)[[NSMutableArray alloc] initWithArray:item.subItems copyItems:YES];
  494. item.subItems = nil;
  495. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeMode]) {//더울때/추울때 경우,
  496. //기존 아이템을 그대로 사용함.
  497. }
  498. }
  499. }
  500. - (void)setRefRuleDetail:(RuleDetailModel *)refRuleDetail {
  501. _refRuleDetail = refRuleDetail;
  502. _triggers = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_refRuleDetail.triggers copyItems:YES];
  503. _actions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_refRuleDetail.actions copyItems:YES];
  504. _conditions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_refRuleDetail.conditions copyItems:YES];
  505. [self cloneSubItemsForItemSubType:_triggers];
  506. [self cloneSubItemsForItemSubType:_actions];
  507. [self cloneSubItemsForItemSubType:_conditions];
  508. _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeHomeMode];
  509. _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeDueDate] ? YES : _hasCondition;
  510. _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice] ? YES : _hasCondition;
  511. }
  512. #pragma mark - UITableView DataSource & Delegate
  513. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  514. return 5;
  515. }
  516. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  517. NSInteger count = 0;
  518. if (section == 0) {//title
  519. count = 1;
  520. } else if (section == 1) {//triggers
  521. count = _triggers && _triggers.count ? _triggers.count : 0;
  522. } else if (section == 2) {//actions
  523. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  524. count = deviceItem.subItems && deviceItem.subItems.count ? deviceItem.subItems.count : 0;
  525. } else if (section == 3) {//push-message
  526. count = 1;
  527. } else if (section == 4) {//conditions
  528. count = 1;//_chkConditions.checked || _hasCondition ? 1 : 0;
  529. }
  530. return count;
  531. }
  532. - (NSString *)headerTitleForSection:(NSInteger)section {
  533. NSString *title = nil;
  534. if (section == 1) {//trigger
  535. title = @"실행 조건";
  536. if (_triggers && _triggers.count) {
  537. ItemModel *item = _triggers[0];
  538. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  539. title = @"장치 상태가 바뀔 때";
  540. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {
  541. title = @"이 시간마다";
  542. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {
  543. title = @"더울때 / 추울때";
  544. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {
  545. title = @"해뜰때 / 질때";
  546. }
  547. }
  548. } else if (section == 2) {//action
  549. title = @"동작할 장치";
  550. // title = _actions && _actions.count ? @"선택됨" : title;
  551. }
  552. // else if (section == 3) {//conditions
  553. // title = @"추가 조건";
  554. // title = _triggers && triggers.count ? @"선택됨" : title;
  555. // }
  556. return title;
  557. }
  558. - (void)addTargetToHeaderAddButton:(CustomButton *)btnAdd section:(NSInteger)section {
  559. if (section == 1) {
  560. _btnTriggerAdd = btnAdd;
  561. [btnAdd addTarget:self action:@selector(btnAddTriggerTouched:) forControlEvents:UIControlEventTouchUpInside];
  562. } else if (section == 2) {
  563. [btnAdd addTarget:self action:@selector(btnAddActionTouched:) forControlEvents:UIControlEventTouchUpInside];
  564. }
  565. }
  566. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  567. UIView *view = _arrayForHeader.count > section ? _arrayForHeader[section] : nil;
  568. if (!view) {
  569. if (section == 0 || section == 3) {//title
  570. view = [[UIView alloc] init];
  571. } else if (section == 1 || section == 2) {//hide add button or not;
  572. RulesAddHeaderTableViewCell *hcell = (RulesAddHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
  573. hcell.width = IPHONE_WIDTH;
  574. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  575. [view addSubview:hcell];
  576. hcell.lblTitle.text = [self headerTitleForSection:section];
  577. if (![hcell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  578. [self addTargetToHeaderAddButton:hcell.btnAdd section:section];
  579. }
  580. } else if (section == 4) {//conditions
  581. RulesAddConditionHeaderTableViewCell *hcell = (RulesAddConditionHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ConditionHeaderCellIdentifier"];
  582. hcell.width = IPHONE_WIDTH;
  583. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  584. [view addSubview:hcell];
  585. _chkConditions = hcell.chkConditions;
  586. if (!_isNotFirstLoading) {
  587. _chkConditions.checked = _hasCondition;
  588. _isNotFirstLoading = YES;
  589. }
  590. if (![_chkConditions actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  591. [_chkConditions addTarget:self action:@selector(chkConditionsTouched:) forControlEvents:UIControlEventTouchUpInside];
  592. }
  593. if (![hcell.btnConditions actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  594. [hcell.btnConditions addTarget:self action:@selector(btnConditionsTouched:) forControlEvents:UIControlEventTouchUpInside];
  595. }
  596. }
  597. if (_arrayForHeader.count == section) {
  598. [_arrayForHeader insertObject:view atIndex:section];
  599. }
  600. } else {
  601. if (section == 1 || section == 2) {
  602. RulesAddHeaderTableViewCell *hcell = (RulesAddHeaderTableViewCell *)view.subviews[0];
  603. hcell.lblTitle.text = hcell.lblTitle.text = [self headerTitleForSection:section];
  604. }
  605. else if (section == 4) {
  606. // RulesAddConditionHeaderTableViewCell *hcell = (RulesAddConditionHeaderTableViewCell *)view.subviews[0];
  607. }
  608. }
  609. [self checkHeaderButton:section];
  610. return view;
  611. }
  612. - (void)checkHeaderButton:(NSInteger)section {
  613. if (section == 1) {
  614. _btnTriggerAdd.hidden = self.triggers.count > 0;
  615. }
  616. }
  617. #pragma mark - conditions
  618. //rule controll vc section 4 활성화 비활성화
  619. - (void)chkConditionsTouched:(id)sender {
  620. [_tableView reloadData];
  621. CustomCheckBox *chkCondition = (CustomCheckBox *)sender;
  622. if (chkCondition.checked) {
  623. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:4];
  624. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
  625. }
  626. }
  627. - (void)btnConditionsTouched:(id)sender {
  628. ConditionSelectPopupView *popup = [[ConditionSelectPopupView alloc] initFromNib];
  629. [popup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  630. NSLog(@"buttonIndex : %ld", buttonIndex );
  631. NSLog(@"_rcv : %@", _rcv);
  632. switch (buttonIndex) {
  633. case 10:
  634. [_rcv btnAddModeTouched:nil];
  635. break;
  636. case 11:
  637. [_rcv btnAddDurationTouched:nil];
  638. break;
  639. case 12:
  640. [_rcv btnAddDeviceTouched:nil];
  641. break;
  642. }
  643. }];
  644. };
  645. //- (void)btnAddModeTouched:(id)sender {
  646. //
  647. // ItemModel *mode = (ItemModel *)[_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeHomeMode];
  648. // BOOL isNewTrigger = mode == nil;
  649. // mode = isNewTrigger ? [[ItemModel alloc] init] : mode;
  650. //
  651. // if (!_mpopup) {
  652. // _mpopup = [[RulesConditionHomeModePopupView alloc] initFromNib];
  653. // }
  654. //
  655. // _mpopup.condition = mode;
  656. // [_mpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  657. // if (buttonIndex == 0) {//OK
  658. // if (isNewTrigger) {
  659. // [_conditions addObject:mode];
  660. // }
  661. //
  662. // [self.tableView reloadData];
  663. // }
  664. // }];
  665. //}
  666. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  667. if (section == 0 || section == 3) {
  668. return 0.01f;
  669. }
  670. else if (section == 4) {
  671. return 56.0f;
  672. }
  673. return 56.0f;
  674. }
  675. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  676. UIView *view = _arrayForFooter.count > section ? _arrayForFooter[section] : nil;
  677. if (!view) {
  678. RulesAddFooterTableViewCell *hcell = (RulesAddFooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FooterCellIdentifier"];
  679. hcell.width = IPHONE_WIDTH;
  680. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  681. [view addSubview:hcell];
  682. if (_arrayForHeader.count == section) {
  683. [_arrayForHeader insertObject:view atIndex:section];
  684. }
  685. }
  686. return view;
  687. }
  688. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  689. // if (section == 0) {
  690. // return 0.01f;
  691. // }
  692. return 0.01f;
  693. }
  694. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  695. CGFloat height = 0.0f;
  696. NSInteger section = indexPath.section;
  697. if (section == 0) {//title
  698. height = 122;
  699. } else if (section == 1) {//triggers
  700. height = 80;
  701. } else if (section == 2) {//actions
  702. height = 80;
  703. } else if (section == 3) {//push-message
  704. // height = UITableViewAutomaticDimension;
  705. height = 122 + textCellChangeHeight;
  706. } else if (section == 4) {//conditions
  707. if (!_rcv) {
  708. _rcv = [CommonUtil instantiateViewControllerWithIdentifier:@"RulesConditionViewController" storyboardName:@"Rules"];
  709. [self addChildViewController:_rcv];
  710. [_rcv didMoveToParentViewController:self];
  711. }
  712. height = _rcv.tableHeight;
  713. }
  714. return height;
  715. }
  716. + (BOOL)hasDeleteNode:(ItemModel *)deviceItem {
  717. NSArray *matchedArray = [deviceItem.subItems filteredArrayUsingPredicateFormat:@"deleteYn == %@", ksYES];
  718. return matchedArray.count;
  719. }
  720. - (void)configureTriggerCell:(RulesAddTableViewCell *)cell item:(ItemModel *)item {
  721. ItemSubModel *subItem = nil;
  722. NSString *deviceImageFileName = nil;
  723. NSLog(@"configureTriggerCell");
  724. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//장치일 경우
  725. subItem = [item.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  726. cell.lblItem.text = subItem.sourceName;
  727. deviceImageFileName = subItem.imageFileName;
  728. if ([subItem.deleteYn boolValue]) {
  729. [cell.lblItem setStrikethrough:cell.lblItem.text];
  730. }
  731. cell.lblSubItem.text = subItem.sourceSubName;
  732. //노드의 액션 값을 세팅함.
  733. if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
  734. CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  735. cell.lblCondition.text = [DeviceModel contentValueMsgByCmdClsCode:subItem.cmdclsCode cmdclsTypeId:(NSString *)subItem.cmdclsTypeId contentValue:cmdclsValue.cmdclsValue];
  736. } else {
  737. if (subItem.cmdclsValueMsg) {
  738. cell.lblCondition.text = subItem.cmdclsValueMsg;
  739. } else {
  740. NSString *condition = [subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하";
  741. cell.lblCondition.text = [NSString stringWithFormat:@"%@%@ %@", subItem.cmdclsValue, subItem.unit, condition];
  742. }
  743. }
  744. //[cell.lblCondition setUnderLine:cell.lblCondition.text];
  745. cell.lblCondition.hidden = NO;
  746. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {
  747. subItem = item.timers && item.timers.count ? item.timers[0] : nil;
  748. if (subItem) {
  749. NSLog(@"subItem : %@", subItem);
  750. NSInteger phour = [subItem.hour integerValue];
  751. NSString *period = phour < 12 ? @"AM" : @"PM";
  752. NSString *title = [NSString stringWithFormat:@"%@ %zd:%@", period, phour, subItem.minute];
  753. // NSDictionary *titleColor = @{NSForegroundColorAttributeName : kUITextColor02};
  754. cell.lblItem.text = title;
  755. cell.lblSubItem.text = [self daysOfWeekAtSubItem];
  756. cell.lblCondition.hidden = YES;
  757. }
  758. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {
  759. subItem = item.daylights && item.daylights.count ? item.daylights[0] : nil;
  760. if (subItem) {
  761. NSString *title = [subItem.sourceSubId isEqualToString:@"sunriseUtcTime"] ? @"해뜰때" : @"해질때";
  762. cell.lblItem.text = title;
  763. cell.lblSubItem.text = [NSString stringWithFormat:@"%@ / %@", subItem.sourceName, [self daysOfWeekAtSubItem]];
  764. cell.lblCondition.hidden = YES;
  765. }
  766. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {
  767. subItem = item.heats && item.heats.count ? item.heats[0] : nil;
  768. if (subItem) {
  769. NSString *title = [subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"더울때" : @"추울때";
  770. cell.lblItem.text = title;
  771. cell.lblSubItem.text = [NSString stringWithFormat:@"%@ / %@℃ / %@", subItem.sourceName, subItem.cmdclsValue, [self daysOfWeekAtSubItem]];
  772. cell.lblCondition.hidden = YES;
  773. }
  774. }
  775. // if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  776. // if (item.imageFileName) {
  777. // [cell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:item.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  778. // } else {
  779. // [cell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:deviceImageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  780. // }
  781. // } else {
  782. cell.imgvIcon.image = [self imageForTrigger:item];
  783. // }
  784. }
  785. - (NSString *)daysOfWeekAtSubItem {
  786. NSString *daysOfWeek = ksEmptyString;
  787. ItemModel *daysCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeDaysOfWeek];
  788. if (daysCondition) {
  789. ItemSubModel *subCondition = daysCondition.subItems[0];
  790. daysOfWeek = subCondition.daysOfWeek;
  791. }
  792. return daysOfWeek;
  793. }
  794. - (UIImage *)imageForTrigger:(ItemModel *)item {
  795. UIImage *image = nil;
  796. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
  797. image = [UIImage imageNamed:@"img_rule_icon_time"];
  798. }
  799. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 경우,
  800. image = [UIImage imageNamed:@"img_rule_icon_sunrises"];
  801. }
  802. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//더울때/추울때 경우,
  803. image = [UIImage imageNamed:@"img_rule_icon_Temperature"];
  804. }
  805. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  806. image = [UIImage imageNamed:@"img_rule_icon_device_plug"];
  807. }
  808. return image;
  809. }
  810. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  811. NSInteger section = indexPath.section;
  812. UITableViewCell *cell = nil;
  813. if (section == 0) {//title
  814. RulesAddTitleTableViewCell *tcell = (RulesAddTitleTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  815. tcell.txtRuleTitle.text = [tcell.txtRuleTitle.text isEmptyString] && _refRuleDetail ? _refRuleDetail.ruleName : tcell.txtRuleTitle.text;
  816. if (!_txtRuleTitle) {
  817. _txtRuleTitle = tcell.txtRuleTitle;
  818. }
  819. [self insertActionAddButton:tcell.btnAdd];
  820. cell = tcell;
  821. } else if (section == 1) {//triggers
  822. ItemModel *item = _triggers[indexPath.row];
  823. RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
  824. [self configureTriggerCell:tcell item:item];
  825. tcell.lblItem.textColor = kUITextColor04;
  826. tcell.btnDelete.value = item;
  827. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  828. [tcell.btnDelete addTarget:self action:@selector(btnDeleteTriggerTouched:) forControlEvents:UIControlEventTouchUpInside];
  829. }
  830. cell = tcell;
  831. } else if (section == 2) {//actions
  832. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  833. ItemSubModel *subItem = deviceItem.subItems[indexPath.row];
  834. RulesAddTableViewCell *tcell = (RulesAddTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
  835. //title
  836. BOOL multiHub = [JDFacade facade].loginUser.isMultiHomeHub;
  837. NSArray *ids = [subItem.sourceId componentsSeparatedByString:@"-"];
  838. DeviceModel *hub = nil;
  839. if (ids.count > 1) hub = [[JDFacade facade].loginUser getHomeHub:[ids objectAtIndex:0]];
  840. NSString *name = hub == nil ? @"전체" : (hub.prdUserName != nil ? hub.prdUserName : hub.prdName);
  841. NSString *hubName = multiHub ? [self convMaxLengthName:name len:4] : @"";
  842. NSString *deviceName = [self convMaxLengthName:subItem.sourceName len:6];
  843. tcell.lblItem.text = multiHub ? [NSString stringWithFormat:@"[%@]%@", hubName,deviceName] : deviceName ;
  844. if ([subItem.deleteYn boolValue]) {
  845. [tcell.lblItem setStrikethrough:tcell.lblItem.text];
  846. }
  847. tcell.lblSubItem.text = subItem.sourceSubName;
  848. // tcell.lblItem.textColor = kUITextColor01;
  849. NSLog(@"subItem.imageFileName : %@", subItem.imageFileName);
  850. [tcell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:subItem.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  851. //노드의 액션 값을 세팅함.
  852. if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
  853. CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  854. tcell.lblCondition.text = cmdclsValue.cmdclsValueMsg;
  855. }
  856. else {
  857. if (subItem.cmdclsValueMsg) {
  858. tcell.lblCondition.text = subItem.cmdclsValueMsg;
  859. }
  860. else {
  861. NSString *condition = [subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하";
  862. tcell.lblCondition.text = [NSString stringWithFormat:@"%@%@ %@", subItem.cmdclsValue, subItem.unit, condition];
  863. }
  864. }
  865. tcell.btnDelete.value = subItem;
  866. if (![tcell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  867. [tcell.btnDelete addTarget:self action:@selector(btnDeleteDeviceActionTouched:) forControlEvents:UIControlEventTouchUpInside];
  868. }
  869. cell = tcell;
  870. } else if (section == 3) {//push-message
  871. ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
  872. ItemSubModel *subItem = pushItem.pushes[indexPath.row];
  873. RulesAddTextTableViewCell *tcell = (RulesAddTextTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"TextCellIdentifier"];
  874. //높이변경
  875. [tcell registHeightChangeBlock:^(CGFloat height) {
  876. textCellChangeHeight += height;
  877. [self tableView:_tableView reloadRowWhileShowingKeyboard:indexPath];
  878. }];
  879. if (!_txvMessage) {
  880. _txvMessage = tcell.txtView;
  881. }
  882. _txvMessage.text = [_txvMessage.text isEqualToString:_txvMessage.placeHolder] && _refRuleDetail ? subItem.cmdclsValue : _txvMessage.text;
  883. [self insertActionAddButton:tcell.btnAdd];
  884. cell = tcell;
  885. } else if (section == 4) {//conditions
  886. RulesAddConditionTableViewCell *tcell = (RulesAddConditionTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"ConditionCellIdentifier"];
  887. // if (_chkConditions.checked) {
  888. if (!tcell.container.subviews.count) {
  889. if ([self isViewLoaded]) {
  890. [_rcv beginAppearanceTransition:YES animated:NO];
  891. [tcell.container addSubview:_rcv.view];
  892. [_rcv endAppearanceTransition];
  893. }
  894. }
  895. // NSLog(@"section == 4");
  896. // NSLog(@"_conditionDevices : %@", _conditionDevices);
  897. _rcv.conditions = _conditions;
  898. _rcv.refDevices = _conditionDevices;
  899. tcell.constraintContainerHeight.constant = _rcv.tableHeight;
  900. // }
  901. cell = tcell;
  902. }
  903. return cell;
  904. }
  905. //키보드 유지 셀 높이 변경
  906. - (void)tableView:(UITableView *)tableView reloadRowWhileShowingKeyboard:(NSIndexPath *)indexPath {
  907. [tableView beginUpdates];
  908. [tableView endUpdates];
  909. [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
  910. }
  911. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  912. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  913. if (indexPath.section == 1) {//triggers
  914. ItemModel *item = _triggers[0];
  915. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//디바이스일 경우,
  916. ItemSubModel *subItem = item.subItems[indexPath.row];
  917. [RulesAddViewController modifyTrigger:item subItem:subItem refDevices:_triggerDevices tableView:_tableView];
  918. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
  919. [self modifyTriggerOfTimer:item];
  920. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 경우,
  921. [self modifyTriggerOfDaylight:item];
  922. } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//더울때/추울때 경우,
  923. [self modifyTriggerOfHeat:item];
  924. }
  925. } else if (indexPath.section == 2) {//actions
  926. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  927. if (deviceItem) {
  928. ItemSubModel *subItem = deviceItem.subItems[indexPath.row];
  929. [RulesAddViewController modifyAction:subItem refDevices:_actionDevices tableView:_tableView];
  930. }
  931. }
  932. }
  933. #pragma mark
  934. - (void)modifyTriggerOfTimer:(ItemModel *)item {
  935. TimePickerPopupView *tpopup = [[TimePickerPopupView alloc] initFromNib];
  936. tpopup.timeTrigger = item;
  937. tpopup.refConditions = _conditions;
  938. [tpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  939. if (buttonIndex == 0) {//OK
  940. [_tableView reloadData];
  941. }
  942. }];
  943. }
  944. - (void)modifyTriggerOfDaylight:(ItemModel *)item {
  945. DaylightPopupView *dpopup = [[DaylightPopupView alloc] initFromNib];
  946. dpopup.daylightTrigger = item;
  947. dpopup.refConditions = _conditions;
  948. [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  949. if (buttonIndex == 0) {//OK
  950. [_tableView reloadData];
  951. }
  952. }];
  953. }
  954. - (void)modifyTriggerOfHeat:(ItemModel *)item {
  955. ExternHeatPopupView *epopup = [[ExternHeatPopupView alloc] initFromNib];
  956. epopup.externHeatTrigger = item;
  957. epopup.refConditions = _conditions;
  958. [epopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  959. if (buttonIndex == 0) {//OK
  960. [_tableView reloadData];
  961. }
  962. }];
  963. }
  964. + (void)modifyTrigger:(ItemModel *)trigger subItem:(ItemSubModel *)subItem refDevices:(NSMutableArray<ItemModel> *)refDevices tableView:(CustomTableView *)tableView {
  965. if ([subItem.deleteYn boolValue]) {
  966. [[JDFacade facade] toast:@"삭제된 장치입니다\r목록에서 제거하세요"];
  967. return;
  968. }
  969. ItemModel *deviceItem = [refDevices objectKey:@"sourceId" eqaulToString:subItem.sourceId];
  970. NSLog(@"deviceItem : %@", deviceItem);
  971. //노드 선택 팝럽.
  972. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  973. npopup.refDevice = deviceItem;
  974. npopup.typeCode = ksItemTypeCodeTrigger;
  975. npopup.isModifyMode = YES;
  976. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  977. if (buttonIndex == 0) {//OK
  978. ItemSubModel *fsubItem = [deviceItem.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  979. if (fsubItem != nil) {
  980. [trigger.subItems removeAllObjects];
  981. [trigger.subItems addObject:fsubItem];
  982. }
  983. // subItem.cmdclsCode = matchedSubItem.cmdclsCode;
  984. // subItem.cmdclsTypeId = matchedSubItem.cmdclsTypeId;
  985. //
  986. // if (matchedSubItem.cmdclsValueList) {
  987. // subItem.cmdclsValueList = matchedSubItem.cmdclsValueList;
  988. //
  989. // CmdClsValueModel *pCmdClsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  990. // subItem.cmdclsValue = pCmdClsValue.cmdclsValue; //CommandClassControlNodeView - 에서 선택됨.
  991. // }
  992. if (tableView) {
  993. [tableView reloadData];
  994. }
  995. }
  996. }];
  997. }
  998. + (void)modifyAction:(ItemSubModel *)subItem refDevices:(NSMutableArray<ItemModel> *)refDevices tableView:(CustomTableView *)tableView {
  999. if ([subItem.deleteYn boolValue]) {
  1000. [[JDFacade facade] toast:@"삭제된 장치입니다\r목록에서 제거하세요"];
  1001. return;
  1002. }
  1003. ItemModel *item = [refDevices objectKey:@"sourceId" eqaulToString:subItem.sourceId];
  1004. ItemSubModel *matchedSubItem = [item.subItems objectKey:@"sourceSubId" eqaulToString:subItem.sourceSubId];
  1005. //노드 선택 팝럽.
  1006. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  1007. npopup.refDevice = item;
  1008. npopup.typeCode = ksItemTypeCodeTrigger;
  1009. npopup.isModifyMode = YES;
  1010. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1011. if (buttonIndex == 0) {//OK
  1012. subItem.cmdclsCode = matchedSubItem.cmdclsCode;
  1013. subItem.cmdclsTypeId = matchedSubItem.cmdclsTypeId;
  1014. subItem.cmdclsValueList = matchedSubItem.cmdclsValueList;
  1015. CmdClsValueModel *pCmdClsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  1016. subItem.cmdclsValue = pCmdClsValue.cmdclsValue; //CommandClassControlNodeView - 에서 선택됨.
  1017. if (tableView) {
  1018. [tableView reloadData];
  1019. }
  1020. }
  1021. }];
  1022. }
  1023. #pragma mark - TableView UI Events
  1024. - (void)btnAddTriggerTouched:(id)sender {
  1025. TriggerSelectPopupView *tpopup = [[TriggerSelectPopupView alloc] initFromNib];
  1026. tpopup.refTriggers = _triggers;
  1027. tpopup.refDevices = _triggerDevices;
  1028. tpopup.refConditions = _conditions;
  1029. [tpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1030. if (buttonIndex == 0) {
  1031. [_tableView reloadData];
  1032. }
  1033. }];
  1034. }
  1035. //동작할 장치 추가
  1036. - (void)btnAddActionTouched:(id)sender {
  1037. DeviceSelectPopupView *dpopup = [[DeviceSelectPopupView alloc] initFromNib];
  1038. dpopup.refDevices = _actionDevices;
  1039. dpopup.typeCode = ksItemTypeCodeTrigger;
  1040. [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1041. if (buttonIndex == 0) {//ok
  1042. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  1043. npopup.refDevice = dpopup.selectedDevices[0];
  1044. npopup.typeCode = ksItemTypeCodeTrigger;
  1045. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1046. //action add
  1047. if (buttonIndex == 0) {//OK
  1048. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  1049. if (!deviceItem) {//디바이스 아이템이 없을 경우,
  1050. deviceItem = [[ItemModel alloc] init];
  1051. deviceItem.itemName = @"이 장치를 실행";
  1052. deviceItem.itemSubTypeCode = ksItemSubTypeCodeDevice;
  1053. deviceItem.subItems = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
  1054. [_actions addObject:deviceItem];
  1055. }
  1056. ItemSubModel *snode = npopup.selectedNode;
  1057. //존재 여부 확인
  1058. if (![deviceItem.subItems objectByUsingPredicateFormat:@"sourceId == %@ && sourceSubId == %@", snode.sourceId, snode.sourceSubId]) {
  1059. [deviceItem.subItems addObject: snode];
  1060. }
  1061. [_tableView reloadData];
  1062. }
  1063. }];
  1064. }
  1065. }];
  1066. }
  1067. - (void)btnAddPushMessageTouched:(id)sender {
  1068. ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
  1069. if (!pushItem) {//디바이스 아이템이 없을 경우,
  1070. pushItem = [[ItemModel alloc] init];
  1071. pushItem.itemName = [JDFacade facade].loginUser.memberId;
  1072. pushItem.itemSubTypeCode = ksItemSubTypeCodeAppPush;
  1073. pushItem.pushes = [(NSMutableArray<ItemSubModel> *)[NSMutableArray alloc] init];
  1074. ItemSubModel *push = [[ItemSubModel alloc] init];
  1075. push.conditionTypeCode = @"02";
  1076. [pushItem.pushes addObject:push];
  1077. [_actions addObject:pushItem];
  1078. }
  1079. [_tableView reloadData];
  1080. }
  1081. - (void)btnDeleteTriggerTouched:(id)sender {
  1082. NSLog(@"btnDeleteTriggerTouched");
  1083. CustomButton *btnDelete = (CustomButton *)sender;
  1084. ItemModel *item = btnDelete.value;
  1085. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//장치일 경우
  1086. ItemSubModel *subItem = [item.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  1087. if (subItem) {
  1088. [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
  1089. for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
  1090. [[JDFacade facade] setRadioButtonStatus:@NO object:cmdclsValue];
  1091. cmdclsValue.isSelected = NO;
  1092. }
  1093. }
  1094. }
  1095. if (_triggers && _triggers.count) {
  1096. [_triggers removeAllObjects];
  1097. }
  1098. [_tableView reloadData];
  1099. }
  1100. - (void)btnDeleteDeviceActionTouched:(id)sender {
  1101. CustomButton *btnDelete = (CustomButton *)sender;
  1102. ItemSubModel *subItem = (ItemSubModel *)btnDelete.value;
  1103. //선택 설정을 초기화
  1104. [[JDFacade facade] setRadioButtonStatus:@NO object:subItem];
  1105. for (CmdClsValueModel *cmdclsValue in subItem.cmdclsValueList) {
  1106. [[JDFacade facade] setRadioButtonStatus:@NO object:cmdclsValue];
  1107. cmdclsValue.isSelected = NO;
  1108. }
  1109. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  1110. [deviceItem.subItems removeObject:subItem];
  1111. [_tableView reloadData];
  1112. }
  1113. - (void)btnDeletePushActionTouched:(id)sender {
  1114. ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
  1115. [_actions removeObject:pushItem];
  1116. [_tableView reloadData];
  1117. }
  1118. - (void)btnAddTouched:(id)sender {
  1119. UIButton *btn = sender;
  1120. //1001:규칙add, 2001:알림add
  1121. UIControl *control;
  1122. if ([btn tag] > 2000) {
  1123. control = [self.view viewWithTag:2000];
  1124. }
  1125. else {
  1126. control = [self.view viewWithTag:1000];
  1127. }
  1128. [control becomeFirstResponder];
  1129. }
  1130. //- (void)btnInputConfirmTouched:(id)sender {
  1131. //
  1132. // UIButton *btn = sender;
  1133. //
  1134. // //1002:규칙add, 2002:알림add
  1135. // if ([btn tag] > 2000) {
  1136. //
  1137. //
  1138. // }
  1139. // else {
  1140. //
  1141. // }
  1142. //
  1143. //}
  1144. - (void)insertActionAddButton:(UIButton*)button {
  1145. if (![button actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  1146. [button addTarget:self action:@selector(btnAddTouched:) forControlEvents:UIControlEventTouchUpInside];
  1147. }
  1148. }
  1149. //- (void)insertActionInputConfirmButton:(UIButton*)button {
  1150. //
  1151. // if (![button actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  1152. // [button addTarget:self action:@selector(btnInputConfirmTouched:) forControlEvents:UIControlEventTouchUpInside];
  1153. // }
  1154. //}
  1155. - (IBAction)btnCancelTouched:(id)sender {
  1156. [self dismissViewControllerAnimated:YES completion:nil];
  1157. }
  1158. #pragma mark - UI Events
  1159. - (IBAction)btnConfirmTouched:(id)sender {
  1160. [self.view endEditing:YES];
  1161. //1.Validate Title
  1162. RulesAddTitleTableViewCell *tcell = (RulesAddTitleTableViewCell *)[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; //타이틀 섹션
  1163. if (tcell && ![ValidateUtil validateTextfiled:tcell.txtRuleTitle type:ValidateTypeNull title:NSLocalizedString(@"규칙 이름", @"규칙 이름")]) {
  1164. return;
  1165. }
  1166. if (tcell.txtRuleTitle.text.length > 40) {
  1167. [[JDFacade facade] alert:@"규칙이름은 최대 40자까지 입력할 수 있습니다"];
  1168. return;
  1169. }
  1170. ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
  1171. if (pushItem) {
  1172. if (_txvMessage && [_txvMessage.text isEmptyString]) {//푸시메시지 여부 확인
  1173. [_actions removeObject:pushItem];
  1174. }
  1175. else if (_txvMessage) {
  1176. ItemSubModel *subItem = pushItem.pushes[0];
  1177. subItem.cmdclsValue = _txvMessage.text;
  1178. if (_txvMessage.text.length > 120) {
  1179. [[JDFacade facade] alert:@"알림메시지는 최대 120자까지 입력할 수 있습니다"];
  1180. return;
  1181. }
  1182. }
  1183. }
  1184. if (!_refRuleDetail) {//생성
  1185. [self requestRegisterRule];
  1186. }
  1187. else {//수정
  1188. BOOL hasDeleteNode = NO;
  1189. ItemModel *triggerDevice = [_triggers objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  1190. ItemModel *actionDevice = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  1191. ItemModel *conditionDevice = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  1192. hasDeleteNode = [RulesAddViewController hasDeleteNode:triggerDevice];
  1193. hasDeleteNode = [RulesAddViewController hasDeleteNode:actionDevice] ? YES : hasDeleteNode;
  1194. hasDeleteNode = [RulesAddViewController hasDeleteNode:conditionDevice] ? YES : hasDeleteNode;
  1195. if (hasDeleteNode) {
  1196. [[JDFacade facade] confirm:@"삭제된 장치를 제외 후 저장합니다" completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  1197. if (buttonIndex == 0) {//OK
  1198. [self getRidOfDeleteDevices:triggerDevice];
  1199. [self getRidOfDeleteDevices:actionDevice];
  1200. [self getRidOfDeleteDevices:conditionDevice];
  1201. [_tableView reloadData];
  1202. [self requestModifyRule];
  1203. }
  1204. }];
  1205. }
  1206. else {
  1207. [self requestModifyRule];
  1208. }
  1209. }
  1210. }
  1211. //삭제된 장치를 제거함.
  1212. - (void)getRidOfDeleteDevices:(ItemModel *)deviceItem {
  1213. [deviceItem.subItems enumerateObjectsUsingBlock:^(ItemSubModel *subItem, NSUInteger idx, BOOL * _Nonnull stop) {
  1214. if (subItem.deleteYn) {
  1215. if ([_triggers containsObject:deviceItem]) {
  1216. [_triggers removeObject:deviceItem];
  1217. return;
  1218. }
  1219. [deviceItem.subItems removeObject:subItem];
  1220. }
  1221. }];
  1222. }
  1223. #pragma mark - MemoryWarning
  1224. - (void)didReceiveMemoryWarning
  1225. {
  1226. [super didReceiveMemoryWarning];
  1227. // Dispose of any resources that can be recreated.
  1228. }
  1229. @end