RulesDetailViewController.m 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. //
  2. // RulesDetailViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/30/15.
  6. // Copyright © 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "RuleModel.h"
  11. #import "CustomTableView.h"
  12. #import "CustomLabel.h"
  13. #import "CustomButton.h"
  14. #import "CustomImageView.h"
  15. #import "UIImageView+WebCache.h"
  16. #import "RulesDetailViewController.h"
  17. #import "RulesConditionViewController.h"
  18. #import "DeviceModel.h"
  19. #import "RulesAddViewController.h"
  20. @implementation RulesDetailTitleTableViewCell
  21. @end
  22. @implementation RulesDetailHeaderTableViewCell
  23. @end
  24. @implementation RulesDetailTableViewCell
  25. @end
  26. @implementation RulesDetailPushTableViewCell
  27. @end
  28. @implementation RulesDetailConditionHeaderTableViewCell
  29. @end
  30. @implementation RulesDetailConditionTableViewCell
  31. @end
  32. @implementation RulesDetailExecutionTableViewCell
  33. @end
  34. @implementation RulesDetailFooterTableViewCell
  35. @end
  36. @interface RulesDetailViewController () {
  37. RuleDetailModel *_ruleDetail;
  38. NSMutableArray<ItemModel> *_triggers, *_actions, *_conditions;
  39. NSMutableArray *_arrayForHeader, *_arrayForFooter;
  40. RulesConditionViewController *_rcv;
  41. NSInteger _sectionCount;
  42. BOOL _hasDeviceAction, _hasPushAction, _hasCondition; //요일 조건 외의 조건
  43. CustomButton *_btnUseRule;
  44. UIImage *_btnImageRun, *_btnImageStop;
  45. }
  46. @end
  47. #pragma mark - Class Definition
  48. @implementation RulesDetailViewController
  49. - (void)viewDidLoad {
  50. [super viewDidLoad];
  51. // Do any additional setup after loading the view.
  52. [self initProperties];
  53. [self initUI];
  54. }
  55. - (void)viewWillAppear:(BOOL)animated {
  56. [super viewWillAppear:animated];
  57. [self requestRuleDetail];
  58. }
  59. - (void)initProperties {
  60. _arrayForHeader = [[NSMutableArray alloc] init];
  61. _arrayForFooter = [[NSMutableArray alloc] init];
  62. }
  63. - (void)initUI {
  64. if (![JDFacade facade].loginUser.isHomehubOnline) {
  65. _lblTitle.text = @"홈허브 오프라인";
  66. [_lblTitle setColor:kUITextColor02 text:_lblTitle.text];
  67. }
  68. [self initTableViewAsDefaultStyle:_tableView];
  69. [self setMoreBtnArray];
  70. _btnImageRun = [UIImage imageNamed:@"img_rule_list_playbtn_active"];
  71. _btnImageStop = [UIImage imageNamed:@"img_rule_list_playbtn_disable"];
  72. }
  73. //- (void)setThingsPopoverOptions {
  74. //
  75. // //set Popover Contents
  76. // __weak typeof(self) weakSelf = self;
  77. // _popooverOptionArray = [[NSMutableArray alloc] init];
  78. //
  79. // [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로고침",nil),
  80. // @"iconName": @"img_bg_morepopup_icon_refresh",
  81. // @"target": weakSelf,
  82. // @"selector": [NSValue valueWithPointer:@selector(requestRuleDetail)]}];
  83. //
  84. // if ([JDFacade facade].loginUser.level > 10) {//파워유저 이상일 경우,
  85. //
  86. // [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"편집", @"편집"),
  87. // @"iconName": @"img_bg_morepopup_icon_edit",
  88. // @"target": weakSelf,
  89. // @"selector": [NSValue valueWithPointer:@selector(editRule)]}];
  90. //
  91. // [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"삭제", @"삭제"),
  92. // @"iconName": @"img_bg_morepopup_icon_del",
  93. // @"target": weakSelf,
  94. // @"selector": [NSValue valueWithPointer:@selector(deleteRule)]}];
  95. // }
  96. //}
  97. - (void)setMoreBtnArray {
  98. _moreBtnArray = [NSMutableArray array];
  99. // if (![[JDFacade facade].loginUser hasHomeHub]) {
  100. // [_btnOption setHidden:YES];
  101. // }
  102. if([[JDFacade facade].loginUser.gradeCode isEqualToString:KNEET_MEMBER_MASTER]) {
  103. for (int i = 0; i < 3; i++) {
  104. MoreBtnModel *btnModel;
  105. switch (i) {
  106. case 0:
  107. btnModel = [[MoreBtnModel alloc] initWithTyep:NameChange isEnable:YES];
  108. break;
  109. case 1:
  110. btnModel = [[MoreBtnModel alloc] initWithTyep:Del isEnable:YES];
  111. break;
  112. case 2:
  113. btnModel = [[MoreBtnModel alloc] initWithTyep:Refresh isEnable:YES];
  114. break;
  115. default:
  116. break;
  117. }
  118. [_moreBtnArray addObject:btnModel];
  119. }
  120. }
  121. else {
  122. MoreBtnModel *btnModel = [[MoreBtnModel alloc] initWithTyep:Refresh isEnable:YES];
  123. [_moreBtnArray addObject:btnModel];
  124. }
  125. }
  126. - (void)setDeviceValue:(NSMutableArray<ItemModel> *)items {
  127. NSArray *matchedArray = [_triggers filteredArrayUsingPredicateFormat:@"itemTypeCode == %@", ksItemSubTypeCodeDevice];
  128. for (ItemModel *item in matchedArray) {//device items
  129. for (ItemSubModel *subItem in item.subItems) {
  130. [[JDFacade facade] setRadioButtonStatus:@YES object:subItem];
  131. [[JDFacade facade] setCheckBoxStatus:@YES object:subItem];
  132. }
  133. }
  134. }
  135. - (void)setContents {
  136. _triggers = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_ruleDetail.triggers];
  137. _actions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_ruleDetail.actions];
  138. _conditions = (NSMutableArray<ItemModel> *)[[NSMutableArray alloc] initWithArray:_ruleDetail.conditions];
  139. [self setDeviceValue:_triggers];
  140. [self setDeviceValue:_actions];
  141. [self setDeviceValue:_conditions];
  142. //check elements
  143. _hasDeviceAction = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice] != nil;
  144. _hasPushAction = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush] != nil;
  145. _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeHomeMode] != nil;
  146. _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeDueDate] ? YES : _hasCondition;
  147. _hasCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice] ? YES : _hasCondition;
  148. [_tableView reloadData];
  149. }
  150. #pragma mark - UITableView DataSource & Delegate
  151. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  152. return 6;
  153. }
  154. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  155. NSInteger count = 0;
  156. if (section == 0 || section == 5) {//title
  157. count = 1;
  158. } else if (section == 1) {//triggers
  159. count = _triggers && _triggers.count ? _triggers.count : 0;
  160. } else if (section == 2) {//actions
  161. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  162. count = deviceItem.subItems && deviceItem.subItems.count ? deviceItem.subItems.count : 0;
  163. } else if (section == 3) {
  164. count = _hasPushAction;
  165. } else if (section == 4) {
  166. count = _hasCondition;
  167. }
  168. return count;
  169. }
  170. - (NSString *)headerTitleForSection:(NSInteger)section {
  171. NSString *title = nil;
  172. if (section == 1) {//trigger
  173. // title = @"이 때가 되면";
  174. // if (_triggers && _triggers.count) {
  175. // ItemModel *item = _triggers[0];
  176. //
  177. // if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  178. // title = @"장치 상태가 바뀔 때";
  179. // } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {
  180. // title = @"이 시간마다";
  181. // } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {
  182. // title = @"더울때 / 추울때";
  183. // } else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {
  184. // title = @"해뜰때 / 질때";
  185. // }
  186. // }
  187. title = @"실행 조건";
  188. } else if (section == 2) {//action
  189. title = @"동작할 장치";//@"이 장치를 실행";
  190. } else if (section == 3) {//pushes
  191. title = @"알림 메시지";
  192. }
  193. else if (section == 4) {
  194. title = @"전제조건(선택)";
  195. }
  196. return title;
  197. }
  198. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  199. UIView *view = _arrayForHeader.count > section ? _arrayForHeader[section] : nil;
  200. if (!view) {
  201. if (section == 0 || section == 5) {//title
  202. view = [[UIView alloc] init];
  203. }
  204. else if (section == 1 || section == 2 || section == 3 || section == 4) {
  205. RulesDetailHeaderTableViewCell *hcell = (RulesDetailHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
  206. hcell.width = IPHONE_WIDTH;
  207. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  208. [view addSubview:hcell];
  209. hcell.lblTitle.text = [self headerTitleForSection:section];
  210. }
  211. // else if (section == 4) {//conditions
  212. // RulesDetailConditionHeaderTableViewCell *hcell = (RulesDetailConditionHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ConditionHeaderCellIdentifier"];
  213. // hcell.width = IPHONE_WIDTH;
  214. //
  215. // view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  216. // [view addSubview:hcell];
  217. // }
  218. if (_arrayForHeader.count == section) {
  219. [_arrayForHeader insertObject:view atIndex:section];
  220. }
  221. }
  222. else {
  223. if ((section == 2 && !_hasDeviceAction) || (section == 3 && !_hasPushAction) || (section == 4 && !_hasCondition)) {
  224. view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, CGFLOAT_MIN)];
  225. } else if (section == 1 || section == 2 || section == 3 || section == 4) {
  226. RulesDetailHeaderTableViewCell *hcell = (RulesDetailHeaderTableViewCell *)view.subviews[0];
  227. hcell.lblTitle.text = hcell.lblTitle.text = [self headerTitleForSection:section];
  228. }
  229. }
  230. return view;
  231. }
  232. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  233. CGFloat height = 56.0f;
  234. if (section == 0 || section == 5) {
  235. height = CGFLOAT_MIN;
  236. } else if (section == 2) {
  237. height = _hasDeviceAction ? 56.0f : CGFLOAT_MIN;
  238. } else if (section == 3) {
  239. height = _hasPushAction ? 56.0f : CGFLOAT_MIN;
  240. }
  241. else if (section == 4) {
  242. height = _hasCondition ? 56.0f : CGFLOAT_MIN;
  243. }
  244. return height;
  245. }
  246. //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  247. //
  248. // UIView *view = _arrayForFooter.count > section ? _arrayForFooter[section] : nil;
  249. // if (!view) {
  250. // RulesDetailFooterTableViewCell *hcell = (RulesDetailFooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FooterCellIdentifier"];
  251. // hcell.width = IPHONE_WIDTH;
  252. //
  253. // if (section == 4) {
  254. // hcell.topLine.hidden = YES;
  255. // }
  256. // view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  257. // [view addSubview:hcell];
  258. //
  259. // if (_arrayForHeader.count == section) {
  260. // [_arrayForHeader insertObject:view atIndex:section];
  261. // }
  262. // }
  263. //
  264. // return view;
  265. //}
  266. //- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  267. // CGFloat height = 15.0f;
  268. // if (section == 0 || section == 5) {
  269. // height = CGFLOAT_MIN;
  270. // } else if (section == 2) {
  271. // height = _hasDeviceAction ? 15.0f : CGFLOAT_MIN;
  272. // } else if (section == 3) {
  273. // height = _hasPushAction ? 15.0f : CGFLOAT_MIN;
  274. // } else if (section == 4) {
  275. // height = _hasCondition ? 15.0f : CGFLOAT_MIN;
  276. // }
  277. // return height;
  278. //}
  279. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  280. CGFloat height = 0.0f;
  281. NSInteger section = indexPath.section;
  282. if (section == 0) {//title
  283. height = 95;
  284. } else if (section == 1) {//triggers
  285. height = 80;
  286. } else if (section == 2) {//actions
  287. height = 80;
  288. } else if (section == 3) {//push-message
  289. ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
  290. ItemSubModel *push = pushItem.subItems[0];
  291. CGFloat width = IPHONE_WIDTH - 85;
  292. CustomLabel *lblTemp = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, width, 18)];
  293. lblTemp.font = [UIFont systemFontOfSize:kUIFontSize02];
  294. lblTemp.numberOfLines = 0;
  295. CGFloat adjustHeight = [CommonUtil getSizeFromString:push.cmdclsValue font:lblTemp.font width:width].height;
  296. adjustHeight = adjustHeight < 18 ? 0 : adjustHeight - 18;
  297. height = 80 + adjustHeight;
  298. } else if (section == 4) {//conditions
  299. if (!_rcv) {
  300. _rcv = [CommonUtil instantiateViewControllerWithIdentifier:@"RulesConditionViewController" storyboardName:@"Rules"];
  301. [self addChildViewController:_rcv];
  302. [_rcv didMoveToParentViewController:self];
  303. }
  304. height = _rcv.tableHeight;
  305. } else if (section == 5) {//execution
  306. height = [JDFacade facade].loginUser.level == 90 ? 112.0f: 112.0f - 75.0f; //마스터만 규칙 실행 여부를 설정 가능함.
  307. }
  308. return height;
  309. }
  310. - (void)configureTriggerCell:(RulesDetailTableViewCell *)cell item:(ItemModel *)item {
  311. ItemSubModel *subItem = nil;
  312. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {//장치일 경우
  313. subItem = [item.subItems matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  314. cell.lblItem.text = subItem.sourceName;
  315. if ([subItem.deleteYn boolValue]) {
  316. [cell.lblItem setStrikethrough:cell.lblItem.text];
  317. }
  318. cell.lblSubItem.text = subItem.sourceSubName;
  319. //노드의 액션 값을 세팅함.
  320. if (subItem.cmdclsValueList && subItem.cmdclsValueList.count) {//커맨드클래스 밸루 리스트가 있을 경우,
  321. CmdClsValueModel *cmdclsValue = [subItem.cmdclsValueList matchedObjectName:ksCustomRadioButtonStatus condition:YES];
  322. cell.lblCondition.text = [DeviceModel contentValueMsgByCmdClsCode:subItem.cmdclsCode cmdclsTypeId:(NSString *)subItem.cmdclsTypeId contentValue:cmdclsValue.cmdclsValue];
  323. }
  324. else {
  325. if (subItem.cmdclsValueMsg) {
  326. cell.lblCondition.text = subItem.cmdclsValueMsg;
  327. } else {
  328. NSString *condition = [subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"이상" : @"이하";
  329. cell.lblCondition.text = [NSString stringWithFormat:@"%@%@ %@", subItem.cmdclsValue, subItem.unit, condition];
  330. }
  331. }
  332. cell.lblCondition.hidden = NO;
  333. }
  334. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {
  335. subItem = item.subItems && item.subItems.count ? item.subItems[0] : nil;
  336. if (subItem) {
  337. NSInteger phour = [subItem.hour integerValue];
  338. NSString *period = phour < 12 ? @"AM" : @"PM";//NSLocalizedString(@"오전", @"오전") : NSLocalizedString(@"오후", @"오후");
  339. if ([period isEqualToString:@"PM"]) {
  340. if (phour > 12) {
  341. phour -= 12;
  342. }
  343. }
  344. NSString *title = [NSString stringWithFormat:@"%@ %zd:%@", period, phour, subItem.minute];
  345. // NSDictionary *titleColor = @{NSForegroundColorAttributeName : kUITextColor02};
  346. cell.lblItem.text = title;
  347. cell.lblSubItem.text = subItem.dayofweek;//[self daysOfWeekAtSubItem];
  348. cell.lblCondition.hidden = YES;
  349. }
  350. }
  351. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {
  352. subItem = item.subItems && item.subItems.count ? item.subItems[0] : nil;
  353. if (subItem) {
  354. NSString *title = [subItem.sourceSubId isEqualToString:@"sunriseUtcTime"] ? @"해뜰때" : @"해질때";
  355. cell.lblItem.text = title;
  356. cell.lblSubItem.text = [NSString stringWithFormat:@"%@ / %@", subItem.sourceName, subItem.dayofweek];
  357. cell.lblCondition.hidden = YES;
  358. }
  359. }
  360. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {
  361. subItem = item.subItems && item.subItems.count ? item.subItems[0] : nil;
  362. if (subItem) {
  363. NSString *title = [subItem.conditionTypeCode isEqualToString:ksConditionTypeCodeGreatOrEqual] ? @"더울때" : @"추울때";
  364. cell.lblItem.text = title;
  365. cell.lblSubItem.text = [NSString stringWithFormat:@"%@ / %@℃ / %@", subItem.sourceName, subItem.cmdclsValue, subItem.dayofweek];
  366. cell.lblCondition.hidden = YES;
  367. }
  368. }
  369. NSLog(@"configureTriggerCell\n\n%@", subItem);
  370. // [cell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:subItem.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  371. //수정
  372. cell.imgvIcon.image = [self imageForTrigger:item];
  373. }
  374. - (UIImage *)imageForTrigger:(ItemModel *)item {
  375. UIImage *image = nil;
  376. if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeTimer]) {//타이머일 경우,
  377. image = [UIImage imageNamed:@"img_rule_icon_time"];
  378. }
  379. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDaylight]) {//해뜰때/질때 경우,
  380. image = [UIImage imageNamed:@"img_rule_icon_sunrises"];
  381. }
  382. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeHeat]) {//더울때/추울때 경우,
  383. image = [UIImage imageNamed:@"img_rule_icon_Temperature"];
  384. }
  385. else if ([item.itemSubTypeCode isEqualToString:ksItemSubTypeCodeDevice]) {
  386. image = [UIImage imageNamed:@"img_rule_icon_device_plug"];
  387. }
  388. return image;
  389. }
  390. - (NSString *)daysOfWeekAtSubItem {
  391. NSString *daysOfWeek = ksEmptyString;
  392. ItemModel *daysCondition = [_conditions objectKey:@"itemSubTypeCode" eqaulToString:ksConditionSubTypeCodeDaysOfWeek];
  393. if (daysCondition) {
  394. ItemSubModel *subCondition = daysCondition.subItems[0];
  395. daysOfWeek = subCondition.dayofweek;
  396. }
  397. return daysOfWeek;
  398. }
  399. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  400. NSInteger section = indexPath.section;
  401. UITableViewCell *cell = nil;
  402. if (section == 0) {//title
  403. RulesDetailTitleTableViewCell *tcell = (RulesDetailTitleTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  404. tcell.lblRuleTitle.text = _ruleDetail.ruleName;
  405. cell = tcell;
  406. }
  407. else if (section == 1) {//triggers
  408. ItemModel *item = _triggers[indexPath.row];
  409. RulesDetailTableViewCell *tcell = (RulesDetailTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
  410. [self configureTriggerCell:tcell item:item];
  411. tcell.lblItem.textColor = kUITextColor02;
  412. cell = tcell;
  413. }
  414. else if (section == 2) {//actions - 장치일 경우,
  415. ItemModel *deviceItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeDevice];
  416. ItemSubModel *subItem = deviceItem.subItems[indexPath.row];
  417. RulesDetailTableViewCell *tcell = (RulesDetailTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"RulesCellIdentifier"];
  418. tcell.lblItem.text = subItem.sourceName;
  419. tcell.lblItem.textColor = kUITextColor01;
  420. if ([subItem.deleteYn boolValue]) {
  421. [tcell.lblItem setStrikethrough:tcell.lblItem.text];
  422. }
  423. tcell.lblSubItem.text = subItem.sourceSubName;
  424. tcell.lblCondition.text = subItem.cmdclsValueMsg; //노드의 액션 값을 세팅함.
  425. // [tcell.imgvIcon sd_setImageWithURL:[NSURL URLWithString:subItem.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  426. tcell.imgvIcon.image = [UIImage imageNamed:[[JDFacade facade] deviceIconFromSourceSubId:subItem.sourceSubId]];
  427. cell = tcell;
  428. }
  429. else if (section == 3) {//actions - push-message
  430. ItemModel *pushItem = [_actions objectKey:@"itemSubTypeCode" eqaulToString:ksItemSubTypeCodeAppPush];
  431. ItemSubModel *subItem = pushItem.subItems[indexPath.row];
  432. RulesDetailPushTableViewCell *tcell = (RulesDetailPushTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"PushCellIdentifier"];
  433. tcell.lblMessage.text = subItem.cmdclsValue;
  434. cell = tcell;
  435. }
  436. else if (section == 4) {//conditions
  437. RulesDetailConditionTableViewCell *tcell = (RulesDetailConditionTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"ConditionCellIdentifier"];
  438. if (!tcell.container.subviews.count) {
  439. if ([self isViewLoaded]) {
  440. [_rcv beginAppearanceTransition:YES animated:NO];
  441. [tcell.container addSubview:_rcv.view];
  442. [_rcv endAppearanceTransition];
  443. }
  444. }
  445. tcell.constraintContainerHeight.constant = _rcv.tableHeight;
  446. _rcv.isShowingMode = YES;
  447. _rcv.conditions = _conditions;
  448. cell = tcell;
  449. }
  450. else if (section == 5) {//execution
  451. RulesDetailExecutionTableViewCell *tcell = (RulesDetailExecutionTableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"ExecutionCellIdentifier"];
  452. tcell.lblExecutionDate.text = !_ruleDetail.latestExecutionDatetime || [_ruleDetail.latestExecutionDatetime isEmptyString] ? @"-" : [CommonUtil localDateFromUTC2:_ruleDetail.latestExecutionDatetime];
  453. tcell.btnUseRule.selected = [_ruleDetail.useYn boolValue];
  454. tcell.statusView.hidden = [JDFacade facade].loginUser.level < 90; //심플 유저일 경우,
  455. if (tcell.statusView.hidden) {
  456. // tcell.constraintStatusViewBottom.constant = 0;
  457. tcell.constraintStatusViewHeight.constant = 0;
  458. tcell.statusView.hidden = YES;
  459. }
  460. else {//마스터일 경우,
  461. if (!_btnUseRule) {
  462. _btnUseRule = tcell.btnUseRule;
  463. }
  464. [self changeBtnRunStatus:[_ruleDetail.useYn boolValue]];
  465. if (![tcell.btnUseRule actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  466. [tcell.btnUseRule addTarget:self action:@selector(btnUseRuleTouched:) forControlEvents:UIControlEventTouchUpInside];
  467. }
  468. }
  469. if (![tcell.switchRulePlay actionsForTarget:self forControlEvent:UIControlEventValueChanged]) {
  470. [tcell.switchRulePlay addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
  471. }
  472. cell = tcell;
  473. }
  474. return cell;
  475. }
  476. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  477. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  478. }
  479. #pragma mark - Table Events
  480. - (void)changeSwitch:(id)sender {
  481. if ([_ruleDetail.useYn isEqualToString:ksKneetRulesDisable]) {
  482. [[JDFacade facade] toast:@"일부 장치 또는 멤버가 삭제되어 비활성 되었습니다.\n편집 후 다시 사용해주세요."];
  483. return;
  484. }
  485. [self requestChangeRuleStatus];
  486. }
  487. - (void)changeBtnRunStatus:(BOOL)isRun {
  488. if (isRun) {
  489. [_btnUseRule setImage:_btnImageRun forState:UIControlStateNormal];
  490. }
  491. else {
  492. [_btnUseRule setImage:_btnImageStop forState:UIControlStateNormal];
  493. }
  494. }
  495. #pragma mark - UI Events
  496. - (IBAction)btnOptionTouched:(id)sender {
  497. // [self toggleOptions:sender];
  498. [self toggleOptionsWithArray:sender btnArray:_moreBtnArray];
  499. }
  500. - (void)btnCloseTouched:(id)sender {
  501. [[JDFacade facade] dismissModalStack:YES completion:nil];
  502. }
  503. -(void)moreBtnAction:(id)sender
  504. {
  505. NSInteger tag = [(UIButton *)sender tag];
  506. NSLog(@"MoreBtn Action : %li", (long)tag);
  507. switch (tag) {
  508. case NameChange:
  509. {
  510. [self editRule];
  511. }
  512. break;
  513. case Del:
  514. {
  515. [self deleteRule];
  516. }
  517. break;
  518. case Refresh:
  519. {
  520. }
  521. break;
  522. default:
  523. break;
  524. }
  525. }
  526. - (void)editRule {
  527. RulesAddViewController *vc = (RulesAddViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"RulesAddViewController" storyboardName:@"Rules"];
  528. vc.refRuleDetail = _ruleDetail;
  529. [self presentViewController:vc animated:YES completion:nil];
  530. }
  531. - (void)deleteRule {
  532. [[JDFacade facade] confirmTitle:@"규칙 삭제" message:@"규칙을 삭제하시겠습니까?" completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  533. if (buttonIndex == 0) {//OK
  534. [self requestDeleteRule];
  535. }
  536. }];
  537. }
  538. #pragma mark - MemoryWarning
  539. - (void)didReceiveMemoryWarning
  540. {
  541. [super didReceiveMemoryWarning];
  542. // Dispose of any resources that can be recreated.
  543. }
  544. #pragma mark - api
  545. - (void)requestDeleteRule {
  546. NSArray *arr = @[_ruleDetail.ruleId];
  547. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_DELETE_RULE arguments:arr];
  548. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:nil modelClass:[JDJSONModel class] showLoadingView:YES completion:^(id responseObject) {
  549. [[JDFacade facade] dismissModalStack:YES
  550. completion:^{
  551. [[JDFacade facade] toast:NSLocalizedString(@"삭제되었습니다", @"삭제되었습니다")];
  552. }];
  553. } failure:^(id errorObject) {
  554. JDErrorModel *error = (JDErrorModel *)errorObject;
  555. [[JDFacade facade] alert:error.errorMessage];
  556. }];
  557. }
  558. - (void)requestChangeRuleStatus {
  559. NSDictionary *parameter = @{@"status_code" : [_ruleDetail.useYn isEqualToString:@"Y"] ? @"03" : @"02"};
  560. NSArray *arr = @[_ruleDetail.ruleId];
  561. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_PUT_RULE_STATUS arguments:arr];
  562. [[RequestHandler handler] sendAsyncPutRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  563. _ruleDetail.useYn = [_ruleDetail.useYn boolValue] ? ksNO : ksYES; //toggle
  564. if ([_ruleDetail.useYn boolValue]) {
  565. [[JDFacade facade] toast:@"규칙을 다시 시작합니다"];
  566. } else {
  567. [[JDFacade facade] toast:@"규칙을 중단합니다"];
  568. }
  569. [_tableView reloadData];
  570. } failure:^(id errorObject) {
  571. JDErrorModel *error = (JDErrorModel *)errorObject;
  572. [[JDFacade facade] alert:error.errorMessage];
  573. }];
  574. }
  575. - (void)requestRuleDetail {
  576. NSArray *arr = @[_refRule.ruleId];
  577. NSString *path = [[JDFacade facade] getUrlWithCustAndGroupID:API_GET_RULE_DETAIL arguments:arr];
  578. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[RuleDetailModel class] completion:^(id responseObject) {
  579. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  580. return;
  581. }
  582. _ruleDetail = (RuleDetailModel *) responseObject;
  583. NSLog(@"\n\n_ruleDetail : %@", _ruleDetail);
  584. if (_ruleDetail) {//API 성공
  585. [self setContents];
  586. }
  587. } failure:^(id errorObject) {
  588. JDErrorModel *error = (JDErrorModel *)errorObject;
  589. [[JDFacade facade] alert:error.errorMessage];
  590. }];
  591. }
  592. @end