CommandClassControlView.m 16 KB

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