CommandClassControlView.m 17 KB

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