CommandClassControlView.m 17 KB

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