| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592 |
- //
- // CommandClassControlView.m
- // kneet
- //
- // Created by Jason Lee on 3/16/15.
- // Copyright (c) 2015 ntels. All rights reserved.
- //
- #import "JDObject.h"
- #import "RequestHandler.h"
- #import "JDJSONModel.h"
- #import "DeviceModel.h"
- #import "CustomLabel.h"
- #import "CommonUtil.h"
- #import "CustomButton.h"
- #import "CommandClassControlView.h"
- #import "ThingsDetailViewController.h"
- #import "ThingsViewController.h"
- #import "MainViewController.h"
- @implementation CommandClassControlView
- + (CommandClassControlView *)viewForCommandClass:(CmdClsType)cmdClsType {
- CommandClassControlView *controlView = nil;
- for (UIView *view in [CommonUtil nibViews:@"CommandClassControlView"]) {
- if (view.tag == cmdClsType) {
- controlView = (CommandClassControlView *)view;
-
- controlView.width = kfControlDefaultWidth;
- controlView.height = kfControlDefaultHeight;
-
- break;
- }
- }
- return controlView;
- }
- - (void)setDevice:(DeviceModel *)device {
- _device = device;
- }
- - (void)setNode:(NodeModel *)node {
- _node = node;
- }
- - (void)setLayoutUI {
-
- }
- - (void)updateLayout {
-
- }
- //todo : 로딩화면 띄우기
- //예외처리 하기
- - (NSDictionary *)parameterForNodeCommand {
- NSDictionary *parameter = nil;
- if (_device) {
- parameter = @{
- @"device_id": _device.deviceId,
- @"node_id": _device.nodeId};
- } else if (_node) {
- parameter = @{@"device_id": _node.deviceId,
- @"node_id": _node.nodeId};
- }
- return parameter;
- }
- - (CGSize)sizeForIntrinsic {
- return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
- }
- - (CGSize)sizeForIntrinsicForItemCount:(NSInteger)count {
- return CGSizeMake(kfControlMaxWidth, kfControlDefaultHeight);
- }
- - (void)requestNodeCommand:(NSDictionary *)parameters requestValue:(NSString *)requestValue completionHandler:(NodeCommandCompletionBlock)completion failureHandler:(NodeCommandFailureBlock)failure {
- if (_device.isRequesting) {
- return;
- }
- _device.isRequesting = YES;
-
- [[[RACObserve(_device, contentValue) skip:1] take:1] subscribeNext:^(id x) {
- [self updateLayout];
- }];
-
-
-
-
- NSString *deviceId = parameters[@"device_id"];
- NSString *nodeId = parameters[@"node_id"];
- NSDictionary *param = @{@"request_value": requestValue};
- NSString *path = [NSString stringWithFormat:API_POST_NODE_COMMAND, deviceId, nodeId];
- [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestPOST parameters:param modelClass:[DeviceModel class] showLoadingView:NO completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
-
- DeviceModel *result = (DeviceModel *)responseObject;
- if (result && result.commandId && ![result.commandId isEmptyString]) {
-
- _device.requestTime = result.requestTime;
-
- ThingsViewController *vc = [JDFacade facade].mainViewController.tvc;
- if (vc && [vc isKindOfClass:[ThingsViewController class]]) {
- [vc requestPollingCommandStatusOfDeviceInBackground:_device];
- }
- }
-
- if (completion) {
- completion(responseObject);
- }
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- if (failure) {
- failure(error);
- }
- }];
- }
- - (void)requestNodeCommand:(NSString *)requestValue {
- if (_device.isRequesting) {
- return;
- }
-
- _device.isRequesting = YES;
-
- [[[RACObserve(_device, contentValue) skip:1] take:1] subscribeNext:^(id x) {
- [self updateLayout];
- }];
-
- SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_NODE_COMMAND];
-
- NSDictionary *param = @{@"device_id": _device.deviceId,
- @"node_id":_device.nodeId,
- @"command_type":CMD_TYPE_CONTROL,
- @"request_value":@{_device.cmdclsId:requestValue},
- @"cust_id": [[JDFacade facade].loginUser custId],
- @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
-
-
- [request setRequestMsg:param];
-
- [[SocketServiceHandler sharedManager] sendDataWithDelegate:request delegate:self];
- }
- + (NSString *)cmdclsValueForType:(NSString *)dataTypeCode value:(CGFloat)value {
- NSString *result = nil;
- if ([dataTypeCode isEqualToString:ksDataTypeCodeInteger]) {
- result = [NSString stringWithFormat:@"%zd", (NSInteger)value];
- } else if ([dataTypeCode isEqualToString:ksDataTypeCodeFloat]) {
- result = [NSString stringWithFormat:@"%.1f", value];
- } else {
- result = [NSString stringWithFormat:@"%zd", (NSInteger)value];
- }
- return result;
- }
- - (void)updateThingsViewContoller {
- if ([[JDFacade facade].currentViewController isKindOfClass:[ThingsDetailViewController class]]) {
- ThingsViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[ThingsViewController class]];
- if (vc) {
- [vc prepareViewDidLoad];
- }
- }
- }
- - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result
- {
- NSLog(@"Message : %@", message);
- NSLog(@"Message : %@", result);
- if (result.isSuccess) {
- ThingsViewController *vc = [JDFacade facade].mainViewController.tvc;
- if (vc && [vc isKindOfClass:[ThingsViewController class]]) {
- [vc requestDeviceControl];
- }
- }
- }
- @end
- @implementation CommandClassSwitchView
- - (void)willMoveToSuperview:(UIView *)newSuperview {
- if (!newSuperview)
- return;
- [self initControl];
- }
- - (void)initControl {
- _requestValueForOff = @"OFF";
- _requestValueForOn = @"ON";
- [self setLayoutUI];
- }
- - (void)setLayoutUI {
-
- _device = _device ? _device : _node;
- _isON = [_device.contentValue isEqualToString:_requestValueForOn];
- _device.cmdclsValue = _isON ? _requestValueForOff : _requestValueForOn;
- _lblContentValueMsg.text = _device.contentValueMsg;
- _btnSwitch.hidden = self.isReOrderMode;
-
- [self setSwitchBinary];
- }
- - (IBAction)btnSwitchTouched:(id)sender {
- NSString *requestValue = _isON ? _requestValueForOff : _requestValueForOn;
- _device.cmdclsValue = requestValue;
-
- // [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
- //
- //// _device.contentValue = requestValue;
- //// _isON = !_isON;
- //// _device.contentValue = _isON ? _requestValueForOn : _requestValueForOff;
- //// [self toggleSwitch];
- //
- // } failureHandler:nil];
- [self requestNodeCommand:requestValue];
- }
- - (void)setSwitchBinary {
-
- _lblContentValueMsg.text = _device.contentValueMsg;
- // [_btnSwitch setTitle:_device.cmdclsValueMsg forState:UIControlStateNormal];
-
- if (_isON) {//OFF
- [_btnSwitch setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_on"] forState:UIControlStateNormal];
- [self.lblContentValueMsg setColor:kUITextColor02 text:self.lblContentValueMsg.text];
-
- } else {//ON
- [_btnSwitch setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_off"] forState:UIControlStateNormal];
- }
- }
- - (void)toggleSwitch {
- [self setSwitchBinary];
- [self updateThingsViewContoller];
- }
- - (void)updateLayout {
- _isON = !_isON;
- [self setSwitchBinary];
- }
- @end
- @implementation CommandClassSwitchMultiLevelView
- - (void)willMoveToSuperview:(UIView *)newSuperview {
- if (!newSuperview)
- return;
- [self initControl];
- }
- - (void)initControl {
- [self setLayoutUI];
- }
- - (void)setLayoutUI {
- _device = _device ? _device : _node;
- _device.cmdclsValue = _device.cmdclsValue ? _device.cmdclsValue : [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:[_device.controlMaxValue floatValue]/ 2];
- _currentValue = !_isConditionMode ? [_device.contentValue floatValue] : [_device.cmdclsValue floatValue];
- _minValue = [_device.controlMinValue floatValue];
- _maxValue = [_device.controlMaxValue floatValue];
- _step = [_device.controlStep floatValue];
- NSString *sMax = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_maxValue];
- CGFloat width = [CommonUtil getSizeFromString:sMax font:_btnLabel.titleLabel.font width:FLT_MAX].width + kfSwitchLabelPadding;
- CGFloat eWidth = width + kfCommandClassSwitchLeftPadding + kfCommandClassSwitchRightPadding;
- eWidth = eWidth < kfControlDefaultWidth ? kfControlDefaultWidth : eWidth;
- _constraintBtnLabelWidth.constant = eWidth;
- CGRect sr = self.frame;
- sr.size.width = eWidth;
- self.frame = sr;
- [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
- }
- - (void)setEnable:(BOOL)enable {
- _enable = enable;
- _btnMinus.userInteractionEnabled = _enable;
- _btnPlus.userInteractionEnabled = _enable;
- if (_enable) {
- [_btnLabel setTitleColor:kUITextColor02 forState:UIControlStateNormal];
- [_btnMinus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_minus"] forState:UIControlStateNormal];
- [_btnPlus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_plus"] forState:UIControlStateNormal];
- } else {
- [_btnLabel setTitleColor:kUITextColor03 forState:UIControlStateNormal];
- [_btnMinus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_minus_disable"] forState:UIControlStateNormal];
- [_btnPlus setImage:[UIImage imageNamed:@"tp_01_img_control_unit_plus_disable"] forState:UIControlStateNormal];
- }
- }
- - (IBAction)btnPlusTouchDown:(id)sender {
- if (!_pressTimer) {
- _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(addValue) userInfo:nil repeats:YES];
- }
- }
- - (void)btnMinusTouchDown:(id)sender {
- if (!_pressTimer) {
- _pressTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(subtractValue) userInfo:nil repeats:YES];
- }
- }
- - (IBAction)btnPlusTouchUp:(id)sender {
- [_pressTimer invalidate];
- _pressTimer = nil;
- [self addValue];
- [self requestCommand];
- }
- - (IBAction)btnMinusTouchUp:(id)sender {
- [_pressTimer invalidate];
- _pressTimer = nil;
- [self subtractValue];
- [self requestCommand];
- }
- - (void)addValue {
- _currentValue = _currentValue + _step > _maxValue ? _minValue : _currentValue + _step;
- [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
- if (_device) {
- _device.cmdclsValue = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue];
- [self updateThingsViewContoller];
- }
- }
- - (void)subtractValue {
- _currentValue = _currentValue - _step < _minValue ? _maxValue : _currentValue - _step;
- [_btnLabel setTitle:[NSString stringWithFormat:@"%@%@", [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue], _device.unit ? _device.unit : ksEmptyString] forState:UIControlStateNormal];
- if (_device) {
- _device.cmdclsValue = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue];
- [self updateThingsViewContoller];
- }
- }
- - (void)requestCommand {
- if (_isConditionMode) //룰, 씬일 경우, 제어를 하지 않음
- return;
- NSString *requestValue = _device.cmdclsValue;
- // [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
- // _device.contentValue = [CommandClassControlView cmdclsValueForType:_device.dataTypeCode value:_currentValue];
- // } failureHandler:nil];
-
- [self requestNodeCommand:requestValue];
- }
- @end
- @implementation CommandClassValveView
- - (void)willMoveToSuperview:(UIView *)newSuperview {
- if (!newSuperview)
- return;
- [self initControl];
- }
- - (void)initControl {
- _requestValueForOpen = @"OPEN";
- _requestValueForClose = @"CLOSE";
- [self setLayoutUI];
- }
- - (void)setLayoutUI {
- _device = _device ? _device : _node;
- _lblContentValueMsg.text = _device.contentValueMsg;
-
- _isOpened = [_device.contentValue isEqualToString:_requestValueForOpen];
- _device.cmdclsValue = _isOpened ? _requestValueForClose : _requestValueForOpen;
- if ([_device.contentValue isEqualToString:_requestValueForOpen]) {
- [self.lblContentValueMsg setColor:kUITextColor02 text:self.lblContentValueMsg.text];
- }
-
- _btnOpen.hidden = self.isReOrderMode;
-
- [self setSwitchBinary];
- }
- - (void)setSwitchBinary {
- if (_isOpened) {//Unlocked
- // [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_lockunlock_unlock"] forState:UIControlStateNormal];
- [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_on"] forState:UIControlStateNormal];
-
- } else {//Locked
- // [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_lockunlock_lock"] forState:UIControlStateNormal];
- [_btnOpen setImage:[UIImage imageNamed:@"img_things_list_btn_onoff_off"] forState:UIControlStateNormal];
- }
- }
- - (IBAction)btnOpenTouched:(id)sender {
-
- NSString *requestValue = _isOpened ? @"CLOSE" : @"OPEN";
- if ([_device.cmdclsTypeId isEqualToString:@"36002"]) {
- requestValue = @"CLOSE";
-
- if (!_isOpened) {
- [[JDFacade facade] toast:@"이미 잠긴 상태입니다"];
- return;
- }
- }
-
- _device.cmdclsValue = requestValue;
-
- // [self requestNodeCommand:self.parameterForNodeCommand requestValue:requestValue completionHandler:^(id result) {
- //
- //// if ([_device.cmdclsTypeId isEqualToString:@"14001"]) {//Lock.Default일 경우에만 컨트롤을 토글시킴.
- //// _isLocked = !_isLocked;
- //// _device.contentValue = _isLocked ? @"LOCKED" : @"UNLOCKED";
- //// } else if ([_device.cmdclsTypeId isEqualToString:@"14002"]) {
- //// _isLocked = YES;
- //// _device.contentValue = @"LOCKED";
- //// } else if ([_device.cmdclsTypeId isEqualToString:@"14003"]) {
- //// _isLocked = NO;
- //// _device.contentValue = @"UNLOCKED";
- //// }
- //
- // [self toggleOpenButton];
- //
- // } failureHandler:nil];
- [self requestNodeCommand:requestValue];
- }
- - (void)toggleOpenButton {
- [self setSwitchBinary];
- // [_btnSwitch faceOffImage];
- [self updateThingsViewContoller];
- }
- @end
- #pragma mark - Sensors View
- @implementation CommandClassSensorBinaryView
- - (void)willMoveToSuperview:(UIView *)newSuperview {
- if (!newSuperview)
- return;
- [self initControl];
- }
- - (void)initControl {
-
- [self setLayoutUI];
- }
- - (void)setLayoutUI {
- _device = _device ? _device : _node;
- NSString *contentValue = _device.contentValueMsg;
- NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
- unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
- self.lblControl.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
- if ([_device.contentValue isEqualToString:@"TRUE"]) {
- [self.lblControl setColor:kUITextColor02 text:self.lblControl.text];
- }
- }
- @end
- @implementation CommandClassSensorMultiLevelView
- - (void)setLayoutUI {
- _device = _device ? _device : _node;
- NSString *contentValue = (!_device.contentValue || [_device.contentValue isEmptyString] ||[_device.contentValue isEqualToString:@"none"]) ? @"-" : _device.contentValue;
- NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
- unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
- self.lblControl.text = [NSString stringWithFormat:@"%@%@", contentValue, unit];
- // if (self.constraintControlLabelWidth) {
- // self.constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblControl.text font:self.lblControl.font width:kfControlMaxWidth].width;
- //
- // CGRect sr = self.frame;
- // sr.size.width = self.constraintControlLabelWidth.constant;
- // self.frame = sr;
- // }
- }
- @end
- @implementation CommandClassMeterCurrentView
- - (void)willMoveToSuperview:(UIView *)newSuperview {
- if (!newSuperview)
- return;
- [self initControl];
- }
- - (void)initControl {
- _lblMeter.textColor = kUITextColor02;
- [self setLayoutUI];
- }
- - (void)setLayoutUI {
- _device = _device ? _device : _node;
- NSString *contentValue = (!_device.contentValue || [_device.contentValue isEmptyString] ||[_device.contentValue isEqualToString:@"none"]) ? @"-" : _device.contentValue;
- NSString *unit = _device.unit && ![_device.unit isEmptyString] ? _device.unit : ksEmptyString;
- unit = [contentValue isEqualToString:@"-"] ? ksEmptyString : unit;
- if (_node) {//디바이스 상세일 경우,
- self.lblMeter.text = [NSString stringWithFormat:@"%@ %@", contentValue, unit];
- self.lblMeter.font = [UIFont systemFontOfSize:kUIFontSize07];
- } else if (_device) {
- self.lblMeter.text = [NSString stringWithFormat:@"%@ : %@ %@", _device.nodeName, contentValue, unit];
- [self.lblMeter setColor:kUITextColor02 text:contentValue];
- }
- // self.constraintControlLabelWidth.constant = [CommonUtil getSizeFromString:self.lblMeter.text font:self.lblMeter.font width:kfControlMaxWidth].width;
- //
- // CGRect sr = self.frame;
- // sr.size.width = self.constraintControlLabelWidth.constant;
- // self.frame = sr;
- }
- @end
- @implementation CommandClassMeterTotalView
- @end
|