CommandClassControlView.m 16 KB

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