CommandClassControlView.m 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. @implementation CommandClassControlView
  19. + (CommandClassControlView *)viewForCommandClass:(CmdClsType)cmdClsType
  20. {
  21. CommandClassControlView *controlView = nil;
  22. for (UIView *view in [CommonUtil nibViews:@"CommandClassControlView"]) {
  23. if (view.tag == cmdClsType) {
  24. controlView = (CommandClassControlView *)view;
  25. break;
  26. }
  27. }
  28. return controlView;
  29. }
  30. - (void)setDevice:(DeviceModel *)device {
  31. _device = device;
  32. }
  33. - (void)setNode:(NodeModel *)node {
  34. _node = node;
  35. }
  36. - (NSDictionary *)parameterForNodeCommand {
  37. NSDictionary *parameter = nil;
  38. if (_device) {
  39. parameter = @{@"device_id": _device.deviceId,
  40. @"node_id": _device.nodeId};
  41. } else if (_node) {
  42. parameter = @{@"device_id": _node.deviceId,
  43. @"node_id": _node.nodeId};
  44. }
  45. return parameter;
  46. }
  47. - (CGSize)sizeForIntrinsic {
  48. return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
  49. }
  50. - (CGSize)sizeForIntrinsicForItemCount:(NSInteger)count {
  51. return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
  52. }
  53. + (void)requestNodeCommand:(NSDictionary *)parameters requestValue:(NSString *)requestValue completionHandler:(NodeCommandCompletionBlock)completion failureHandler:(NodeCommandFailureBlock)failure {
  54. NSString *deviceId = parameters[@"device_id"];
  55. NSString *nodeId = parameters[@"node_id"];
  56. NSDictionary *param = @{@"request_value": requestValue};
  57. NSString *path = [NSString stringWithFormat:API_POST_NODE_COMMAND, deviceId, nodeId];
  58. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:param modelClass:[JDJSONModel class] completion:^(id responseObject) {
  59. if (completion) {
  60. completion(responseObject);
  61. }
  62. } failure:^(id errorObject) {
  63. JDErrorModel *error = (JDErrorModel *)errorObject;
  64. [[JDFacade facade] alert:error.errorMessage];
  65. if (failure) {
  66. failure(error);
  67. }
  68. }];
  69. }
  70. - (NSString *)cmdclsValueForType:(NSString *)dataTypeCode value:(CGFloat)value {
  71. NSString *result = nil;
  72. if ([dataTypeCode isEqualToString:ksDataTypeCodeInteger]) {
  73. result = [NSString stringWithFormat:@"%zd", (NSInteger)value];
  74. } else if ([dataTypeCode isEqualToString:ksDataTypeCodeFloat]) {
  75. result = [NSString stringWithFormat:@"%.1f", value];
  76. } else {
  77. result = [NSString stringWithFormat:@"%zd", (NSInteger)value];
  78. }
  79. return result;
  80. }
  81. - (void)updateThingsViewContoller {
  82. if ([[JDFacade facade].currentViewController isKindOfClass:[ThingsDetailViewController class]]) {
  83. ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
  84. if (vc) {
  85. [vc prepareViewDidLoad];
  86. }
  87. }
  88. }
  89. @end
  90. @implementation CommandClassSwitchView
  91. - (void)willMoveToSuperview:(UIView *)newSuperview {
  92. if (!newSuperview)
  93. return;
  94. [self initControl];
  95. }
  96. - (void)initControl {
  97. _requestValueForOff = @"OFF";
  98. _requestValueForOn = @"ON";
  99. [self setLayoutUI];
  100. }
  101. - (void)setLayoutUI {
  102. _device = _device ? _device : _node;
  103. NSString *stmp = _device.cmdclsValueMsgLongest;
  104. CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnSwitch.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
  105. CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
  106. _constraintBtnSwitchWidth.constant = eWidth;
  107. CGRect sr = self.frame;
  108. sr.size.width = eWidth;
  109. self.frame = sr;
  110. _btnSwitch.rectForCapBackground = CGRectMake(kfCommandClassSwitchLeftPadding, 0, kfCommandClassSwitchRightPadding, 0);
  111. _isON = [_device.contentValue isEqualToString:_requestValueForOn];
  112. _device.cmdclsValue = _isON ? _requestValueForOn : _requestValueForOff;
  113. [self setSwitchBinary];
  114. if (!_isON) {
  115. [_btnSwitch faceOffImage];
  116. }
  117. }
  118. - (IBAction)btnSwitchTouched:(id)sender {
  119. _device.cmdclsValue = _isON ? _requestValueForOn : _requestValueForOff;
  120. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  121. return;
  122. NSString *requestValue = _isON ? _requestValueForOff : _requestValueForOn;
  123. [CommandClassControlView requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  124. _isON = !_isON;
  125. _device.contentValue = _isON ? _requestValueForOn : _requestValueForOff;
  126. [self toggleSwitch];
  127. } failureHandler:nil];
  128. }
  129. - (void)setSwitchBinary {
  130. if (_isON) {//OFF
  131. [_btnSwitch setTitle:_device.cmdclsValueMsg forState:UIControlStateNormal];
  132. _btnSwitch.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  133. _btnSwitch.rectForCapBackground = CGRectMake(30, 0, 0, 40);
  134. } else {//ON
  135. [_btnSwitch setTitle:_device.cmdclsValueMsg forState:UIControlStateNormal];
  136. _btnSwitch.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
  137. _btnSwitch.rectForCapBackground = CGRectMake(40, 0, 0, 30);
  138. }
  139. }
  140. - (void)toggleSwitch {
  141. [self setSwitchBinary];
  142. [_btnSwitch faceOffImage];
  143. [self updateThingsViewContoller];
  144. }
  145. @end
  146. @implementation CommandClassSwitchMultiLevelView
  147. - (void)willMoveToSuperview:(UIView *)newSuperview {
  148. if (!newSuperview)
  149. return;
  150. [self initControl];
  151. }
  152. - (void)initControl {
  153. [self setLayoutUI];
  154. }
  155. - (void)setLayoutUI {
  156. _device = _device ? _device : _node;
  157. _device.cmdclsValue = _device.cmdclsValue ? _device.cmdclsValue : [self cmdclsValueForType:_device.dataTypeCode value:[_device.controlMaxValue floatValue]/ 2];
  158. _currentValue = !_isConditionMode ? [_device.contentValue floatValue] : [_device.cmdclsValue floatValue];
  159. _minValue = [_device.controlMinValue floatValue];
  160. _maxValue = [_device.controlMaxValue floatValue];
  161. _step = [_device.controlStep floatValue];
  162. NSString *sMax = [self cmdclsValueForType:_device.dataTypeCode value:_maxValue];
  163. CGFloat width = [CommonUtil getSizeFromString:sMax font:_btnLabel.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
  164. CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
  165. eWidth = eWidth < kfControlDefaultWidth ? kfControlDefaultWidth : eWidth;
  166. _constraintBtnLabelWidth.constant = eWidth;
  167. CGRect sr = self.frame;
  168. sr.size.width = eWidth;
  169. self.frame = sr;
  170. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [self cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  171. }
  172. - (void)setEnable:(BOOL)enable {
  173. _enable = enable;
  174. _btnMinus.userInteractionEnabled = _enable;
  175. _btnPlus.userInteractionEnabled = _enable;
  176. if (_enable) {
  177. [_btnLabel setTitleColor:kUITextColor02 forState:UIControlStateNormal];
  178. [_btnMinus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_minus"] forState:UIControlStateNormal];
  179. [_btnPlus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_plus"] forState:UIControlStateNormal];
  180. } else {
  181. [_btnLabel setTitleColor:kUITextColor03 forState:UIControlStateNormal];
  182. [_btnMinus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_minus_disable"] forState:UIControlStateNormal];
  183. [_btnPlus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_plus_disable"] forState:UIControlStateNormal];
  184. }
  185. }
  186. - (IBAction)btnPlusTouchDown:(id)sender {
  187. if (!_pressTimer) {
  188. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(addValue) userInfo:nil repeats:YES];
  189. }
  190. }
  191. - (void)btnMinusTouchDown:(id)sender {
  192. if (!_pressTimer) {
  193. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(subtractValue) userInfo:nil repeats:YES];
  194. }
  195. }
  196. - (IBAction)btnPlusTouchUp:(id)sender {
  197. [_pressTimer invalidate];
  198. _pressTimer = nil;
  199. [self addValue];
  200. [self requestCommand];
  201. }
  202. - (IBAction)btnMinusTouchUp:(id)sender {
  203. [_pressTimer invalidate];
  204. _pressTimer = nil;
  205. [self subtractValue];
  206. [self requestCommand];
  207. }
  208. - (void)addValue {
  209. _currentValue = _currentValue + _step > _maxValue ? _minValue : _currentValue + _step;
  210. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [self cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  211. if (_device) {
  212. _device.cmdclsValue = [self cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  213. [self updateThingsViewContoller];
  214. }
  215. }
  216. - (void)subtractValue {
  217. _currentValue = _currentValue - _step < _minValue ? _maxValue : _currentValue - _step;
  218. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [self cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  219. if (_device) {
  220. _device.cmdclsValue = [self cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  221. [self updateThingsViewContoller];
  222. }
  223. }
  224. - (void)requestCommand {
  225. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  226. return;
  227. NSString *requestValue = _device.cmdclsValue;
  228. [CommandClassControlView requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  229. _device.contentValue = [self cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  230. } failureHandler:nil];
  231. }
  232. @end
  233. @implementation CommandClassThermostatModeView
  234. - (void)willMoveToSuperview:(UIView *)newSuperview {
  235. if (!newSuperview)
  236. return;
  237. [self initControl];
  238. }
  239. - (void)initControl {
  240. [self setLayoutUI];
  241. }
  242. - (void)setLayoutUI {
  243. //todo contentValue 비교 추가,
  244. _modeArray = _device.cmdclsValueList;
  245. NSString *mode;
  246. NSUInteger highest = 0, i = 0;
  247. for (CmdClsValueModel *tmpCmdclsValue in _modeArray) {
  248. if (tmpCmdclsValue.cmdclsValueMsg.length > highest) {
  249. highest = tmpCmdclsValue.cmdclsValueMsg.length;
  250. mode = tmpCmdclsValue.cmdclsValueMsg;
  251. }
  252. if ([_device.contentValue isEqualToString:tmpCmdclsValue.cmdclsValue]) {
  253. _modeIndex = i;
  254. }
  255. i++;
  256. }
  257. CGFloat width = [CommonUtil getSizeFromString:mode font:_btnLabel.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
  258. CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
  259. eWidth = eWidth < kfControlDefaultWidth ? kfControlDefaultWidth : eWidth;
  260. _constraintBtnLabelWidth.constant = eWidth;
  261. CGRect sr = self.frame;
  262. sr.size.width = eWidth;
  263. self.frame = sr;
  264. CmdClsValueModel *cmdclsValue = _modeArray[_modeIndex];
  265. if (!_isConditionMode) {//컨디션 모드가 아닐 경우,
  266. _device.cmdclsValue = cmdclsValue.cmdclsValue;
  267. [_btnLabel setTitle:_device.cmdclsValueMsg forState:UIControlStateNormal];
  268. } else if (_isConditionTypeMode) {//컨디션 모드일 경우,
  269. _device.conditionTypeCode = cmdclsValue.cmdclsValue;
  270. [_btnLabel setTitle:_device.cmdclsValue forState:UIControlStateNormal];
  271. }
  272. }
  273. - (void)setEnable:(BOOL)enable {
  274. _enable = enable;
  275. _btnPrev.userInteractionEnabled = _enable;
  276. _btnNext.userInteractionEnabled = _enable;
  277. if (_enable) {
  278. [_btnLabel setTitleColor:kUITextColor02 forState:UIControlStateNormal];
  279. [_btnPrev setImage:[UIImage imageNamed:@"tp_01_img_control_unit_prev"] forState:UIControlStateNormal];
  280. [_btnNext setImage:[UIImage imageNamed:@"tp_01_img_control_unit_next"] forState:UIControlStateNormal];
  281. } else {
  282. [_btnLabel setTitleColor:kUITextColor03 forState:UIControlStateNormal];
  283. [_btnPrev setImage:[UIImage imageNamed:@"tp_01_img_control_unit_prev_disable"] forState:UIControlStateNormal];
  284. [_btnNext setImage:[UIImage imageNamed:@"tp_01_img_control_unit_next_disable"] forState:UIControlStateNormal];
  285. }
  286. }
  287. - (IBAction)btnNextTouchDown:(id)sender {
  288. if (!_pressTimer) {
  289. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changeNextMode) userInfo:nil repeats:YES];
  290. }
  291. }
  292. - (IBAction)btnPrevTouchDown:(id)sender {
  293. if (!_pressTimer) {
  294. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changePrevMode) userInfo:nil repeats:YES];
  295. }
  296. }
  297. - (IBAction)btnNextTouchUp:(id)sender {
  298. [_pressTimer invalidate];
  299. _pressTimer = nil;
  300. [self changeNextMode];
  301. [self requestCommand];
  302. }
  303. - (IBAction)btnPrevTouchUp:(id)sender {
  304. [_pressTimer invalidate];
  305. _pressTimer = nil;
  306. [self changePrevMode];
  307. [self requestCommand];
  308. }
  309. - (void)changeNextMode {
  310. _modeIndex = _modeIndex + 1 == _modeArray.count ? 0 : _modeIndex + 1;
  311. CmdClsValueModel *cmdclsValue = _modeArray[_modeIndex];
  312. [_btnLabel setTitle:cmdclsValue.cmdclsValueMsg forState:UIControlStateNormal];
  313. if (!_isConditionTypeMode) {
  314. _device.cmdclsValue = cmdclsValue.cmdclsValue;
  315. [self updateThingsViewContoller];
  316. } else {//컨디션 모드일 경우,
  317. _device.conditionTypeCode = cmdclsValue.cmdclsValue;
  318. }
  319. }
  320. - (void)changePrevMode {
  321. _modeIndex = _modeIndex - 1 == -1 ? _modeArray.count - 1 : _modeIndex - 1;
  322. CmdClsValueModel *cmdclsValue = _modeArray[_modeIndex];
  323. [_btnLabel setTitle:cmdclsValue.cmdclsValueMsg forState:UIControlStateNormal];
  324. if (!_isConditionTypeMode) {
  325. _device.cmdclsValue = cmdclsValue.cmdclsValue;
  326. [self updateThingsViewContoller];
  327. } else {//컨디션 모드일 경우,
  328. _device.conditionTypeCode = cmdclsValue.cmdclsValue;
  329. }
  330. }
  331. - (void)requestCommand {
  332. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  333. return;
  334. NSString *requestValue = _device.cmdclsValue;
  335. [CommandClassControlView requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  336. _device.contentValue = requestValue;
  337. } failureHandler:nil];
  338. }
  339. @end
  340. @implementation CommandClassThermostatFanModeView
  341. @end
  342. @implementation CommandClassThermostatSetPointView
  343. - (void)willMoveToSuperview:(UIView *)newSuperview {
  344. if (!newSuperview)
  345. return;
  346. [self initControl];
  347. }
  348. - (void)initControl {
  349. [self setLayoutUI];
  350. }
  351. - (void)setLayoutUI {
  352. _device = _device ? _device : _node;
  353. _device.cmdclsValue = _device.cmdclsValue ? _device.cmdclsValue : [self cmdclsValueForType:_device.dataTypeCode value:[_device.controlMaxValue floatValue]/ 2];
  354. _currentValue = !_isConditionMode ? [_device.contentValue floatValue] : [_device.cmdclsValue floatValue];
  355. _minValue = [_device.controlMinValue floatValue];
  356. _maxValue = [_device.controlMaxValue floatValue];
  357. _step = [_device.controlStep floatValue];
  358. NSString *sMax = [self cmdclsValueForType:_device.dataTypeCode value:_maxValue];
  359. CGFloat width = [CommonUtil getSizeFromString:sMax font:_btnLabel.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
  360. CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
  361. eWidth = eWidth < kfControlDefaultWidth ? kfControlDefaultWidth : eWidth;
  362. _constraintBtnLabelWidth.constant = eWidth;
  363. CGRect sr = self.frame;
  364. sr.size.width = eWidth;
  365. self.frame = sr;
  366. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [self cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  367. }
  368. - (void)setEnable:(BOOL)enable {
  369. _enable = enable;
  370. _btnDown.userInteractionEnabled = _enable;
  371. _btnUp.userInteractionEnabled = _enable;
  372. if (_enable) {
  373. [_btnLabel setTitleColor:kUITextColor02 forState:UIControlStateNormal];
  374. [_btnDown setImage:[UIImage imageNamed:@"tp_01_img_control_unit_down"] forState:UIControlStateNormal];
  375. [_btnUp setImage:[UIImage imageNamed:@"tp_01_img_control_unit_up"] forState:UIControlStateNormal];
  376. } else {
  377. [_btnLabel setTitleColor:kUITextColor03 forState:UIControlStateNormal];
  378. [_btnDown setImage:[UIImage imageNamed:@"tp_01_img_control_unit_down_disable"] forState:UIControlStateNormal];
  379. [_btnUp setImage:[UIImage imageNamed:@"tp_01_img_control_unit_up_disable"] forState:UIControlStateNormal];
  380. }
  381. }
  382. - (IBAction)btnUpTouchDown:(id)sender {
  383. if (!_pressTimer) {
  384. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(addValue) userInfo:nil repeats:YES];
  385. }
  386. }
  387. - (void)btnDownTouchDown:(id)sender {
  388. if (!_pressTimer) {
  389. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(subtractValue) userInfo:nil repeats:YES];
  390. }
  391. }
  392. - (IBAction)btnUpTouchUp:(id)sender {
  393. [_pressTimer invalidate];
  394. _pressTimer = nil;
  395. [self addValue];
  396. [self requestCommand];
  397. }
  398. - (void)btnDownTouchUp:(id)sender {
  399. [_pressTimer invalidate];
  400. _pressTimer = nil;
  401. [self subtractValue];
  402. [self requestCommand];
  403. }
  404. - (void)addValue {
  405. _currentValue = _currentValue + _step > _maxValue ? _minValue : _currentValue + _step;
  406. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [self cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  407. if (_device) {
  408. _device.cmdclsValue = [self cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  409. [self updateThingsViewContoller];
  410. }
  411. }
  412. - (void)subtractValue {
  413. _currentValue = _currentValue - _step < _minValue ? _maxValue : _currentValue - _step;
  414. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [self cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  415. if (_device) {
  416. _device.cmdclsValue = [self cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  417. [self updateThingsViewContoller];
  418. }
  419. }
  420. - (void)requestCommand {
  421. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  422. return;
  423. NSString *requestValue = _device.cmdclsValue;
  424. [CommandClassControlView requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  425. _device.contentValue = requestValue;
  426. } failureHandler:nil];
  427. }
  428. @end
  429. @implementation CommandClassLockView
  430. - (void)willMoveToSuperview:(UIView *)newSuperview {
  431. if (!newSuperview)
  432. return;
  433. [self initControl];
  434. }
  435. - (void)initControl {
  436. _requestValueForOff = @"UNLOCKED";
  437. _requestValueForOn = @"LOCKED";
  438. [self setLayoutUI];
  439. }
  440. - (void)setLayoutUI {
  441. _device = _device ? _device : _node;
  442. CGFloat adjustRightPadding = kfCommandClassSwitchRightPadding;
  443. if ([_device.cmdclsTypeId isEqualToString:@"14002"]) {
  444. [_btnLock setImage:[UIImage imageNamed:@"tp_01_img_control_switch_lock_locking"] forState:UIControlStateNormal];
  445. [_btnLock setImage:nil forState:UIControlStateHighlighted];
  446. _constraintBtnLockWidth.constant = 32;
  447. _constraintBtnLabelWidth.constant = 159 - 32;
  448. [self layoutIfNeeded];
  449. adjustRightPadding = kfCommandClassLockRightPadding;
  450. } else if ([_device.cmdclsTypeId isEqualToString:@"14003"]) {
  451. [_btnLock setImage:[UIImage imageNamed:@"tp_01_img_control_switch_lock_unlocking"] forState:UIControlStateNormal];
  452. [_btnLock setImage:nil forState:UIControlStateHighlighted];
  453. _constraintBtnLockWidth.constant = 32;
  454. _constraintBtnLabelWidth.constant = 159 - 32;
  455. [self layoutIfNeeded];
  456. adjustRightPadding = kfCommandClassLockRightPadding;
  457. }
  458. NSString *stmp = _device.cmdclsValueMsgLongest;
  459. CGFloat width = [CommonUtil getSizeFromString:stmp font:_btnLabel.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
  460. CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + adjustRightPadding;
  461. _constraintBtnLabelWidth.constant = eWidth;
  462. CGRect sr = self.frame;
  463. sr.size.width = eWidth;
  464. self.frame = sr;
  465. _isLocked = [_device.contentValue isEqualToString:_requestValueForOn];
  466. _device.cmdclsValue = _isLocked ? @"LOCK" : @"UNLOCK";
  467. [self setLock];
  468. if (!_isLocked) {
  469. [_btnLock faceOffImage];
  470. }
  471. }
  472. - (void)setLock {
  473. if (_isLocked) {//Unlocked
  474. [_btnLabel setTitle:_device.cmdclsValueMsg forState:UIControlStateNormal];
  475. // _btnLabel.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
  476. // _btnLabel.rectForCapBackground = CGRectMake(40, 0, 0, 30);
  477. } else {//Locked
  478. [_btnLabel setTitle:_device.cmdclsValueMsg forState:UIControlStateNormal];
  479. // _btnLabel.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  480. // _btnLabel.rectForCapBackground = CGRectMake(30, 0, 0, 40);
  481. }
  482. }
  483. - (IBAction)btnLockTouched:(id)sender {
  484. _device.cmdclsValue = _isLocked ? @"LOCK" : @"UNLOCK";
  485. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  486. return;
  487. NSString *requestValue = _isLocked ? @"UNLOCK" : @"LOCK";
  488. [CommandClassControlView requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  489. if ([_device.cmdclsTypeId isEqualToString:@"14001"]) {//Lock.Default일 경우에만 컨트롤을 토글시킴.
  490. _isLocked = !_isLocked;
  491. _device.contentValue = _isLocked ? @"LOCKED" : @"UNLOCKED";
  492. } else if ([_device.cmdclsTypeId isEqualToString:@"14002"]) {
  493. _isLocked = YES;
  494. _device.contentValue = @"LOCKED";
  495. } else if ([_device.cmdclsTypeId isEqualToString:@"14003"]) {
  496. _isLocked = NO;
  497. _device.contentValue = @"UNLOCKED";
  498. }
  499. [self toggleLock];
  500. } failureHandler:nil];
  501. }
  502. - (void)toggleLock {
  503. [self setLock];
  504. [_btnLock faceOffImage];
  505. [self updateThingsViewContoller];
  506. }
  507. @end
  508. @implementation CommandClassAVPlaybackView
  509. - (void)willMoveToSuperview:(UIView *)newSuperview {
  510. if (!newSuperview)
  511. return;
  512. [self initControl];
  513. }
  514. - (void)initControl {
  515. _requestValueForOff = @"PAUSED";
  516. _requestValueForOn = @"PLAYING";
  517. [self setLayoutUI];
  518. }
  519. - (void)setLayoutUI {
  520. _device = _device ? _device : _node;
  521. CGRect sr = self.frame;
  522. //FIXME : get image size
  523. UIImage *controlImage = [_btnPlayback imageForState:UIControlStateNormal];
  524. sr.size.width = controlImage.size.width;
  525. self.frame = sr;
  526. _isPlay = [_device.contentValue isEqualToString:_requestValueForOn];
  527. _device.cmdclsValue = _isPlay ? @"PLAY" : @"PAUSE";
  528. if (_isPlay) {
  529. [_btnPlayback faceOffImage];
  530. }
  531. }
  532. - (IBAction)btnPlaybackTouched:(id)sender {
  533. _device.cmdclsValue = _isPlay ? @"PLAY" : @"PAUSE";
  534. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  535. return;
  536. NSString *requestValue = _isPlay ? @"PAUSE" : @"PLAY";
  537. [CommandClassControlView requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  538. _isPlay = !_isPlay;
  539. _device.contentValue = _isPlay ? @"PLAYING" : @"PAUSED";
  540. [_btnPlayback faceOffImage];
  541. [self updateThingsViewContoller];
  542. } failureHandler:nil];
  543. }
  544. @end
  545. #pragma mark - Sensors View
  546. @implementation CommandClassSensorBinaryView
  547. - (void)willMoveToSuperview:(UIView *)newSuperview {
  548. if (!newSuperview)
  549. return;
  550. [self initControl];
  551. }
  552. - (void)initControl {
  553. _lblControl.textColor = kUITextColor01;
  554. [self setLayoutUI];
  555. }
  556. - (void)setLayoutUI {
  557. _device = _device ? _device : _node;
  558. NSString *contentValue = _device.cmdclsValueMsg;
  559. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  560. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  561. if (_node) {//디바이스 상세일 경우,
  562. self.lblControl.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
  563. self.lblControl.font = [UIFont systemFontOfSize:kUIFontSize07];
  564. if ([contentValue isEqualToString:@"TRUE"]) {
  565. [self.lblControl setColor:kUITextColor02 text:contentValue];
  566. }
  567. } else if (_device) {
  568. self.lblControl.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
  569. if ([contentValue isEqualToString:@"TRUE"]) {
  570. [self.lblControl setColor:kUITextColor02 text:contentValue];
  571. }
  572. }
  573. _constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblControl.text font:self.lblControl.font width:kfControlMaxWidth].width;
  574. CGRect sr = self.frame;
  575. sr.size.width = _constraintControlLabelWidth.constant;
  576. self.frame = sr;
  577. }
  578. @end
  579. @implementation CommandClassSensorMultiLevelView
  580. - (void)setLayoutUI {
  581. _device = _device ? _device : _node;
  582. NSString *contentValue = (!_device.contentValue || [_device.contentValue isEmptyString] ||[_device.contentValue isEqualToString:@"none"]) ? @"-" : _device.contentValue;
  583. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  584. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  585. if (_node) {//디바이스 상세일 경우,
  586. self.lblControl.text = [NSString stringWithFormat:@"%@%@", contentValue, unit];
  587. self.lblControl.font = [UIFont systemFontOfSize:kUIFontSize07];
  588. } else if (_device) {
  589. self.lblControl.text = [NSString stringWithFormat:@"%@ : %@%@", _device.nodeName, contentValue, unit];
  590. [self.lblControl setColor:kUITextColor02 text:contentValue];
  591. }
  592. self.constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblControl.text font:self.lblControl.font width:kfControlMaxWidth].width;
  593. CGRect sr = self.frame;
  594. sr.size.width = self.constraintControlLabelWidth.constant;
  595. self.frame = sr;
  596. }
  597. @end
  598. @implementation CommandClassMeterCurrentView
  599. - (void)willMoveToSuperview:(UIView *)newSuperview {
  600. if (!newSuperview)
  601. return;
  602. [self initControl];
  603. }
  604. - (void)initControl {
  605. _lblMeter.textColor = kUITextColor01;
  606. [self setLayoutUI];
  607. }
  608. - (void)setLayoutUI {
  609. _device = _device ? _device : _node;
  610. NSString *contentValue = (!_device.contentValue || [_device.contentValue isEmptyString] ||[_device.contentValue isEqualToString:@"none"]) ? @"-" : _device.contentValue;
  611. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  612. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  613. if (_node) {//디바이스 상세일 경우,
  614. self.lblMeter.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
  615. self.lblMeter.font = [UIFont systemFontOfSize:kUIFontSize07];
  616. } else if (_device) {
  617. self.lblMeter.text = [NSString stringWithFormat:@"%@ : %@ %@", _device.nodeName, contentValue, unit];
  618. [self.lblMeter setColor:kUITextColor02 text:contentValue];
  619. }
  620. self.constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblMeter.text font:self.lblMeter.font width:kfControlMaxWidth].width;
  621. CGRect sr = self.frame;
  622. sr.size.width = self.constraintControlLabelWidth.constant;
  623. self.frame = sr;
  624. }
  625. @end
  626. @implementation CommandClassMeterTotalView
  627. @end
  628. @implementation CommandClassCameraStreamView
  629. - (void)willMoveToSuperview:(UIView *)newSuperview {
  630. if (!newSuperview)
  631. return;
  632. [self initControl];
  633. }
  634. - (void)initControl {
  635. _lblCamera.textColor = kUITextColor01;
  636. [self setLayoutUI];
  637. }
  638. - (void)setLayoutUI {
  639. if (_device) {
  640. _lblCamera.text = [NSString stringWithFormat:@"%@", _device.nodeName];
  641. } else if (_node) {
  642. _lblCamera.text = [NSString stringWithFormat:@"%@", _node.nodeName];
  643. }
  644. _constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:_lblCamera.text font:_lblCamera.font width:kfControlMaxWidth].width;
  645. CGRect sr = self.frame;
  646. sr.size.width = _constraintControlLabelWidth.constant;
  647. self.frame = sr;
  648. }
  649. @end
  650. @implementation CommandClassSpeedControlView
  651. - (void)willMoveToSuperview:(UIView *)newSuperview {
  652. if (!newSuperview)
  653. return;
  654. [self initControl];
  655. }
  656. - (void)initControl {
  657. [self setLayoutUI];
  658. }
  659. - (void)setLayoutUI {
  660. _modeArray = _device.cmdclsValueList;
  661. NSString *mode;
  662. NSUInteger highest = 0, i = 0;
  663. for (CmdClsValueModel *tmpCmdclsValue in _modeArray) {
  664. if (tmpCmdclsValue.cmdclsValueMsg.length > highest) {
  665. highest = tmpCmdclsValue.cmdclsValueMsg.length;
  666. mode = tmpCmdclsValue.cmdclsValueMsg;
  667. }
  668. if ([_device.contentValue isEqualToString:tmpCmdclsValue.cmdclsValue]) {
  669. _modeIndex = i;
  670. }
  671. i++;
  672. }
  673. CGFloat width = [CommonUtil getSizeFromString:mode font:_btnLabel.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
  674. CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
  675. eWidth = eWidth < kfControlDefaultWidth ? kfControlDefaultWidth : eWidth;
  676. _constraintBtnLabelWidth.constant = eWidth;
  677. CGRect sr = self.frame;
  678. sr.size.width = eWidth;
  679. self.frame = sr;
  680. CmdClsValueModel *cmdclsValue = _modeArray[_modeIndex];
  681. if (!_isConditionMode) {//컨디션 모드가 아닐 경우,
  682. _device.cmdclsValue = cmdclsValue.cmdclsValue;
  683. [_btnLabel setTitle:_device.cmdclsValueMsg forState:UIControlStateNormal];
  684. } else if (_isConditionTypeMode) {//컨디션 모드일 경우,
  685. _device.conditionTypeCode = cmdclsValue.cmdclsValue;
  686. [_btnLabel setTitle:_device.cmdclsValue forState:UIControlStateNormal];
  687. }
  688. }
  689. - (void)setEnable:(BOOL)enable {
  690. _enable = enable;
  691. _btnPrev.userInteractionEnabled = _enable;
  692. _btnNext.userInteractionEnabled = _enable;
  693. if (_enable) {
  694. [_btnLabel setTitleColor:kUITextColor02 forState:UIControlStateNormal];
  695. [_btnPrev setImage:[UIImage imageNamed:@"tp_01_img_control_unit_prev"] forState:UIControlStateNormal];
  696. [_btnNext setImage:[UIImage imageNamed:@"tp_01_img_control_unit_next"] forState:UIControlStateNormal];
  697. } else {
  698. [_btnLabel setTitleColor:kUITextColor03 forState:UIControlStateNormal];
  699. [_btnPrev setImage:[UIImage imageNamed:@"tp_01_img_control_unit_prev_disable"] forState:UIControlStateNormal];
  700. [_btnNext setImage:[UIImage imageNamed:@"tp_01_img_control_unit_next_disable"] forState:UIControlStateNormal];
  701. }
  702. }
  703. - (IBAction)btnNextTouchDown:(id)sender {
  704. if (!_pressTimer) {
  705. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changeNextMode) userInfo:nil repeats:YES];
  706. }
  707. }
  708. - (IBAction)btnPrevTouchDown:(id)sender {
  709. if (!_pressTimer) {
  710. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(changePrevMode) userInfo:nil repeats:YES];
  711. }
  712. }
  713. - (IBAction)btnNextTouchUp:(id)sender {
  714. [_pressTimer invalidate];
  715. _pressTimer = nil;
  716. [self changeNextMode];
  717. [self requestCommand];
  718. }
  719. - (IBAction)btnPrevTouchUp:(id)sender {
  720. [_pressTimer invalidate];
  721. _pressTimer = nil;
  722. [self changePrevMode];
  723. [self requestCommand];
  724. }
  725. - (void)changeNextMode {
  726. _modeIndex = _modeIndex + 1 == _modeArray.count ? 0 : _modeIndex + 1;
  727. CmdClsValueModel *cmdclsValue = _modeArray[_modeIndex];
  728. [_btnLabel setTitle:cmdclsValue.cmdclsValueMsg forState:UIControlStateNormal];
  729. if (!_isConditionTypeMode) {
  730. _device.cmdclsValue = cmdclsValue.cmdclsValue;
  731. [self updateThingsViewContoller];
  732. } else {//컨디션 모드일 경우,
  733. _device.conditionTypeCode = cmdclsValue.cmdclsValue;
  734. }
  735. }
  736. - (void)changePrevMode {
  737. _modeIndex = _modeIndex - 1 == -1 ? _modeArray.count - 1 : _modeIndex - 1;
  738. CmdClsValueModel *cmdclsValue = _modeArray[_modeIndex];
  739. [_btnLabel setTitle:cmdclsValue.cmdclsValueMsg forState:UIControlStateNormal];
  740. if (!_isConditionTypeMode) {
  741. _device.cmdclsValue = cmdclsValue.cmdclsValue;
  742. [self updateThingsViewContoller];
  743. } else {//컨디션 모드일 경우,
  744. _device.conditionTypeCode = cmdclsValue.cmdclsValue;
  745. }
  746. }
  747. - (void)requestCommand {
  748. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  749. return;
  750. NSString *requestValue = _device.cmdclsValue;
  751. [CommandClassControlView requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  752. _device.contentValue = requestValue;
  753. } failureHandler:nil];
  754. }
  755. @end