RulesAddViewController.m 71 KB

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