CommandClassControlView.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. //
  2. // CommandClassControlView.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 3/16/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "JDJSONModel.h"
  11. #import "DeviceModel.h"
  12. #import "CustomLabel.h"
  13. #import "CommonUtil.h"
  14. #import "CustomButton.h"
  15. #import "CommandClassControlView.h"
  16. #import "ThingsDetailViewController.h"
  17. #import "ThingsViewController.h"
  18. #import "MainViewController.h"
  19. @implementation CommandClassControlView
  20. + (CommandClassControlView *)viewForCommandClass:(CmdClsType)cmdClsType {
  21. CommandClassControlView *controlView = nil;
  22. for (UIView *view in [CommonUtil nibViews:@"CommandClassControlView"]) {
  23. if (view.tag == cmdClsType) {
  24. controlView = (CommandClassControlView *)view;
  25. controlView.width = kfControlDefaultWidth;
  26. controlView.height = kfControlDefaultHeight;
  27. break;
  28. }
  29. }
  30. return controlView;
  31. }
  32. - (void)setDevice:(DeviceModel *)device {
  33. _device = device;
  34. }
  35. - (void)setNode:(NodeModel *)node {
  36. _node = node;
  37. }
  38. - (void)setLayoutUI {
  39. }
  40. - (void)updateLayout {
  41. }
  42. //todo : 로딩화면 띄우기
  43. //예외처리 하기
  44. - (NSDictionary *)parameterForNodeCommand {
  45. NSDictionary *parameter = nil;
  46. if (_device) {
  47. parameter = @{
  48. @"device_id": _device.deviceId,
  49. @"node_id": _device.nodeId};
  50. } else if (_node) {
  51. parameter = @{@"device_id": _node.deviceId,
  52. @"node_id": _node.nodeId};
  53. }
  54. return parameter;
  55. }
  56. - (CGSize)sizeForIntrinsic {
  57. return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
  58. }
  59. - (CGSize)sizeForIntrinsicForItemCount:(NSInteger)count {
  60. return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
  61. }
  62. - (void)requestNodeCommand:(NSDictionary *)parameters requestValue:(NSString *)requestValue completionHandler:(NodeCommandCompletionBlock)completion failureHandler:(NodeCommandFailureBlock)failure {
  63. if (_device.isRequesting) {
  64. return;
  65. }
  66. _device.isRequesting = YES;
  67. [[[RACObserve(_device, contentValue) skip:1] take:1] subscribeNext:^(id x) {
  68. [self updateLayout];
  69. }];
  70. NSString *deviceId = parameters[@"device_id"];
  71. NSString *nodeId = parameters[@"node_id"];
  72. NSDictionary *param = @{@"request_value": requestValue};
  73. NSString *path = [NSString stringWithFormat:API_POST_NODE_COMMAND, deviceId, nodeId];
  74. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestPOST parameters:param modelClass:[DeviceModel class] showLoadingView:NO completion:^(id responseObject) {
  75. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  76. return;
  77. }
  78. DeviceModel *result = (DeviceModel *)responseObject;
  79. if (result && result.commandId && ![result.commandId isEmptyString]) {
  80. _device.requestTime = result.requestTime;
  81. ThingsViewController *vc = [JDFacade facade].mainViewController.tvc;
  82. if (vc && [vc isKindOfClass:[ThingsViewController class]]) {
  83. [vc requestPollingCommandStatusOfDeviceInBackground:_device];
  84. }
  85. }
  86. if (completion) {
  87. completion(responseObject);
  88. }
  89. } failure:^(id errorObject) {
  90. JDErrorModel *error = (JDErrorModel *)errorObject;
  91. [[JDFacade facade] alert:error.errorMessage];
  92. if (failure) {
  93. failure(error);
  94. }
  95. }];
  96. }
  97. - (void)requestNodeCommand:(NSString *)requestValue {
  98. if (_device.isRequesting) {
  99. return;
  100. }
  101. _device.isRequesting = YES;
  102. [[[RACObserve(_device, contentValue) skip:1] take:1] subscribeNext:^(id x) {
  103. [self updateLayout];
  104. }];
  105. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_NODE_COMMAND];
  106. NSDictionary *param = @{@"device_id": _device.deviceId,
  107. @"node_id":_device.nodeId,
  108. @"command_type":CMD_TYPE_CONTROL,
  109. @"request_value":@{_device.cmdclsId:requestValue},
  110. @"cust_id": [[JDFacade facade].loginUser custId],
  111. @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
  112. [request setRequestMsg:param];
  113. [[SocketServiceHandler sharedManager] sendDataWithDelegate:request delegate:self];
  114. }
  115. + (NSString *)cmdclsValueForType:(NSString *)dataTypeCode value:(CGFloat)value {
  116. NSString *result = nil;
  117. if ([dataTypeCode isEqualToString:ksDataTypeCodeInteger]) {
  118. result = [NSString stringWithFormat:@"%zd", (NSInteger)value];
  119. } else if ([dataTypeCode isEqualToString:ksDataTypeCodeFloat]) {
  120. result = [NSString stringWithFormat:@"%.1f", value];
  121. } else {
  122. result = [NSString stringWithFormat:@"%zd", (NSInteger)value];
  123. }
  124. return result;
  125. }
  126. - (void)updateThingsViewContoller {
  127. if ([[JDFacade facade].currentViewController isKindOfClass:[ThingsDetailViewController class]]) {
  128. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  129. if (vc) {
  130. [vc prepareViewDidLoad];
  131. }
  132. }
  133. }
  134. - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result
  135. {
  136. NSLog(@"Message : %@", message);
  137. NSLog(@"Message : %@", result);
  138. if (result.isSuccess) {
  139. ThingsViewController *vc = [JDFacade facade].mainViewController.tvc;
  140. if (vc && [vc isKindOfClass:[ThingsViewController class]]) {
  141. [vc requestDeviceControl];
  142. }
  143. }
  144. }
  145. @end
  146. @implementation CommandClassSwitchView
  147. - (void)willMoveToSuperview:(UIView *)newSuperview {
  148. if (!newSuperview)
  149. return;
  150. [self initControl];
  151. }
  152. - (void)initControl {
  153. _requestValueForOff = @"OFF";
  154. _requestValueForOn = @"ON";
  155. [self setLayoutUI];
  156. }
  157. - (void)setLayoutUI {
  158. _device = _device ? _device : _node;
  159. _isON = [_device.contentValue isEqualToString:_requestValueForOn];
  160. _device.cmdclsValue = _isON ? _requestValueForOff : _requestValueForOn;
  161. _lblContentValueMsg.text = _device.contentValueMsg;
  162. _btnSwitch.hidden = self.isReOrderMode;
  163. [self setSwitchBinary];
  164. }
  165. - (IBAction)btnSwitchTouched:(id)sender {
  166. NSString *requestValue = _isON ? _requestValueForOff : _requestValueForOn;
  167. _device.cmdclsValue = requestValue;
  168. // [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  169. //
  170. //// _device.contentValue = requestValue;
  171. //// _isON = !_isON;
  172. //// _device.contentValue = _isON ? _requestValueForOn : _requestValueForOff;
  173. //// [self toggleSwitch];
  174. //
  175. // } failureHandler:nil];
  176. [self requestNodeCommand:requestValue];
  177. }
  178. - (void)setSwitchBinary {
  179. _lblContentValueMsg.text = _device.contentValueMsg;
  180. // [_btnSwitch setTitle:_device.cmdclsValueMsg forState:UIControlStateNormal];
  181. if (_isON) {//OFF
  182. [_btnSwitch setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_on"] forState:UIControlStateNormal];
  183. [self.lblContentValueMsg setColor:kUITextColor02 text:self.lblContentValueMsg.text];
  184. } else {//ON
  185. [_btnSwitch setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_off"] forState:UIControlStateNormal];
  186. }
  187. }
  188. - (void)toggleSwitch {
  189. [self setSwitchBinary];
  190. [self updateThingsViewContoller];
  191. }
  192. - (void)updateLayout {
  193. _isON = !_isON;
  194. [self setSwitchBinary];
  195. }
  196. @end
  197. @implementation CommandClassSwitchMultiLevelView
  198. - (void)willMoveToSuperview:(UIView *)newSuperview {
  199. if (!newSuperview)
  200. return;
  201. [self initControl];
  202. }
  203. - (void)initControl {
  204. [self setLayoutUI];
  205. }
  206. - (void)setLayoutUI {
  207. _device = _device ? _device : _node;
  208. _device.cmdclsValue = _device.cmdclsValue ? _device.cmdclsValue : [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:[_device.controlMaxValue floatValue]/ 2];
  209. _currentValue = !_isConditionMode ? [_device.contentValue floatValue] : [_device.cmdclsValue floatValue];
  210. _minValue = [_device.controlMinValue floatValue];
  211. _maxValue = [_device.controlMaxValue floatValue];
  212. _step = [_device.controlStep floatValue];
  213. NSString *sMax = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_maxValue];
  214. CGFloat width = [CommonUtil getSizeFromString:sMax font:_btnLabel.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
  215. CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
  216. eWidth = eWidth < kfControlDefaultWidth ? kfControlDefaultWidth : eWidth;
  217. _constraintBtnLabelWidth.constant = eWidth;
  218. CGRect sr = self.frame;
  219. sr.size.width = eWidth;
  220. self.frame = sr;
  221. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  222. }
  223. - (void)setEnable:(BOOL)enable {
  224. _enable = enable;
  225. _btnMinus.userInteractionEnabled = _enable;
  226. _btnPlus.userInteractionEnabled = _enable;
  227. if (_enable) {
  228. [_btnLabel setTitleColor:kUITextColor02 forState:UIControlStateNormal];
  229. [_btnMinus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_minus"] forState:UIControlStateNormal];
  230. [_btnPlus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_plus"] forState:UIControlStateNormal];
  231. } else {
  232. [_btnLabel setTitleColor:kUITextColor03 forState:UIControlStateNormal];
  233. [_btnMinus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_minus_disable"] forState:UIControlStateNormal];
  234. [_btnPlus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_plus_disable"] forState:UIControlStateNormal];
  235. }
  236. }
  237. - (IBAction)btnPlusTouchDown:(id)sender {
  238. if (!_pressTimer) {
  239. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(addValue) userInfo:nil repeats:YES];
  240. }
  241. }
  242. - (void)btnMinusTouchDown:(id)sender {
  243. if (!_pressTimer) {
  244. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(subtractValue) userInfo:nil repeats:YES];
  245. }
  246. }
  247. - (IBAction)btnPlusTouchUp:(id)sender {
  248. [_pressTimer invalidate];
  249. _pressTimer = nil;
  250. [self addValue];
  251. [self requestCommand];
  252. }
  253. - (IBAction)btnMinusTouchUp:(id)sender {
  254. [_pressTimer invalidate];
  255. _pressTimer = nil;
  256. [self subtractValue];
  257. [self requestCommand];
  258. }
  259. - (void)addValue {
  260. _currentValue = _currentValue + _step > _maxValue ? _minValue : _currentValue + _step;
  261. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  262. if (_device) {
  263. _device.cmdclsValue = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  264. [self updateThingsViewContoller];
  265. }
  266. }
  267. - (void)subtractValue {
  268. _currentValue = _currentValue - _step < _minValue ? _maxValue : _currentValue - _step;
  269. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  270. if (_device) {
  271. _device.cmdclsValue = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  272. [self updateThingsViewContoller];
  273. }
  274. }
  275. - (void)requestCommand {
  276. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  277. return;
  278. NSString *requestValue = _device.cmdclsValue;
  279. // [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  280. // _device.contentValue = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  281. // } failureHandler:nil];
  282. [self requestNodeCommand:requestValue];
  283. }
  284. @end
  285. @implementation CommandClassValveView
  286. - (void)willMoveToSuperview:(UIView *)newSuperview {
  287. if (!newSuperview)
  288. return;
  289. [self initControl];
  290. }
  291. - (void)initControl {
  292. _requestValueForOpen = @"OPEN";
  293. _requestValueForClose = @"CLOSE";
  294. [self setLayoutUI];
  295. }
  296. - (void)setLayoutUI {
  297. _device = _device ? _device : _node;
  298. _lblContentValueMsg.text = _device.contentValueMsg;
  299. _isOpened = [_device.contentValue isEqualToString:_requestValueForOpen];
  300. _device.cmdclsValue = _isOpened ? _requestValueForClose : _requestValueForOpen;
  301. if ([_device.contentValue isEqualToString:_requestValueForOpen]) {
  302. [self.lblContentValueMsg setColor:kUITextColor02 text:self.lblContentValueMsg.text];
  303. }
  304. _btnOpen.hidden = self.isReOrderMode;
  305. [self setSwitchBinary];
  306. }
  307. - (void)setSwitchBinary {
  308. if (_isOpened) {//Unlocked
  309. // [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_lockunlock_unlock"] forState:UIControlStateNormal];
  310. [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_on"] forState:UIControlStateNormal];
  311. } else {//Locked
  312. // [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_lockunlock_lock"] forState:UIControlStateNormal];
  313. [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_off"] forState:UIControlStateNormal];
  314. }
  315. }
  316. - (IBAction)btnOpenTouched:(id)sender {
  317. NSString *requestValue = _isOpened ? @"CLOSE" : @"OPEN";
  318. if ([_device.cmdclsTypeId isEqualToString:@"36002"]) {
  319. requestValue = @"CLOSE";
  320. if (!_isOpened) {
  321. [[JDFacade facade] toast:@"이미 잠긴 상태입니다"];
  322. return;
  323. }
  324. }
  325. _device.cmdclsValue = requestValue;
  326. // [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  327. //
  328. //// if ([_device.cmdclsTypeId isEqualToString:@"14001"]) {//Lock.Default일 경우에만 컨트롤을 토글시킴.
  329. //// _isLocked = !_isLocked;
  330. //// _device.contentValue = _isLocked ? @"LOCKED" : @"UNLOCKED";
  331. //// } else if ([_device.cmdclsTypeId isEqualToString:@"14002"]) {
  332. //// _isLocked = YES;
  333. //// _device.contentValue = @"LOCKED";
  334. //// } else if ([_device.cmdclsTypeId isEqualToString:@"14003"]) {
  335. //// _isLocked = NO;
  336. //// _device.contentValue = @"UNLOCKED";
  337. //// }
  338. //
  339. // [self toggleOpenButton];
  340. //
  341. // } failureHandler:nil];
  342. [self requestNodeCommand:requestValue];
  343. }
  344. - (void)toggleOpenButton {
  345. [self setSwitchBinary];
  346. // [_btnSwitch faceOffImage];
  347. [self updateThingsViewContoller];
  348. }
  349. @end
  350. #pragma mark - Sensors View
  351. @implementation CommandClassSensorBinaryView
  352. - (void)willMoveToSuperview:(UIView *)newSuperview {
  353. if (!newSuperview)
  354. return;
  355. [self initControl];
  356. }
  357. - (void)initControl {
  358. [self setLayoutUI];
  359. }
  360. - (void)setLayoutUI {
  361. _device = _device ? _device : _node;
  362. NSString *contentValue = _device.contentValueMsg;
  363. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  364. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  365. self.lblControl.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
  366. if ([_device.contentValue isEqualToString:@"TRUE"]) {
  367. [self.lblControl setColor:kUITextColor02 text:self.lblControl.text];
  368. }
  369. }
  370. @end
  371. @implementation CommandClassSensorMultiLevelView
  372. - (void)setLayoutUI {
  373. _device = _device ? _device : _node;
  374. NSString *contentValue = (!_device.contentValue || [_device.contentValue isEmptyString] ||[_device.contentValue isEqualToString:@"none"]) ? @"-" : _device.contentValue;
  375. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  376. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  377. self.lblControl.text = [NSString stringWithFormat:@"%@%@", contentValue, unit];
  378. // if (self.constraintControlLabelWidth) {
  379. // self.constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblControl.text font:self.lblControl.font width:kfControlMaxWidth].width;
  380. //
  381. // CGRect sr = self.frame;
  382. // sr.size.width = self.constraintControlLabelWidth.constant;
  383. // self.frame = sr;
  384. // }
  385. }
  386. @end
  387. @implementation CommandClassMeterCurrentView
  388. - (void)willMoveToSuperview:(UIView *)newSuperview {
  389. if (!newSuperview)
  390. return;
  391. [self initControl];
  392. }
  393. - (void)initControl {
  394. _lblMeter.textColor = kUITextColor02;
  395. [self setLayoutUI];
  396. }
  397. - (void)setLayoutUI {
  398. _device = _device ? _device : _node;
  399. NSString *contentValue = (!_device.contentValue || [_device.contentValue isEmptyString] ||[_device.contentValue isEqualToString:@"none"]) ? @"-" : _device.contentValue;
  400. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  401. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  402. if (_node) {//디바이스 상세일 경우,
  403. self.lblMeter.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
  404. self.lblMeter.font = [UIFont systemFontOfSize:kUIFontSize07];
  405. } else if (_device) {
  406. self.lblMeter.text = [NSString stringWithFormat:@"%@ : %@ %@", _device.nodeName, contentValue, unit];
  407. [self.lblMeter setColor:kUITextColor02 text:contentValue];
  408. }
  409. // self.constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblMeter.text font:self.lblMeter.font width:kfControlMaxWidth].width;
  410. //
  411. // CGRect sr = self.frame;
  412. // sr.size.width = self.constraintControlLabelWidth.constant;
  413. // self.frame = sr;
  414. }
  415. @end
  416. @implementation CommandClassMeterTotalView
  417. @end