SMS 추가 (테스트 용)

This commit is contained in:
RubyOn 2025-04-25 11:14:20 +09:00
parent 9ce0efdb8c
commit 60138964cf
5 changed files with 136 additions and 14 deletions

View File

@ -0,0 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="rubyon">
<words>
<w>farmitry</w>
</words>
</dictionary>
</component>

View File

@ -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

7
sms/config.json Executable file
View File

@ -0,0 +1,7 @@
{
"api_key": "NCSK1QGPTBWMUKBO",
"api_secret": "RSGJ20CGUOTRKK33RFONFUFHY2SWMU9X",
"protocol": "https",
"domain": "api.coolsms.co.kr",
"prefix": ""
}

84
sms/request.rb Executable file
View File

@ -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

11
sms/sms.rb Executable file
View File

@ -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