@@ -841,35 +841,38 @@ def operation_status(posted_hash)
841841 def device ( posted_hash )
842842 posted_hash = posted_hash . symbolize_keys
843843
844- [ :hardware_uid , :class_group_code , :class_code , :properties ] . each { |k |
845- unless posted_hash . has_key? ( k )
846- halt 400 , TEXT_PLAIN , "'#{ k . to_s } ' is not found."
847- end
848- }
844+ [ :gateway_uid , :device_uid , :class_group_code , :class_code , :properties ] . each do |k |
845+ halt ( 400 , TEXT_PLAIN , "'#{ k . to_s } ' key is not found." ) unless posted_hash . has_key? ( k )
846+ end
847+
848+ if Gateway . exists? ( hardware_uid : posted_hash [ :gateway_uid ] )
849+ gateway_id = Gateway . where ( hardware_uid : posted_hash [ :gateway_uid ] ) [ 0 ]
850+ posted_hash . delete ( :gateway_uid )
851+ else
852+ halt 400 , TEXT_PLAIN , "Posted gateway not found."
853+ end
849854
850855 # propertiesを変数に代入して、ハッシュから削除
851856 pties = posted_hash . delete ( :properties )
852857
853- # gateway_idは暫定
854- gateway_id = 1
855858 if Device . where ( hardware_uid : posted_hash [ :hardware_uid ] ) . exists?
856859 device = Device . where ( hardware_uid : posted_hash [ :hardware_uid ] ) [ 0 ]
857860 else
858861 device = Device . new ( { gateway_id : gateway_id } . merge ( posted_hash ) )
859862 end
860863
864+ unless device . save
865+ halt 500 , TEXT_PLAIN , "Failed to update device."
866+ end
867+
861868 ary = [ ]
862- p_ary = [ ]
863- ids = [ ]
864869
865- pties . each { |h |
870+ pties . each do |h |
866871 h = h . symbolize_keys
867872
868- [ :class_group_code , :class_code , :property_code , :type ] . each { |k |
869- unless h . has_key? ( k )
870- halt 400 , TEXT_PLAIN , "'properties'.'#{ k . to_s } ' is not found."
871- end
872- }
873+ [ :class_group_code , :class_code , :property_code , :type ] . each do |k |
874+ halt ( 400 , TEXT_PLAIN , "'properties'.'#{ k . to_s } ' key is not found." ) unless h . has_key? ( k )
875+ end
873876
874877 properties = DeviceProperty . where (
875878 device_id : device . id ,
@@ -889,31 +892,24 @@ def device(posted_hash)
889892 )
890893 else
891894 property = properties [ 0 ]
895+ property . gateway_id = gateway_id
892896 property . lrestore
893897 end
894898
899+ unless property . save
900+ halt 500 , TEXT_PLAIN , "Failed to save property '#{ property . id . to_s } '."
901+ end
902+
895903 ary << {
896904 "id" => property . id . to_s ,
897905 "class_group_code" => h [ :class_group_code ] ,
898906 "class_code" => h [ :class_code ] ,
899907 "property_code" => h [ :property_code ]
900908 }
901- p_ary << property
902- ids << property . id
903- }
904-
905- unless device . save
906- halt 500 , TEXT_PLAIN , "Failed to update device."
907909 end
908910
909- p_ary . each { |property |
910- unless property . save
911- halt 500 , TEXT_PLAIN , "Failed to save property '#{ property . id . to_s } '."
912- end
913- }
914-
915911 objs = DeviceProperty . where ( device_id : device . id )
916- objs . where . not ( id : ids )
912+ objs . where . not ( id : ary . map { | h | h [ "id" ] . to_i } )
917913 objs . each { |obj |
918914 obj . ldelete
919915 obj . save
0 commit comments