CommandClassControlView.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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] sendAsyncRequestAPIPath:path method:ksHTTPRequestPOST parameters:param modelClass:[DeviceModel class] showLoadingView:NO 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. _btnSwitch.hidden = self.isReOrderMode;
  131. [self setSwitchBinary];
  132. }
  133. - (IBAction)btnSwitchTouched:(id)sender {
  134. NSString *requestValue = _isON ? _requestValueForOff : _requestValueForOn;
  135. _device.cmdclsValue = requestValue;
  136. [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  137. // _device.contentValue = requestValue;
  138. // _isON = !_isON;
  139. // _device.contentValue = _isON ? _requestValueForOn : _requestValueForOff;
  140. // [self toggleSwitch];
  141. } failureHandler:nil];
  142. }
  143. - (void)setSwitchBinary {
  144. _lblContentValueMsg.text = _device.contentValueMsg;
  145. // [_btnSwitch setTitle:_device.cmdclsValueMsg forState:UIControlStateNormal];
  146. if (_isON) {//OFF
  147. [_btnSwitch setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_on"] forState:UIControlStateNormal];
  148. [self.lblContentValueMsg setColor:kUITextColor02 text:self.lblContentValueMsg.text];
  149. } else {//ON
  150. [_btnSwitch setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_off"] forState:UIControlStateNormal];
  151. }
  152. }
  153. - (void)toggleSwitch {
  154. [self setSwitchBinary];
  155. [self updateThingsViewContoller];
  156. }
  157. - (void)updateLayout {
  158. _isON = !_isON;
  159. [self setSwitchBinary];
  160. }
  161. @end
  162. @implementation CommandClassSwitchMultiLevelView
  163. - (void)willMoveToSuperview:(UIView *)newSuperview {
  164. if (!newSuperview)
  165. return;
  166. [self initControl];
  167. }
  168. - (void)initControl {
  169. [self setLayoutUI];
  170. }
  171. - (void)setLayoutUI {
  172. _device = _device ? _device : _node;
  173. _device.cmdclsValue = _device.cmdclsValue ? _device.cmdclsValue : [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:[_device.controlMaxValue floatValue]/ 2];
  174. _currentValue = !_isConditionMode ? [_device.contentValue floatValue] : [_device.cmdclsValue floatValue];
  175. _minValue = [_device.controlMinValue floatValue];
  176. _maxValue = [_device.controlMaxValue floatValue];
  177. _step = [_device.controlStep floatValue];
  178. NSString *sMax = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_maxValue];
  179. CGFloat width = [CommonUtil getSizeFromString:sMax font:_btnLabel.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
  180. CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
  181. eWidth = eWidth < kfControlDefaultWidth ? kfControlDefaultWidth : eWidth;
  182. _constraintBtnLabelWidth.constant = eWidth;
  183. CGRect sr = self.frame;
  184. sr.size.width = eWidth;
  185. self.frame = sr;
  186. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  187. }
  188. - (void)setEnable:(BOOL)enable {
  189. _enable = enable;
  190. _btnMinus.userInteractionEnabled = _enable;
  191. _btnPlus.userInteractionEnabled = _enable;
  192. if (_enable) {
  193. [_btnLabel setTitleColor:kUITextColor02 forState:UIControlStateNormal];
  194. [_btnMinus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_minus"] forState:UIControlStateNormal];
  195. [_btnPlus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_plus"] forState:UIControlStateNormal];
  196. } else {
  197. [_btnLabel setTitleColor:kUITextColor03 forState:UIControlStateNormal];
  198. [_btnMinus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_minus_disable"] forState:UIControlStateNormal];
  199. [_btnPlus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_plus_disable"] forState:UIControlStateNormal];
  200. }
  201. }
  202. - (IBAction)btnPlusTouchDown:(id)sender {
  203. if (!_pressTimer) {
  204. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(addValue) userInfo:nil repeats:YES];
  205. }
  206. }
  207. - (void)btnMinusTouchDown:(id)sender {
  208. if (!_pressTimer) {
  209. _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(subtractValue) userInfo:nil repeats:YES];
  210. }
  211. }
  212. - (IBAction)btnPlusTouchUp:(id)sender {
  213. [_pressTimer invalidate];
  214. _pressTimer = nil;
  215. [self addValue];
  216. [self requestCommand];
  217. }
  218. - (IBAction)btnMinusTouchUp:(id)sender {
  219. [_pressTimer invalidate];
  220. _pressTimer = nil;
  221. [self subtractValue];
  222. [self requestCommand];
  223. }
  224. - (void)addValue {
  225. _currentValue = _currentValue + _step > _maxValue ? _minValue : _currentValue + _step;
  226. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  227. if (_device) {
  228. _device.cmdclsValue = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  229. [self updateThingsViewContoller];
  230. }
  231. }
  232. - (void)subtractValue {
  233. _currentValue = _currentValue - _step < _minValue ? _maxValue : _currentValue - _step;
  234. [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
  235. if (_device) {
  236. _device.cmdclsValue = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  237. [self updateThingsViewContoller];
  238. }
  239. }
  240. - (void)requestCommand {
  241. if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
  242. return;
  243. NSString *requestValue = _device.cmdclsValue;
  244. [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  245. _device.contentValue = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue];
  246. } failureHandler:nil];
  247. }
  248. @end
  249. @implementation CommandClassValveView
  250. - (void)willMoveToSuperview:(UIView *)newSuperview {
  251. if (!newSuperview)
  252. return;
  253. [self initControl];
  254. }
  255. - (void)initControl {
  256. _requestValueForOpen = @"OPEN";
  257. _requestValueForClose = @"CLOSE";
  258. [self setLayoutUI];
  259. }
  260. - (void)setLayoutUI {
  261. _device = _device ? _device : _node;
  262. _lblContentValueMsg.text = _device.contentValueMsg;
  263. _isOpened = [_device.contentValue isEqualToString:_requestValueForOpen];
  264. _device.cmdclsValue = _isOpened ? _requestValueForClose : _requestValueForOpen;
  265. if ([_device.contentValue isEqualToString:_requestValueForOpen]) {
  266. [self.lblContentValueMsg setColor:kUITextColor02 text:self.lblContentValueMsg.text];
  267. }
  268. _btnOpen.hidden = self.isReOrderMode;
  269. [self setSwitchBinary];
  270. }
  271. - (void)setSwitchBinary {
  272. if (_isOpened) {//Unlocked
  273. // [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_lockunlock_unlock"] forState:UIControlStateNormal];
  274. [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_off"] forState:UIControlStateNormal];
  275. } else {//Locked
  276. // [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_lockunlock_lock"] forState:UIControlStateNormal];
  277. [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_on"] forState:UIControlStateNormal];
  278. }
  279. }
  280. - (IBAction)btnOpenTouched:(id)sender {
  281. NSString *requestValue = _isOpened ? @"CLOSE" : @"OPEN";
  282. if ([_device.cmdclsTypeId isEqualToString:@"36002"]) {
  283. requestValue = @"CLOSE";
  284. if (!_isOpened) {
  285. [[JDFacade facade] toast:@"이미 잠긴 상태입니다"];
  286. return;
  287. }
  288. }
  289. _device.cmdclsValue = requestValue;
  290. [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
  291. // if ([_device.cmdclsTypeId isEqualToString:@"14001"]) {//Lock.Default일 경우에만 컨트롤을 토글시킴.
  292. // _isLocked = !_isLocked;
  293. // _device.contentValue = _isLocked ? @"LOCKED" : @"UNLOCKED";
  294. // } else if ([_device.cmdclsTypeId isEqualToString:@"14002"]) {
  295. // _isLocked = YES;
  296. // _device.contentValue = @"LOCKED";
  297. // } else if ([_device.cmdclsTypeId isEqualToString:@"14003"]) {
  298. // _isLocked = NO;
  299. // _device.contentValue = @"UNLOCKED";
  300. // }
  301. [self toggleOpenButton];
  302. } failureHandler:nil];
  303. }
  304. - (void)toggleOpenButton {
  305. [self setSwitchBinary];
  306. // [_btnSwitch faceOffImage];
  307. [self updateThingsViewContoller];
  308. }
  309. @end
  310. #pragma mark - Sensors View
  311. @implementation CommandClassSensorBinaryView
  312. - (void)willMoveToSuperview:(UIView *)newSuperview {
  313. if (!newSuperview)
  314. return;
  315. [self initControl];
  316. }
  317. - (void)initControl {
  318. [self setLayoutUI];
  319. }
  320. - (void)setLayoutUI {
  321. _device = _device ? _device : _node;
  322. NSString *contentValue = _device.contentValueMsg;
  323. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  324. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  325. self.lblControl.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
  326. if ([_device.contentValue isEqualToString:@"TRUE"]) {
  327. [self.lblControl setColor:kUITextColor02 text:self.lblControl.text];
  328. }
  329. }
  330. @end
  331. @implementation CommandClassSensorMultiLevelView
  332. - (void)setLayoutUI {
  333. _device = _device ? _device : _node;
  334. NSString *contentValue = (!_device.contentValue || [_device.contentValue isEmptyString] ||[_device.contentValue isEqualToString:@"none"]) ? @"-" : _device.contentValue;
  335. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  336. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  337. self.lblControl.text = [NSString stringWithFormat:@"%@%@", contentValue, unit];
  338. // if (self.constraintControlLabelWidth) {
  339. // self.constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblControl.text font:self.lblControl.font width:kfControlMaxWidth].width;
  340. //
  341. // CGRect sr = self.frame;
  342. // sr.size.width = self.constraintControlLabelWidth.constant;
  343. // self.frame = sr;
  344. // }
  345. }
  346. @end
  347. @implementation CommandClassMeterCurrentView
  348. - (void)willMoveToSuperview:(UIView *)newSuperview {
  349. if (!newSuperview)
  350. return;
  351. [self initControl];
  352. }
  353. - (void)initControl {
  354. _lblMeter.textColor = kUITextColor02;
  355. [self setLayoutUI];
  356. }
  357. - (void)setLayoutUI {
  358. _device = _device ? _device : _node;
  359. NSString *contentValue = (!_device.contentValue || [_device.contentValue isEmptyString] ||[_device.contentValue isEqualToString:@"none"]) ? @"-" : _device.contentValue;
  360. NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
  361. unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
  362. if (_node) {//디바이스 상세일 경우,
  363. self.lblMeter.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
  364. self.lblMeter.font = [UIFont systemFontOfSize:kUIFontSize07];
  365. } else if (_device) {
  366. self.lblMeter.text = [NSString stringWithFormat:@"%@ : %@ %@", _device.nodeName, contentValue, unit];
  367. [self.lblMeter setColor:kUITextColor02 text:contentValue];
  368. }
  369. // self.constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblMeter.text font:self.lblMeter.font width:kfControlMaxWidth].width;
  370. //
  371. // CGRect sr = self.frame;
  372. // sr.size.width = self.constraintControlLabelWidth.constant;
  373. // self.frame = sr;
  374. }
  375. @end
  376. @implementation CommandClassMeterTotalView
  377. @end