From 60138964cf6db03be3fc2047ec09b8972432a8cc Mon Sep 17 00:00:00 2001 From: RubyOn Date: Fri, 25 Apr 2025 11:14:20 +0900 Subject: [PATCH] =?UTF-8?q?SMS=20=EC=B6=94=EA=B0=80=20(=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9A=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/dictionaries/rubyon.xml | 7 +++ modbus.rb | 41 +++++++++++------ sms/config.json | 7 +++ sms/request.rb | 84 +++++++++++++++++++++++++++++++++++ sms/sms.rb | 11 +++++ 5 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 .idea/dictionaries/rubyon.xml create mode 100755 sms/config.json create mode 100755 sms/request.rb create mode 100755 sms/sms.rb diff --git a/.idea/dictionaries/rubyon.xml b/.idea/dictionaries/rubyon.xml new file mode 100644 index 0000000..7d43637 --- /dev/null +++ b/.idea/dictionaries/rubyon.xml @@ -0,0 +1,7 @@ + + + + farmitry + + + \ No newline at end of file diff --git a/modbus.rb b/modbus.rb index a4ff827..77c989b 100644 --- a/modbus.rb +++ b/modbus.rb @@ -1,21 +1,34 @@ require "rmodbus" require "ccutrer-serialport" +require './sms/sms' -mode = ARGV[0].strip -controller_id = ARGV[1].to_i -value = ARGV[2].to_i +mode = ARGV[0]&.strip +controller_id = ARGV[1]&.to_i +value = ARGV[2]&.to_i -ModBus::RTUClient.new("/dev/ttyUSB0", 9600) do |cl| - cl.with_slave(controller_id) do |slave| - regs = slave.holding_registers - case mode - when "temp" - regs[2] = value - when "power" - regs[22] = value - else - # type code here +begin + ModBus::RTUClient.new("/dev/ttyUSB0", 9600) do |cl| + cl.with_slave(controller_id) do |slave| + regs = slave.holding_registers + case mode + when "temp" + regs[2] = value + when "power" + regs[22] = value + else + # type code here + end + sleep 0.1 end - sleep 0.1 end +rescue + error_message = "[#{Time.now}] #{mode} 실행 실패 (station_id: #{controller_id}, value: #{value})" + res = Sms.send_one( + { + to: '01062619801', + from: '01062619801', + text: error_message + } + ) + puts res end diff --git a/sms/config.json b/sms/config.json new file mode 100755 index 0000000..0cb4d21 --- /dev/null +++ b/sms/config.json @@ -0,0 +1,7 @@ +{ + "api_key": "NCSK1QGPTBWMUKBO", + "api_secret": "RSGJ20CGUOTRKK33RFONFUFHY2SWMU9X", + "protocol": "https", + "domain": "api.coolsms.co.kr", + "prefix": "" +} \ No newline at end of file diff --git a/sms/request.rb b/sms/request.rb new file mode 100755 index 0000000..e85cb41 --- /dev/null +++ b/sms/request.rb @@ -0,0 +1,84 @@ +require 'openssl' +require 'base64' +require 'net/http' +require 'json' +require 'securerandom' + +module Request + @file = File.read File.join(File.dirname(__FILE__), './config.json') + @config = JSON.parse(@file) + + def self.get_uri(path) + str = @config['protocol'] + '://' + @config['domain'] + @config['prefix'] + puts str + path + uri = URI(str + path) + uri + end + + def self.header_get + api_key = @config['api_key'] + api_secret = @config['api_secret'] + date = Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z') + salt = SecureRandom.hex + signature = OpenSSL::HMAC.hexdigest('SHA256', api_secret, date + salt) + 'HMAC-SHA256 apiKey=' + api_key + ', date=' + date + ', salt=' + salt + ', signature=' + signature + end + + def self.post(path, body) + header = header_get + uri = get_uri(path) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json') + req.add_field('Authorization', header) + if body + req.body = body.to_json + end + res = http.request(req) + JSON.parse(res.body) + end + + def self.put(path, body, headers = nil) + auth = header_get + uri = get_uri(path) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + req = Net::HTTP::Put.new(uri.path, 'Content-Type' => 'application/json') + req.add_field('Authorization', auth) + if headers + headers.each do |k, v| + req.add_field(k, v) + end + end + if body + req.body = body.to_json + end + res = http.request(req) + JSON.parse(res.body) + end + + def self.get(path) + header = header_get + uri = get_uri(path) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + req = Net::HTTP::Get.new(uri.path, 'Content-Type' => 'application/json') + req.add_field('Authorization', header) + res = http.request(req) + JSON.parse(res.body) + end + + def self.delete(path, body) + header = header_get + uri = get_uri(path) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + req = Net::HTTP::Delete.new(uri.path, 'Content-Type' => 'application/json') + req.add_field('Authorization', header) + res = http.request(req) + if body + req.body = body.to_json + end + JSON.parse(res.body) + end +end diff --git a/sms/sms.rb b/sms/sms.rb new file mode 100755 index 0000000..735ed06 --- /dev/null +++ b/sms/sms.rb @@ -0,0 +1,11 @@ +require_relative "request" + +module Sms + def self.send_one(message) + Request.post("/messages/v4/send", { message: message }) + end + + def self.send_many(messages) + Request.post("/messages/v4/send-many", { messages: messages }) + end +end